Skip to content

Commit

Permalink
update testing wallet to partially support multiple spending counter …
Browse files Browse the repository at this point in the history
…lanes
  • Loading branch information
zeegomo committed Nov 25, 2021
1 parent e804752 commit 5f7cb03
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::wallet::Wallet;
use assert_cmd::assert::OutputAssertExt;
use assert_fs::TempDir;
use chain_core::property::Deserialize;
use chain_impl_mockchain::{fee::LinearFee, fragment::Fragment};
use chain_impl_mockchain::{account::SpendingCounter, fee::LinearFee, fragment::Fragment};
use jormungandr_lib::{
crypto::hash::Hash,
interfaces::{BlockDate, LegacyUTxO, UTxOInfo, Value},
Expand Down Expand Up @@ -173,7 +173,7 @@ impl Transaction {
&witness.block_hash.to_hex(),
&witness.transaction_id.to_hex(),
&witness.addr_type,
witness.spending_account_counter,
witness.account_spending_counter,
&witness.file,
&witness.private_key_path,
)
Expand All @@ -188,7 +188,7 @@ impl Transaction {
&witness.block_hash.to_hex(),
&witness.transaction_id.to_hex(),
&witness.addr_type,
witness.spending_account_counter,
witness.account_spending_counter,
&witness.file,
&witness.private_key_path,
)
Expand All @@ -211,7 +211,7 @@ impl Transaction {
genesis_hash,
&account.signing_key().to_bech32_str(),
"account",
Some(account.internal_counter().into()),
Some(account.internal_counter()),
staging_file,
),
Wallet::UTxO(utxo) => self.create_witness_from_key(
Expand Down Expand Up @@ -239,7 +239,7 @@ impl Transaction {
genesis_hash: Hash,
private_key: &str,
addr_type: &str,
spending_key: Option<u32>,
spending_counter: Option<SpendingCounter>,
staging_file: P,
) -> Witness {
let transaction_id = self.id(staging_file);
Expand All @@ -249,7 +249,7 @@ impl Transaction {
&transaction_id,
addr_type,
private_key,
spending_key,
spending_counter,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chain_impl_mockchain::fee::LinearFee;
use chain_impl_mockchain::{account::SpendingCounter, fee::LinearFee};
use std::path::Path;
use std::process::Command;

Expand Down Expand Up @@ -108,12 +108,11 @@ impl TransactionCommand {
block0_hash: &str,
tx_id: &str,
addr_type: &str,
spending_account_counter: Option<u32>,
account_spending_counter: Option<SpendingCounter>,
witness_file: P,
witness_key: Q,
) -> Self {
let spending_counter = spending_account_counter.unwrap_or(0);

let spending_counter = account_spending_counter.unwrap_or_else(SpendingCounter::zero);
self.command
.arg("make-witness")
.arg("--genesis-block-hash")
Expand All @@ -123,7 +122,9 @@ impl TransactionCommand {
.arg(&tx_id)
.arg(witness_file.as_ref())
.arg("--account-spending-counter")
.arg(spending_counter.to_string())
.arg(spending_counter.unlaned_counter().to_string())
.arg("--account-spending-counter-lane")
.arg(spending_counter.lane().to_string())
.arg(witness_key.as_ref());
self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::wallet::Wallet;
use assert_fs::fixture::ChildPath;
use assert_fs::{prelude::*, TempDir};
use chain_core::property::Deserialize;
use chain_impl_mockchain::{fee::LinearFee, fragment::Fragment};
use chain_impl_mockchain::{account::SpendingCounter, fee::LinearFee, fragment::Fragment};
use jormungandr_lib::{
crypto::hash::Hash,
interfaces::BlockDate,
Expand Down Expand Up @@ -199,7 +199,7 @@ impl TransactionBuilder {
&mut self,
private_key: &str,
transaction_type: &str,
spending_key: Option<u32>,
spending_key: Option<SpendingCounter>,
) -> &mut Self {
let witness = self.create_witness_from_key(private_key, transaction_type, spending_key);
self.seal_with_witness(&witness);
Expand Down Expand Up @@ -230,7 +230,7 @@ impl TransactionBuilder {
Wallet::Account(account) => self.create_witness_from_key(
&account.signing_key().to_bech32_str(),
"account",
Some(account.internal_counter().into()),
Some(account.internal_counter()),
),
Wallet::UTxO(utxo) => {
self.create_witness_from_key(&utxo.last_signing_key().to_bech32_str(), "utxo", None)
Expand All @@ -247,7 +247,7 @@ impl TransactionBuilder {
&self,
private_key: &str,
addr_type: &str,
spending_key: Option<u32>,
spending_key: Option<SpendingCounter>,
) -> Witness {
let transaction_id = self.transaction_id();
Witness::new(
Expand All @@ -260,7 +260,11 @@ impl TransactionBuilder {
)
}

pub fn create_witness_default(&self, addr_type: &str, spending_key: Option<u32>) -> Witness {
pub fn create_witness_default(
&self,
addr_type: &str,
spending_key: Option<SpendingCounter>,
) -> Witness {
let private_key = self.jcli.key().generate_default();
self.create_witness_from_key(&private_key, addr_type, spending_key)
}
Expand Down
10 changes: 5 additions & 5 deletions testing/jormungandr-testing-utils/src/testing/witness.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use chain_impl_mockchain::account::SpendingCounter;
use jormungandr_lib::crypto::hash::Hash;
use serde_derive::{Deserialize, Serialize};

use assert_fs::fixture::PathChild;
use assert_fs::prelude::*;
use std::path::PathBuf;

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug)]
pub struct Witness {
pub block_hash: Hash,
pub transaction_id: Hash,
pub addr_type: String,
pub private_key_path: PathBuf,
pub spending_account_counter: Option<u32>,
pub account_spending_counter: Option<SpendingCounter>,
pub file: PathBuf,
}

Expand All @@ -22,15 +22,15 @@ impl Witness {
transaction_id: &Hash,
addr_type: &str,
private_key: &str,
spending_account_counter: Option<u32>,
account_spending_counter: Option<SpendingCounter>,
) -> Witness {
Witness {
block_hash: *block_hash,
transaction_id: *transaction_id,
addr_type: addr_type.to_string(),
private_key_path: write_witness_key(temp_dir, private_key),
file: temp_dir.child("witness").path().into(),
spending_account_counter,
account_spending_counter,
}
}
}
Expand Down
47 changes: 31 additions & 16 deletions testing/jormungandr-testing-utils/src/wallet/account.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::testing::FragmentBuilderError;
use chain_addr::Discrimination;
use chain_impl_mockchain::{
account,
account::SpendingCounter,
accounting::account::SpendingCounterIncreasing,
fee::{FeeAlgorithm, LinearFee},
transaction::{
Balance, Input, InputOutputBuilder, Payload, PayloadSlice, TransactionSignDataHash,
Expand Down Expand Up @@ -29,7 +30,7 @@ pub struct Wallet {

/// the counter as we know of this value needs to be in sync
/// with what is in the blockchain
internal_counter: account::SpendingCounter,
internal_counters: SpendingCounterIncreasing,

discrimination: Discrimination,
}
Expand All @@ -44,18 +45,23 @@ impl Wallet {
Wallet {
signing_key,
identifier,
internal_counter: account::SpendingCounter::zero(),
internal_counters: SpendingCounterIncreasing::default(),
discrimination,
}
}

pub fn from_existing_account(bech32_str: &str, spending_counter: Option<u32>) -> Self {
pub fn from_existing_account(
bech32_str: &str,
spending_counter: Option<SpendingCounter>,
) -> Self {
let signing_key = SigningKey::from_bech32_str(bech32_str).expect("bad bech32");
let identifier = signing_key.identifier();
Wallet {
signing_key,
identifier,
internal_counter: spending_counter.unwrap_or(0).into(),
internal_counters: SpendingCounterIncreasing::new_from_counter(
spending_counter.unwrap_or_else(|| SpendingCounter::zero()),
),
discrimination: Discrimination::Test,
}
}
Expand All @@ -68,22 +74,31 @@ impl Wallet {
self.identifier().to_address(self.discrimination).into()
}

pub fn set_counter(&mut self, value: u32) {
self.internal_counter = account::SpendingCounter::from(value);
pub fn set_counter(&mut self, counter: SpendingCounter) {
let mut counters = self.internal_counters.get_valid_counters();
counters[counter.lane()] = counter;
self.internal_counters = SpendingCounterIncreasing::new_from_counters(counters).unwrap();
}

pub fn increment_counter(&mut self) {
let v: u32 = self.internal_counter.into();
self.internal_counter = account::SpendingCounter::from(v + 1);
pub fn increment_counter(&mut self, lane: usize) {
self.internal_counters
.next_verify(self.internal_counters.get_valid_counters()[lane])
.unwrap();
}

pub fn decrement_counter(&mut self) {
let v: u32 = self.internal_counter.into();
self.internal_counter = account::SpendingCounter::from(v - 1);
pub fn decrement_counter(&mut self, lane: usize) {
self.set_counter(SpendingCounter::from(
<u32>::from(self.internal_counters()[lane]) - 1,
))
}

pub fn internal_counter(&self) -> account::SpendingCounter {
self.internal_counter
/// Use the default counter
pub fn internal_counter(&self) -> SpendingCounter {
self.internal_counters.get_valid_counter()
}

pub fn internal_counters(&self) -> Vec<SpendingCounter> {
self.internal_counters.get_valid_counters()
}

pub fn stake_key(&self) -> UnspecifiedAccountIdentifier {
Expand All @@ -106,7 +121,7 @@ impl Wallet {
Witness::new_account(
&(*block0_hash).into_hash(),
signing_data,
self.internal_counter(),
self.internal_counters.get_valid_counter(),
|d| self.signing_key().as_ref().sign(d),
)
}
Expand Down
12 changes: 7 additions & 5 deletions testing/jormungandr-testing-utils/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub enum WalletError {
InvalidBech32Key { expected: String, actual: String },
}

const DEFAULT_LANE: usize = 0;

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum Wallet {
Expand All @@ -81,7 +83,7 @@ impl Wallet {

pub fn import_account<P: AsRef<Path>>(
secret_key_file: P,
spending_counter: Option<u32>,
spending_counter: Option<SpendingCounter>,
) -> Wallet {
let bech32_str = jortestkit::file::read_file(secret_key_file);
Wallet::Account(account::Wallet::from_existing_account(
Expand All @@ -102,7 +104,7 @@ impl Wallet {

pub fn from_existing_account(
signing_key_bech32: &str,
spending_counter: Option<u32>,
spending_counter: Option<SpendingCounter>,
) -> Wallet {
Wallet::Account(account::Wallet::from_existing_account(
signing_key_bech32,
Expand Down Expand Up @@ -270,14 +272,14 @@ impl Wallet {

pub fn confirm_transaction(&mut self) {
match self {
Wallet::Account(account) => account.increment_counter(),
Wallet::Account(account) => account.increment_counter(DEFAULT_LANE),
_ => unimplemented!(),
}
}

pub fn decrement_counter(&mut self) {
match self {
Wallet::Account(account) => account.decrement_counter(),
Wallet::Account(account) => account.decrement_counter(DEFAULT_LANE),
_ => unimplemented!(),
}
}
Expand Down Expand Up @@ -469,7 +471,7 @@ impl Wallet {
))
}

pub fn update_counter(&mut self, counter: u32) {
pub fn update_counter(&mut self, counter: SpendingCounter) {
if let Wallet::Account(account) = self {
account.set_counter(counter)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl AdversaryAll {
let title = "adversary load transactions";
let mut faucet = Wallet::import_account(
self.faucet_key_file.clone(),
Some(self.faucet_spending_counter),
Some(self.faucet_spending_counter.into()),
);
let mut builder = RemoteJormungandrBuilder::new("node".to_owned());
builder.with_rest(self.endpoint.parse().unwrap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl VotesOnly {
let title = "adversary load transactions";
let faucet = Wallet::import_account(
self.faucet_key_file.clone(),
Some(self.faucet_spending_counter),
Some(self.faucet_spending_counter.into()),
);
let block0 = get_block(&self.block0_path)?;
let vote_plans = block0.vote_plans();
Expand Down
6 changes: 4 additions & 2 deletions testing/mjolnir/src/mjolnir_app/fragment/batch/tx_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ pub struct TxOnly {

impl TxOnly {
pub fn exec(&self) -> Result<(), MjolnirError> {
let mut faucet =
Wallet::import_account(&self.faucet_key_file, Some(self.faucet_spending_counter));
let mut faucet = Wallet::import_account(
&self.faucet_key_file,
Some(self.faucet_spending_counter.into()),
);
let mut builder = RemoteJormungandrBuilder::new("node".to_owned());
builder.with_rest(self.endpoint.parse().unwrap());
let remote_jormungandr = builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl AllAdversary {
let title = "adversary load transactions";
let mut faucet = Wallet::import_account(
self.faucet_key_file.clone(),
Some(self.faucet_spending_counter),
Some(self.faucet_spending_counter.into()),
);
let mut builder = RemoteJormungandrBuilder::new("node".to_owned());
builder.with_rest(self.endpoint.parse().unwrap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl VotesOnly {
let title = "adversary load transactions";
let faucet = Wallet::import_account(
self.faucet_key_file.clone(),
Some(self.faucet_spending_counter),
Some(self.faucet_spending_counter.into()),
);
let block0 = get_block(&self.block0_path)?;
let vote_plans = block0.vote_plans();
Expand Down
6 changes: 4 additions & 2 deletions testing/mjolnir/src/mjolnir_app/fragment/standard/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ pub struct AllFragments {
impl AllFragments {
pub fn exec(&self) -> Result<(), MjolnirError> {
let title = "all fragment load test";
let faucet =
Wallet::import_account(&self.faucet_key_file, Some(self.faucet_spending_counter));
let faucet = Wallet::import_account(
&self.faucet_key_file,
Some(self.faucet_spending_counter.into()),
);
let receiver = startup::create_new_account_address();
let mut builder = RemoteJormungandrBuilder::new("node".to_string());
builder.with_rest(self.endpoint.parse().unwrap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl TxOnly {
let title = "standard load only transactions";
let mut faucet = Wallet::import_account(
self.faucet_key_file.clone(),
Some(self.faucet_spending_counter),
Some(self.faucet_spending_counter.into()),
);
let mut builder = RemoteJormungandrBuilder::new("node".to_owned());
builder.with_rest(self.endpoint.parse().unwrap());
Expand Down

0 comments on commit 5f7cb03

Please sign in to comment.