Skip to content

Commit c21b30d

Browse files
committed
Added new storage key that allows looking up a wasm name from a hash
1 parent 11e849d commit c21b30d

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

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
@@ -418,15 +418,17 @@ where
418418
let code_key = Key::wasm_code(&code_hash);
419419
let code_len_key = Key::wasm_code_len(&code_hash);
420420
let hash_key = Key::wasm_hash(name);
421-
let code_name_key = Key::wasm_code_name(name.to_owned());
421+
let code_hash_key = Key::wasm_code_hash(name.to_owned());
422+
let code_name_key = Key::wasm_code_name(&code_hash);
422423

423424
self.state.write(&code_key, code).unwrap();
424425
self.state.write(&code_len_key, code_len).unwrap();
425426
self.state.write(&hash_key, code_hash).unwrap();
426427
if &Some(code_hash) == implicit_vp_code_hash {
427428
is_implicit_vp_stored = true;
428429
}
429-
self.state.write(&code_name_key, code_hash).unwrap();
430+
self.state.write(&code_hash_key, code_hash).unwrap();
431+
self.state.write(&code_name_key, name).unwrap();
430432
} else {
431433
tracing::warn!("The wasm {name} isn't allowed.");
432434
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)