Skip to content

Commit a5f2215

Browse files
committed
Merge branch 'bat/wasm-name-lookups' (#4690)
* origin/bat/wasm-name-lookups: Fix changelog Added changelog Added new storage key that allows looking up a wasm name from a hash
2 parents cc5ec95 + 834954c commit a5f2215

File tree

7 files changed

+32
-8
lines changed

7 files changed

+32
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Indexers fail to identify the types of older txs since these types use wasm hashes. If those hashes were overwritten by governance proposals, the indexer is unable to determine how to deserialize these txs. This PR adds a new key to storage which allows looking up a tx type from a wasm hash, even if that hash is of code no longer in use. ([\#4690](https://github.com/anoma/namada/pull/4690))

crates/core/src/storage.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ impl Key {
551551
}
552552

553553
/// Returns a key of wasm code's hash of the given name
554-
pub fn wasm_code_name(code_name: String) -> Self {
554+
pub fn wasm_code_hash(code_name: String) -> Self {
555555
let mut segments =
556556
Self::from(PARAMETERS.to_owned().to_db_key()).segments;
557557
segments.push(DbKeySeg::StringSeg(WASM_KEY_PREFIX.to_owned()));
@@ -580,6 +580,16 @@ impl Key {
580580
Key { segments }
581581
}
582582

583+
/// Returns a key of the wasm name of the given code hash
584+
pub fn wasm_code_name(code_hash: &Hash) -> Self {
585+
let mut segments =
586+
Self::from(PARAMETERS.to_owned().to_db_key()).segments;
587+
segments.push(DbKeySeg::StringSeg(WASM_KEY_PREFIX.to_owned()));
588+
segments.push(DbKeySeg::StringSeg(WASM_HASH_PREFIX.to_owned()));
589+
segments.push(DbKeySeg::StringSeg(code_hash.to_string()));
590+
Key { segments }
591+
}
592+
583593
/// Returns a key of the validity predicate of the given address
584594
/// Only this function can push "?" segment for validity predicate
585595
pub fn validity_predicate(addr: &Address) -> Self {

crates/migrations/src/foreign_types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use namada_macros::derive_typehash;
88

99
use crate::TypeHash;
1010

11+
derive_typehash!(String);
1112
derive_typehash!(Vec::<u8>);
1213
derive_typehash!(Vec::<String>);
1314
derive_typehash!(u64);

crates/node/src/shell/init_chain.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,15 +424,17 @@ where
424424
let code_key = Key::wasm_code(&code_hash);
425425
let code_len_key = Key::wasm_code_len(&code_hash);
426426
let hash_key = Key::wasm_hash(name);
427-
let code_name_key = Key::wasm_code_name(name.to_owned());
427+
let code_hash_key = Key::wasm_code_hash(name.to_owned());
428+
let code_name_key = Key::wasm_code_name(&code_hash);
428429

429430
self.state.write(&code_key, code).unwrap();
430431
self.state.write(&code_len_key, code_len).unwrap();
431432
self.state.write(&hash_key, code_hash).unwrap();
432433
if &Some(code_hash) == implicit_vp_code_hash {
433434
is_implicit_vp_stored = true;
434435
}
435-
self.state.write(&code_name_key, code_hash).unwrap();
436+
self.state.write(&code_hash_key, code_hash).unwrap();
437+
self.state.write(&code_name_key, name).unwrap();
436438
} else {
437439
tracing::warn!("The wasm {name} isn't allowed.");
438440
self.warn(Warning::DisallowedWasm(name.to_string()));

crates/tx/src/data/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ mod test_process_tx {
772772

773773
let batched_result = BatchedTxResult {
774774
changed_keys: [
775-
namada_account::Key::wasm_code_name("test-name".to_string()),
775+
namada_account::Key::wasm_code_hash("test-name".to_string()),
776776
namada_account::Key::wasm_hash("test-name"),
777777
]
778778
.into(),

examples/make-db-migration.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,8 @@ pub fn wasm_migration(updates: &mut Vec<migrations::DbUpdateType>) {
592592
let code_key = Key::wasm_code(&new_code_hash);
593593
let code_len_key = Key::wasm_code_len(&new_code_hash);
594594
let hash_key = Key::wasm_hash(name);
595-
let code_name_key = Key::wasm_code_name(name.to_owned());
595+
let code_hash_key = Key::wasm_code_hash(name.to_owned());
596+
let code_name_key = Key::wasm_code_name(&new_code_hash);
596597

597598
updates.push(migrations::DbUpdateType::Add {
598599
key: code_key,
@@ -613,11 +614,17 @@ pub fn wasm_migration(updates: &mut Vec<migrations::DbUpdateType>) {
613614
force: false,
614615
});
615616
updates.push(migrations::DbUpdateType::Add {
616-
key: code_name_key,
617+
key: code_hash_key,
617618
cf: DbColFam::SUBSPACE,
618619
value: new_code_hash.into(),
619620
force: false,
620621
});
622+
updates.push(migrations::DbUpdateType::Add {
623+
key: code_name_key,
624+
cf: DbColFam::SUBSPACE,
625+
value: name.to_string().into(),
626+
force: false,
627+
});
621628
}
622629
// Put the allow list in storage
623630
updates.push(migrations::DbUpdateType::Add {

wasm_for_tests/tx_proposal_code/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ fn apply_tx(ctx: &mut Ctx, _tx_data: BatchedTx) -> TxResult {
2626
let wasm_code_len_key = Key::wasm_code_len(&wasm_code_hash);
2727
ctx.write(&wasm_code_len_key, 30.serialize_to_vec())?;
2828

29-
let wasm_code_name_key = Key::wasm_code_name("test".to_string());
30-
ctx.write_bytes(&wasm_code_name_key, wasm_code_name.clone())?;
29+
let wasm_code_hash_key = Key::wasm_code_hash("test".to_string());
30+
ctx.write_bytes(&wasm_code_hash_key, wasm_code_hash)?;
31+
32+
let wasm_code_name_key = Key::wasm_code_name(&wasm_code_hash);
33+
ctx.write_bytes(&wasm_code_name_key, &wasm_code_name)?;
3134

3235
let wasm_hash_key = Key::wasm_hash("test");
3336
ctx.write_bytes(&wasm_hash_key, wasm_code_name)?;

0 commit comments

Comments
 (0)