Skip to content

Commit

Permalink
Restore vp updates in vp_user
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Feb 13, 2024
1 parent 2108c6d commit e18ae94
Showing 1 changed file with 81 additions and 7 deletions.
88 changes: 81 additions & 7 deletions wasm/wasm_source/src/vp_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,14 @@ fn validate_tx(
KeyType::PoS => validate_pos_changes(ctx, &addr, key, &valid_sig)?,
KeyType::PgfSteward(address) => address != &addr || *valid_sig,
KeyType::GovernanceVote(voter) => voter != &addr || *valid_sig,
KeyType::Vp(owner) => owner != &addr,
KeyType::Vp(owner) => {
let has_post: bool = ctx.has_key_post(key)?;
if owner == &addr {
has_post && *valid_sig
} else {
true
}
}
KeyType::Masp | KeyType::Ibc => true,
KeyType::Unknown => {
// Unknown changes require a valid signature
Expand Down Expand Up @@ -330,6 +337,7 @@ mod tests {
use namada::tx::{Code, Data, Signature};
use namada::types::dec::Dec;
use namada::types::storage::Epoch;
use namada_test_utils::TestWasms;
// Use this as `#[test]` annotation to enable logging
use namada_tests::log::test;
use namada_tests::native_vp::pos::init_pos;
Expand Down Expand Up @@ -1257,15 +1265,20 @@ mod tests {
let mut tx_env = TestTxEnv::default();

let vp_owner = address::testing::established_address_1();
let vp_code = TestWasms::VpAlwaysTrue.read_bytes();
let vp_hash = sha256(&vp_code);
// for the update
tx_env.store_wasm_code(vp_code);

// Spawn the accounts to be able to modify their storage
tx_env.spawn_accounts([&vp_owner]);

// Initialize VP environment from a transaction
vp_host_env::init_from_tx(vp_owner.clone(), tx_env, |address| {
// Update threshold in a transaction
let threshold_key = account::threshold_key(address);
tx::ctx().write(&threshold_key, 10).unwrap();
// Update VP in a transaction
tx::ctx()
.update_validity_predicate(address, vp_hash, &None)
.unwrap();
});

let vp_env = vp_host_env::take();
Expand Down Expand Up @@ -1293,16 +1306,77 @@ mod tests {
let vp_owner = address::testing::established_address_1();
let keypair = key::testing::keypair_1();
let public_key = keypair.ref_to();
let vp_code = TestWasms::VpAlwaysTrue.read_bytes();
let vp_hash = sha256(&vp_code);
// for the update
tx_env.store_wasm_code(vp_code);

// Spawn the accounts to be able to modify their storage
tx_env.spawn_accounts([&vp_owner]);
tx_env.init_account_storage(&vp_owner, vec![public_key.clone()], 1);

// Initialize VP environment from a transaction
vp_host_env::init_from_tx(vp_owner.clone(), tx_env, |address| {
// Update threshold in a transaction
let threshold_key = account::threshold_key(address);
tx::ctx().write(&threshold_key, 10).unwrap();
// Update VP in a transaction
tx::ctx()
.update_validity_predicate(address, vp_hash, &None)
.unwrap();
});

let pks_map = AccountPublicKeysMap::from_iter(vec![public_key]);

let mut vp_env = vp_host_env::take();
let mut tx = vp_env.tx.clone();
tx.set_data(Data::new(vec![]));
tx.set_code(Code::new(vec![], None));
tx.add_section(Section::Signature(Signature::new(
vec![tx.raw_header_hash()],
pks_map.index_secret_keys(vec![keypair]),
None,
)));
let signed_tx = tx.clone();
vp_env.tx = signed_tx.clone();
let keys_changed: BTreeSet<storage::Key> =
vp_env.all_touched_storage_keys();
let verifiers: BTreeSet<Address> = BTreeSet::default();
vp_host_env::set(vp_env);
assert!(
validate_tx(&CTX, signed_tx, vp_owner, keys_changed, verifiers)
.unwrap()
);
}

/// Test that a validity predicate update is accepted if allowed
#[test]
fn test_signed_vp_update_allowed_accepted() {
// Initialize a tx environment
let mut tx_env = TestTxEnv::default();

let vp_owner = address::testing::established_address_1();
let keypair = key::testing::keypair_1();
let public_key = keypair.ref_to();
let vp_code = TestWasms::VpAlwaysTrue.read_bytes();
let vp_hash = sha256(&vp_code);
// for the update
tx_env.store_wasm_code(vp_code);

tx_env.init_parameters(
None,
Some(vec![vp_hash.to_string()]),
None,
None,
);

// Spawn the accounts to be able to modify their storage
tx_env.spawn_accounts([&vp_owner]);
tx_env.init_account_storage(&vp_owner, vec![public_key.clone()], 1);

// Initialize VP environment from a transaction
vp_host_env::init_from_tx(vp_owner.clone(), tx_env, |address| {
// Update VP in a transaction
tx::ctx()
.update_validity_predicate(address, vp_hash, &None)
.unwrap();
});

let pks_map = AccountPublicKeysMap::from_iter(vec![public_key]);
Expand Down

0 comments on commit e18ae94

Please sign in to comment.