Skip to content

Commit 7ece945

Browse files
committed
cleanup
1 parent 62dc253 commit 7ece945

File tree

4 files changed

+75
-63
lines changed

4 files changed

+75
-63
lines changed

timeboost-utils/src/load_generation.rs

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::str::FromStr;
2-
31
use alloy::{
42
consensus::{SignableTransaction, TxEip1559, TxEnvelope},
53
network::TxSignerSync,
@@ -18,9 +16,14 @@ use timeboost_crypto::{
1816
};
1917
use timeboost_types::{Address, Bundle, BundleVariant, Epoch, PriorityBundle, SeqNo, Signer};
2018

21-
// Private key from pre funded dev account on test node
22-
// https://docs.arbitrum.io/run-arbitrum-node/run-local-full-chain-simulation#default-endpoints-and-addresses
23-
const DEV_ACCT_PRIV_KEY: &str = "b6b15c8cb491557369f3c7d2c287b053eb229daa9c22138887752191c9520659";
19+
pub struct TxInfo {
20+
pub chain_id: u64,
21+
pub nonce: u64,
22+
pub to: alloy::primitives::Address,
23+
pub base_fee: u128,
24+
pub gas_limit: u64,
25+
pub signer: PrivateKeySigner,
26+
}
2427

2528
pub fn make_bundle(pubkey: &ThresholdEncKeyCell) -> anyhow::Result<BundleVariant> {
2629
let mut rng = rand::thread_rng();
@@ -58,19 +61,15 @@ pub fn make_bundle(pubkey: &ThresholdEncKeyCell) -> anyhow::Result<BundleVariant
5861

5962
pub fn make_dev_acct_bundle(
6063
pubkey: &ThresholdEncKeyCell,
61-
chain_id: u64,
62-
nonce: u64,
63-
to: alloy::primitives::Address,
64-
gas_limit: u64,
65-
max_base_fee: u128,
64+
txn: TxInfo,
6665
) -> anyhow::Result<BundleVariant> {
6766
let mut rng = rand::thread_rng();
6867
let mut v = [0; 256];
6968
rng.fill(&mut v);
7069
let mut u = Unstructured::new(&v);
7170

7271
let max_seqno = 10;
73-
let mut bundle = create_dev_acct_txn_bundle(chain_id, nonce, to, gas_limit, max_base_fee)?;
72+
let mut bundle = create_dev_acct_txn_bundle(txn)?;
7473

7574
if let Some(pubkey) = &*pubkey.get_ref()
7675
&& rng.gen_bool(0.5)
@@ -97,34 +96,25 @@ pub fn make_dev_acct_bundle(
9796
}
9897
}
9998

100-
pub fn create_dev_acct_txn_bundle(
101-
chain_id: u64,
102-
nonce: u64,
103-
to: alloy::primitives::Address,
104-
gas_limit: u64,
105-
max_fee_per_gas: u128,
106-
) -> anyhow::Result<Bundle> {
99+
pub fn create_dev_acct_txn_bundle(tx_info: TxInfo) -> anyhow::Result<Bundle> {
107100
let mut tx = TxEip1559 {
108-
chain_id,
109-
nonce,
110-
max_fee_per_gas,
111-
gas_limit,
112-
to: TxKind::Call(to),
101+
chain_id: tx_info.chain_id,
102+
nonce: tx_info.nonce,
103+
max_fee_per_gas: tx_info.base_fee,
104+
gas_limit: tx_info.gas_limit,
105+
to: TxKind::Call(tx_info.to),
113106
value: U256::from(1),
114107
..Default::default()
115108
};
116109

117-
// Private key from pre funded dev account on test node
118-
// https://docs.arbitrum.io/run-arbitrum-node/run-local-full-chain-simulation#default-endpoints-and-addresses
119-
let signer = PrivateKeySigner::from_str(DEV_ACCT_PRIV_KEY)?;
120-
let sig = signer.sign_transaction_sync(&mut tx)?;
110+
let sig = tx_info.signer.sign_transaction_sync(&mut tx)?;
121111
let signed_tx = tx.into_signed(sig);
122112
let env = TxEnvelope::Eip1559(signed_tx);
123113
let mut rlp = Vec::new();
124114
env.encode(&mut rlp);
125115

126116
let encoded = ssz::ssz_encode(&vec![&rlp]);
127-
let b = Bundle::new(chain_id.into(), Epoch::now(), encoded.into(), false);
117+
let b = Bundle::new(tx_info.chain_id.into(), Epoch::now(), encoded.into(), false);
128118

129119
Ok(b)
130120
}

yapper/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ pub(crate) struct YapperConfig {
1313
pub(crate) nitro_url: String,
1414
/// Limit on the number of transactions we want to send
1515
pub(crate) txn_limit: u64,
16+
/// Chain id for l2 chain
17+
pub(crate) chain_id: u64,
1618
}

yapper/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ struct Cli {
4949
#[clap(long, default_value_t = 20)]
5050
nitro_txn_limit: u64,
5151

52+
/// Chain id for l2 chain
53+
/// default: https://docs.arbitrum.io/run-arbitrum-node/run-local-full-chain-simulation#default-endpoints-and-addresses
54+
#[clap(long, default_value_t = 412346)]
55+
chain_id: u64,
56+
5257
/// How many txns to send before terminating yapper
5358
#[clap(long, default_value = "http://localhost:8547")]
5459
nitro_url: String,
@@ -82,6 +87,7 @@ async fn main() -> Result<()> {
8287
.tps(cli.tps)
8388
.txn_limit(cli.nitro_txn_limit)
8489
.nitro_url(cli.nitro_url)
90+
.chain_id(cli.chain_id)
8591
.build();
8692
let yapper = Yapper::new(config).await?;
8793

yapper/src/yapper.rs

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
use std::time::Duration;
1+
use std::{str::FromStr, time::Duration};
22

33
use alloy::{
44
network::{Ethereum, TransactionBuilder},
55
primitives::{Address, U256, address},
66
providers::{Provider, RootProvider},
77
rpc::types::TransactionRequest,
8+
signers::local::PrivateKeySigner,
89
};
910
use anyhow::{Context, Result};
1011
use futures::future::join_all;
1112
use reqwest::{Client, Url};
1213
use timeboost::types::BundleVariant;
13-
use timeboost_utils::load_generation::{make_bundle, make_dev_acct_bundle, tps_to_millis};
14+
use timeboost_utils::load_generation::{TxInfo, make_bundle, make_dev_acct_bundle, tps_to_millis};
1415
use tokio::time::interval;
1516
use tracing::warn;
1617

@@ -20,6 +21,10 @@ use crate::{config::YapperConfig, enc_key::ThresholdEncKeyCellAccumulator};
2021
/// https://docs.arbitrum.io/run-arbitrum-node/run-local-full-chain-simulation#default-endpoints-and-addresses
2122
const DEV_ACCT_ADDRESS: Address = address!("0x3f1Eae7D46d88F08fc2F8ed27FCb2AB183EB2d0E");
2223

24+
/// Private key from pre funded dev account on test node
25+
/// https://docs.arbitrum.io/run-arbitrum-node/run-local-full-chain-simulation#default-endpoints-and-addresses
26+
const DEV_ACCT_PRIV_KEY: &str = "b6b15c8cb491557369f3c7d2c287b053eb229daa9c22138887752191c9520659";
27+
2328
/// This is the address of validator for the chain
2429
/// https://docs.arbitrum.io/run-arbitrum-node/run-local-full-chain-simulation#default-endpoints-and-addresses
2530
const VALIDATOR_ADDRESS: Address = address!("0x6A568afe0f82d34759347bb36F14A6bB171d2CBe");
@@ -34,15 +39,16 @@ pub(crate) struct Yapper {
3439
urls: Vec<ApiUrls>,
3540
client: Client,
3641
interval: Duration,
42+
chain_id: u64,
3743
provider: Option<RootProvider>,
3844
txn_limit: Option<u64>,
3945
}
4046

4147
impl Yapper {
42-
pub(crate) async fn new(config: YapperConfig) -> Result<Self> {
48+
pub(crate) async fn new(cfg: YapperConfig) -> Result<Self> {
4349
let mut urls = Vec::new();
4450

45-
for addr in config.addresses {
51+
for addr in cfg.addresses {
4652
let regular_url = Url::parse(&format!("http://{addr}/v0/submit-regular"))
4753
.with_context(|| format!("parsing {addr} into a url"))?;
4854
let priority_url = Url::parse(&format!("http://{addr}/v0/submit-priority"))
@@ -57,23 +63,24 @@ impl Yapper {
5763
});
5864
}
5965
let client = Client::builder().timeout(Duration::from_secs(1)).build()?;
60-
let (provider, interval, txn_limit) = if config.nitro_integration {
66+
let (provider, interval, txn_limit) = if cfg.nitro_integration {
6167
(
62-
Some(RootProvider::<Ethereum>::connect(&config.nitro_url).await?),
68+
Some(RootProvider::<Ethereum>::connect(&cfg.nitro_url).await?),
6369
Duration::from_secs(1),
6470
// For nitro running in ci, avoid race conditions with block height by setting txn
6571
// limit
66-
Some(config.txn_limit),
72+
Some(cfg.txn_limit),
6773
)
6874
} else {
69-
(None, Duration::from_millis(tps_to_millis(config.tps)), None)
75+
(None, Duration::from_millis(tps_to_millis(cfg.tps)), None)
7076
};
7177
Ok(Self {
7278
urls,
7379
interval,
7480
client,
7581
provider,
7682
txn_limit,
83+
chain_id: cfg.chain_id,
7784
})
7885
}
7986

@@ -89,40 +96,17 @@ impl Yapper {
8996
let mut txn_sent = 0;
9097
loop {
9198
let b = if let Some(ref p) = self.provider {
92-
let nonce = p.get_transaction_count(DEV_ACCT_ADDRESS).await?;
93-
// Chain id from l2 chain
94-
// https://docs.arbitrum.io/run-arbitrum-node/run-local-full-chain-simulation#default-endpoints-and-addresses
95-
let chain_id = 412346;
96-
let tx = TransactionRequest::default()
97-
.with_chain_id(chain_id)
98-
.with_nonce(nonce)
99-
.with_from(DEV_ACCT_ADDRESS)
100-
.with_to(VALIDATOR_ADDRESS)
101-
.with_value(U256::from(1));
102-
103-
let Ok(estimate) = p.estimate_gas(tx).await else {
104-
warn!("failed to get estimate");
105-
continue;
106-
};
107-
let Ok(price) = p.get_gas_price().await else {
108-
warn!("failed to get gas price");
99+
// For testing just send from the dev account to the validator address
100+
let Ok(txn) = Self::prepare_txn(p, self.chain_id).await else {
101+
warn!("failed to prepare txn");
109102
continue;
110103
};
111-
// For testing just send from the dev account to the validator address
112-
let Ok(b) = make_dev_acct_bundle(
113-
acc.enc_key().await,
114-
chain_id,
115-
nonce,
116-
VALIDATOR_ADDRESS,
117-
estimate,
118-
price,
119-
) else {
104+
let Ok(b) = make_dev_acct_bundle(acc.enc_key().await, txn) else {
120105
warn!("failed to generate dev account bundle");
121106
continue;
122107
};
123108
b
124109
} else {
125-
// create a bundle for next `interval.tick()`, then send this bundle to each node
126110
let Ok(b) = make_bundle(acc.enc_key().await) else {
127111
warn!("failed to generate bundle");
128112
continue;
@@ -145,6 +129,36 @@ impl Yapper {
145129
}
146130
}
147131

132+
async fn prepare_txn(p: &RootProvider, chain_id: u64) -> Result<TxInfo> {
133+
let nonce = p.get_transaction_count(DEV_ACCT_ADDRESS).await?;
134+
let tx = TransactionRequest::default()
135+
.with_chain_id(chain_id)
136+
.with_nonce(nonce)
137+
.with_from(DEV_ACCT_ADDRESS)
138+
// Just choosing an address that already exists on the chain
139+
.with_to(VALIDATOR_ADDRESS)
140+
.with_value(U256::from(1));
141+
142+
let gas_limit = p
143+
.estimate_gas(tx)
144+
.await
145+
.with_context(|| "failed to estimate gas")?;
146+
147+
let base_fee = p
148+
.get_gas_price()
149+
.await
150+
.with_context(|| "failed to get gas price")?;
151+
152+
Ok(TxInfo {
153+
chain_id,
154+
nonce,
155+
to: VALIDATOR_ADDRESS,
156+
gas_limit,
157+
base_fee,
158+
signer: PrivateKeySigner::from_str(DEV_ACCT_PRIV_KEY)?,
159+
})
160+
}
161+
148162
async fn send_bundle_to_node(
149163
&self,
150164
bundle: &BundleVariant,

0 commit comments

Comments
 (0)