Skip to content

Commit

Permalink
refactor(remove spending counter): performance testing
Browse files Browse the repository at this point in the history
spending counter is causing erroneous and undefined and incorrect behaviour
  • Loading branch information
cong-or committed Nov 21, 2023
1 parent 3dc2665 commit 3fb2b4e
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 113 deletions.
44 changes: 22 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions explorer/src/db/indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl ExplorerTransaction {
.map(|i| i.to_enum())
.zip(witnesses)
.filter_map(|input_with_witness| match input_with_witness {
(InputEnum::AccountInput(id, value), Witness::Account(_, _)) => {
(InputEnum::AccountInput(id, value), Witness::Account(_)) => {
let kind = chain_addr::Kind::Account(
id.to_single_account()
.expect("the input to be validated")
Expand All @@ -387,7 +387,7 @@ impl ExplorerTransaction {
let address = ExplorerAddress::New(Address(context.discrimination, kind));
Some(ExplorerInput { address, value })
}
(InputEnum::AccountInput(id, value), Witness::Multisig(_, _)) => {
(InputEnum::AccountInput(id, value), Witness::Multisig(_)) => {
let kind = chain_addr::Kind::Multisig(
id.to_multi_account()
.as_ref()
Expand Down
39 changes: 5 additions & 34 deletions jcli/src/jcli_lib/transaction/mk_witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use bech32::{self, ToBase32 as _};
use chain_core::property::Serialize as _;
use chain_impl_mockchain::key::EitherEd25519SecretKey;
use chain_impl_mockchain::{
account::SpendingCounter,
accounting::account::spending::SpendingCounterIncreasing,
header::HeaderId,
transaction::{TransactionSignDataHash, Witness},
};
Expand All @@ -33,17 +31,6 @@ pub struct MkWitness {
#[structopt(long = "genesis-block-hash", parse(try_from_str))]
pub genesis_block_hash: HeaderId,

/// value is mandatory if `--type=account`. It is the counter value for
/// every time the account is being utilized.
#[structopt(long = "account-spending-counter")]
pub account_spending_counter: Option<u32>,

/// lane to use for the spending counter. Each lane has an independent
/// spending counter value.
/// If unsure, leave blank and lane 0 will be used
#[structopt(long)]
pub account_spending_counter_lane: Option<usize>,

/// the file path to the file to read the signing key from.
/// If omitted it will be read from the standard input.
pub secret: Option<PathBuf>,
Expand Down Expand Up @@ -71,24 +58,11 @@ impl std::str::FromStr for WitnessType {
impl MkWitness {
pub fn exec(self) -> Result<(), Error> {
let secret_key = read_ed25519_secret_key_from_file(&self.secret)?;
let sc = self
.account_spending_counter
.map(|counter| {
let lane = self.account_spending_counter_lane.unwrap_or_default();
if lane > SpendingCounterIncreasing::LANES {
return Err(Error::MakeWitnessAccountInvalidCounterLane {
max: SpendingCounterIncreasing::LANES,
actual: lane,
});
}
Ok(SpendingCounter::new(lane, counter))
})
.transpose()?;

let witness = make_witness(
&self.witness_type,
&self.genesis_block_hash,
&self.sign_data_hash,
sc,
&secret_key,
)?;
self.write_witness(&witness)
Expand Down Expand Up @@ -117,7 +91,7 @@ pub fn make_witness(
witness_type: &WitnessType,
genesis_block_hash: &HeaderId,
sign_data_hash: &TransactionSignDataHash,
account_spending_counter: Option<SpendingCounter>,

secret_key: &EitherEd25519SecretKey,
) -> Result<Witness, Error> {
let witness = match witness_type {
Expand All @@ -130,12 +104,9 @@ pub fn make_witness(
|d| (secret_key.to_public(), secret_key.sign(d)),
&[0; 32],
),
WitnessType::Account => Witness::new_account(
genesis_block_hash,
sign_data_hash,
account_spending_counter.ok_or(Error::MakeWitnessAccountCounterMissing)?,
|d| secret_key.sign(d),
),
WitnessType::Account => {
Witness::new_account(genesis_block_hash, sign_data_hash, |d| secret_key.sign(d))
}
};
Ok(witness)
}
4 changes: 1 addition & 3 deletions jcli/src/jcli_lib/transaction/simplified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{rest, transaction};
use chain_addr::Kind;
use chain_core::property::FromStr;
use chain_crypto::{Ed25519, Ed25519Extended, PublicKey, SecretKey};
use chain_impl_mockchain::account::SpendingCounter;
use chain_impl_mockchain::fee::FeeAlgorithm;
use chain_impl_mockchain::key::EitherEd25519SecretKey;
use chain_impl_mockchain::transaction::Output;
Expand Down Expand Up @@ -204,7 +203,7 @@ pub fn make_transaction(
let transaction_sign_data_hash = transaction.transaction_sign_data_hash()?;

// get spending counter
let account_state = rest::v0::account::request_account_information(
let _account_state = rest::v0::account::request_account_information(
rest_args,
AccountId::try_from_str(&sender_account.to_string())?,
)?;
Expand All @@ -214,7 +213,6 @@ pub fn make_transaction(
&WitnessType::Account,
&block0_hash,
&transaction_sign_data_hash,
Some(SpendingCounter::from(account_state.counters()[0])),
&secret_key,
)?;

Expand Down
22 changes: 0 additions & 22 deletions jormungandr-lib/src/interfaces/account_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ impl LastRewards {
pub struct AccountState {
delegation: DelegationType,
value: Value,
counters: Vec<u32>,
tokens: BTreeMap<TokenIdentifier, Value>,
last_rewards: LastRewards,
}
Expand All @@ -110,15 +109,6 @@ impl AccountState {
&self.value
}

/// The transaction counters for spending lanes.
/// A counter in one of the existing lanes is used as part of the parameter
/// when adding a new account input to a transaction.
///
#[inline]
pub fn counters(&self) -> Vec<u32> {
self.counters.clone()
}

/// the last rewards transfered to account
#[inline]
pub fn last_rewards(&self) -> &LastRewards {
Expand Down Expand Up @@ -157,12 +147,6 @@ impl<E> From<account::AccountState<E>> for AccountState {
AccountState {
delegation: account.delegation().clone().into(),
value: account.value().into(),
counters: account
.spending
.get_valid_counters()
.into_iter()
.map(Into::into)
.collect(),
tokens: account
.tokens
.iter()
Expand All @@ -183,12 +167,6 @@ impl<'a, E> From<&'a account::AccountState<E>> for AccountState {
AccountState {
delegation: account.delegation().clone().into(),
value: account.value().into(),
counters: account
.spending
.get_valid_counters()
.into_iter()
.map(Into::into)
.collect(),
tokens: account
.tokens
.iter()
Expand Down
2 changes: 1 addition & 1 deletion jormungandr-lib/src/interfaces/transaction_witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ mod test {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
match u8::arbitrary(g) % 3 {
0 => Witness::Utxo(Arbitrary::arbitrary(g)).into(),
1 => Witness::Account(Arbitrary::arbitrary(g), Arbitrary::arbitrary(g)).into(),
1 => Witness::Account(Arbitrary::arbitrary(g)).into(),
2 => {
use crate::crypto::key::KeyPair;
use chain_crypto::Ed25519Bip32;
Expand Down
2 changes: 0 additions & 2 deletions testing/jormungandr-automation/src/jcli/api/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ impl Transaction {
&witness.block_hash.to_hex(),
&witness.transaction_id.to_hex(),
witness.addr_type,
witness.account_spending_counter,
&witness.file,
&witness.private_key_path,
)
Expand All @@ -188,7 +187,6 @@ impl Transaction {
&witness.block_hash.to_hex(),
&witness.transaction_id.to_hex(),
witness.addr_type,
witness.account_spending_counter,
&witness.file,
&witness.private_key_path,
)
Expand Down
Loading

0 comments on commit 3fb2b4e

Please sign in to comment.