Skip to content

Commit

Permalink
Removes vp from UpdateAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Feb 8, 2024
1 parent 9f3ed2c commit adb81d5
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 126 deletions.
7 changes: 2 additions & 5 deletions crates/account/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ pub struct InitAccount {
/// account.
pub public_keys: Vec<common::PublicKey>,
/// The VP code hash
// FIXME: remove this too?
pub vp_code_hash: Hash,
/// The account signature threshold
pub threshold: u8,
}

/// A tx data type to update an account's validity predicate
/// A tx data type to update an account's signature threshold and keys
#[derive(
Debug,
Clone,
Expand All @@ -40,8 +41,6 @@ pub struct InitAccount {
pub struct UpdateAccount {
/// An address of the account
pub addr: Address,
/// The new VP code hash
pub vp_code_hash: Option<Hash>,
/// Public keys to be written into the account's storage. This can be used
/// for signature verification of transactions for the newly created
/// account.
Expand Down Expand Up @@ -84,13 +83,11 @@ pub mod tests {
public_keys in collection::vec(arb_common_pk(), 0..10),
)(
addr in arb_non_internal_address(),
vp_code_hash in option::of(arb_hash()),
threshold in option::of(0..=public_keys.len() as u8),
public_keys in Just(public_keys),
) -> UpdateAccount {
UpdateAccount {
addr,
vp_code_hash,
public_keys,
threshold,
}
Expand Down
3 changes: 0 additions & 3 deletions crates/apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4369,7 +4369,6 @@ pub mod args {
let chain_ctx = ctx.borrow_mut_chain_or_exit();
TxUpdateAccount::<SdkTypes> {
tx,
vp_code_path: self.vp_code_path,
tx_code_path: self.tx_code_path,
addr: chain_ctx.get(&self.addr),
public_keys: self
Expand All @@ -4385,14 +4384,12 @@ pub mod args {
impl Args for TxUpdateAccount<CliTypes> {
fn parse(matches: &ArgMatches) -> Self {
let tx = Tx::parse(matches);
let vp_code_path = CODE_PATH_OPT.parse(matches);
let addr = ADDRESS.parse(matches);
let tx_code_path = PathBuf::from(TX_UPDATE_ACCOUNT_WASM);
let public_keys = PUBLIC_KEYS.parse(matches);
let threshold = THRESHOLD.parse(matches);
Self {
tx,
vp_code_path,
addr,
tx_code_path,
public_keys,
Expand Down
15 changes: 1 addition & 14 deletions crates/benches/txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,29 +366,16 @@ fn reveal_pk(c: &mut Criterion) {

fn update_account(c: &mut Criterion) {
let shell = BenchShell::default();
let vp_code_hash: Hash = shell
.read_storage_key(&Key::wasm_hash(VP_USER_WASM))
.unwrap();
let extra_section = Section::ExtraData(Code::from_hash(
vp_code_hash,
Some(VP_USER_WASM.to_string()),
));
let data = UpdateAccount {
addr: defaults::albert_address(),
vp_code_hash: Some(Hash(
extra_section
.hash(&mut sha2::Sha256::new())
.finalize_reset()
.into(),
)),
public_keys: vec![defaults::albert_keypair().ref_to()],
threshold: None,
};
let vp = shell.generate_tx(
TX_UPDATE_ACCOUNT_WASM,
data,
None,
Some(vec![extra_section]),
None,
vec![&defaults::albert_keypair()],
);

Expand Down
29 changes: 2 additions & 27 deletions crates/benches/vps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use namada::governance::VoteProposalData;
use namada::ledger::gas::{TxGasMeter, VpGasMeter};
use namada::token::{Amount, Transfer};
use namada::tx::data::pos::{Bond, CommissionChange};
use namada::tx::{Code, Section};
use namada::types::hash::Hash;
use namada::types::key::ed25519;
use namada::types::storage::{Key, TxIndex};
Expand All @@ -23,7 +22,6 @@ use namada_apps::bench_utils::{
VP_USER_WASM,
};
use namada_apps::wallet::defaults;
use sha2::Digest;

const VP_IMPLICIT_WASM: &str = "vp_implicit.wasm";

Expand Down Expand Up @@ -67,29 +65,16 @@ fn vp_user(c: &mut Criterion) {
vec![&defaults::bertha_keypair()],
);

let vp_validator_hash = shell
.read_storage_key(&Key::wasm_hash(VP_USER_WASM))
.unwrap();
let extra_section = Section::ExtraData(Code::from_hash(
vp_validator_hash,
Some(VP_USER_WASM.to_string()),
));
let data = UpdateAccount {
addr: defaults::albert_address(),
vp_code_hash: Some(Hash(
extra_section
.hash(&mut sha2::Sha256::new())
.finalize_reset()
.into(),
)),
public_keys: vec![defaults::albert_keypair().to_public()],
threshold: None,
};
let vp = shell.generate_tx(
TX_UPDATE_ACCOUNT_WASM,
data,
None,
Some(vec![extra_section]),
None,
vec![&defaults::albert_keypair()],
);

Expand Down Expand Up @@ -356,26 +341,16 @@ fn vp_validator(c: &mut Criterion) {
vec![&defaults::bertha_keypair()],
);

let extra_section = Section::ExtraData(Code::from_hash(
vp_code_hash,
Some(VP_USER_WASM.to_string()),
));
let data = UpdateAccount {
addr: defaults::validator_address(),
vp_code_hash: Some(Hash(
extra_section
.hash(&mut sha2::Sha256::new())
.finalize_reset()
.into(),
)),
public_keys: vec![defaults::validator_account_keypair().to_public()],
threshold: None,
};
let vp = shell.generate_tx(
TX_UPDATE_ACCOUNT_WASM,
data,
None,
Some(vec![extra_section]),
None,
vec![&defaults::validator_account_keypair()],
);

Expand Down
2 changes: 0 additions & 2 deletions crates/light_sdk/src/transaction/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,12 @@ impl UpdateAccount {
/// Build a raw UpdateAccount transaction from the given parameters
pub fn new(
addr: Address,
vp_code_hash: Option<Hash>,
public_keys: Vec<common::PublicKey>,
threshold: Option<u8>,
args: GlobalArgs,
) -> Self {
let update_account = namada_sdk::account::UpdateAccount {
addr,
vp_code_hash,
public_keys,
threshold,
};
Expand Down
10 changes: 0 additions & 10 deletions crates/sdk/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,8 +798,6 @@ pub struct TxInitValidator<C: NamadaTypes = SdkTypes> {
pub struct TxUpdateAccount<C: NamadaTypes = SdkTypes> {
/// Common tx arguments
pub tx: Tx<C>,
/// Path to the VP WASM code file
pub vp_code_path: Option<PathBuf>,
/// Path to the TX WASM code file
pub tx_code_path: PathBuf,
/// Address of the account whose VP is to be updated
Expand All @@ -823,14 +821,6 @@ impl<C: NamadaTypes> TxBuilder<C> for TxUpdateAccount<C> {
}

impl<C: NamadaTypes> TxUpdateAccount<C> {
/// Path to the VP WASM code file
pub fn vp_code_path(self, vp_code_path: PathBuf) -> Self {
Self {
vp_code_path: Some(vp_code_path),
..self
}
}

/// Path to the TX WASM code file
pub fn tx_code_path(self, tx_code_path: PathBuf) -> Self {
Self {
Expand Down
10 changes: 2 additions & 8 deletions crates/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ pub trait Namada: Sized + MaybeSync + MaybeSend {
fn new_update_account(&self, addr: Address) -> args::TxUpdateAccount {
args::TxUpdateAccount {
addr,
vp_code_path: None,
public_keys: vec![],
threshold: None,
tx_code_path: PathBuf::from(TX_UPDATE_ACCOUNT_WASM),
Expand Down Expand Up @@ -1186,20 +1185,15 @@ pub mod testing {
}

prop_compose! {
// Generate an arbitrary account initialization transaction
// Generate an arbitrary account update transaction
pub fn arb_update_account_tx()(
mut header in arb_header(),
wrapper in arb_wrapper_tx(),
mut update_account in arb_update_account(),
extra_data in arb_code(),
update_account in arb_update_account(),
code_hash in arb_hash(),
) -> (Tx, TxData) {
header.tx_type = TxType::Wrapper(Box::new(wrapper));
let mut tx = Tx { header, sections: vec![] };
if let Some(vp_code_hash) = &mut update_account.vp_code_hash {
let new_code_hash = tx.add_section(Section::ExtraData(extra_data)).get_hash();
*vp_code_hash = new_code_hash;
}
tx.add_data(update_account.clone());
tx.add_code_from_hash(code_hash, Some(TX_UPDATE_ACCOUNT_WASM.to_owned()));
(tx, TxData::UpdateAccount(update_account))
Expand Down
26 changes: 0 additions & 26 deletions crates/sdk/src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,26 +1198,6 @@ pub async fn to_ledger_vector(
)])
}

let vp_code_data = match &update_account.vp_code_hash {
Some(hash) => {
let extra = tx
.get_section(hash)
.and_then(|x| Section::extra_data_sec(x.as_ref()))
.ok_or_else(|| {
Error::Other("unable to load vp code".to_string())
})?;
let vp_code = if extra.tag == Some(VP_USER_WASM.to_string()) {
"User".to_string()
} else {
HEXLOWER.encode(&extra.code.hash().0)
};
Some((vp_code, extra.code.hash()))
}
None => None,
};
if let Some((vp_code, _)) = &vp_code_data {
tv.output.extend(vec![format!("VP type : {}", vp_code)]);
}
tv.output_expert
.extend(vec![format!("Address : {}", update_account.addr)]);
tv.output_expert.extend(
Expand All @@ -1230,12 +1210,6 @@ pub async fn to_ledger_vector(
tv.output_expert
.extend(vec![format!("Threshold : {}", threshold,)])
}
if let Some((_, extra_code_hash)) = vp_code_data {
tv.output_expert.extend(vec![format!(
"VP type : {}",
HEXLOWER.encode(&extra_code_hash.0)
)]);
}
} else if code_sec.tag == Some(TX_TRANSFER_WASM.to_string()) {
let transfer = Transfer::try_from_slice(
&tx.data()
Expand Down
32 changes: 1 addition & 31 deletions crates/sdk/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2521,7 +2521,6 @@ pub async fn build_update_account(
context: &impl Namada,
args::TxUpdateAccount {
tx: tx_args,
vp_code_path,
tx_code_path,
addr,
public_keys,
Expand Down Expand Up @@ -2549,53 +2548,24 @@ pub async fn build_update_account(
)));
};

let vp_code_hash = match vp_code_path {
Some(code_path) => {
let vp_hash = query_wasm_code_hash_buf(context, code_path).await?;
Some(vp_hash)
}
None => None,
};

let chain_id = tx_args.chain_id.clone().unwrap();
let mut tx = Tx::new(chain_id, tx_args.expiration);
if let Some(memo) = &tx_args.memo {
tx.add_memo(memo);
}
let extra_section_hash = vp_code_path.as_ref().zip(vp_code_hash).map(
|(code_path, vp_code_hash)| {
tx.add_extra_section_from_hash(
vp_code_hash,
Some(code_path.to_string_lossy().into_owned()),
)
},
);

let data = UpdateAccount {
addr,
vp_code_hash: extra_section_hash,
public_keys: public_keys.clone(),
threshold: *threshold,
};

let add_code_hash = |tx: &mut Tx, data: &mut UpdateAccount| {
let extra_section_hash = vp_code_path.as_ref().zip(vp_code_hash).map(
|(code_path, vp_code_hash)| {
tx.add_extra_section_from_hash(
vp_code_hash,
Some(code_path.to_string_lossy().into_owned()),
)
},
);
data.vp_code_hash = extra_section_hash;
Ok(())
};
build(
context,
tx_args,
tx_code_path.clone(),
data,
add_code_hash,
do_nothing,
&signing_data.fee_payer,
None,
)
Expand Down

0 comments on commit adb81d5

Please sign in to comment.