From 2f5fed19be233fafa79b50355f5683224b0f3530 Mon Sep 17 00:00:00 2001 From: Rosina Fitz Hobby Date: Mon, 10 Mar 2025 17:48:20 -0400 Subject: [PATCH 01/19] stub for commit reveal background sending --- .../sources/ol_sources/secret_bid.move | 14 +-- tools/query/src/chain_queries.rs | 10 +++ tools/txs/src/stream/bid_commit_reveal.rs | 86 ++++++++++++++++++- tools/txs/src/stream/epoch_tickle_poll.rs | 8 +- tools/txs/src/stream/mod.rs | 1 + tools/txs/src/txs_cli.rs | 2 +- tools/txs/src/txs_cli_stream.rs | 25 +++--- tools/wallet/src/load_keys.rs | 1 - util/fixtures/mnemonic/alice.mnem | 2 +- 9 files changed, 123 insertions(+), 26 deletions(-) diff --git a/framework/libra-framework/sources/ol_sources/secret_bid.move b/framework/libra-framework/sources/ol_sources/secret_bid.move index 105797288..200140637 100644 --- a/framework/libra-framework/sources/ol_sources/secret_bid.move +++ b/framework/libra-framework/sources/ol_sources/secret_bid.move @@ -68,14 +68,14 @@ module ol_framework::secret_bid { /// Transaction entry function for committing bid public entry fun commit(user: &signer, digest: vector) acquires CommittedBid { - // don't allow commiting within reveal window + // don't allow committing within reveal window assert!(!in_reveal_window(), error::invalid_state(ENOT_IN_REVEAL_WINDOW)); commit_entry_fee_impl(user, digest); } /// Transaction entry function for revealing bid public entry fun reveal(user: &signer, pk: vector, entry_fee: u64, signed_msg: vector) acquires CommittedBid { - // don't allow commiting within reveal window + // don't allow committing within reveal window assert!(in_reveal_window(), error::invalid_state(ENOT_IN_REVEAL_WINDOW)); reveal_entry_fee_impl(user, pk, entry_fee, signed_msg); } @@ -130,7 +130,7 @@ module ol_framework::secret_bid { commitment } - /// user sends transaction which takes the committed signed message + /// user sends transaction which takes the committed signed message /// submits the public key used to sign message, which we compare to the authentication key. /// we use the epoch as the sequence number, so that messages are different on each submission. public fun reveal_entry_fee_impl(user: &signer, pk: vector, entry_fee: u64, signed_msg: vector) acquires CommittedBid { @@ -225,13 +225,7 @@ module ol_framework::secret_bid { #[view] /// get the current bid, and exclude bids that are stale public fun current_revealed_bid(user: address): u64 acquires CommittedBid { - // // if we are not in reveal window this information will be confusing. - // assert!(in_reveal_window(), error::invalid_state(ENOT_IN_REVEAL_WINDOW)); - - // let state = borrow_global(user); - // assert!(state.commit_epoch == epoch_helper::get_current_epoch(), error::invalid_state(EBID_EXPIRED)); - - // state.reveal_entry_fee + // if we are not in reveal window this information will be confusing. get_bid_unchecked(user) } diff --git a/tools/query/src/chain_queries.rs b/tools/query/src/chain_queries.rs index 12ee4b985..37d5c9e84 100644 --- a/tools/query/src/chain_queries.rs +++ b/tools/query/src/chain_queries.rs @@ -95,3 +95,13 @@ pub async fn epoch_over_can_trigger(client: &Client) -> anyhow::Result { Ok(value[0]) } + + +/// Retrieves the current blockchain height. +pub async fn within_commit_reveal_window(client: &Client) -> anyhow::Result { + let res = get_view(client, "0x1::secret_bid::in_reveal_window", None, None).await?; + + let value: Vec = serde_json::from_value(res)?; + + Ok(value[0]) +} diff --git a/tools/txs/src/stream/bid_commit_reveal.rs b/tools/txs/src/stream/bid_commit_reveal.rs index 31a52d013..915371894 100644 --- a/tools/txs/src/stream/bid_commit_reveal.rs +++ b/tools/txs/src/stream/bid_commit_reveal.rs @@ -1,4 +1,17 @@ -#[derive(clap::Args)] +use crate::submit_transaction::Sender as LibraSender; +use diem_logger::info; +use diem_types::transaction::TransactionPayload; +use libra_cached_packages::libra_stdlib; +use libra_query::chain_queries; +use libra_types::core_types::app_cfg::AppCfg; +use serde::{Deserialize, Serialize}; +use libra_types::exports::Client; +use std::borrow::BorrowMut; +use std::sync::mpsc::Sender; +use std::time::Duration; +use std::thread; + +#[derive(clap::Args, Debug)] pub struct PofBidArgs { #[clap(short, long)] pub net_reward: u64, @@ -6,3 +19,74 @@ pub struct PofBidArgs { #[clap(short, long)] pub test_private_key: Option, } + + +#[derive(Debug, Serialize, Deserialize)] +pub struct PofBidData { + entry_fee: u64, + epoch: u64, +} + +#[allow(dead_code)] +/// these are the bytes that will be encoded with BCS +/// and then signed with ed25519 key +/// See the move equivalent in secret_bid.move +impl PofBidData { + fn new(entry_fee: u64) -> Self { + PofBidData { + entry_fee, + epoch: 0 + } + } + fn to_bcs(&self) -> anyhow::Result>{ + Ok(bcs::to_bytes(&self)?) + } + + fn from_bcs(bytes: &[u8]) -> anyhow::Result { + Ok(bcs::from_bytes(bytes)?) + } + + async fn update_epoch(&mut self, client: &Client) -> anyhow::Result<()>{ + let num = chain_queries::get_epoch(client).await?; + self.epoch = num; + Ok(()) + } + + // fn sign_bcs_bytes(&self, public_key) -> anyhow::Result> { + // let bcs = self.to_bcs(); + + // Ok(bcs) + // } + +} + +pub fn commit_reveal_poll(mut tx: Sender, client: Client, delay_secs: u64, app_cfg: AppCfg) { + println!("polling epoch boundary"); + let handle = thread::spawn(move || loop { + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + + // TODO: make the client borrow instead of clone + let res = rt.block_on(libra_query::chain_queries::within_commit_reveal_window( + &client.clone(), + )); + + match res { + Ok(true) => { + let func = libra_stdlib::diem_governance_trigger_epoch(); + + tx.borrow_mut().send(func).unwrap(); + } + _ => { + + info!("Committing bid"); + + } + } + + thread::sleep(Duration::from_secs(delay_secs)); + }); + handle.join().expect("cannot poll for epoch boundary"); +} diff --git a/tools/txs/src/stream/epoch_tickle_poll.rs b/tools/txs/src/stream/epoch_tickle_poll.rs index 7ccd6f6c1..45943d6d2 100644 --- a/tools/txs/src/stream/epoch_tickle_poll.rs +++ b/tools/txs/src/stream/epoch_tickle_poll.rs @@ -1,20 +1,26 @@ +use crate::submit_transaction::Sender as LibraSender; use diem_logger::info; use diem_types::transaction::TransactionPayload; use libra_cached_packages::libra_stdlib; use libra_types::exports::Client; use std::borrow::BorrowMut; +use std::sync::Mutex; use std::sync::mpsc::Sender; +use std::sync::Arc; use std::thread; use std::time::Duration; -pub fn epoch_tickle_poll(mut tx: Sender, client: Client, delay_secs: u64) { +pub fn epoch_tickle_poll(mut tx: Sender, sender: Arc>, delay_secs: u64) { println!("polling epoch boundary"); + let handle = thread::spawn(move || loop { let rt = tokio::runtime::Builder::new_current_thread() .enable_all() .build() .unwrap(); + let client = sender.lock().unwrap().client().clone(); + // TODO: make the client borrow instead of clone let res = rt.block_on(libra_query::chain_queries::epoch_over_can_trigger( &client.clone(), diff --git a/tools/txs/src/stream/mod.rs b/tools/txs/src/stream/mod.rs index 2cfb50065..319578e02 100644 --- a/tools/txs/src/stream/mod.rs +++ b/tools/txs/src/stream/mod.rs @@ -1 +1,2 @@ +pub mod bid_commit_reveal; pub mod epoch_tickle_poll; diff --git a/tools/txs/src/txs_cli.rs b/tools/txs/src/txs_cli.rs index a0c94aab2..dcf85a85d 100644 --- a/tools/txs/src/txs_cli.rs +++ b/tools/txs/src/txs_cli.rs @@ -221,7 +221,7 @@ impl TxsCli { Some(TxsSub::Community(comm_txs)) => comm_txs.run(&mut send).await, Some(TxsSub::Stream(stream_txs)) => { let arc_send = Arc::new(Mutex::new(send)); - stream_txs.start(arc_send); + stream_txs.start(arc_send, &app_cfg); Ok(()) } _ => { diff --git a/tools/txs/src/txs_cli_stream.rs b/tools/txs/src/txs_cli_stream.rs index 049fb5a16..bf0bd8870 100644 --- a/tools/txs/src/txs_cli_stream.rs +++ b/tools/txs/src/txs_cli_stream.rs @@ -1,9 +1,13 @@ -// use crate::bid_commit_reveal::PofBidArgs; -use crate::stream::epoch_tickle_poll::epoch_tickle_poll; + +use crate::stream::{ + bid_commit_reveal::PofBidArgs, + epoch_tickle_poll::epoch_tickle_poll +}; use crate::submit_transaction::Sender as LibraSender; use diem_logger::prelude::{error, info}; use diem_types::transaction::TransactionPayload; -use std::process::exit; +use libra_types::core_types::app_cfg::AppCfg; +use libra_types::exports::Ed25519PrivateKey; use std::sync::mpsc::{self, Receiver, Sender}; use std::sync::{Arc, Mutex}; use std::thread::{self, JoinHandle}; @@ -17,23 +21,22 @@ pub enum StreamTxs { delay: Option, }, /// Submit secret PoF bids in background, and reveal when window opens - PofBid, + ValBid(PofBidArgs), } impl StreamTxs { - pub fn start(&self, send: Arc>) { + pub fn start(&self, send: Arc>, _app_cfg: &AppCfg) { let (tx, rx) = init_channel(); - let client = send.lock().unwrap().client().clone(); - let stream_service = listen(rx, send); + let stream_service = listen(rx, send.clone()); match &self { StreamTxs::EpochTickle { delay } => { println!("EpochTickle entry"); - epoch_tickle_poll(tx, client, delay.unwrap_or(60)); + epoch_tickle_poll(tx, send, delay.unwrap_or(60)); } - _ => { - println!("no service specified"); - exit(1); + StreamTxs::ValBid(args) => { + + dbg!(&args); } }; diff --git a/tools/wallet/src/load_keys.rs b/tools/wallet/src/load_keys.rs index 71dd280c2..8e6ef3d24 100644 --- a/tools/wallet/src/load_keys.rs +++ b/tools/wallet/src/load_keys.rs @@ -61,7 +61,6 @@ pub fn get_account_from_prompt() -> (AuthenticationKey, AccountAddress, WalletLi #[test] fn wallet() { - // use diem_wallet::Mnemonic; let mut wallet = WalletLibrary::new(); let (auth_key, child_number) = wallet.new_address().expect("Could not generate address"); diff --git a/util/fixtures/mnemonic/alice.mnem b/util/fixtures/mnemonic/alice.mnem index a8d8b681b..cd2fbc932 100644 --- a/util/fixtures/mnemonic/alice.mnem +++ b/util/fixtures/mnemonic/alice.mnem @@ -1 +1 @@ -talent sunset lizard pill fame nuclear spy noodle basket okay critic grow sleep legend hurry pitch blanket clerk impose rough degree sock insane purse \ No newline at end of file +talent sunset lizard pill fame nuclear spy noodle basket okay critic grow sleep legend hurry pitch blanket clerk impose rough degree sock insane purse From e197cc4941fc57c7c75a207efa4d00b2dbde7ead Mon Sep 17 00:00:00 2001 From: Basil McHind Date: Mon, 10 Mar 2025 17:53:26 -0400 Subject: [PATCH 02/19] get public key --- tools/txs/src/stream/bid_commit_reveal.rs | 10 ++++++---- tools/txs/src/txs_cli_stream.rs | 3 +++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/txs/src/stream/bid_commit_reveal.rs b/tools/txs/src/stream/bid_commit_reveal.rs index 915371894..6a08214b0 100644 --- a/tools/txs/src/stream/bid_commit_reveal.rs +++ b/tools/txs/src/stream/bid_commit_reveal.rs @@ -8,6 +8,8 @@ use serde::{Deserialize, Serialize}; use libra_types::exports::Client; use std::borrow::BorrowMut; use std::sync::mpsc::Sender; +use std::sync::Arc; +use std::sync::Mutex; use std::time::Duration; use std::thread; @@ -60,17 +62,17 @@ impl PofBidData { } -pub fn commit_reveal_poll(mut tx: Sender, client: Client, delay_secs: u64, app_cfg: AppCfg) { - println!("polling epoch boundary"); +pub fn commit_reveal_poll(mut tx: Sender, sender: Arc>, delay_secs: u64, app_cfg: AppCfg) { + println!("commit reveal bidding"); let handle = thread::spawn(move || loop { let rt = tokio::runtime::Builder::new_current_thread() .enable_all() .build() .unwrap(); - + let client = sender.lock().unwrap().client().clone(); // TODO: make the client borrow instead of clone let res = rt.block_on(libra_query::chain_queries::within_commit_reveal_window( - &client.clone(), + &client, )); match res { diff --git a/tools/txs/src/txs_cli_stream.rs b/tools/txs/src/txs_cli_stream.rs index bf0bd8870..d7815c5f4 100644 --- a/tools/txs/src/txs_cli_stream.rs +++ b/tools/txs/src/txs_cli_stream.rs @@ -35,7 +35,10 @@ impl StreamTxs { epoch_tickle_poll(tx, send, delay.unwrap_or(60)); } StreamTxs::ValBid(args) => { + let la = &send.lock().unwrap().local_account; + let pubk = la.public_key(); + dbg!(&pubk); dbg!(&args); } }; From 2188f98af04ff610da11c37fc0f5cb359f4a14f4 Mon Sep 17 00:00:00 2001 From: Reginald Von Pianissimo Date: Mon, 10 Mar 2025 20:37:57 -0400 Subject: [PATCH 03/19] sign message --- tools/txs/src/stream/bid_commit_reveal.rs | 19 ++++++++++++++----- tools/txs/src/txs_cli_stream.rs | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/tools/txs/src/stream/bid_commit_reveal.rs b/tools/txs/src/stream/bid_commit_reveal.rs index 6a08214b0..bad43ec4f 100644 --- a/tools/txs/src/stream/bid_commit_reveal.rs +++ b/tools/txs/src/stream/bid_commit_reveal.rs @@ -1,9 +1,11 @@ use crate::submit_transaction::Sender as LibraSender; use diem_logger::info; +use diem_sdk::crypto::SigningKey; use diem_types::transaction::TransactionPayload; use libra_cached_packages::libra_stdlib; use libra_query::chain_queries; use libra_types::core_types::app_cfg::AppCfg; +use libra_types::exports::{Ed25519PrivateKey, Ed25519PublicKey}; use serde::{Deserialize, Serialize}; use libra_types::exports::Client; use std::borrow::BorrowMut; @@ -54,11 +56,11 @@ impl PofBidData { Ok(()) } - // fn sign_bcs_bytes(&self, public_key) -> anyhow::Result> { - // let bcs = self.to_bcs(); - - // Ok(bcs) - // } + fn sign_bcs_bytes(&self, private_key: Ed25519PrivateKey) -> anyhow::Result> { + let bcs = self.to_bcs()?; + let signed = private_key.sign_arbitrary_message(&bcs); + Ok(signed.to_bytes().to_vec()) + } } @@ -92,3 +94,10 @@ pub fn commit_reveal_poll(mut tx: Sender, sender: Arc { let la = &send.lock().unwrap().local_account; - let pubk = la.public_key(); + let pubk = hex::encode(la.private_key().to_bytes()); dbg!(&pubk); dbg!(&args); } From 67b2c776a636f9530eda74f8875003f60712c5df Mon Sep 17 00:00:00 2001 From: Lucietta Hobby Date: Tue, 11 Mar 2025 12:02:01 -0400 Subject: [PATCH 04/19] unify rust dev and test profiles --- Cargo.toml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a13420a29..77908db18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -357,11 +357,18 @@ strip = true # strip debug and symbols for size debug = true [profile.dev] -opt-level = 0 +opt-level = 1 debug = true split-debuginfo = "unpacked" -lto = false +lto = "off" +codegen-units = 256 # More parallel compilation units incremental = true [profile.test] inherits = "dev" +opt-level = 1 +debug = true +split-debuginfo = "unpacked" +lto = "off" +codegen-units = 256 # More parallel compilation units +incremental = true From e1b6b5548c72177afb2f930195094201927d5e30 Mon Sep 17 00:00:00 2001 From: Montague Sforzando Date: Tue, 11 Mar 2025 12:23:43 -0400 Subject: [PATCH 05/19] suggested vs code settings to improve compile times --- .vscode/suggested_vscode_settings.json | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .vscode/suggested_vscode_settings.json diff --git a/.vscode/suggested_vscode_settings.json b/.vscode/suggested_vscode_settings.json new file mode 100644 index 000000000..c5a5db4e5 --- /dev/null +++ b/.vscode/suggested_vscode_settings.json @@ -0,0 +1,22 @@ +{ + "rust-analyzer.cargo.buildScripts.enable": false, + "rust-analyzer.restartServerOnConfigChange": true, + "rust-analyzer.debug.engine": "vadimcn.vscode-lldb", + "rust-analyzer.debug.openDebugPane": true, + "rust-analyzer.runnables.extraEnv": { + "DIEM_FORGE_NODE_BIN_PATH": "/root/.cargo/bin/diem-node" + }, + "rust-analyzer.server.extraEnv": { + "DIEM_FORGE_NODE_BIN_PATH": "/root/.cargo/bin/diem-node" + }, + "rust-analyzer.checkOnSave": false, + "rust-analyzer.check.noDefaultFeatures": false, + "rust-analyzer.cargo.buildScripts.useRustcWrapper": false, + "rust-analyzer.cargo.extraEnv": { + "RUSTC_WRAPPER": "sccache" + }, + "rust-analyzer.check.extraArgs": [ + "RUSTC_WRAPPER=sccache" + ], + "rust-analyzer.check.overrideCommand": "build" +} From 2cd1dcd0542ed22fa4043947e49a0628a7336a14 Mon Sep 17 00:00:00 2001 From: Mariella Fitz Coney Date: Tue, 11 Mar 2025 13:17:13 -0400 Subject: [PATCH 06/19] wip commit reveal signing --- tools/query/src/chain_queries.rs | 1 - tools/txs/src/stream/bid_commit_reveal.rs | 138 ++++++++++++---------- tools/txs/src/stream/epoch_tickle_poll.rs | 8 +- tools/txs/src/stream/stream.rs | 54 +++++++++ tools/txs/src/txs_cli_stream.rs | 53 +-------- 5 files changed, 143 insertions(+), 111 deletions(-) create mode 100644 tools/txs/src/stream/stream.rs diff --git a/tools/query/src/chain_queries.rs b/tools/query/src/chain_queries.rs index 37d5c9e84..080e07209 100644 --- a/tools/query/src/chain_queries.rs +++ b/tools/query/src/chain_queries.rs @@ -96,7 +96,6 @@ pub async fn epoch_over_can_trigger(client: &Client) -> anyhow::Result { Ok(value[0]) } - /// Retrieves the current blockchain height. pub async fn within_commit_reveal_window(client: &Client) -> anyhow::Result { let res = get_view(client, "0x1::secret_bid::in_reveal_window", None, None).await?; diff --git a/tools/txs/src/stream/bid_commit_reveal.rs b/tools/txs/src/stream/bid_commit_reveal.rs index bad43ec4f..f8c571169 100644 --- a/tools/txs/src/stream/bid_commit_reveal.rs +++ b/tools/txs/src/stream/bid_commit_reveal.rs @@ -5,15 +5,15 @@ use diem_types::transaction::TransactionPayload; use libra_cached_packages::libra_stdlib; use libra_query::chain_queries; use libra_types::core_types::app_cfg::AppCfg; +use libra_types::exports::Client; use libra_types::exports::{Ed25519PrivateKey, Ed25519PublicKey}; use serde::{Deserialize, Serialize}; -use libra_types::exports::Client; use std::borrow::BorrowMut; use std::sync::mpsc::Sender; use std::sync::Arc; use std::sync::Mutex; -use std::time::Duration; use std::thread; +use std::time::Duration; #[derive(clap::Args, Debug)] pub struct PofBidArgs { @@ -24,11 +24,10 @@ pub struct PofBidArgs { pub test_private_key: Option, } - #[derive(Debug, Serialize, Deserialize)] pub struct PofBidData { - entry_fee: u64, - epoch: u64, + entry_fee: u64, + epoch: u64, } #[allow(dead_code)] @@ -36,68 +35,87 @@ pub struct PofBidData { /// and then signed with ed25519 key /// See the move equivalent in secret_bid.move impl PofBidData { - fn new(entry_fee: u64) -> Self { - PofBidData { - entry_fee, - epoch: 0 + fn new(entry_fee: u64) -> Self { + PofBidData { + entry_fee, + epoch: 0, + } + } + fn to_bcs(&self) -> anyhow::Result> { + Ok(bcs::to_bytes(&self)?) } - } - fn to_bcs(&self) -> anyhow::Result>{ - Ok(bcs::to_bytes(&self)?) - } - - fn from_bcs(bytes: &[u8]) -> anyhow::Result { - Ok(bcs::from_bytes(bytes)?) - } - - async fn update_epoch(&mut self, client: &Client) -> anyhow::Result<()>{ - let num = chain_queries::get_epoch(client).await?; - self.epoch = num; - Ok(()) - } - - fn sign_bcs_bytes(&self, private_key: Ed25519PrivateKey) -> anyhow::Result> { - let bcs = self.to_bcs()?; - let signed = private_key.sign_arbitrary_message(&bcs); - Ok(signed.to_bytes().to_vec()) - } -} + fn from_bcs(bytes: &[u8]) -> anyhow::Result { + Ok(bcs::from_bytes(bytes)?) + } -pub fn commit_reveal_poll(mut tx: Sender, sender: Arc>, delay_secs: u64, app_cfg: AppCfg) { - println!("commit reveal bidding"); - let handle = thread::spawn(move || loop { - let rt = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .unwrap(); - let client = sender.lock().unwrap().client().clone(); - // TODO: make the client borrow instead of clone - let res = rt.block_on(libra_query::chain_queries::within_commit_reveal_window( - &client, - )); - - match res { - Ok(true) => { - let func = libra_stdlib::diem_governance_trigger_epoch(); - - tx.borrow_mut().send(func).unwrap(); - } - _ => { - - info!("Committing bid"); - - } - } + async fn update_epoch(&mut self, client: &Client) -> anyhow::Result<()> { + let num = chain_queries::get_epoch(client).await?; + self.epoch = num; + Ok(()) + } - thread::sleep(Duration::from_secs(delay_secs)); - }); - handle.join().expect("cannot poll for epoch boundary"); + fn sign_bcs_bytes(&self, keys: &LocalAccount) -> anyhow::Result> { + let bcs = self.to_bcs()?; + let signed = keys.private_key().sign_arbitrary_message(&bcs); + Ok(signed.to_bytes().to_vec()) + } + + fn encode_commit_tx_payload(&self, keys: &LocalAccount) -> TransactionPayload { + let digest = self.sign_bcs_bytes(keys); + libra_stdlib::secret_bid_commit(digest) + } + + fn encode_reveal_tx_payload(&self, keys: &LocalAccount) -> TransactionPayload { + let digest = self.sign_bcs_bytes(keys); + + libra_stdlib::secret_bid_reveal( + keys.public_key().to_bytes().to_vec(), + self.entry_fee, + digest, + ) + } } +pub fn commit_reveal_poll( + mut tx: Sender, + sender: Arc>, + entry_fee: u64, + delay_secs: u64, + app_cfg: AppCfg, +) { + println!("commit reveal bid: {}", entry_fee); + let bid = PofBidData::new(entry_fee); + + loop { + thread::sleep(Duration::from_secs(delay_secs)); + let client = sender.lock().unwrap().client().clone(); // release the mutex + + // check what epoch we are in + bid.update_epoch().await(client); + // info!("bid: {:?}", &bid); + let must_reveal = libra_query::chain_queries::within_commit_reveal_window(client).await?; + + let la = sender.lock().unwrap().local_account.clone(); + let tx_payload = if must_reveal { + bid.encode_reveal_tx_payload(&la); + } else { + bid.encode_commit_tx_payload(&la); + }; + + // send to channel + match tx.borrow_mut().send(tx_payload.clone()) { + Ok(_) => debug!("success with payload: {:?}", tx_payload), + // Don't abort on error + Err(e) => error!("transaction fails with message: {:?}\ncontinuing...", e), + }; + } +} #[test] fn encode_signed_message() { - let pk = PrivateKey::from_bytes(hex::decode("74f18da2b80b1820b58116197b1c41f8a36e1b37a15c7fb434bb42dd7bdaa66b")); - dbg!(&pk); + let pk = PrivateKey::from_bytes(hex::decode( + "74f18da2b80b1820b58116197b1c41f8a36e1b37a15c7fb434bb42dd7bdaa66b", + )); + dbg!(&pk); } diff --git a/tools/txs/src/stream/epoch_tickle_poll.rs b/tools/txs/src/stream/epoch_tickle_poll.rs index 45943d6d2..fac021781 100644 --- a/tools/txs/src/stream/epoch_tickle_poll.rs +++ b/tools/txs/src/stream/epoch_tickle_poll.rs @@ -4,13 +4,17 @@ use diem_types::transaction::TransactionPayload; use libra_cached_packages::libra_stdlib; use libra_types::exports::Client; use std::borrow::BorrowMut; -use std::sync::Mutex; use std::sync::mpsc::Sender; use std::sync::Arc; +use std::sync::Mutex; use std::thread; use std::time::Duration; -pub fn epoch_tickle_poll(mut tx: Sender, sender: Arc>, delay_secs: u64) { +pub fn epoch_tickle_poll( + mut tx: Sender, + sender: Arc>, + delay_secs: u64, +) { println!("polling epoch boundary"); let handle = thread::spawn(move || loop { diff --git a/tools/txs/src/stream/stream.rs b/tools/txs/src/stream/stream.rs new file mode 100644 index 000000000..efdfa572f --- /dev/null +++ b/tools/txs/src/stream/stream.rs @@ -0,0 +1,54 @@ + +use crate::submit_transaction::Sender as LibraSender; +use diem_logger::prelude::{error, info}; +use diem_types::transaction::TransactionPayload; +use libra_types::core_types::app_cfg::AppCfg; +use libra_types::exports::Ed25519PrivateKey; +use std::sync::mpsc::{self, Receiver, Sender}; +use std::sync::{Arc, Mutex}; +use std::thread::{self, JoinHandle}; + +/// Creates a channel to receive an process for TransactionPayload +pub(crate) fn init_channel() -> (Sender, Receiver) { + mpsc::channel::() +} + +/// Start the listener on the channel which receives TransactionPayload +#[allow(unused_assignments)] +pub(crate) fn listen( + rx: Receiver, + send: Arc>, +) -> JoinHandle<()> { + thread::spawn(move || { + let mut busy = false; + + while !busy { + match rx.recv() { + Ok(payload) => { + info!("Received Channel Message\nTx: {:?}", payload); + if !busy { + busy = true; + match send + .lock() + .expect("could not access Sender client") + .sync_sign_submit_wait(payload) + { + Ok(r) => { + info!("tx success: {:?}", r); + busy = false; + } + Err(e) => { + // DOES NOT abort on failed tx + error!("transaction failed: {:?}\ncontinuing...", &e); + } + }; + } + } + Err(_) => { + error!("could not parse channel message received, aborting"); + break + }, + }; + } + }) +} diff --git a/tools/txs/src/txs_cli_stream.rs b/tools/txs/src/txs_cli_stream.rs index 2cfdb9ee6..4121943d2 100644 --- a/tools/txs/src/txs_cli_stream.rs +++ b/tools/txs/src/txs_cli_stream.rs @@ -1,8 +1,4 @@ - -use crate::stream::{ - bid_commit_reveal::PofBidArgs, - epoch_tickle_poll::epoch_tickle_poll -}; +use crate::stream::{bid_commit_reveal::PofBidArgs, epoch_tickle_poll::epoch_tickle_poll}; use crate::submit_transaction::Sender as LibraSender; use diem_logger::prelude::{error, info}; use diem_types::transaction::TransactionPayload; @@ -35,11 +31,11 @@ impl StreamTxs { epoch_tickle_poll(tx, send, delay.unwrap_or(60)); } StreamTxs::ValBid(args) => { - let la = &send.lock().unwrap().local_account; + let la = &send.lock().unwrap().local_account; - let pubk = hex::encode(la.private_key().to_bytes()); - dbg!(&pubk); - dbg!(&args); + let pubk = hex::encode(la.private_key().to_bytes()); + dbg!(&pubk); + dbg!(&args); } }; @@ -48,42 +44,3 @@ impl StreamTxs { .expect("could not complete tasks in stream"); } } - -pub(crate) fn init_channel() -> (Sender, Receiver) { - mpsc::channel::() -} - -#[allow(unused_assignments)] -pub(crate) fn listen( - rx: Receiver, - send: Arc>, -) -> JoinHandle<()> { - thread::spawn(move || { - let mut busy = false; - - while !busy { - match rx.recv() { - Ok(payload) => { - info!("Tx: {:?}", payload); - if !busy { - busy = true; - match send - .lock() - .expect("could not access Sender client") - .sync_sign_submit_wait(payload) - { - Ok(_r) => { - busy = false; - } - Err(e) => { - error!("transaction failed: {:?}", &e); - break; - } - }; - } - } - Err(_) => break, - }; - } - }) -} From 34ea033ee0fdb63ecdb423032860de769247bc82 Mon Sep 17 00:00:00 2001 From: Caro McAdagio Date: Tue, 11 Mar 2025 13:47:08 -0400 Subject: [PATCH 07/19] builds --- tools/txs/src/stream/bid_commit_reveal.rs | 71 +++++++++++++------ .../txs/src/stream/{stream.rs => channel.rs} | 2 - tools/txs/src/stream/epoch_tickle_poll.rs | 1 - tools/txs/src/stream/mod.rs | 1 + tools/txs/src/txs_cli.rs | 2 +- tools/txs/src/txs_cli_stream.rs | 14 ++-- 6 files changed, 55 insertions(+), 36 deletions(-) rename tools/txs/src/stream/{stream.rs => channel.rs} (95%) diff --git a/tools/txs/src/stream/bid_commit_reveal.rs b/tools/txs/src/stream/bid_commit_reveal.rs index f8c571169..c615fba87 100644 --- a/tools/txs/src/stream/bid_commit_reveal.rs +++ b/tools/txs/src/stream/bid_commit_reveal.rs @@ -1,20 +1,24 @@ use crate::submit_transaction::Sender as LibraSender; -use diem_logger::info; +use diem_logger::{debug, error}; use diem_sdk::crypto::SigningKey; +use diem_sdk::types::LocalAccount; use diem_types::transaction::TransactionPayload; use libra_cached_packages::libra_stdlib; use libra_query::chain_queries; use libra_types::core_types::app_cfg::AppCfg; use libra_types::exports::Client; -use libra_types::exports::{Ed25519PrivateKey, Ed25519PublicKey}; -use serde::{Deserialize, Serialize}; + +// use libra_types::exports::{Ed25519PrivateKey, Ed25519PublicKey}; use std::borrow::BorrowMut; +use serde::{Deserialize, Serialize}; use std::sync::mpsc::Sender; use std::sync::Arc; use std::sync::Mutex; use std::thread; use std::time::Duration; + + #[derive(clap::Args, Debug)] pub struct PofBidArgs { #[clap(short, long)] @@ -62,12 +66,12 @@ impl PofBidData { } fn encode_commit_tx_payload(&self, keys: &LocalAccount) -> TransactionPayload { - let digest = self.sign_bcs_bytes(keys); + let digest = self.sign_bcs_bytes(keys).expect("could not sign bytes"); libra_stdlib::secret_bid_commit(digest) } fn encode_reveal_tx_payload(&self, keys: &LocalAccount) -> TransactionPayload { - let digest = self.sign_bcs_bytes(keys); + let digest = self.sign_bcs_bytes(keys).unwrap(); libra_stdlib::secret_bid_reveal( keys.public_key().to_bytes().to_vec(), @@ -77,30 +81,30 @@ impl PofBidData { } } -pub fn commit_reveal_poll( +pub async fn commit_reveal_poll( mut tx: Sender, sender: Arc>, entry_fee: u64, delay_secs: u64, - app_cfg: AppCfg, -) { + _app_cfg: AppCfg, +) -> anyhow::Result<()>{ println!("commit reveal bid: {}", entry_fee); - let bid = PofBidData::new(entry_fee); + let mut bid = PofBidData::new(entry_fee); loop { thread::sleep(Duration::from_secs(delay_secs)); let client = sender.lock().unwrap().client().clone(); // release the mutex // check what epoch we are in - bid.update_epoch().await(client); - // info!("bid: {:?}", &bid); - let must_reveal = libra_query::chain_queries::within_commit_reveal_window(client).await?; + let _ = bid.update_epoch(&client).await; + debug!("bid: {:?}", &bid); + let must_reveal = libra_query::chain_queries::within_commit_reveal_window(&client).await?; - let la = sender.lock().unwrap().local_account.clone(); + let la = &sender.lock().unwrap().local_account; let tx_payload = if must_reveal { - bid.encode_reveal_tx_payload(&la); + bid.encode_reveal_tx_payload(&la) } else { - bid.encode_commit_tx_payload(&la); + bid.encode_commit_tx_payload(&la) }; // send to channel @@ -112,10 +116,33 @@ pub fn commit_reveal_poll( } } -#[test] -fn encode_signed_message() { - let pk = PrivateKey::from_bytes(hex::decode( - "74f18da2b80b1820b58116197b1c41f8a36e1b37a15c7fb434bb42dd7bdaa66b", - )); - dbg!(&pk); -} +// #[cfg(test)] +// fn test_local_account() -> LocalAccount { +// use libra_types::exports::AccountAddress; +// use diem_sdk::types::AccountKey; +// use diem_sdk::crypto::ed25519::PrivateKey; + +// let pk = PrivateKey::from_bytes(hex::decode( +// "74f18da2b80b1820b58116197b1c41f8a36e1b37a15c7fb434bb42dd7bdaa66b", +// )); +// let account_key = AccountKey::from_private_key(pk); +// LocalAccount::new( +// AccountAddress::from_hex_literal("74f18da2b80b1820b58116197b1c41f8a36e1b37a15c7fb434bb42dd7bdaa66b").unwrap(), account_key, 0) +// } + +// // #[test] +// // fn encode_signed_message() { +// // let pk = PrivateKey::from_bytes(hex::decode( +// // "74f18da2b80b1820b58116197b1c41f8a36e1b37a15c7fb434bb42dd7bdaa66b", +// // )); +// // dbg!(&pk); +// // } + +// #[test] +// fn sign_bid() { +// let bid = PofBidData::new(11); +// let la = test_local_account(); +// let sig = bid.sign_bcs_bytes(&la).unwrap(); +// let pubkey = la.public_key(); +// // pubkey.verify_message(&sig); +// } diff --git a/tools/txs/src/stream/stream.rs b/tools/txs/src/stream/channel.rs similarity index 95% rename from tools/txs/src/stream/stream.rs rename to tools/txs/src/stream/channel.rs index efdfa572f..cb10607bb 100644 --- a/tools/txs/src/stream/stream.rs +++ b/tools/txs/src/stream/channel.rs @@ -2,8 +2,6 @@ use crate::submit_transaction::Sender as LibraSender; use diem_logger::prelude::{error, info}; use diem_types::transaction::TransactionPayload; -use libra_types::core_types::app_cfg::AppCfg; -use libra_types::exports::Ed25519PrivateKey; use std::sync::mpsc::{self, Receiver, Sender}; use std::sync::{Arc, Mutex}; use std::thread::{self, JoinHandle}; diff --git a/tools/txs/src/stream/epoch_tickle_poll.rs b/tools/txs/src/stream/epoch_tickle_poll.rs index fac021781..46656562e 100644 --- a/tools/txs/src/stream/epoch_tickle_poll.rs +++ b/tools/txs/src/stream/epoch_tickle_poll.rs @@ -2,7 +2,6 @@ use crate::submit_transaction::Sender as LibraSender; use diem_logger::info; use diem_types::transaction::TransactionPayload; use libra_cached_packages::libra_stdlib; -use libra_types::exports::Client; use std::borrow::BorrowMut; use std::sync::mpsc::Sender; use std::sync::Arc; diff --git a/tools/txs/src/stream/mod.rs b/tools/txs/src/stream/mod.rs index 319578e02..c53ef6fc0 100644 --- a/tools/txs/src/stream/mod.rs +++ b/tools/txs/src/stream/mod.rs @@ -1,2 +1,3 @@ pub mod bid_commit_reveal; pub mod epoch_tickle_poll; +pub mod channel; diff --git a/tools/txs/src/txs_cli.rs b/tools/txs/src/txs_cli.rs index dcf85a85d..a0c94aab2 100644 --- a/tools/txs/src/txs_cli.rs +++ b/tools/txs/src/txs_cli.rs @@ -221,7 +221,7 @@ impl TxsCli { Some(TxsSub::Community(comm_txs)) => comm_txs.run(&mut send).await, Some(TxsSub::Stream(stream_txs)) => { let arc_send = Arc::new(Mutex::new(send)); - stream_txs.start(arc_send, &app_cfg); + stream_txs.start(arc_send); Ok(()) } _ => { diff --git a/tools/txs/src/txs_cli_stream.rs b/tools/txs/src/txs_cli_stream.rs index 4121943d2..ec9b45bda 100644 --- a/tools/txs/src/txs_cli_stream.rs +++ b/tools/txs/src/txs_cli_stream.rs @@ -1,12 +1,6 @@ -use crate::stream::{bid_commit_reveal::PofBidArgs, epoch_tickle_poll::epoch_tickle_poll}; +use crate::stream::{channel, bid_commit_reveal::PofBidArgs, epoch_tickle_poll::epoch_tickle_poll}; use crate::submit_transaction::Sender as LibraSender; -use diem_logger::prelude::{error, info}; -use diem_types::transaction::TransactionPayload; -use libra_types::core_types::app_cfg::AppCfg; -use libra_types::exports::Ed25519PrivateKey; -use std::sync::mpsc::{self, Receiver, Sender}; use std::sync::{Arc, Mutex}; -use std::thread::{self, JoinHandle}; #[derive(clap::Subcommand)] pub enum StreamTxs { @@ -21,9 +15,9 @@ pub enum StreamTxs { } impl StreamTxs { - pub fn start(&self, send: Arc>, _app_cfg: &AppCfg) { - let (tx, rx) = init_channel(); - let stream_service = listen(rx, send.clone()); + pub fn start(&self, send: Arc>) { + let (tx, rx) = channel::init_channel(); + let stream_service = channel::listen(rx, send.clone()); match &self { StreamTxs::EpochTickle { delay } => { From 7da954a8e060173f91d045fc2bc59e818202eb10 Mon Sep 17 00:00:00 2001 From: Rupert MacMarten Date: Tue, 11 Mar 2025 14:49:39 -0400 Subject: [PATCH 08/19] add signature test --- tools/txs/src/stream/bid_commit_reveal.rs | 85 ++++++++++++----------- tools/txs/src/stream/channel.rs | 7 +- tools/txs/src/stream/mod.rs | 2 +- tools/txs/src/txs_cli_stream.rs | 2 +- 4 files changed, 48 insertions(+), 48 deletions(-) diff --git a/tools/txs/src/stream/bid_commit_reveal.rs b/tools/txs/src/stream/bid_commit_reveal.rs index c615fba87..df24d6fb5 100644 --- a/tools/txs/src/stream/bid_commit_reveal.rs +++ b/tools/txs/src/stream/bid_commit_reveal.rs @@ -1,5 +1,7 @@ use crate::submit_transaction::Sender as LibraSender; use diem_logger::{debug, error}; +use diem_sdk::crypto::ed25519::Ed25519Signature; +use diem_sdk::crypto::Signature; use diem_sdk::crypto::SigningKey; use diem_sdk::types::LocalAccount; use diem_types::transaction::TransactionPayload; @@ -7,18 +9,14 @@ use libra_cached_packages::libra_stdlib; use libra_query::chain_queries; use libra_types::core_types::app_cfg::AppCfg; use libra_types::exports::Client; - -// use libra_types::exports::{Ed25519PrivateKey, Ed25519PublicKey}; -use std::borrow::BorrowMut; use serde::{Deserialize, Serialize}; +use std::borrow::BorrowMut; use std::sync::mpsc::Sender; use std::sync::Arc; use std::sync::Mutex; use std::thread; use std::time::Duration; - - #[derive(clap::Args, Debug)] pub struct PofBidArgs { #[clap(short, long)] @@ -59,15 +57,19 @@ impl PofBidData { Ok(()) } - fn sign_bcs_bytes(&self, keys: &LocalAccount) -> anyhow::Result> { + fn sign_bcs_bytes(&self, keys: &LocalAccount) -> anyhow::Result { let bcs = self.to_bcs()?; let signed = keys.private_key().sign_arbitrary_message(&bcs); - Ok(signed.to_bytes().to_vec()) + assert!( + signed.verify_arbitrary_msg(&bcs, keys.public_key()).is_ok(), + "cannot verify signed message" + ); + Ok(signed) } fn encode_commit_tx_payload(&self, keys: &LocalAccount) -> TransactionPayload { let digest = self.sign_bcs_bytes(keys).expect("could not sign bytes"); - libra_stdlib::secret_bid_commit(digest) + libra_stdlib::secret_bid_commit(digest.to_bytes().to_vec()) } fn encode_reveal_tx_payload(&self, keys: &LocalAccount) -> TransactionPayload { @@ -76,7 +78,7 @@ impl PofBidData { libra_stdlib::secret_bid_reveal( keys.public_key().to_bytes().to_vec(), self.entry_fee, - digest, + digest.to_bytes().to_vec(), ) } } @@ -87,7 +89,7 @@ pub async fn commit_reveal_poll( entry_fee: u64, delay_secs: u64, _app_cfg: AppCfg, -) -> anyhow::Result<()>{ +) -> anyhow::Result<()> { println!("commit reveal bid: {}", entry_fee); let mut bid = PofBidData::new(entry_fee); @@ -102,9 +104,9 @@ pub async fn commit_reveal_poll( let la = &sender.lock().unwrap().local_account; let tx_payload = if must_reveal { - bid.encode_reveal_tx_payload(&la) + bid.encode_reveal_tx_payload(la) } else { - bid.encode_commit_tx_payload(&la) + bid.encode_commit_tx_payload(la) }; // send to channel @@ -116,33 +118,32 @@ pub async fn commit_reveal_poll( } } -// #[cfg(test)] -// fn test_local_account() -> LocalAccount { -// use libra_types::exports::AccountAddress; -// use diem_sdk::types::AccountKey; -// use diem_sdk::crypto::ed25519::PrivateKey; - -// let pk = PrivateKey::from_bytes(hex::decode( -// "74f18da2b80b1820b58116197b1c41f8a36e1b37a15c7fb434bb42dd7bdaa66b", -// )); -// let account_key = AccountKey::from_private_key(pk); -// LocalAccount::new( -// AccountAddress::from_hex_literal("74f18da2b80b1820b58116197b1c41f8a36e1b37a15c7fb434bb42dd7bdaa66b").unwrap(), account_key, 0) -// } - -// // #[test] -// // fn encode_signed_message() { -// // let pk = PrivateKey::from_bytes(hex::decode( -// // "74f18da2b80b1820b58116197b1c41f8a36e1b37a15c7fb434bb42dd7bdaa66b", -// // )); -// // dbg!(&pk); -// // } - -// #[test] -// fn sign_bid() { -// let bid = PofBidData::new(11); -// let la = test_local_account(); -// let sig = bid.sign_bcs_bytes(&la).unwrap(); -// let pubkey = la.public_key(); -// // pubkey.verify_message(&sig); -// } +#[cfg(test)] +fn test_local_account() -> LocalAccount { + use diem_sdk::crypto::ed25519::Ed25519PrivateKey; + use diem_sdk::crypto::ValidCryptoMaterialStringExt; + use diem_sdk::types::AccountKey; + use libra_types::exports::AccountAddress; + + let pk = Ed25519PrivateKey::from_encoded_string( + "74f18da2b80b1820b58116197b1c41f8a36e1b37a15c7fb434bb42dd7bdaa66b", + ) + .unwrap(); + let account_key = AccountKey::from_private_key(pk); + LocalAccount::new( + AccountAddress::from_bytes(account_key.public_key().to_bytes()).unwrap(), + account_key, + 0, + ) +} + +#[test] +fn sign_bid() { + let bid = PofBidData::new(11); + let la = test_local_account(); + let sig = bid.sign_bcs_bytes(&la).expect("could not sign"); + let pubkey = la.public_key(); + let msg = bid.to_bcs().unwrap(); + + assert!(sig.verify_arbitrary_msg(&msg, pubkey).is_ok()); +} diff --git a/tools/txs/src/stream/channel.rs b/tools/txs/src/stream/channel.rs index cb10607bb..b73a9ab16 100644 --- a/tools/txs/src/stream/channel.rs +++ b/tools/txs/src/stream/channel.rs @@ -1,4 +1,3 @@ - use crate::submit_transaction::Sender as LibraSender; use diem_logger::prelude::{error, info}; use diem_types::transaction::TransactionPayload; @@ -43,9 +42,9 @@ pub(crate) fn listen( } } Err(_) => { - error!("could not parse channel message received, aborting"); - break - }, + error!("could not parse channel message received, aborting"); + break; + } }; } }) diff --git a/tools/txs/src/stream/mod.rs b/tools/txs/src/stream/mod.rs index c53ef6fc0..c2f4f00c3 100644 --- a/tools/txs/src/stream/mod.rs +++ b/tools/txs/src/stream/mod.rs @@ -1,3 +1,3 @@ pub mod bid_commit_reveal; -pub mod epoch_tickle_poll; pub mod channel; +pub mod epoch_tickle_poll; diff --git a/tools/txs/src/txs_cli_stream.rs b/tools/txs/src/txs_cli_stream.rs index ec9b45bda..f10e8c189 100644 --- a/tools/txs/src/txs_cli_stream.rs +++ b/tools/txs/src/txs_cli_stream.rs @@ -1,4 +1,4 @@ -use crate::stream::{channel, bid_commit_reveal::PofBidArgs, epoch_tickle_poll::epoch_tickle_poll}; +use crate::stream::{bid_commit_reveal::PofBidArgs, channel, epoch_tickle_poll::epoch_tickle_poll}; use crate::submit_transaction::Sender as LibraSender; use std::sync::{Arc, Mutex}; From bb6f50ad6a40180bcb46bc1a83fa2625f5ad54f9 Mon Sep 17 00:00:00 2001 From: Giulietta Fitz Allegro Date: Tue, 11 Mar 2025 18:30:03 -0400 Subject: [PATCH 09/19] patch async implementations --- tools/txs/src/stream/bid_commit_reveal.rs | 13 ++- tools/txs/src/stream/channel.rs | 3 +- tools/txs/src/stream/epoch_tickle_poll.rs | 25 ++---- tools/txs/src/submit_transaction.rs | 1 + tools/txs/src/txs_cli.rs | 2 +- tools/txs/src/txs_cli_stream.rs | 26 +++--- tools/txs/tests/commit_reveal.rs | 102 ++++++++++++++++++++++ tools/txs/tests/trigger_epoch.rs | 14 ++- 8 files changed, 146 insertions(+), 40 deletions(-) create mode 100644 tools/txs/tests/commit_reveal.rs diff --git a/tools/txs/src/stream/bid_commit_reveal.rs b/tools/txs/src/stream/bid_commit_reveal.rs index df24d6fb5..075cb8192 100644 --- a/tools/txs/src/stream/bid_commit_reveal.rs +++ b/tools/txs/src/stream/bid_commit_reveal.rs @@ -7,23 +7,22 @@ use diem_sdk::types::LocalAccount; use diem_types::transaction::TransactionPayload; use libra_cached_packages::libra_stdlib; use libra_query::chain_queries; -use libra_types::core_types::app_cfg::AppCfg; use libra_types::exports::Client; use serde::{Deserialize, Serialize}; use std::borrow::BorrowMut; use std::sync::mpsc::Sender; use std::sync::Arc; use std::sync::Mutex; -use std::thread; use std::time::Duration; #[derive(clap::Args, Debug)] pub struct PofBidArgs { #[clap(short, long)] + /// bid amount for validator net reward pub net_reward: u64, - #[clap(short, long)] - pub test_private_key: Option, + /// seconds to wait between retries + pub delay: Option, } #[derive(Debug, Serialize, Deserialize)] @@ -88,17 +87,16 @@ pub async fn commit_reveal_poll( sender: Arc>, entry_fee: u64, delay_secs: u64, - _app_cfg: AppCfg, ) -> anyhow::Result<()> { println!("commit reveal bid: {}", entry_fee); let mut bid = PofBidData::new(entry_fee); loop { - thread::sleep(Duration::from_secs(delay_secs)); - let client = sender.lock().unwrap().client().clone(); // release the mutex + let client = sender.lock().unwrap().client().clone(); // releases the mutex // check what epoch we are in let _ = bid.update_epoch(&client).await; + dbg!(&bid); debug!("bid: {:?}", &bid); let must_reveal = libra_query::chain_queries::within_commit_reveal_window(&client).await?; @@ -115,6 +113,7 @@ pub async fn commit_reveal_poll( // Don't abort on error Err(e) => error!("transaction fails with message: {:?}\ncontinuing...", e), }; + tokio::time::sleep(Duration::from_secs(delay_secs)).await; } } diff --git a/tools/txs/src/stream/channel.rs b/tools/txs/src/stream/channel.rs index b73a9ab16..cd288a2e9 100644 --- a/tools/txs/src/stream/channel.rs +++ b/tools/txs/src/stream/channel.rs @@ -1,4 +1,5 @@ use crate::submit_transaction::Sender as LibraSender; +use diem_logger::debug; use diem_logger::prelude::{error, info}; use diem_types::transaction::TransactionPayload; use std::sync::mpsc::{self, Receiver, Sender}; @@ -31,7 +32,7 @@ pub(crate) fn listen( .sync_sign_submit_wait(payload) { Ok(r) => { - info!("tx success: {:?}", r); + debug!("tx success: {:?}", r); busy = false; } Err(e) => { diff --git a/tools/txs/src/stream/epoch_tickle_poll.rs b/tools/txs/src/stream/epoch_tickle_poll.rs index 46656562e..71b65e3e7 100644 --- a/tools/txs/src/stream/epoch_tickle_poll.rs +++ b/tools/txs/src/stream/epoch_tickle_poll.rs @@ -6,41 +6,34 @@ use std::borrow::BorrowMut; use std::sync::mpsc::Sender; use std::sync::Arc; use std::sync::Mutex; -use std::thread; use std::time::Duration; -pub fn epoch_tickle_poll( +pub async fn epoch_tickle_poll( mut tx: Sender, sender: Arc>, delay_secs: u64, -) { +) -> anyhow::Result<()> { println!("polling epoch boundary"); - let handle = thread::spawn(move || loop { - let rt = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .unwrap(); - + loop { let client = sender.lock().unwrap().client().clone(); // TODO: make the client borrow instead of clone - let res = rt.block_on(libra_query::chain_queries::epoch_over_can_trigger( - &client.clone(), - )); + let res = libra_query::chain_queries::epoch_over_can_trigger(&client.clone()).await; match res { Ok(true) => { let func = libra_stdlib::diem_governance_trigger_epoch(); - tx.borrow_mut().send(func).unwrap(); + tx.borrow_mut() + .send(func) + .expect("could not send message to channel"); } _ => { info!("Not ready to call epoch.") } } - thread::sleep(Duration::from_secs(delay_secs)); - }); - handle.join().expect("cannot poll for epoch boundary"); + tokio::time::sleep(Duration::from_secs(delay_secs)).await; + } } diff --git a/tools/txs/src/submit_transaction.rs b/tools/txs/src/submit_transaction.rs index 1651e1dac..11ecbff6e 100644 --- a/tools/txs/src/submit_transaction.rs +++ b/tools/txs/src/submit_transaction.rs @@ -251,6 +251,7 @@ impl Sender { &mut self, signed_trans: &SignedTransaction, ) -> anyhow::Result { + debug!("signed tx payload: {:?}", &signed_trans.payload()); let pending_trans = self.client.submit(signed_trans).await?.into_inner(); info!("pending tx hash: {}", &pending_trans.hash.to_string()); diff --git a/tools/txs/src/txs_cli.rs b/tools/txs/src/txs_cli.rs index a0c94aab2..40e8cb0ea 100644 --- a/tools/txs/src/txs_cli.rs +++ b/tools/txs/src/txs_cli.rs @@ -221,7 +221,7 @@ impl TxsCli { Some(TxsSub::Community(comm_txs)) => comm_txs.run(&mut send).await, Some(TxsSub::Stream(stream_txs)) => { let arc_send = Arc::new(Mutex::new(send)); - stream_txs.start(arc_send); + stream_txs.start(arc_send).await; Ok(()) } _ => { diff --git a/tools/txs/src/txs_cli_stream.rs b/tools/txs/src/txs_cli_stream.rs index f10e8c189..49f109038 100644 --- a/tools/txs/src/txs_cli_stream.rs +++ b/tools/txs/src/txs_cli_stream.rs @@ -1,3 +1,4 @@ +use crate::stream::bid_commit_reveal::commit_reveal_poll; use crate::stream::{bid_commit_reveal::PofBidArgs, channel, epoch_tickle_poll::epoch_tickle_poll}; use crate::submit_transaction::Sender as LibraSender; use std::sync::{Arc, Mutex}; @@ -15,22 +16,25 @@ pub enum StreamTxs { } impl StreamTxs { - pub fn start(&self, send: Arc>) { - let (tx, rx) = channel::init_channel(); - let stream_service = channel::listen(rx, send.clone()); + pub async fn start(&self, libra_sender: Arc>) { + let (send_chan, receive_chan) = channel::init_channel(); + let stream_service = channel::listen(receive_chan, libra_sender.clone()); match &self { StreamTxs::EpochTickle { delay } => { println!("EpochTickle entry"); - epoch_tickle_poll(tx, send, delay.unwrap_or(60)); - } - StreamTxs::ValBid(args) => { - let la = &send.lock().unwrap().local_account; - - let pubk = hex::encode(la.private_key().to_bytes()); - dbg!(&pubk); - dbg!(&args); + epoch_tickle_poll(send_chan, libra_sender, delay.unwrap_or(60)) + .await + .expect("could not poll epoch boundary"); } + StreamTxs::ValBid(args) => commit_reveal_poll( + send_chan, + libra_sender, + args.net_reward, + args.delay.unwrap_or(60), + ) + .await + .expect("commit reveal poll ended"), }; stream_service diff --git a/tools/txs/tests/commit_reveal.rs b/tools/txs/tests/commit_reveal.rs new file mode 100644 index 000000000..4f9d71ea8 --- /dev/null +++ b/tools/txs/tests/commit_reveal.rs @@ -0,0 +1,102 @@ +//! test trigger epoch + +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use diem_forge::Swarm; +use libra_cached_packages::libra_stdlib; +use libra_query::query_view; +use libra_smoke_tests::libra_smoke::LibraSmoke; +use libra_txs::stream::bid_commit_reveal::PofBidArgs; +use libra_txs::{submit_transaction::Sender, txs_cli_stream::StreamTxs}; + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +/// Test triggering a new epoch +// Scenario: We want to trigger a new epoch using the TriggerEpoch command +// We will assume that triggering an epoch is an operation that we can test in a single node testnet +async fn background_commit_reveal() -> anyhow::Result<()> { + // create libra swarm and get app config for the validator + let mut ls = LibraSmoke::new(Some(1), None) + .await + .expect("could not start libra smoke"); + + let before_trigger_epoch_query_res = query_view::get_view( + &ls.client(), + "0x1::epoch_helper::get_current_epoch", + None, + None, + ) + .await + .expect("query failed: get epoch failed"); + + // TODO: why is it that smoke tests start on epoch 2? + assert_eq!( + &before_trigger_epoch_query_res.as_array().unwrap()[0], + "2", + "epoch should be 2" + ); + + //////// TRIGGER THE EPOCH //////// + // The TriggerEpoch command does not require arguments, + // so we create it directly and attempt to run it. + let commit_reveal_cmd = StreamTxs::ValBid(PofBidArgs { + net_reward: 1010, + delay: Some(5), + }); + // create a Sender using the validator's app config + let val_app_cfg = ls.first_account_app_cfg()?; + let validator_sender = Sender::from_app_cfg(&val_app_cfg, None).await?; + let wrapped_sender = Arc::new(Mutex::new(validator_sender)); + + // run the txs tool in background in stream mode + // run the txs tool in background in stream + + tokio::spawn(async move { + commit_reveal_cmd.start(wrapped_sender).await; + }); + + // //////// FLIP BIT //////// + std::thread::sleep(Duration::from_secs(20)); + + // helper_set_enable_trigger(&mut ls).await; + + // std::thread::sleep(Duration::from_secs(20)); + + // // now the background service should succeed in triggering epoch. + + // let after_trigger_epoch_query_res = query_view::get_view( + // &ls.client(), + // "0x1::epoch_helper::get_current_epoch", + // None, + // None, + // ) + // .await + // .expect("Query failed: get epoch failed"); + + // assert!( + // &after_trigger_epoch_query_res.as_array().unwrap()[0] == "3", + // "epoch should be 3" + // ); + + Ok(()) +} + +// helper for the testnet root to enable epoch boundary trigger +async fn helper_set_enable_trigger(ls: &mut LibraSmoke) { + let mut public_info = ls.swarm.diem_public_info(); + + let payload = public_info + .transaction_factory() + .payload(libra_stdlib::epoch_boundary_smoke_enable_trigger()); + + let enable_trigger_tx = public_info + .root_account() + .sign_with_transaction_builder(payload); + + public_info + .client() + .submit_and_wait(&enable_trigger_tx) + .await + .expect("could not send demo tx"); + println!("testnet root account enables epoch trigger"); +} diff --git a/tools/txs/tests/trigger_epoch.rs b/tools/txs/tests/trigger_epoch.rs index 614de189a..0471e93d6 100644 --- a/tools/txs/tests/trigger_epoch.rs +++ b/tools/txs/tests/trigger_epoch.rs @@ -70,7 +70,7 @@ async fn sync_trigger_epoch() -> anyhow::Result<()> { Ok(()) } -#[tokio::test(flavor = "multi_thread", worker_threads = 1)] +#[tokio::test (flavor = "multi_thread", worker_threads = 2)] /// Test triggering a new epoch // Scenario: We want to trigger a new epoch using the TriggerEpoch command // We will assume that triggering an epoch is an operation that we can test in a single node testnet @@ -105,8 +105,12 @@ async fn background_trigger_epoch() -> anyhow::Result<()> { let validator_sender = Sender::from_app_cfg(&val_app_cfg, None).await?; let wrapped_sender = Arc::new(Mutex::new(validator_sender)); - // run the txs tool in background in stream mode - std::thread::spawn(move || trigger_epoch_cmd.start(wrapped_sender)); + // run the txs tool in background in stream + + tokio::spawn(async move { + trigger_epoch_cmd.start(wrapped_sender).await; + }); + //////// FLIP BIT //////// std::thread::sleep(Duration::from_secs(10)); @@ -136,6 +140,8 @@ async fn background_trigger_epoch() -> anyhow::Result<()> { // helper for the testnet root to enable epoch boundary trigger async fn helper_set_enable_trigger(ls: &mut LibraSmoke) { + println!("enable trigger"); + let mut public_info = ls.swarm.diem_public_info(); let payload = public_info @@ -151,5 +157,5 @@ async fn helper_set_enable_trigger(ls: &mut LibraSmoke) { .submit_and_wait(&enable_trigger_tx) .await .expect("could not send demo tx"); - println!("testnet root account enables epoch trigger"); + println!("testnet root account sets epoch boundary trigger"); } From aa9248cdd771b514da80be965ae2e0bba016e49c Mon Sep 17 00:00:00 2001 From: Bea Saint Polecat Date: Tue, 11 Mar 2025 18:30:11 -0400 Subject: [PATCH 10/19] add framework build --- .../src/libra_framework_sdk_builder.rs | 128 +++++++++--------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/framework/cached-packages/src/libra_framework_sdk_builder.rs b/framework/cached-packages/src/libra_framework_sdk_builder.rs index 52437f602..5b6df6c06 100644 --- a/framework/cached-packages/src/libra_framework_sdk_builder.rs +++ b/framework/cached-packages/src/libra_framework_sdk_builder.rs @@ -2489,7 +2489,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::AccountOfferRotationCapability { - rotation_capability_sig_bytes: bcs::from_bytes(script.args().first()?).ok()?, + rotation_capability_sig_bytes: bcs::from_bytes(script.args().get(0)?).ok()?, account_scheme: bcs::from_bytes(script.args().get(1)?).ok()?, account_public_key_bytes: bcs::from_bytes(script.args().get(2)?).ok()?, recipient_address: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2504,7 +2504,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::AccountOfferSignerCapability { - signer_capability_sig_bytes: bcs::from_bytes(script.args().first()?).ok()?, + signer_capability_sig_bytes: bcs::from_bytes(script.args().get(0)?).ok()?, account_scheme: bcs::from_bytes(script.args().get(1)?).ok()?, account_public_key_bytes: bcs::from_bytes(script.args().get(2)?).ok()?, recipient_address: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2539,7 +2539,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::AccountRevokeRotationCapability { - to_be_revoked_address: bcs::from_bytes(script.args().first()?).ok()?, + to_be_revoked_address: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -2551,7 +2551,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::AccountRevokeSignerCapability { - to_be_revoked_address: bcs::from_bytes(script.args().first()?).ok()?, + to_be_revoked_address: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -2563,7 +2563,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::AccountRotateAuthenticationKey { - from_scheme: bcs::from_bytes(script.args().first()?).ok()?, + from_scheme: bcs::from_bytes(script.args().get(0)?).ok()?, from_public_key_bytes: bcs::from_bytes(script.args().get(1)?).ok()?, to_scheme: bcs::from_bytes(script.args().get(2)?).ok()?, to_public_key_bytes: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2581,7 +2581,7 @@ mod decoder { if let TransactionPayload::EntryFunction(script) = payload { Some( EntryFunctionCall::AccountRotateAuthenticationKeyWithRotationCapability { - rotation_cap_offerer_address: bcs::from_bytes(script.args().first()?).ok()?, + rotation_cap_offerer_address: bcs::from_bytes(script.args().get(0)?).ok()?, new_scheme: bcs::from_bytes(script.args().get(1)?).ok()?, new_public_key_bytes: bcs::from_bytes(script.args().get(2)?).ok()?, cap_update_table: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2595,7 +2595,7 @@ mod decoder { pub fn burn_set_send_community(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::BurnSetSendCommunity { - community: bcs::from_bytes(script.args().first()?).ok()?, + community: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -2605,7 +2605,7 @@ mod decoder { pub fn code_publish_package_txn(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::CodePublishPackageTxn { - metadata_serialized: bcs::from_bytes(script.args().first()?).ok()?, + metadata_serialized: bcs::from_bytes(script.args().get(0)?).ok()?, code: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2616,8 +2616,8 @@ mod decoder { pub fn coin_transfer(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::CoinTransfer { - coin_type: script.ty_args().first()?.clone(), - to: bcs::from_bytes(script.args().first()?).ok()?, + coin_type: script.ty_args().get(0)?.clone(), + to: bcs::from_bytes(script.args().get(0)?).ok()?, amount: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2631,7 +2631,7 @@ mod decoder { if let TransactionPayload::EntryFunction(script) = payload { Some( EntryFunctionCall::CommunityWalletInitChangeSignerCommunityMultisig { - multisig_address: bcs::from_bytes(script.args().first()?).ok()?, + multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, new_signer: bcs::from_bytes(script.args().get(1)?).ok()?, is_add_operation: bcs::from_bytes(script.args().get(2)?).ok()?, n_of_m: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2648,7 +2648,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::CommunityWalletInitFinalizeAndCage { - num_signers: bcs::from_bytes(script.args().first()?).ok()?, + num_signers: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -2660,7 +2660,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::CommunityWalletInitInitCommunity { - initial_authorities: bcs::from_bytes(script.args().first()?).ok()?, + initial_authorities: bcs::from_bytes(script.args().get(0)?).ok()?, check_threshold: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2673,7 +2673,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::CommunityWalletInitProposeOffer { - new_signers: bcs::from_bytes(script.args().first()?).ok()?, + new_signers: bcs::from_bytes(script.args().get(0)?).ok()?, num_signers: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2687,7 +2687,7 @@ mod decoder { if let TransactionPayload::EntryFunction(script) = payload { Some( EntryFunctionCall::DiemGovernanceAddApprovedScriptHashScript { - proposal_id: bcs::from_bytes(script.args().first()?).ok()?, + proposal_id: bcs::from_bytes(script.args().get(0)?).ok()?, }, ) } else { @@ -2700,7 +2700,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DiemGovernanceAssertCanResolve { - proposal_id: bcs::from_bytes(script.args().first()?).ok()?, + proposal_id: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -2712,7 +2712,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DiemGovernanceCreateProposalV2 { - execution_hash: bcs::from_bytes(script.args().first()?).ok()?, + execution_hash: bcs::from_bytes(script.args().get(0)?).ok()?, metadata_location: bcs::from_bytes(script.args().get(1)?).ok()?, metadata_hash: bcs::from_bytes(script.args().get(2)?).ok()?, is_multi_step_proposal: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2727,7 +2727,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DiemGovernanceOlCreateProposalV2 { - execution_hash: bcs::from_bytes(script.args().first()?).ok()?, + execution_hash: bcs::from_bytes(script.args().get(0)?).ok()?, metadata_location: bcs::from_bytes(script.args().get(1)?).ok()?, metadata_hash: bcs::from_bytes(script.args().get(2)?).ok()?, is_multi_step_proposal: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2740,7 +2740,7 @@ mod decoder { pub fn diem_governance_ol_vote(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DiemGovernanceOlVote { - proposal_id: bcs::from_bytes(script.args().first()?).ok()?, + proposal_id: bcs::from_bytes(script.args().get(0)?).ok()?, should_pass: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2771,7 +2771,7 @@ mod decoder { pub fn diem_governance_vote(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DiemGovernanceVote { - proposal_id: bcs::from_bytes(script.args().first()?).ok()?, + proposal_id: bcs::from_bytes(script.args().get(0)?).ok()?, should_pass: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2784,7 +2784,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DonorVoiceTxsProposeLiquidateTx { - multisig_address: bcs::from_bytes(script.args().first()?).ok()?, + multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -2796,7 +2796,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DonorVoiceTxsProposePaymentTx { - multisig_address: bcs::from_bytes(script.args().first()?).ok()?, + multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, payee: bcs::from_bytes(script.args().get(1)?).ok()?, value: bcs::from_bytes(script.args().get(2)?).ok()?, description: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2811,7 +2811,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DonorVoiceTxsProposeVetoTx { - multisig_address: bcs::from_bytes(script.args().first()?).ok()?, + multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, id: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2824,7 +2824,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DonorVoiceTxsVoteLiquidationTx { - multisig_address: bcs::from_bytes(script.args().first()?).ok()?, + multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -2834,7 +2834,7 @@ mod decoder { pub fn donor_voice_txs_vote_veto_tx(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DonorVoiceTxsVoteVetoTx { - multisig_address: bcs::from_bytes(script.args().first()?).ok()?, + multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, id: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2865,7 +2865,7 @@ mod decoder { pub fn jail_unjail_by_voucher(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::JailUnjailByVoucher { - addr: bcs::from_bytes(script.args().first()?).ok()?, + addr: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -2887,7 +2887,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::LibraCoinDelegateMintCapability { - to: bcs::from_bytes(script.args().first()?).ok()?, + to: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -2897,7 +2897,7 @@ mod decoder { pub fn libra_coin_mint_to_impl(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::LibraCoinMintToImpl { - dst_addr: bcs::from_bytes(script.args().first()?).ok()?, + dst_addr: bcs::from_bytes(script.args().get(0)?).ok()?, amount: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2908,7 +2908,7 @@ mod decoder { pub fn multi_action_claim_offer(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultiActionClaimOffer { - multisig_address: bcs::from_bytes(script.args().first()?).ok()?, + multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -2930,7 +2930,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultiActionMigrationMigrateOffer { - multisig_address: bcs::from_bytes(script.args().first()?).ok()?, + multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -2940,7 +2940,7 @@ mod decoder { pub fn multisig_account_add_owner(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountAddOwner { - new_owner: bcs::from_bytes(script.args().first()?).ok()?, + new_owner: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -2950,7 +2950,7 @@ mod decoder { pub fn multisig_account_add_owners(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountAddOwners { - new_owners: bcs::from_bytes(script.args().first()?).ok()?, + new_owners: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -2962,7 +2962,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountApproveTransaction { - multisig_account: bcs::from_bytes(script.args().first()?).ok()?, + multisig_account: bcs::from_bytes(script.args().get(0)?).ok()?, sequence_number: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2973,7 +2973,7 @@ mod decoder { pub fn multisig_account_create(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountCreate { - num_signatures_required: bcs::from_bytes(script.args().first()?).ok()?, + num_signatures_required: bcs::from_bytes(script.args().get(0)?).ok()?, metadata_keys: bcs::from_bytes(script.args().get(1)?).ok()?, metadata_values: bcs::from_bytes(script.args().get(2)?).ok()?, }) @@ -2987,7 +2987,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountCreateTransaction { - multisig_account: bcs::from_bytes(script.args().first()?).ok()?, + multisig_account: bcs::from_bytes(script.args().get(0)?).ok()?, payload: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3001,7 +3001,7 @@ mod decoder { if let TransactionPayload::EntryFunction(script) = payload { Some( EntryFunctionCall::MultisigAccountCreateTransactionWithHash { - multisig_account: bcs::from_bytes(script.args().first()?).ok()?, + multisig_account: bcs::from_bytes(script.args().get(0)?).ok()?, payload_hash: bcs::from_bytes(script.args().get(1)?).ok()?, }, ) @@ -3016,7 +3016,7 @@ mod decoder { if let TransactionPayload::EntryFunction(script) = payload { Some( EntryFunctionCall::MultisigAccountCreateWithExistingAccount { - multisig_address: bcs::from_bytes(script.args().first()?).ok()?, + multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, owners: bcs::from_bytes(script.args().get(1)?).ok()?, num_signatures_required: bcs::from_bytes(script.args().get(2)?).ok()?, account_scheme: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -3037,7 +3037,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountCreateWithOwners { - additional_owners: bcs::from_bytes(script.args().first()?).ok()?, + additional_owners: bcs::from_bytes(script.args().get(0)?).ok()?, num_signatures_required: bcs::from_bytes(script.args().get(1)?).ok()?, metadata_keys: bcs::from_bytes(script.args().get(2)?).ok()?, metadata_values: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -3053,7 +3053,7 @@ mod decoder { if let TransactionPayload::EntryFunction(script) = payload { Some( EntryFunctionCall::MultisigAccountExecuteRejectedTransaction { - multisig_account: bcs::from_bytes(script.args().first()?).ok()?, + multisig_account: bcs::from_bytes(script.args().get(0)?).ok()?, }, ) } else { @@ -3066,7 +3066,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountMigrateWithOwners { - additional_owners: bcs::from_bytes(script.args().first()?).ok()?, + additional_owners: bcs::from_bytes(script.args().get(0)?).ok()?, num_signatures_required: bcs::from_bytes(script.args().get(1)?).ok()?, metadata_keys: bcs::from_bytes(script.args().get(2)?).ok()?, metadata_values: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -3081,7 +3081,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountRejectTransaction { - multisig_account: bcs::from_bytes(script.args().first()?).ok()?, + multisig_account: bcs::from_bytes(script.args().get(0)?).ok()?, sequence_number: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3094,7 +3094,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountRemoveOwner { - owner_to_remove: bcs::from_bytes(script.args().first()?).ok()?, + owner_to_remove: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -3106,7 +3106,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountRemoveOwners { - owners_to_remove: bcs::from_bytes(script.args().first()?).ok()?, + owners_to_remove: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -3118,7 +3118,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountUpdateMetadata { - keys: bcs::from_bytes(script.args().first()?).ok()?, + keys: bcs::from_bytes(script.args().get(0)?).ok()?, values: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3131,7 +3131,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountUpdateSignaturesRequired { - new_num_signatures_required: bcs::from_bytes(script.args().first()?).ok()?, + new_num_signatures_required: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -3143,7 +3143,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountVoteTransanction { - multisig_account: bcs::from_bytes(script.args().first()?).ok()?, + multisig_account: bcs::from_bytes(script.args().get(0)?).ok()?, sequence_number: bcs::from_bytes(script.args().get(1)?).ok()?, approved: bcs::from_bytes(script.args().get(2)?).ok()?, }) @@ -3155,7 +3155,7 @@ mod decoder { pub fn object_transfer_call(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::ObjectTransferCall { - object: bcs::from_bytes(script.args().first()?).ok()?, + object: bcs::from_bytes(script.args().get(0)?).ok()?, to: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3166,7 +3166,7 @@ mod decoder { pub fn ol_account_create_account(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::OlAccountCreateAccount { - auth_key: bcs::from_bytes(script.args().first()?).ok()?, + auth_key: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -3178,7 +3178,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::OlAccountSetAllowDirectCoinTransfers { - allow: bcs::from_bytes(script.args().first()?).ok()?, + allow: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -3188,7 +3188,7 @@ mod decoder { pub fn ol_account_transfer(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::OlAccountTransfer { - to: bcs::from_bytes(script.args().first()?).ok()?, + to: bcs::from_bytes(script.args().get(0)?).ok()?, amount: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3215,7 +3215,7 @@ mod decoder { pub fn proof_of_fee_pof_update_bid(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::ProofOfFeePofUpdateBid { - bid: bcs::from_bytes(script.args().first()?).ok()?, + bid: bcs::from_bytes(script.args().get(0)?).ok()?, epoch_expiry: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3228,7 +3228,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::ProofOfFeePofUpdateBidNetReward { - net_reward: bcs::from_bytes(script.args().first()?).ok()?, + net_reward: bcs::from_bytes(script.args().get(0)?).ok()?, epoch_expiry: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3239,7 +3239,7 @@ mod decoder { pub fn safe_init_payment_multisig(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::SafeInitPaymentMultisig { - authorities: bcs::from_bytes(script.args().first()?).ok()?, + authorities: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -3249,7 +3249,7 @@ mod decoder { pub fn secret_bid_commit(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::SecretBidCommit { - digest: bcs::from_bytes(script.args().first()?).ok()?, + digest: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -3259,7 +3259,7 @@ mod decoder { pub fn secret_bid_reveal(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::SecretBidReveal { - pk: bcs::from_bytes(script.args().first()?).ok()?, + pk: bcs::from_bytes(script.args().get(0)?).ok()?, entry_fee: bcs::from_bytes(script.args().get(1)?).ok()?, signed_msg: bcs::from_bytes(script.args().get(2)?).ok()?, }) @@ -3273,7 +3273,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::SlowWalletSmokeTestVmUnlock { - user_addr: bcs::from_bytes(script.args().first()?).ok()?, + user_addr: bcs::from_bytes(script.args().get(0)?).ok()?, unlocked: bcs::from_bytes(script.args().get(1)?).ok()?, transferred: bcs::from_bytes(script.args().get(2)?).ok()?, }) @@ -3293,7 +3293,7 @@ mod decoder { pub fn stake_initialize_validator(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::StakeInitializeValidator { - consensus_pubkey: bcs::from_bytes(script.args().first()?).ok()?, + consensus_pubkey: bcs::from_bytes(script.args().get(0)?).ok()?, proof_of_possession: bcs::from_bytes(script.args().get(1)?).ok()?, network_addresses: bcs::from_bytes(script.args().get(2)?).ok()?, fullnode_addresses: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -3306,7 +3306,7 @@ mod decoder { pub fn stake_rotate_consensus_key(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::StakeRotateConsensusKey { - validator_address: bcs::from_bytes(script.args().first()?).ok()?, + validator_address: bcs::from_bytes(script.args().get(0)?).ok()?, new_consensus_pubkey: bcs::from_bytes(script.args().get(1)?).ok()?, proof_of_possession: bcs::from_bytes(script.args().get(2)?).ok()?, }) @@ -3320,7 +3320,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::StakeUpdateNetworkAndFullnodeAddresses { - validator_address: bcs::from_bytes(script.args().first()?).ok()?, + validator_address: bcs::from_bytes(script.args().get(0)?).ok()?, new_network_addresses: bcs::from_bytes(script.args().get(1)?).ok()?, new_fullnode_addresses: bcs::from_bytes(script.args().get(2)?).ok()?, }) @@ -3334,7 +3334,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::ValidatorUniverseRegisterValidator { - consensus_pubkey: bcs::from_bytes(script.args().first()?).ok()?, + consensus_pubkey: bcs::from_bytes(script.args().get(0)?).ok()?, proof_of_possession: bcs::from_bytes(script.args().get(1)?).ok()?, network_addresses: bcs::from_bytes(script.args().get(2)?).ok()?, fullnode_addresses: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -3347,7 +3347,7 @@ mod decoder { pub fn version_set_version(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::VersionSetVersion { - major: bcs::from_bytes(script.args().first()?).ok()?, + major: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -3357,7 +3357,7 @@ mod decoder { pub fn vouch_insist_vouch_for(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::VouchInsistVouchFor { - friend_account: bcs::from_bytes(script.args().first()?).ok()?, + friend_account: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -3367,7 +3367,7 @@ mod decoder { pub fn vouch_revoke(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::VouchRevoke { - friend_account: bcs::from_bytes(script.args().first()?).ok()?, + friend_account: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None @@ -3377,7 +3377,7 @@ mod decoder { pub fn vouch_vouch_for(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::VouchVouchFor { - friend_account: bcs::from_bytes(script.args().first()?).ok()?, + friend_account: bcs::from_bytes(script.args().get(0)?).ok()?, }) } else { None From f1fc7241d1d8fcfae5b016d7a26bb598f2992017 Mon Sep 17 00:00:00 2001 From: Rupert De Presto Date: Wed, 12 Mar 2025 13:03:28 -0400 Subject: [PATCH 11/19] tokio evil. remove channel impl --- tools/txs/src/stream/epoch_tickle_poll.rs | 24 +++++-------- tools/txs/src/stream/mod.rs | 2 +- tools/txs/src/txs_cli.rs | 5 ++- tools/txs/src/txs_cli_stream.rs | 34 +++++++------------ .../{commit_reveal.rs => commit_reveal.todo} | 11 +++--- tools/txs/tests/trigger_epoch.rs | 7 ++-- 6 files changed, 31 insertions(+), 52 deletions(-) rename tools/txs/tests/{commit_reveal.rs => commit_reveal.todo} (92%) diff --git a/tools/txs/src/stream/epoch_tickle_poll.rs b/tools/txs/src/stream/epoch_tickle_poll.rs index 71b65e3e7..7d899d717 100644 --- a/tools/txs/src/stream/epoch_tickle_poll.rs +++ b/tools/txs/src/stream/epoch_tickle_poll.rs @@ -1,33 +1,27 @@ use crate::submit_transaction::Sender as LibraSender; use diem_logger::info; -use diem_types::transaction::TransactionPayload; use libra_cached_packages::libra_stdlib; -use std::borrow::BorrowMut; -use std::sync::mpsc::Sender; -use std::sync::Arc; -use std::sync::Mutex; use std::time::Duration; pub async fn epoch_tickle_poll( - mut tx: Sender, - sender: Arc>, + sender: &mut LibraSender, delay_secs: u64, ) -> anyhow::Result<()> { println!("polling epoch boundary"); - + let client = sender.client().clone(); loop { - let client = sender.lock().unwrap().client().clone(); - // TODO: make the client borrow instead of clone - let res = libra_query::chain_queries::epoch_over_can_trigger(&client.clone()).await; + let res = libra_query::chain_queries::epoch_over_can_trigger(&client).await; match res { Ok(true) => { - let func = libra_stdlib::diem_governance_trigger_epoch(); + let payload = libra_stdlib::diem_governance_trigger_epoch(); + + sender.sign_submit_wait(payload).await?; - tx.borrow_mut() - .send(func) - .expect("could not send message to channel"); + // tx.borrow_mut() + // .send(func) + // .expect("could not send message to channel"); } _ => { info!("Not ready to call epoch.") diff --git a/tools/txs/src/stream/mod.rs b/tools/txs/src/stream/mod.rs index c2f4f00c3..d710d9379 100644 --- a/tools/txs/src/stream/mod.rs +++ b/tools/txs/src/stream/mod.rs @@ -1,3 +1,3 @@ pub mod bid_commit_reveal; -pub mod channel; +// pub mod channel; pub mod epoch_tickle_poll; diff --git a/tools/txs/src/txs_cli.rs b/tools/txs/src/txs_cli.rs index 40e8cb0ea..4691062ed 100644 --- a/tools/txs/src/txs_cli.rs +++ b/tools/txs/src/txs_cli.rs @@ -18,7 +18,6 @@ use libra_types::{ }; use libra_wallet::account_keys::{get_keys_from_mnem, get_keys_from_prompt}; use std::path::PathBuf; -use std::sync::{Arc, Mutex}; use url::Url; #[derive(Parser)] @@ -220,8 +219,8 @@ impl TxsCli { Some(TxsSub::User(user_txs)) => user_txs.run(&mut send).await, Some(TxsSub::Community(comm_txs)) => comm_txs.run(&mut send).await, Some(TxsSub::Stream(stream_txs)) => { - let arc_send = Arc::new(Mutex::new(send)); - stream_txs.start(arc_send).await; + // let arc_send = Arc::new(Mutex::new(send)); + stream_txs.start(&mut send).await?; Ok(()) } _ => { diff --git a/tools/txs/src/txs_cli_stream.rs b/tools/txs/src/txs_cli_stream.rs index 49f109038..967720157 100644 --- a/tools/txs/src/txs_cli_stream.rs +++ b/tools/txs/src/txs_cli_stream.rs @@ -1,7 +1,6 @@ use crate::stream::bid_commit_reveal::commit_reveal_poll; -use crate::stream::{bid_commit_reveal::PofBidArgs, channel, epoch_tickle_poll::epoch_tickle_poll}; +use crate::stream::{bid_commit_reveal::PofBidArgs, epoch_tickle_poll::epoch_tickle_poll}; use crate::submit_transaction::Sender as LibraSender; -use std::sync::{Arc, Mutex}; #[derive(clap::Subcommand)] pub enum StreamTxs { @@ -16,29 +15,22 @@ pub enum StreamTxs { } impl StreamTxs { - pub async fn start(&self, libra_sender: Arc>) { - let (send_chan, receive_chan) = channel::init_channel(); - let stream_service = channel::listen(receive_chan, libra_sender.clone()); - + pub async fn start(&self, libra_sender: &mut LibraSender) -> anyhow::Result<()> { match &self { StreamTxs::EpochTickle { delay } => { println!("EpochTickle entry"); - epoch_tickle_poll(send_chan, libra_sender, delay.unwrap_or(60)) - .await - .expect("could not poll epoch boundary"); + epoch_tickle_poll(libra_sender, delay.unwrap_or(60)).await?; } - StreamTxs::ValBid(args) => commit_reveal_poll( - send_chan, - libra_sender, - args.net_reward, - args.delay.unwrap_or(60), - ) - .await - .expect("commit reveal poll ended"), + StreamTxs::ValBid(_args) => { + todo!() + } // commit_reveal_poll( + // send_chan, + // libra_sender, + // args.net_reward, + // args.delay.unwrap_or(60), + // ) + // .expect("commit reveal poll ended"), }; - - stream_service - .join() - .expect("could not complete tasks in stream"); + Ok(()) } } diff --git a/tools/txs/tests/commit_reveal.rs b/tools/txs/tests/commit_reveal.todo similarity index 92% rename from tools/txs/tests/commit_reveal.rs rename to tools/txs/tests/commit_reveal.todo index 4f9d71ea8..9f753a5d5 100644 --- a/tools/txs/tests/commit_reveal.rs +++ b/tools/txs/tests/commit_reveal.todo @@ -1,8 +1,5 @@ -//! test trigger epoch - -use std::sync::{Arc, Mutex}; +//! test commit reveal use std::time::Duration; - use diem_forge::Swarm; use libra_cached_packages::libra_stdlib; use libra_query::query_view; @@ -45,14 +42,14 @@ async fn background_commit_reveal() -> anyhow::Result<()> { }); // create a Sender using the validator's app config let val_app_cfg = ls.first_account_app_cfg()?; - let validator_sender = Sender::from_app_cfg(&val_app_cfg, None).await?; - let wrapped_sender = Arc::new(Mutex::new(validator_sender)); + let mut validator_sender = Sender::from_app_cfg(&val_app_cfg, None).await?; + // let wrapped_sender = Arc::new(Mutex::new(validator_sender)); // run the txs tool in background in stream mode // run the txs tool in background in stream tokio::spawn(async move { - commit_reveal_cmd.start(wrapped_sender).await; + commit_reveal_cmd.start(&mut validator_sender).await?; }); // //////// FLIP BIT //////// diff --git a/tools/txs/tests/trigger_epoch.rs b/tools/txs/tests/trigger_epoch.rs index 0471e93d6..10ffdf84f 100644 --- a/tools/txs/tests/trigger_epoch.rs +++ b/tools/txs/tests/trigger_epoch.rs @@ -1,6 +1,4 @@ //! test trigger epoch - -use std::sync::{Arc, Mutex}; use std::time::Duration; use diem_forge::Swarm; @@ -102,13 +100,12 @@ async fn background_trigger_epoch() -> anyhow::Result<()> { let trigger_epoch_cmd = StreamTxs::EpochTickle { delay: Some(5) }; // create a Sender using the validator's app config let val_app_cfg = ls.first_account_app_cfg()?; - let validator_sender = Sender::from_app_cfg(&val_app_cfg, None).await?; - let wrapped_sender = Arc::new(Mutex::new(validator_sender)); + let mut validator_sender = Sender::from_app_cfg(&val_app_cfg, None).await?; // run the txs tool in background in stream tokio::spawn(async move { - trigger_epoch_cmd.start(wrapped_sender).await; + trigger_epoch_cmd.start(&mut validator_sender).await.unwrap(); }); From 569e13b6bb6cd86e9c21973321d70537cb5a1d4f Mon Sep 17 00:00:00 2001 From: Crispin Von Rubato Date: Wed, 12 Mar 2025 13:14:17 -0400 Subject: [PATCH 12/19] commit reveal poll refactor --- tools/txs/src/stream/bid_commit_reveal.rs | 22 ++++++---------------- tools/txs/src/stream/epoch_tickle_poll.rs | 4 ---- tools/txs/src/txs_cli_stream.rs | 19 +++++++++---------- 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/tools/txs/src/stream/bid_commit_reveal.rs b/tools/txs/src/stream/bid_commit_reveal.rs index 075cb8192..e4ab67722 100644 --- a/tools/txs/src/stream/bid_commit_reveal.rs +++ b/tools/txs/src/stream/bid_commit_reveal.rs @@ -1,5 +1,5 @@ use crate::submit_transaction::Sender as LibraSender; -use diem_logger::{debug, error}; +use diem_logger::debug; use diem_sdk::crypto::ed25519::Ed25519Signature; use diem_sdk::crypto::Signature; use diem_sdk::crypto::SigningKey; @@ -9,10 +9,6 @@ use libra_cached_packages::libra_stdlib; use libra_query::chain_queries; use libra_types::exports::Client; use serde::{Deserialize, Serialize}; -use std::borrow::BorrowMut; -use std::sync::mpsc::Sender; -use std::sync::Arc; -use std::sync::Mutex; use std::time::Duration; #[derive(clap::Args, Debug)] @@ -83,36 +79,30 @@ impl PofBidData { } pub async fn commit_reveal_poll( - mut tx: Sender, - sender: Arc>, + sender: &mut LibraSender, entry_fee: u64, delay_secs: u64, ) -> anyhow::Result<()> { println!("commit reveal bid: {}", entry_fee); let mut bid = PofBidData::new(entry_fee); + let client = sender.client().clone(); loop { - let client = sender.lock().unwrap().client().clone(); // releases the mutex - // check what epoch we are in let _ = bid.update_epoch(&client).await; dbg!(&bid); debug!("bid: {:?}", &bid); let must_reveal = libra_query::chain_queries::within_commit_reveal_window(&client).await?; - let la = &sender.lock().unwrap().local_account; - let tx_payload = if must_reveal { + let la = &sender.local_account; + let payload = if must_reveal { bid.encode_reveal_tx_payload(la) } else { bid.encode_commit_tx_payload(la) }; // send to channel - match tx.borrow_mut().send(tx_payload.clone()) { - Ok(_) => debug!("success with payload: {:?}", tx_payload), - // Don't abort on error - Err(e) => error!("transaction fails with message: {:?}\ncontinuing...", e), - }; + sender.sign_submit_wait(payload).await?; tokio::time::sleep(Duration::from_secs(delay_secs)).await; } } diff --git a/tools/txs/src/stream/epoch_tickle_poll.rs b/tools/txs/src/stream/epoch_tickle_poll.rs index 7d899d717..1205295cb 100644 --- a/tools/txs/src/stream/epoch_tickle_poll.rs +++ b/tools/txs/src/stream/epoch_tickle_poll.rs @@ -10,7 +10,6 @@ pub async fn epoch_tickle_poll( println!("polling epoch boundary"); let client = sender.client().clone(); loop { - // TODO: make the client borrow instead of clone let res = libra_query::chain_queries::epoch_over_can_trigger(&client).await; match res { @@ -19,9 +18,6 @@ pub async fn epoch_tickle_poll( sender.sign_submit_wait(payload).await?; - // tx.borrow_mut() - // .send(func) - // .expect("could not send message to channel"); } _ => { info!("Not ready to call epoch.") diff --git a/tools/txs/src/txs_cli_stream.rs b/tools/txs/src/txs_cli_stream.rs index 967720157..532cd48db 100644 --- a/tools/txs/src/txs_cli_stream.rs +++ b/tools/txs/src/txs_cli_stream.rs @@ -21,16 +21,15 @@ impl StreamTxs { println!("EpochTickle entry"); epoch_tickle_poll(libra_sender, delay.unwrap_or(60)).await?; } - StreamTxs::ValBid(_args) => { - todo!() - } // commit_reveal_poll( - // send_chan, - // libra_sender, - // args.net_reward, - // args.delay.unwrap_or(60), - // ) - // .expect("commit reveal poll ended"), - }; + StreamTxs::ValBid(args) => { + commit_reveal_poll( + libra_sender, + args.net_reward, + args.delay.unwrap_or(60), + ) + .await?; + } + } Ok(()) } } From 9a5899ae85fe95ee9aec9a679ede4e1742cf2b35 Mon Sep 17 00:00:00 2001 From: Chiaretta Fitz Legato Date: Wed, 12 Mar 2025 14:27:06 -0400 Subject: [PATCH 13/19] commit reveal test scaffold --- tools/query/src/chain_queries.rs | 12 ++++- tools/txs/src/stream/bid_commit_reveal.rs | 10 +++-- .../{commit_reveal.todo => commit_reveal.rs} | 44 +++++++++---------- 3 files changed, 39 insertions(+), 27 deletions(-) rename tools/txs/tests/{commit_reveal.todo => commit_reveal.rs} (73%) diff --git a/tools/query/src/chain_queries.rs b/tools/query/src/chain_queries.rs index 080e07209..f8981f959 100644 --- a/tools/query/src/chain_queries.rs +++ b/tools/query/src/chain_queries.rs @@ -96,7 +96,7 @@ pub async fn epoch_over_can_trigger(client: &Client) -> anyhow::Result { Ok(value[0]) } -/// Retrieves the current blockchain height. +/// Retrieves if we are within the commit reveal window pub async fn within_commit_reveal_window(client: &Client) -> anyhow::Result { let res = get_view(client, "0x1::secret_bid::in_reveal_window", None, None).await?; @@ -104,3 +104,13 @@ pub async fn within_commit_reveal_window(client: &Client) -> anyhow::Result anyhow::Result { + let res = get_view(client, "0x1::reconfiguration::get_remaining_epoch_secs", None, None).await?; + + let value: Vec = serde_json::from_value(res)?; + let secs = value.first().unwrap().parse::()?; + + Ok(secs) +} diff --git a/tools/txs/src/stream/bid_commit_reveal.rs b/tools/txs/src/stream/bid_commit_reveal.rs index e4ab67722..49679d6df 100644 --- a/tools/txs/src/stream/bid_commit_reveal.rs +++ b/tools/txs/src/stream/bid_commit_reveal.rs @@ -1,5 +1,5 @@ use crate::submit_transaction::Sender as LibraSender; -use diem_logger::debug; +use diem_logger::info; use diem_sdk::crypto::ed25519::Ed25519Signature; use diem_sdk::crypto::Signature; use diem_sdk::crypto::SigningKey; @@ -88,16 +88,20 @@ pub async fn commit_reveal_poll( let client = sender.client().clone(); loop { + let secs = libra_query::chain_queries::secs_remaining_in_epoch(&client).await?; + info!("seconds remaining in epoch {secs}"); + // check what epoch we are in let _ = bid.update_epoch(&client).await; - dbg!(&bid); - debug!("bid: {:?}", &bid); let must_reveal = libra_query::chain_queries::within_commit_reveal_window(&client).await?; + let la = &sender.local_account; let payload = if must_reveal { + info!("must reveal bid"); bid.encode_reveal_tx_payload(la) } else { + info!("sending sealed bid"); bid.encode_commit_tx_payload(la) }; diff --git a/tools/txs/tests/commit_reveal.todo b/tools/txs/tests/commit_reveal.rs similarity index 73% rename from tools/txs/tests/commit_reveal.todo rename to tools/txs/tests/commit_reveal.rs index 9f753a5d5..999f9f6af 100644 --- a/tools/txs/tests/commit_reveal.todo +++ b/tools/txs/tests/commit_reveal.rs @@ -43,17 +43,15 @@ async fn background_commit_reveal() -> anyhow::Result<()> { // create a Sender using the validator's app config let val_app_cfg = ls.first_account_app_cfg()?; let mut validator_sender = Sender::from_app_cfg(&val_app_cfg, None).await?; - // let wrapped_sender = Arc::new(Mutex::new(validator_sender)); // run the txs tool in background in stream mode - // run the txs tool in background in stream tokio::spawn(async move { - commit_reveal_cmd.start(&mut validator_sender).await?; + commit_reveal_cmd.start(&mut validator_sender).await.unwrap(); }); // //////// FLIP BIT //////// - std::thread::sleep(Duration::from_secs(20)); + std::thread::sleep(Duration::from_secs(60)); // helper_set_enable_trigger(&mut ls).await; @@ -78,22 +76,22 @@ async fn background_commit_reveal() -> anyhow::Result<()> { Ok(()) } -// helper for the testnet root to enable epoch boundary trigger -async fn helper_set_enable_trigger(ls: &mut LibraSmoke) { - let mut public_info = ls.swarm.diem_public_info(); - - let payload = public_info - .transaction_factory() - .payload(libra_stdlib::epoch_boundary_smoke_enable_trigger()); - - let enable_trigger_tx = public_info - .root_account() - .sign_with_transaction_builder(payload); - - public_info - .client() - .submit_and_wait(&enable_trigger_tx) - .await - .expect("could not send demo tx"); - println!("testnet root account enables epoch trigger"); -} +// // helper for the testnet root to enable epoch boundary trigger +// async fn helper_set_enable_trigger(ls: &mut LibraSmoke) { +// let mut public_info = ls.swarm.diem_public_info(); + +// let payload = public_info +// .transaction_factory() +// .payload(libra_stdlib::epoch_boundary_smoke_enable_trigger()); + +// let enable_trigger_tx = public_info +// .root_account() +// .sign_with_transaction_builder(payload); + +// public_info +// .client() +// .submit_and_wait(&enable_trigger_tx) +// .await +// .expect("could not send demo tx"); +// println!("testnet root account enables epoch trigger"); +// } From 09009a9292ec9f88d91f2f96fcd970b3e3a0ac12 Mon Sep 17 00:00:00 2001 From: Montague Ritardando Date: Wed, 12 Mar 2025 14:27:27 -0400 Subject: [PATCH 14/19] force genesis on chainid=4 test harness to have 30 second epochs --- framework/libra-framework/sources/genesis.move | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/framework/libra-framework/sources/genesis.move b/framework/libra-framework/sources/genesis.move index ab419a90a..880ee9662 100644 --- a/framework/libra-framework/sources/genesis.move +++ b/framework/libra-framework/sources/genesis.move @@ -137,6 +137,12 @@ module diem_framework::genesis { chain_id::initialize(&diem_framework_account, chain_id); reconfiguration::initialize(&diem_framework_account); + // 0L: force testing environments to have short epochs + // TODO this should be solved at the SwarmBuilder level + // but requires change to vendors. + if (chain_id == 4) { + epoch_interval_microsecs = 1_000_000 * 30; + }; block::initialize(&diem_framework_account, epoch_interval_microsecs); state_storage::initialize(&diem_framework_account); randomness::initialize(&diem_framework_account); From 9b33053f13c9c40bcdea78dde9c5297d106b68ff Mon Sep 17 00:00:00 2001 From: Raffa di Lento Date: Wed, 12 Mar 2025 14:28:37 -0400 Subject: [PATCH 15/19] nits --- framework/libra-framework/sources/genesis.move | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/framework/libra-framework/sources/genesis.move b/framework/libra-framework/sources/genesis.move index 880ee9662..8ebbcef25 100644 --- a/framework/libra-framework/sources/genesis.move +++ b/framework/libra-framework/sources/genesis.move @@ -152,7 +152,6 @@ module diem_framework::genesis { validator_universe::initialize(&diem_framework_account); proof_of_fee::init_genesis_baseline_reward(&diem_framework_account); slow_wallet::initialize(&diem_framework_account); - // tower_state::initialize(&diem_framework_account); safe::initialize(&diem_framework_account); donor_voice::initialize(&diem_framework_account); epoch_boundary::initialize(&diem_framework_account); @@ -166,7 +165,7 @@ module diem_framework::genesis { let zero_x_two_sig = create_signer(@0x2); sacred_cows::init(&zero_x_two_sig); - // end 0L + //////// end 0L ///////// timestamp::set_time_has_started(&diem_framework_account); } @@ -187,8 +186,6 @@ module diem_framework::genesis { diem_framework: &signer, core_resources_auth_key: vector, ) { - // let (burn_cap, mint_cap) = diem_coin::initialize(diem_framework); - let core_resources = account::create_account(@core_resources); account::rotate_authentication_key_internal(&core_resources, core_resources_auth_key); From 8fb7d97a5b906b77d3e6a88a27de916df52a685c Mon Sep 17 00:00:00 2001 From: Caty Saint Beaver Date: Wed, 12 Mar 2025 15:36:10 -0400 Subject: [PATCH 16/19] refactor arbitrary bytes signing --- .../sources/ol_sources/secret_bid.move | 153 +++++++++--------- 1 file changed, 75 insertions(+), 78 deletions(-) diff --git a/framework/libra-framework/sources/ol_sources/secret_bid.move b/framework/libra-framework/sources/ol_sources/secret_bid.move index 200140637..d6bb6ea39 100644 --- a/framework/libra-framework/sources/ol_sources/secret_bid.move +++ b/framework/libra-framework/sources/ol_sources/secret_bid.move @@ -10,21 +10,26 @@ module ol_framework::secret_bid { use std::hash; use std::signer; use std::vector; + use diem_std::debug::print; use diem_std::ed25519; use diem_std::comparator; use diem_framework::epoch_helper; use diem_framework::reconfiguration; use diem_framework::system_addresses; - use ol_framework::testnet; use ol_framework::address_utils; friend ol_framework::genesis; friend ol_framework::proof_of_fee; + #[test_only] + use diem_std::ed25519::SecretKey; #[test_only] use diem_framework::account; + #[test_only] + use diem_std::from_bcs; + #[test_only] friend ol_framework::test_boundary; @@ -36,7 +41,7 @@ module ol_framework::secret_bid { /// User bidding not initialized const ECOMMIT_BID_NOT_INITIALIZED: u64 = 1; /// Invalid signature on bid message - const EINVALID_SIGNATURE: u64 = 2; + const EINVALID_BID_SIGNATURE: u64 = 2; /// Must reveal bid in same epoch as committed const EMISMATCH_EPOCH: u64 = 3; /// Must submit bid before reveal window opens, and reveal bid only after. @@ -160,7 +165,10 @@ module ol_framework::secret_bid { // confirm that the signature bytes belong to the message (the Bid struct) let sig = ed25519::new_signature_from_bytes(signed_message_bytes); - assert!(ed25519::signature_verify_strict_t(&sig, &pubkey, bid_message), error::invalid_argument(EINVALID_SIGNATURE)); + print(&@0x111); + print(&bid_message); + let bid_bytes = bcs::to_bytes(&bid_message); + assert!(ed25519::signature_verify_strict(&sig, &pubkey, bid_bytes), error::invalid_argument(EINVALID_BID_SIGNATURE)); ///////// // NOTE: previously we would check if the public key for the signed message @@ -188,9 +196,11 @@ module ol_framework::secret_bid { (addr, bids) } - ///////// GETTERS //////// - public(friend) fun get_bid_unchecked(user: address): u64 acquires CommittedBid { + + ///////// GETTERS //////// + #[view] + public fun get_bid_unchecked(user: address): u64 acquires CommittedBid { let state = borrow_global(user); state.reveal_entry_fee @@ -278,104 +288,97 @@ module ol_framework::secret_bid { } - #[test] - fun test_sign_message() { - use diem_std::from_bcs; + #[test_only] + // helper to set get a signature for a Bid type + fun sign_bid_struct(sk: &SecretKey, bid: &Bid): vector { + let msg_bytes = bcs::to_bytes(bid); + let to_sig = ed25519::sign_arbitrary_bytes(sk, msg_bytes); + + ed25519::signature_to_bytes(&to_sig) + } + #[test] + fun test_sign_arbitrary_message() { let (new_sk, new_pk) = ed25519::generate_keys(); let new_pk_unvalidated = ed25519::public_key_to_unvalidated(&new_pk); - let new_auth_key = ed25519::unvalidated_public_key_to_authentication_key(&new_pk_unvalidated); - let new_addr = from_bcs::to_address(new_auth_key); - let _alice = account::create_account_for_test(new_addr); - let message = Bid { + let bid = Bid { entry_fee: 0, epoch: 0, }; - let to_sig = ed25519::sign_struct(&new_sk, copy message); - let sig_bytes = ed25519::signature_to_bytes(&to_sig); - // end set-up - - // yes repetitive, but following the same workflow - let sig_again = ed25519::new_signature_from_bytes(sig_bytes); - - assert!(ed25519::signature_verify_strict_t(&sig_again, &new_pk_unvalidated, message), error::invalid_argument(EINVALID_SIGNATURE)); + let signed_message_bytes = sign_bid_struct(&new_sk, &bid); + let sig = ed25519::new_signature_from_bytes(signed_message_bytes); + // encoding directly should yield the same bytes as in signed_message_bytes + let encoded = bcs::to_bytes(&bid); + assert!(ed25519::signature_verify_strict(&sig, &new_pk_unvalidated, encoded), error::invalid_argument(EINVALID_BID_SIGNATURE)); } - #[test] - fun test_check_signature() { - use diem_std::from_bcs; - +#[test] +// sanity test the Signature type vs the bytes +fun test_round_trip_sig_type() { let (new_sk, new_pk) = ed25519::generate_keys(); let new_pk_unvalidated = ed25519::public_key_to_unvalidated(&new_pk); - let new_auth_key = ed25519::unvalidated_public_key_to_authentication_key(&new_pk_unvalidated); - let new_addr = from_bcs::to_address(new_auth_key); - let _alice = account::create_account_for_test(new_addr); let message = Bid { entry_fee: 0, epoch: 0, }; + let msg_bytes = bcs::to_bytes(&message); - let to_sig = ed25519::sign_struct(&new_sk, copy message); + let to_sig = ed25519::sign_arbitrary_bytes(&new_sk, copy msg_bytes); let sig_bytes = ed25519::signature_to_bytes(&to_sig); - // end set-up - - // yes repetitive, but following the same workflow - let sig_again = ed25519::new_signature_from_bytes(sig_bytes); + // should equal to_sig above + let sig_should_be_same = ed25519::new_signature_from_bytes(sig_bytes); + let res = comparator::compare(&to_sig, &sig_should_be_same); + assert!(comparator::is_equal(&res), 7357001); - assert!(ed25519::signature_verify_strict_t(&sig_again, &new_pk_unvalidated, copy message), error::invalid_argument(EINVALID_SIGNATURE)); - - let pk_bytes = ed25519::unvalidated_public_key_to_bytes(&new_pk_unvalidated); - - check_signature(pk_bytes, sig_bytes, message); - - } + assert!(ed25519::signature_verify_strict(&sig_should_be_same, &new_pk_unvalidated, copy msg_bytes), error::invalid_argument(EINVALID_BID_SIGNATURE)); +} #[test] - #[expected_failure(abort_code = 65538, location = Self)] - fun test_check_signature_sad() { - use diem_std::from_bcs; - + fun test_check_signature_happy() { let (new_sk, new_pk) = ed25519::generate_keys(); let new_pk_unvalidated = ed25519::public_key_to_unvalidated(&new_pk); - let new_auth_key = ed25519::unvalidated_public_key_to_authentication_key(&new_pk_unvalidated); - let new_addr = from_bcs::to_address(new_auth_key); - let _alice = account::create_account_for_test(new_addr); + let pk_bytes = ed25519::unvalidated_public_key_to_bytes(&new_pk_unvalidated); - let message = Bid { + let bid = Bid { entry_fee: 0, epoch: 0, }; - let to_sig = ed25519::sign_struct(&new_sk, copy message); - let sig_bytes = ed25519::signature_to_bytes(&to_sig); - // end set-up + let signed_message_bytes = sign_bid_struct(&new_sk, &bid); - // yes repetitive, but following the same workflow - let sig_again = ed25519::new_signature_from_bytes(sig_bytes); + check_signature(pk_bytes, signed_message_bytes, bid); + } - assert!(ed25519::signature_verify_strict_t(&sig_again, &new_pk_unvalidated, copy message), error::invalid_argument(EINVALID_SIGNATURE)); + #[test] + #[expected_failure(abort_code = 65538, location = Self)] + fun wrong_key_sad() { + let (_wrong_sk, wrong_pk) = ed25519::generate_keys(); + let wrong_pk_unvalidated = ed25519::public_key_to_unvalidated(&wrong_pk); + let wrong_pk_bytes = ed25519::unvalidated_public_key_to_bytes(&wrong_pk_unvalidated); - let pk_bytes = ed25519::unvalidated_public_key_to_bytes(&new_pk_unvalidated); - let message = Bid { - entry_fee: 2, // incorrect + let (new_sk, new_pk) = ed25519::generate_keys(); + let new_pk_unvalidated = ed25519::public_key_to_unvalidated(&new_pk); + let _good_pk_bytes = ed25519::unvalidated_public_key_to_bytes(&new_pk_unvalidated); + + let bid = Bid { + entry_fee: 0, epoch: 0, }; - check_signature(pk_bytes, sig_bytes, message); + let signed_message_bytes = sign_bid_struct(&new_sk, &bid); + check_signature(wrong_pk_bytes, signed_message_bytes, bid); } #[test(framework = @0x1)] fun test_commit_message(framework: &signer) acquires CommittedBid { - use diem_std::from_bcs; - let this_epoch = 1; epoch_helper::test_set_epoch(framework, this_epoch); @@ -384,16 +387,17 @@ module ol_framework::secret_bid { let new_auth_key = ed25519::unvalidated_public_key_to_authentication_key(&new_pk_unvalidated); let new_addr = from_bcs::to_address(new_auth_key); let alice = account::create_account_for_test(new_addr); + let entry_fee = 5; let epoch = 0; - let message = Bid { + let bid = Bid { entry_fee, epoch, }; - let to_sig = ed25519::sign_struct(&new_sk, copy message); - let sig_bytes = ed25519::signature_to_bytes(&to_sig); + // let to_sig = ed25519::sign_arbitrary_bytes(&new_sk, copy message); + let sig_bytes = sign_bid_struct(&new_sk, &bid); let digest = make_hash(entry_fee, epoch, sig_bytes); // end set-up @@ -402,8 +406,6 @@ module ol_framework::secret_bid { #[test(framework = @0x1)] fun test_reveal(framework: &signer) acquires CommittedBid { - use diem_std::from_bcs; - let epoch = 1; epoch_helper::test_set_epoch(framework, epoch); @@ -413,13 +415,12 @@ module ol_framework::secret_bid { let new_addr = from_bcs::to_address(new_auth_key); let alice = account::create_account_for_test(new_addr); let entry_fee = 5; - let message = Bid { + let bid = Bid { entry_fee, epoch, }; - let to_sig = ed25519::sign_struct(&new_sk, copy message); - let sig_bytes = ed25519::signature_to_bytes(&to_sig); + let sig_bytes = sign_bid_struct(&new_sk, &bid); let digest = make_hash(entry_fee, epoch, sig_bytes); // end set-up @@ -427,15 +428,14 @@ module ol_framework::secret_bid { let pk_bytes = ed25519::unvalidated_public_key_to_bytes(&new_pk_unvalidated); - check_signature(pk_bytes, sig_bytes, message); + check_signature(pk_bytes, sig_bytes, bid); reveal_entry_fee_impl(&alice, pk_bytes, 5, sig_bytes); } #[test(framework = @0x1)] #[expected_failure(abort_code = 65538, location = Self)] - fun test_reveal_sad_wrong_epoch(framework: &signer) acquires CommittedBid { - use diem_std::from_bcs; + fun test_reveal_bad_epoch(framework: &signer) acquires CommittedBid { let epoch = 1; epoch_helper::test_set_epoch(framework, epoch); @@ -445,15 +445,12 @@ module ol_framework::secret_bid { let new_addr = from_bcs::to_address(new_auth_key); let alice = account::create_account_for_test(new_addr); let entry_fee = 5; - let wrong_epoch = 100; - - let message = Bid { + let bid = Bid { entry_fee, - epoch: wrong_epoch, // wrong epoch, we are at 1 + epoch, }; - let to_sig = ed25519::sign_struct(&new_sk, copy message); - let sig_bytes = ed25519::signature_to_bytes(&to_sig); + let sig_bytes = sign_bid_struct(&new_sk, &bid); let digest = make_hash(entry_fee, epoch, sig_bytes); // end set-up @@ -461,9 +458,9 @@ module ol_framework::secret_bid { let pk_bytes = ed25519::unvalidated_public_key_to_bytes(&new_pk_unvalidated); - check_signature(pk_bytes, sig_bytes, message); + check_signature(pk_bytes, sig_bytes, bid); - reveal_entry_fee_impl(&alice, pk_bytes, 5, sig_bytes); + reveal_entry_fee_impl(&alice, pk_bytes, 10, sig_bytes); } #[test(framework = @0x1, alice = @0x10001, bob = @0x10002, carol = @0x10003)] From 97c33b1ad26a81624bcd71ba09562c934cf90119 Mon Sep 17 00:00:00 2001 From: Gianna Forte Date: Wed, 12 Mar 2025 16:43:34 -0400 Subject: [PATCH 17/19] commit reveal e2e tests passing --- .../sources/ol_sources/secret_bid.move | 4 +- tools/query/src/chain_queries.rs | 25 ++++++- tools/txs/src/stream/bid_commit_reveal.rs | 68 +++++++++++++++---- tools/txs/src/submit_transaction.rs | 1 - tools/txs/tests/commit_reveal.rs | 63 +++++------------ 5 files changed, 97 insertions(+), 64 deletions(-) diff --git a/framework/libra-framework/sources/ol_sources/secret_bid.move b/framework/libra-framework/sources/ol_sources/secret_bid.move index d6bb6ea39..f3f057491 100644 --- a/framework/libra-framework/sources/ol_sources/secret_bid.move +++ b/framework/libra-framework/sources/ol_sources/secret_bid.move @@ -214,8 +214,8 @@ module ol_framework::secret_bid { // get the timestamp let remaining_secs = reconfiguration::get_remaining_epoch_secs(); let window = if (testnet::is_testnet()) { - // ten secs - 10 + // 20 secs + 20 } else { // five mins 60*5 diff --git a/tools/query/src/chain_queries.rs b/tools/query/src/chain_queries.rs index f8981f959..fc62ebacb 100644 --- a/tools/query/src/chain_queries.rs +++ b/tools/query/src/chain_queries.rs @@ -3,7 +3,7 @@ use crate::query_view::{self, get_view}; use anyhow::Context; use diem_sdk::rest_client::Client; - +use libra_types::exports::AccountAddress; /// Retrieves the current epoch from the blockchain. pub async fn get_epoch(client: &Client) -> anyhow::Result { let res = get_view(client, "0x1::epoch_helper::get_current_epoch", None, None).await?; @@ -107,7 +107,28 @@ pub async fn within_commit_reveal_window(client: &Client) -> anyhow::Result anyhow::Result { - let res = get_view(client, "0x1::reconfiguration::get_remaining_epoch_secs", None, None).await?; + let res = get_view( + client, + "0x1::reconfiguration::get_remaining_epoch_secs", + None, + None, + ) + .await?; + + let value: Vec = serde_json::from_value(res)?; + let secs = value.first().unwrap().parse::()?; + + Ok(secs) +} + +pub async fn validator_committed_bid(client: &Client, val: AccountAddress) -> anyhow::Result { + let res = get_view( + client, + "0x1::secret_bid::get_bid_unchecked", + None, + Some(val.to_hex_literal()), + ) + .await?; let value: Vec = serde_json::from_value(res)?; let secs = value.first().unwrap().parse::()?; diff --git a/tools/txs/src/stream/bid_commit_reveal.rs b/tools/txs/src/stream/bid_commit_reveal.rs index 49679d6df..efde57259 100644 --- a/tools/txs/src/stream/bid_commit_reveal.rs +++ b/tools/txs/src/stream/bid_commit_reveal.rs @@ -1,6 +1,8 @@ use crate::submit_transaction::Sender as LibraSender; +use diem_logger::error; use diem_logger::info; use diem_sdk::crypto::ed25519::Ed25519Signature; +use diem_sdk::crypto::HashValue; use diem_sdk::crypto::Signature; use diem_sdk::crypto::SigningKey; use diem_sdk::types::LocalAccount; @@ -62,19 +64,29 @@ impl PofBidData { Ok(signed) } - fn encode_commit_tx_payload(&self, keys: &LocalAccount) -> TransactionPayload { - let digest = self.sign_bcs_bytes(keys).expect("could not sign bytes"); - libra_stdlib::secret_bid_commit(digest.to_bytes().to_vec()) + fn create_hash_bytes(&self, keys: &LocalAccount) -> anyhow::Result> { + let sig = self.sign_bcs_bytes(keys).expect("could not sign bytes"); + + make_commit_hash(self.entry_fee, self.epoch, sig.to_bytes().to_vec()) } - fn encode_reveal_tx_payload(&self, keys: &LocalAccount) -> TransactionPayload { - let digest = self.sign_bcs_bytes(keys).unwrap(); + fn encode_commit_tx_payload(&self, keys: &LocalAccount) -> anyhow::Result { + let digest = self.create_hash_bytes(keys)?; + let payload = libra_stdlib::secret_bid_commit(digest); + Ok(payload) + } - libra_stdlib::secret_bid_reveal( + // In the reveal we simply show the signature and + // open bid info. On the MoveVM side the hash is recreated + // so that it should match the commit + fn encode_reveal_tx_payload(&self, keys: &LocalAccount) -> anyhow::Result { + let sig = self.sign_bcs_bytes(keys)?; + let payload = libra_stdlib::secret_bid_reveal( keys.public_key().to_bytes().to_vec(), self.entry_fee, - digest.to_bytes().to_vec(), - ) + sig.to_bytes().to_vec(), + ); + Ok(payload) } } @@ -95,22 +107,50 @@ pub async fn commit_reveal_poll( let _ = bid.update_epoch(&client).await; let must_reveal = libra_query::chain_queries::within_commit_reveal_window(&client).await?; - let la = &sender.local_account; - let payload = if must_reveal { + if must_reveal { info!("must reveal bid"); - bid.encode_reveal_tx_payload(la) + let payload = bid.encode_reveal_tx_payload(la)?; + // don't abort if we are trying too eager! + match sender.sign_submit_wait(payload).await { + Ok(_) => info!("successfully revealed bid"), + Err(e) => error!( + "could not reveal bid: message {}\ncontinuing...", + e.to_string() + ), + } } else { info!("sending sealed bid"); - bid.encode_commit_tx_payload(la) + let payload = bid.encode_commit_tx_payload(la)?; + match sender.sign_submit_wait(payload).await { + Ok(_) => info!("successfully committed bid"), + Err(e) => error!( + "could not commit bid: message {}\ncontinuing...", + e.to_string() + ), + } }; - // send to channel - sender.sign_submit_wait(payload).await?; tokio::time::sleep(Duration::from_secs(delay_secs)).await; } } +/// produces a hash by combining the bid and the signed bid. +/// On the move side there is an equivalent function at: secret_bid.move +pub fn make_commit_hash( + entry_fee: u64, + epoch: u64, + mut signed_message: Vec, +) -> anyhow::Result> { + let bid = PofBidData { entry_fee, epoch }; + + let mut bid_bytes = bcs::to_bytes(&bid)?; + bid_bytes.append(&mut signed_message); + let commitment = HashValue::sha3_256_of(&bid_bytes); + + Ok(commitment.to_vec()) +} + #[cfg(test)] fn test_local_account() -> LocalAccount { use diem_sdk::crypto::ed25519::Ed25519PrivateKey; diff --git a/tools/txs/src/submit_transaction.rs b/tools/txs/src/submit_transaction.rs index 11ecbff6e..f18794bee 100644 --- a/tools/txs/src/submit_transaction.rs +++ b/tools/txs/src/submit_transaction.rs @@ -212,7 +212,6 @@ impl Sender { println!("transaction sent"); self.response = Some(r.clone()); spin.finish_and_clear(); - // debug!("{:?}", &r); OLProgress::complete("transaction success"); Ok(r) } diff --git a/tools/txs/tests/commit_reveal.rs b/tools/txs/tests/commit_reveal.rs index 999f9f6af..9622dcfc6 100644 --- a/tools/txs/tests/commit_reveal.rs +++ b/tools/txs/tests/commit_reveal.rs @@ -1,7 +1,5 @@ //! test commit reveal use std::time::Duration; -use diem_forge::Swarm; -use libra_cached_packages::libra_stdlib; use libra_query::query_view; use libra_smoke_tests::libra_smoke::LibraSmoke; use libra_txs::stream::bid_commit_reveal::PofBidArgs; @@ -33,65 +31,40 @@ async fn background_commit_reveal() -> anyhow::Result<()> { "epoch should be 2" ); + let test_bid_amount = 1010; + //////// TRIGGER THE EPOCH //////// // The TriggerEpoch command does not require arguments, // so we create it directly and attempt to run it. let commit_reveal_cmd = StreamTxs::ValBid(PofBidArgs { - net_reward: 1010, + net_reward: test_bid_amount, delay: Some(5), }); // create a Sender using the validator's app config let val_app_cfg = ls.first_account_app_cfg()?; let mut validator_sender = Sender::from_app_cfg(&val_app_cfg, None).await?; + let client = validator_sender.client().clone(); + let validator_addr = validator_sender.local_account.address(); // run the txs tool in background in stream mode - tokio::spawn(async move { + let h = tokio::spawn(async move { commit_reveal_cmd.start(&mut validator_sender).await.unwrap(); }); - // //////// FLIP BIT //////// - std::thread::sleep(Duration::from_secs(60)); - - // helper_set_enable_trigger(&mut ls).await; - - // std::thread::sleep(Duration::from_secs(20)); - - // // now the background service should succeed in triggering epoch. - - // let after_trigger_epoch_query_res = query_view::get_view( - // &ls.client(), - // "0x1::epoch_helper::get_current_epoch", - // None, - // None, - // ) - // .await - // .expect("Query failed: get epoch failed"); + // WAIT FOR END OF EPOCH chain_id==4 is 30 secs + std::thread::sleep(Duration::from_secs(30)); + h.abort(); - // assert!( - // &after_trigger_epoch_query_res.as_array().unwrap()[0] == "3", - // "epoch should be 3" - // ); + match libra_query::chain_queries::validator_committed_bid(&client, validator_addr).await { + Ok(onchain_bid) => { + assert!(test_bid_amount == onchain_bid, "incorrect bid found"); + println!("Validator committed bid successfully."); + } + Err(e) => { + assert!(false, "Failed to commit bid: {:?}", e); + } + } Ok(()) } - -// // helper for the testnet root to enable epoch boundary trigger -// async fn helper_set_enable_trigger(ls: &mut LibraSmoke) { -// let mut public_info = ls.swarm.diem_public_info(); - -// let payload = public_info -// .transaction_factory() -// .payload(libra_stdlib::epoch_boundary_smoke_enable_trigger()); - -// let enable_trigger_tx = public_info -// .root_account() -// .sign_with_transaction_builder(payload); - -// public_info -// .client() -// .submit_and_wait(&enable_trigger_tx) -// .await -// .expect("could not send demo tx"); -// println!("testnet root account enables epoch trigger"); -// } From b28242ff1e983b6f051dcffacf35ff5319bd4322 Mon Sep 17 00:00:00 2001 From: Isa O'Legato Date: Wed, 12 Mar 2025 16:44:37 -0400 Subject: [PATCH 18/19] clippy --- .../src/libra_framework_sdk_builder.rs | 128 +++++++++--------- tools/txs/src/stream/epoch_tickle_poll.rs | 6 +- tools/txs/src/txs_cli_stream.rs | 7 +- tools/txs/tests/commit_reveal.rs | 7 +- tools/txs/tests/trigger_epoch.rs | 8 +- 5 files changed, 76 insertions(+), 80 deletions(-) diff --git a/framework/cached-packages/src/libra_framework_sdk_builder.rs b/framework/cached-packages/src/libra_framework_sdk_builder.rs index 5b6df6c06..52437f602 100644 --- a/framework/cached-packages/src/libra_framework_sdk_builder.rs +++ b/framework/cached-packages/src/libra_framework_sdk_builder.rs @@ -2489,7 +2489,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::AccountOfferRotationCapability { - rotation_capability_sig_bytes: bcs::from_bytes(script.args().get(0)?).ok()?, + rotation_capability_sig_bytes: bcs::from_bytes(script.args().first()?).ok()?, account_scheme: bcs::from_bytes(script.args().get(1)?).ok()?, account_public_key_bytes: bcs::from_bytes(script.args().get(2)?).ok()?, recipient_address: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2504,7 +2504,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::AccountOfferSignerCapability { - signer_capability_sig_bytes: bcs::from_bytes(script.args().get(0)?).ok()?, + signer_capability_sig_bytes: bcs::from_bytes(script.args().first()?).ok()?, account_scheme: bcs::from_bytes(script.args().get(1)?).ok()?, account_public_key_bytes: bcs::from_bytes(script.args().get(2)?).ok()?, recipient_address: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2539,7 +2539,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::AccountRevokeRotationCapability { - to_be_revoked_address: bcs::from_bytes(script.args().get(0)?).ok()?, + to_be_revoked_address: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -2551,7 +2551,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::AccountRevokeSignerCapability { - to_be_revoked_address: bcs::from_bytes(script.args().get(0)?).ok()?, + to_be_revoked_address: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -2563,7 +2563,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::AccountRotateAuthenticationKey { - from_scheme: bcs::from_bytes(script.args().get(0)?).ok()?, + from_scheme: bcs::from_bytes(script.args().first()?).ok()?, from_public_key_bytes: bcs::from_bytes(script.args().get(1)?).ok()?, to_scheme: bcs::from_bytes(script.args().get(2)?).ok()?, to_public_key_bytes: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2581,7 +2581,7 @@ mod decoder { if let TransactionPayload::EntryFunction(script) = payload { Some( EntryFunctionCall::AccountRotateAuthenticationKeyWithRotationCapability { - rotation_cap_offerer_address: bcs::from_bytes(script.args().get(0)?).ok()?, + rotation_cap_offerer_address: bcs::from_bytes(script.args().first()?).ok()?, new_scheme: bcs::from_bytes(script.args().get(1)?).ok()?, new_public_key_bytes: bcs::from_bytes(script.args().get(2)?).ok()?, cap_update_table: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2595,7 +2595,7 @@ mod decoder { pub fn burn_set_send_community(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::BurnSetSendCommunity { - community: bcs::from_bytes(script.args().get(0)?).ok()?, + community: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -2605,7 +2605,7 @@ mod decoder { pub fn code_publish_package_txn(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::CodePublishPackageTxn { - metadata_serialized: bcs::from_bytes(script.args().get(0)?).ok()?, + metadata_serialized: bcs::from_bytes(script.args().first()?).ok()?, code: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2616,8 +2616,8 @@ mod decoder { pub fn coin_transfer(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::CoinTransfer { - coin_type: script.ty_args().get(0)?.clone(), - to: bcs::from_bytes(script.args().get(0)?).ok()?, + coin_type: script.ty_args().first()?.clone(), + to: bcs::from_bytes(script.args().first()?).ok()?, amount: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2631,7 +2631,7 @@ mod decoder { if let TransactionPayload::EntryFunction(script) = payload { Some( EntryFunctionCall::CommunityWalletInitChangeSignerCommunityMultisig { - multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_address: bcs::from_bytes(script.args().first()?).ok()?, new_signer: bcs::from_bytes(script.args().get(1)?).ok()?, is_add_operation: bcs::from_bytes(script.args().get(2)?).ok()?, n_of_m: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2648,7 +2648,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::CommunityWalletInitFinalizeAndCage { - num_signers: bcs::from_bytes(script.args().get(0)?).ok()?, + num_signers: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -2660,7 +2660,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::CommunityWalletInitInitCommunity { - initial_authorities: bcs::from_bytes(script.args().get(0)?).ok()?, + initial_authorities: bcs::from_bytes(script.args().first()?).ok()?, check_threshold: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2673,7 +2673,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::CommunityWalletInitProposeOffer { - new_signers: bcs::from_bytes(script.args().get(0)?).ok()?, + new_signers: bcs::from_bytes(script.args().first()?).ok()?, num_signers: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2687,7 +2687,7 @@ mod decoder { if let TransactionPayload::EntryFunction(script) = payload { Some( EntryFunctionCall::DiemGovernanceAddApprovedScriptHashScript { - proposal_id: bcs::from_bytes(script.args().get(0)?).ok()?, + proposal_id: bcs::from_bytes(script.args().first()?).ok()?, }, ) } else { @@ -2700,7 +2700,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DiemGovernanceAssertCanResolve { - proposal_id: bcs::from_bytes(script.args().get(0)?).ok()?, + proposal_id: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -2712,7 +2712,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DiemGovernanceCreateProposalV2 { - execution_hash: bcs::from_bytes(script.args().get(0)?).ok()?, + execution_hash: bcs::from_bytes(script.args().first()?).ok()?, metadata_location: bcs::from_bytes(script.args().get(1)?).ok()?, metadata_hash: bcs::from_bytes(script.args().get(2)?).ok()?, is_multi_step_proposal: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2727,7 +2727,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DiemGovernanceOlCreateProposalV2 { - execution_hash: bcs::from_bytes(script.args().get(0)?).ok()?, + execution_hash: bcs::from_bytes(script.args().first()?).ok()?, metadata_location: bcs::from_bytes(script.args().get(1)?).ok()?, metadata_hash: bcs::from_bytes(script.args().get(2)?).ok()?, is_multi_step_proposal: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2740,7 +2740,7 @@ mod decoder { pub fn diem_governance_ol_vote(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DiemGovernanceOlVote { - proposal_id: bcs::from_bytes(script.args().get(0)?).ok()?, + proposal_id: bcs::from_bytes(script.args().first()?).ok()?, should_pass: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2771,7 +2771,7 @@ mod decoder { pub fn diem_governance_vote(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DiemGovernanceVote { - proposal_id: bcs::from_bytes(script.args().get(0)?).ok()?, + proposal_id: bcs::from_bytes(script.args().first()?).ok()?, should_pass: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2784,7 +2784,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DonorVoiceTxsProposeLiquidateTx { - multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_address: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -2796,7 +2796,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DonorVoiceTxsProposePaymentTx { - multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_address: bcs::from_bytes(script.args().first()?).ok()?, payee: bcs::from_bytes(script.args().get(1)?).ok()?, value: bcs::from_bytes(script.args().get(2)?).ok()?, description: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -2811,7 +2811,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DonorVoiceTxsProposeVetoTx { - multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_address: bcs::from_bytes(script.args().first()?).ok()?, id: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2824,7 +2824,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DonorVoiceTxsVoteLiquidationTx { - multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_address: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -2834,7 +2834,7 @@ mod decoder { pub fn donor_voice_txs_vote_veto_tx(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::DonorVoiceTxsVoteVetoTx { - multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_address: bcs::from_bytes(script.args().first()?).ok()?, id: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2865,7 +2865,7 @@ mod decoder { pub fn jail_unjail_by_voucher(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::JailUnjailByVoucher { - addr: bcs::from_bytes(script.args().get(0)?).ok()?, + addr: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -2887,7 +2887,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::LibraCoinDelegateMintCapability { - to: bcs::from_bytes(script.args().get(0)?).ok()?, + to: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -2897,7 +2897,7 @@ mod decoder { pub fn libra_coin_mint_to_impl(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::LibraCoinMintToImpl { - dst_addr: bcs::from_bytes(script.args().get(0)?).ok()?, + dst_addr: bcs::from_bytes(script.args().first()?).ok()?, amount: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2908,7 +2908,7 @@ mod decoder { pub fn multi_action_claim_offer(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultiActionClaimOffer { - multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_address: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -2930,7 +2930,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultiActionMigrationMigrateOffer { - multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_address: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -2940,7 +2940,7 @@ mod decoder { pub fn multisig_account_add_owner(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountAddOwner { - new_owner: bcs::from_bytes(script.args().get(0)?).ok()?, + new_owner: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -2950,7 +2950,7 @@ mod decoder { pub fn multisig_account_add_owners(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountAddOwners { - new_owners: bcs::from_bytes(script.args().get(0)?).ok()?, + new_owners: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -2962,7 +2962,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountApproveTransaction { - multisig_account: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_account: bcs::from_bytes(script.args().first()?).ok()?, sequence_number: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -2973,7 +2973,7 @@ mod decoder { pub fn multisig_account_create(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountCreate { - num_signatures_required: bcs::from_bytes(script.args().get(0)?).ok()?, + num_signatures_required: bcs::from_bytes(script.args().first()?).ok()?, metadata_keys: bcs::from_bytes(script.args().get(1)?).ok()?, metadata_values: bcs::from_bytes(script.args().get(2)?).ok()?, }) @@ -2987,7 +2987,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountCreateTransaction { - multisig_account: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_account: bcs::from_bytes(script.args().first()?).ok()?, payload: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3001,7 +3001,7 @@ mod decoder { if let TransactionPayload::EntryFunction(script) = payload { Some( EntryFunctionCall::MultisigAccountCreateTransactionWithHash { - multisig_account: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_account: bcs::from_bytes(script.args().first()?).ok()?, payload_hash: bcs::from_bytes(script.args().get(1)?).ok()?, }, ) @@ -3016,7 +3016,7 @@ mod decoder { if let TransactionPayload::EntryFunction(script) = payload { Some( EntryFunctionCall::MultisigAccountCreateWithExistingAccount { - multisig_address: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_address: bcs::from_bytes(script.args().first()?).ok()?, owners: bcs::from_bytes(script.args().get(1)?).ok()?, num_signatures_required: bcs::from_bytes(script.args().get(2)?).ok()?, account_scheme: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -3037,7 +3037,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountCreateWithOwners { - additional_owners: bcs::from_bytes(script.args().get(0)?).ok()?, + additional_owners: bcs::from_bytes(script.args().first()?).ok()?, num_signatures_required: bcs::from_bytes(script.args().get(1)?).ok()?, metadata_keys: bcs::from_bytes(script.args().get(2)?).ok()?, metadata_values: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -3053,7 +3053,7 @@ mod decoder { if let TransactionPayload::EntryFunction(script) = payload { Some( EntryFunctionCall::MultisigAccountExecuteRejectedTransaction { - multisig_account: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_account: bcs::from_bytes(script.args().first()?).ok()?, }, ) } else { @@ -3066,7 +3066,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountMigrateWithOwners { - additional_owners: bcs::from_bytes(script.args().get(0)?).ok()?, + additional_owners: bcs::from_bytes(script.args().first()?).ok()?, num_signatures_required: bcs::from_bytes(script.args().get(1)?).ok()?, metadata_keys: bcs::from_bytes(script.args().get(2)?).ok()?, metadata_values: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -3081,7 +3081,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountRejectTransaction { - multisig_account: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_account: bcs::from_bytes(script.args().first()?).ok()?, sequence_number: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3094,7 +3094,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountRemoveOwner { - owner_to_remove: bcs::from_bytes(script.args().get(0)?).ok()?, + owner_to_remove: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -3106,7 +3106,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountRemoveOwners { - owners_to_remove: bcs::from_bytes(script.args().get(0)?).ok()?, + owners_to_remove: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -3118,7 +3118,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountUpdateMetadata { - keys: bcs::from_bytes(script.args().get(0)?).ok()?, + keys: bcs::from_bytes(script.args().first()?).ok()?, values: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3131,7 +3131,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountUpdateSignaturesRequired { - new_num_signatures_required: bcs::from_bytes(script.args().get(0)?).ok()?, + new_num_signatures_required: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -3143,7 +3143,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::MultisigAccountVoteTransanction { - multisig_account: bcs::from_bytes(script.args().get(0)?).ok()?, + multisig_account: bcs::from_bytes(script.args().first()?).ok()?, sequence_number: bcs::from_bytes(script.args().get(1)?).ok()?, approved: bcs::from_bytes(script.args().get(2)?).ok()?, }) @@ -3155,7 +3155,7 @@ mod decoder { pub fn object_transfer_call(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::ObjectTransferCall { - object: bcs::from_bytes(script.args().get(0)?).ok()?, + object: bcs::from_bytes(script.args().first()?).ok()?, to: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3166,7 +3166,7 @@ mod decoder { pub fn ol_account_create_account(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::OlAccountCreateAccount { - auth_key: bcs::from_bytes(script.args().get(0)?).ok()?, + auth_key: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -3178,7 +3178,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::OlAccountSetAllowDirectCoinTransfers { - allow: bcs::from_bytes(script.args().get(0)?).ok()?, + allow: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -3188,7 +3188,7 @@ mod decoder { pub fn ol_account_transfer(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::OlAccountTransfer { - to: bcs::from_bytes(script.args().get(0)?).ok()?, + to: bcs::from_bytes(script.args().first()?).ok()?, amount: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3215,7 +3215,7 @@ mod decoder { pub fn proof_of_fee_pof_update_bid(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::ProofOfFeePofUpdateBid { - bid: bcs::from_bytes(script.args().get(0)?).ok()?, + bid: bcs::from_bytes(script.args().first()?).ok()?, epoch_expiry: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3228,7 +3228,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::ProofOfFeePofUpdateBidNetReward { - net_reward: bcs::from_bytes(script.args().get(0)?).ok()?, + net_reward: bcs::from_bytes(script.args().first()?).ok()?, epoch_expiry: bcs::from_bytes(script.args().get(1)?).ok()?, }) } else { @@ -3239,7 +3239,7 @@ mod decoder { pub fn safe_init_payment_multisig(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::SafeInitPaymentMultisig { - authorities: bcs::from_bytes(script.args().get(0)?).ok()?, + authorities: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -3249,7 +3249,7 @@ mod decoder { pub fn secret_bid_commit(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::SecretBidCommit { - digest: bcs::from_bytes(script.args().get(0)?).ok()?, + digest: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -3259,7 +3259,7 @@ mod decoder { pub fn secret_bid_reveal(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::SecretBidReveal { - pk: bcs::from_bytes(script.args().get(0)?).ok()?, + pk: bcs::from_bytes(script.args().first()?).ok()?, entry_fee: bcs::from_bytes(script.args().get(1)?).ok()?, signed_msg: bcs::from_bytes(script.args().get(2)?).ok()?, }) @@ -3273,7 +3273,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::SlowWalletSmokeTestVmUnlock { - user_addr: bcs::from_bytes(script.args().get(0)?).ok()?, + user_addr: bcs::from_bytes(script.args().first()?).ok()?, unlocked: bcs::from_bytes(script.args().get(1)?).ok()?, transferred: bcs::from_bytes(script.args().get(2)?).ok()?, }) @@ -3293,7 +3293,7 @@ mod decoder { pub fn stake_initialize_validator(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::StakeInitializeValidator { - consensus_pubkey: bcs::from_bytes(script.args().get(0)?).ok()?, + consensus_pubkey: bcs::from_bytes(script.args().first()?).ok()?, proof_of_possession: bcs::from_bytes(script.args().get(1)?).ok()?, network_addresses: bcs::from_bytes(script.args().get(2)?).ok()?, fullnode_addresses: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -3306,7 +3306,7 @@ mod decoder { pub fn stake_rotate_consensus_key(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::StakeRotateConsensusKey { - validator_address: bcs::from_bytes(script.args().get(0)?).ok()?, + validator_address: bcs::from_bytes(script.args().first()?).ok()?, new_consensus_pubkey: bcs::from_bytes(script.args().get(1)?).ok()?, proof_of_possession: bcs::from_bytes(script.args().get(2)?).ok()?, }) @@ -3320,7 +3320,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::StakeUpdateNetworkAndFullnodeAddresses { - validator_address: bcs::from_bytes(script.args().get(0)?).ok()?, + validator_address: bcs::from_bytes(script.args().first()?).ok()?, new_network_addresses: bcs::from_bytes(script.args().get(1)?).ok()?, new_fullnode_addresses: bcs::from_bytes(script.args().get(2)?).ok()?, }) @@ -3334,7 +3334,7 @@ mod decoder { ) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::ValidatorUniverseRegisterValidator { - consensus_pubkey: bcs::from_bytes(script.args().get(0)?).ok()?, + consensus_pubkey: bcs::from_bytes(script.args().first()?).ok()?, proof_of_possession: bcs::from_bytes(script.args().get(1)?).ok()?, network_addresses: bcs::from_bytes(script.args().get(2)?).ok()?, fullnode_addresses: bcs::from_bytes(script.args().get(3)?).ok()?, @@ -3347,7 +3347,7 @@ mod decoder { pub fn version_set_version(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::VersionSetVersion { - major: bcs::from_bytes(script.args().get(0)?).ok()?, + major: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -3357,7 +3357,7 @@ mod decoder { pub fn vouch_insist_vouch_for(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::VouchInsistVouchFor { - friend_account: bcs::from_bytes(script.args().get(0)?).ok()?, + friend_account: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -3367,7 +3367,7 @@ mod decoder { pub fn vouch_revoke(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::VouchRevoke { - friend_account: bcs::from_bytes(script.args().get(0)?).ok()?, + friend_account: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None @@ -3377,7 +3377,7 @@ mod decoder { pub fn vouch_vouch_for(payload: &TransactionPayload) -> Option { if let TransactionPayload::EntryFunction(script) = payload { Some(EntryFunctionCall::VouchVouchFor { - friend_account: bcs::from_bytes(script.args().get(0)?).ok()?, + friend_account: bcs::from_bytes(script.args().first()?).ok()?, }) } else { None diff --git a/tools/txs/src/stream/epoch_tickle_poll.rs b/tools/txs/src/stream/epoch_tickle_poll.rs index 1205295cb..f3febe578 100644 --- a/tools/txs/src/stream/epoch_tickle_poll.rs +++ b/tools/txs/src/stream/epoch_tickle_poll.rs @@ -3,10 +3,7 @@ use diem_logger::info; use libra_cached_packages::libra_stdlib; use std::time::Duration; -pub async fn epoch_tickle_poll( - sender: &mut LibraSender, - delay_secs: u64, -) -> anyhow::Result<()> { +pub async fn epoch_tickle_poll(sender: &mut LibraSender, delay_secs: u64) -> anyhow::Result<()> { println!("polling epoch boundary"); let client = sender.client().clone(); loop { @@ -17,7 +14,6 @@ pub async fn epoch_tickle_poll( let payload = libra_stdlib::diem_governance_trigger_epoch(); sender.sign_submit_wait(payload).await?; - } _ => { info!("Not ready to call epoch.") diff --git a/tools/txs/src/txs_cli_stream.rs b/tools/txs/src/txs_cli_stream.rs index 532cd48db..6bc6a5fa6 100644 --- a/tools/txs/src/txs_cli_stream.rs +++ b/tools/txs/src/txs_cli_stream.rs @@ -22,12 +22,7 @@ impl StreamTxs { epoch_tickle_poll(libra_sender, delay.unwrap_or(60)).await?; } StreamTxs::ValBid(args) => { - commit_reveal_poll( - libra_sender, - args.net_reward, - args.delay.unwrap_or(60), - ) - .await?; + commit_reveal_poll(libra_sender, args.net_reward, args.delay.unwrap_or(60)).await?; } } Ok(()) diff --git a/tools/txs/tests/commit_reveal.rs b/tools/txs/tests/commit_reveal.rs index 9622dcfc6..895ff4d9c 100644 --- a/tools/txs/tests/commit_reveal.rs +++ b/tools/txs/tests/commit_reveal.rs @@ -1,9 +1,9 @@ //! test commit reveal -use std::time::Duration; use libra_query::query_view; use libra_smoke_tests::libra_smoke::LibraSmoke; use libra_txs::stream::bid_commit_reveal::PofBidArgs; use libra_txs::{submit_transaction::Sender, txs_cli_stream::StreamTxs}; +use std::time::Duration; #[tokio::test(flavor = "multi_thread", worker_threads = 4)] /// Test triggering a new epoch @@ -49,7 +49,10 @@ async fn background_commit_reveal() -> anyhow::Result<()> { // run the txs tool in background in stream mode let h = tokio::spawn(async move { - commit_reveal_cmd.start(&mut validator_sender).await.unwrap(); + commit_reveal_cmd + .start(&mut validator_sender) + .await + .unwrap(); }); // WAIT FOR END OF EPOCH chain_id==4 is 30 secs diff --git a/tools/txs/tests/trigger_epoch.rs b/tools/txs/tests/trigger_epoch.rs index 10ffdf84f..4e2ee0576 100644 --- a/tools/txs/tests/trigger_epoch.rs +++ b/tools/txs/tests/trigger_epoch.rs @@ -68,7 +68,7 @@ async fn sync_trigger_epoch() -> anyhow::Result<()> { Ok(()) } -#[tokio::test (flavor = "multi_thread", worker_threads = 2)] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] /// Test triggering a new epoch // Scenario: We want to trigger a new epoch using the TriggerEpoch command // We will assume that triggering an epoch is an operation that we can test in a single node testnet @@ -105,10 +105,12 @@ async fn background_trigger_epoch() -> anyhow::Result<()> { // run the txs tool in background in stream tokio::spawn(async move { - trigger_epoch_cmd.start(&mut validator_sender).await.unwrap(); + trigger_epoch_cmd + .start(&mut validator_sender) + .await + .unwrap(); }); - //////// FLIP BIT //////// std::thread::sleep(Duration::from_secs(10)); From f379c964efc5181a8fa11ef19a49487192151324 Mon Sep 17 00:00:00 2001 From: Sere O'Rubato Date: Wed, 12 Mar 2025 17:38:46 -0400 Subject: [PATCH 19/19] patch test durations --- .../sources/ol_sources/secret_bid.move | 39 ++++++++------ tools/query/src/chain_queries.rs | 17 +----- tools/txs/src/stream/bid_commit_reveal.rs | 54 ++++++++++++++++--- tools/txs/tests/commit_reveal.rs | 15 +++--- 4 files changed, 79 insertions(+), 46 deletions(-) diff --git a/framework/libra-framework/sources/ol_sources/secret_bid.move b/framework/libra-framework/sources/ol_sources/secret_bid.move index f3f057491..ebeabbce7 100644 --- a/framework/libra-framework/sources/ol_sources/secret_bid.move +++ b/framework/libra-framework/sources/ol_sources/secret_bid.move @@ -44,8 +44,10 @@ module ol_framework::secret_bid { const EINVALID_BID_SIGNATURE: u64 = 2; /// Must reveal bid in same epoch as committed const EMISMATCH_EPOCH: u64 = 3; - /// Must submit bid before reveal window opens, and reveal bid only after. - const ENOT_IN_REVEAL_WINDOW: u64 = 4; + /// Must commit a bid before reveal window opens. + const ETOO_LATE_TO_COMMIT: u64 = 4; + /// Can only reveal bid within reveal window. + const ECANT_REVEAL_YET: u64 = 4; /// Bad Alice, the reveal does not match the commit const ECOMMIT_DIGEST_NOT_EQUAL: u64 = 5; /// Bid is for different epoch, expired @@ -53,8 +55,10 @@ module ol_framework::secret_bid { struct CommittedBid has key { reveal_entry_fee: u64, + reveal_epoch: u64, entry_fee_history: vector, // keep previous 7 days bids commit_digest: vector, + // the epoch receiving commits commit_epoch: u64, } @@ -74,14 +78,14 @@ module ol_framework::secret_bid { /// Transaction entry function for committing bid public entry fun commit(user: &signer, digest: vector) acquires CommittedBid { // don't allow committing within reveal window - assert!(!in_reveal_window(), error::invalid_state(ENOT_IN_REVEAL_WINDOW)); + assert!(!in_reveal_window(), error::invalid_state(ETOO_LATE_TO_COMMIT)); commit_entry_fee_impl(user, digest); } /// Transaction entry function for revealing bid public entry fun reveal(user: &signer, pk: vector, entry_fee: u64, signed_msg: vector) acquires CommittedBid { // don't allow committing within reveal window - assert!(in_reveal_window(), error::invalid_state(ENOT_IN_REVEAL_WINDOW)); + assert!(in_reveal_window(), error::invalid_state(ECANT_REVEAL_YET)); reveal_entry_fee_impl(user, pk, entry_fee, signed_msg); } @@ -91,6 +95,7 @@ module ol_framework::secret_bid { if (!is_init(signer::address_of(user))) { move_to(user, CommittedBid { reveal_entry_fee: 0, + reveal_epoch: 0, entry_fee_history: vector::empty(), commit_digest: vector::empty(), commit_epoch: 0, @@ -158,6 +163,7 @@ module ol_framework::secret_bid { assert!(comparator::is_equal(&comparator::compare(&commitment, &state.commit_digest)), error::invalid_argument(ECOMMIT_DIGEST_NOT_EQUAL)); state.reveal_entry_fee = entry_fee; + state.reveal_epoch = epoch; } fun check_signature(account_public_key_bytes: vector, signed_message_bytes: vector, bid_message: Bid) { @@ -214,8 +220,8 @@ module ol_framework::secret_bid { // get the timestamp let remaining_secs = reconfiguration::get_remaining_epoch_secs(); let window = if (testnet::is_testnet()) { - // 20 secs - 20 + // 15 secs, half of the short 30 sec epoch + 15 } else { // five mins 60*5 @@ -239,26 +245,25 @@ module ol_framework::secret_bid { get_bid_unchecked(user) } - /// Find the most recent epoch the validator has placed a bid on. - public(friend) fun latest_epoch_bid(user: address): u64 acquires CommittedBid { + #[view] + /// Find the most recent epoch the validator has placed a bid on. + public fun latest_epoch_bid(user: address): u64 acquires CommittedBid { let state = borrow_global(user); state.commit_epoch } + #[view] + /// Find the most recent epoch the validator has revealed a bid for + public fun latest_epoch_revealed(user: address): u64 acquires CommittedBid { + let state = borrow_global(user); + state.reveal_epoch + } /// does the user have a current bid public(friend) fun has_valid_bid(user: address): bool acquires CommittedBid { let state = borrow_global(user); state.commit_epoch == epoch_helper::get_current_epoch() } - /// will abort if bid is not valid - fun assert_valid_bid(user: address) acquires CommittedBid { - // if we are not in reveal window this information will be confusing. - assert!(in_reveal_window(), error::invalid_state(ENOT_IN_REVEAL_WINDOW)); - - assert!(has_valid_bid(user), error::invalid_state(EBID_EXPIRED)); - } - #[view] /// get the current bid, and exclude bids that are stale public fun historical_bids(user: address): vector acquires CommittedBid { @@ -276,9 +281,11 @@ module ol_framework::secret_bid { if (!exists(user_addr)) { move_to(user, CommittedBid { reveal_entry_fee, + reveal_epoch: commit_epoch, entry_fee_history: vector[0], commit_digest: vector[0], commit_epoch, + }); } else { let state = borrow_global_mut(user_addr); diff --git a/tools/query/src/chain_queries.rs b/tools/query/src/chain_queries.rs index fc62ebacb..dfb925b11 100644 --- a/tools/query/src/chain_queries.rs +++ b/tools/query/src/chain_queries.rs @@ -3,7 +3,7 @@ use crate::query_view::{self, get_view}; use anyhow::Context; use diem_sdk::rest_client::Client; -use libra_types::exports::AccountAddress; + /// Retrieves the current epoch from the blockchain. pub async fn get_epoch(client: &Client) -> anyhow::Result { let res = get_view(client, "0x1::epoch_helper::get_current_epoch", None, None).await?; @@ -120,18 +120,3 @@ pub async fn secs_remaining_in_epoch(client: &Client) -> anyhow::Result { Ok(secs) } - -pub async fn validator_committed_bid(client: &Client, val: AccountAddress) -> anyhow::Result { - let res = get_view( - client, - "0x1::secret_bid::get_bid_unchecked", - None, - Some(val.to_hex_literal()), - ) - .await?; - - let value: Vec = serde_json::from_value(res)?; - let secs = value.first().unwrap().parse::()?; - - Ok(secs) -} diff --git a/tools/txs/src/stream/bid_commit_reveal.rs b/tools/txs/src/stream/bid_commit_reveal.rs index efde57259..00fdeecef 100644 --- a/tools/txs/src/stream/bid_commit_reveal.rs +++ b/tools/txs/src/stream/bid_commit_reveal.rs @@ -9,6 +9,8 @@ use diem_sdk::types::LocalAccount; use diem_types::transaction::TransactionPayload; use libra_cached_packages::libra_stdlib; use libra_query::chain_queries; +use libra_query::query_view::get_view; +use libra_types::exports::AccountAddress; use libra_types::exports::Client; use serde::{Deserialize, Serialize}; use std::time::Duration; @@ -100,16 +102,17 @@ pub async fn commit_reveal_poll( let client = sender.client().clone(); loop { - let secs = libra_query::chain_queries::secs_remaining_in_epoch(&client).await?; - info!("seconds remaining in epoch {secs}"); - // check what epoch we are in let _ = bid.update_epoch(&client).await; - let must_reveal = libra_query::chain_queries::within_commit_reveal_window(&client).await?; + let reveal_window_open = chain_queries::within_commit_reveal_window(&client).await?; + let last_epoch_revealed = + validator_last_epoch_revealed(&client, sender.local_account.address()).await?; + let must_reveal = last_epoch_revealed != bid.epoch && reveal_window_open; let la = &sender.local_account; if must_reveal { info!("must reveal bid"); + let payload = bid.encode_reveal_tx_payload(la)?; // don't abort if we are trying too eager! match sender.sign_submit_wait(payload).await { @@ -119,8 +122,11 @@ pub async fn commit_reveal_poll( e.to_string() ), } - } else { - info!("sending sealed bid"); + } else if !reveal_window_open { + info!("sending secret bid"); + let secs = chain_queries::secs_remaining_in_epoch(&client).await?; + info!("seconds remaining in epoch {secs}"); + let payload = bid.encode_commit_tx_payload(la)?; match sender.sign_submit_wait(payload).await { Ok(_) => info!("successfully committed bid"), @@ -130,7 +136,8 @@ pub async fn commit_reveal_poll( ), } }; - + // NOTE: it's possible the user sets a delay that is longer than the + // reveal window tokio::time::sleep(Duration::from_secs(delay_secs)).await; } } @@ -180,3 +187,36 @@ fn sign_bid() { assert!(sig.verify_arbitrary_msg(&msg, pubkey).is_ok()); } + +pub async fn validator_committed_bid(client: &Client, val: AccountAddress) -> anyhow::Result { + let res = get_view( + client, + "0x1::secret_bid::get_bid_unchecked", + None, + Some(val.to_hex_literal()), + ) + .await?; + + let value: Vec = serde_json::from_value(res)?; + let secs = value.first().unwrap().parse::()?; + + Ok(secs) +} + +pub async fn validator_last_epoch_revealed( + client: &Client, + val: AccountAddress, +) -> anyhow::Result { + let res = get_view( + client, + "0x1::secret_bid::latest_epoch_revealed", + None, + Some(val.to_hex_literal()), + ) + .await?; + + let value: Vec = serde_json::from_value(res)?; + let secs = value.first().unwrap().parse::()?; + + Ok(secs) +} diff --git a/tools/txs/tests/commit_reveal.rs b/tools/txs/tests/commit_reveal.rs index 895ff4d9c..0cc2d268f 100644 --- a/tools/txs/tests/commit_reveal.rs +++ b/tools/txs/tests/commit_reveal.rs @@ -1,7 +1,7 @@ //! test commit reveal use libra_query::query_view; use libra_smoke_tests::libra_smoke::LibraSmoke; -use libra_txs::stream::bid_commit_reveal::PofBidArgs; +use libra_txs::stream::bid_commit_reveal::{self, PofBidArgs}; use libra_txs::{submit_transaction::Sender, txs_cli_stream::StreamTxs}; use std::time::Duration; @@ -38,7 +38,7 @@ async fn background_commit_reveal() -> anyhow::Result<()> { // so we create it directly and attempt to run it. let commit_reveal_cmd = StreamTxs::ValBid(PofBidArgs { net_reward: test_bid_amount, - delay: Some(5), + delay: Some(2), // Try to submit every 2 seconds in this test }); // create a Sender using the validator's app config let val_app_cfg = ls.first_account_app_cfg()?; @@ -56,18 +56,19 @@ async fn background_commit_reveal() -> anyhow::Result<()> { }); // WAIT FOR END OF EPOCH chain_id==4 is 30 secs - std::thread::sleep(Duration::from_secs(30)); + // reveal window happens 15 seconds before end of epoch + std::thread::sleep(Duration::from_secs(20)); h.abort(); - match libra_query::chain_queries::validator_committed_bid(&client, validator_addr).await { + match bid_commit_reveal::validator_committed_bid(&client, validator_addr).await { Ok(onchain_bid) => { + dbg!(&onchain_bid); assert!(test_bid_amount == onchain_bid, "incorrect bid found"); println!("Validator committed bid successfully."); + return Ok(()); } Err(e) => { - assert!(false, "Failed to commit bid: {:?}", e); + panic!("Failed to reveal bid: {:?}", e); } } - - Ok(()) }