Skip to content

Commit

Permalink
better spam script (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion authored Jan 3, 2024
1 parent 860c57a commit 299e14b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 33 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bundler_client_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ tokio = { version = "1.34.0", features = ["macros", "rt"] }
dotenv = "0.15.0"
eyre = "0.6.9"
hex = "0.4.3"
rand = "0.8.5"
92 changes: 85 additions & 7 deletions bundler_client_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fs;
use std::io::Read;
use std::time::Duration;

use bundler_client::{types::DataIntentStatus, Client, GasPreference, NoncePreference};
Expand All @@ -8,7 +9,8 @@ use ethers::middleware::SignerMiddleware;
use ethers::providers::{Http, Middleware, Provider};
use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer};
use ethers::types::TransactionRequest;
use eyre::{eyre, Context, Result};
use eyre::{bail, eyre, Context, Result};
use rand::{thread_rng, Rng};
use tokio::time::sleep;

#[derive(Parser, Debug)]
Expand All @@ -32,7 +34,7 @@ pub struct Args {

/// Path of data to post
#[arg(env, long)]
pub data: String,
pub data: Option<String>,

/// Lower bound balance to trigger a topup: 1e17
#[arg(env, long, default_value_t = 100000000000000000)]
Expand All @@ -46,8 +48,18 @@ pub struct Args {
pub blob_gas_price_factor: f64,

/// Do not wait for intent to be included
#[arg(long)]
#[arg(env, long)]
pub skip_wait: bool,

/// FOR TESTING PURPOSES ONLY
/// Send random data of a specific length. Serialized `RandomData` enum.
#[arg(env, long)]
pub test_data_random: Option<String>,

/// FOR TESTING PURPOSES ONLY
/// Times to send a data request
#[arg(env, long)]
pub test_repeat_send: Option<usize>,
}

#[tokio::main(flavor = "current_thread")]
Expand All @@ -68,18 +80,38 @@ async fn main() -> Result<()> {
let provider: Provider<Http> = Provider::<Http>::try_from(&args.eth_provider)?;
let chain_id = provider.get_chainid().await?.as_u64();

let wallet = if let Some(mnemonic) = args.mnemonic {
let wallet = if let Some(mnemonic) = &args.mnemonic {
MnemonicBuilder::<English>::default()
.phrase(mnemonic.as_str())
.build()?
} else if let Some(priv_key) = args.priv_key {
} else if let Some(priv_key) = &args.priv_key {
LocalWallet::from_bytes(&hex::decode(priv_key).wrap_err_with(|| "invalid priv_key format")?)
.wrap_err_with(|| "priv_key bytes not valid")?
} else {
panic!("Must set either mnemonic or priv_key");
}
.with_chain_id(chain_id);

maybe_fund_sender_account(&args, &client, &provider, &wallet).await?;

if let Some(send_count) = args.test_repeat_send {
for i in 0..send_count {
println!("sending request time {i}/{send_count}");
send_data_request(&args, &client, &wallet).await?;
}
} else {
send_data_request(&args, &client, &wallet).await?;
}

Ok(())
}

async fn maybe_fund_sender_account(
args: &Args,
client: &Client,
provider: &Provider<Http>,
wallet: &LocalWallet,
) -> Result<()> {
// Maybe fund account
let balance = client
.get_balance_by_address(wallet.address())
Expand Down Expand Up @@ -125,14 +157,33 @@ async fn main() -> Result<()> {
);
}

Ok(())
}

async fn send_data_request(args: &Args, client: &Client, wallet: &LocalWallet) -> Result<()> {
// Read data to publish
let data = fs::read(args.data)?;
let data = if let Some(test_data_random) = &args.test_data_random {
let mut rng = thread_rng();
match RandomData::from_str(test_data_random)? {
RandomData::Rand(data_len) => (0..data_len).map(|_| rng.gen()).collect::<Vec<u8>>(),
RandomData::RandSameAlphanumeric(data_len) => {
let random_char = rng.sample(rand::distributions::Alphanumeric) as u8;
vec![random_char; data_len]
}
}
} else if let Some(data_filepath) = &args.data {
fs::read(data_filepath)?
} else {
let mut buffer = Vec::new();
std::io::stdin().read_to_end(&mut buffer)?;
buffer
};

let gas = GasPreference::RelativeToHead(args.blob_gas_price_factor);
let nonce = NoncePreference::Timebased;

let response = client
.post_data_with_wallet(&wallet, data, &gas, &nonce)
.post_data_with_wallet(wallet, data, &gas, &nonce)
.await
.wrap_err_with(|| "post_data_with_wallet")?;
let id = response.id;
Expand All @@ -155,3 +206,30 @@ async fn main() -> Result<()> {

Ok(())
}

#[derive(Clone)]
enum RandomData {
Rand(usize),
RandSameAlphanumeric(usize),
}

impl RandomData {
fn from_str(s: &str) -> Result<Self> {
let parts: Vec<&str> = s.split(',').collect();
let first = parts
.first()
.ok_or_else(|| eyre!("invalid rand args"))?
.to_lowercase();

Ok(match first.as_str() {
"rand" => RandomData::Rand(parts.get(1).ok_or_else(|| eyre!("no rand arg"))?.parse()?),
"rand_same_alphanumeric" => RandomData::RandSameAlphanumeric(
parts
.get(1)
.ok_or_else(|| eyre!("no rand_same_alphanumeric arg"))?
.parse()?,
),
_ => bail!(format!("unknown RandomData variant {first}")),
})
}
}
27 changes: 1 addition & 26 deletions scripts/spam.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,5 @@ set -eo pipefail

source .env

# Define a function to get the nth character in the alphanumeric sequence
get_char() {
local index=$1
if [ "$index" -lt 26 ]; then
# Get one of the 26 letters
printf "\\$(printf '%03o' $((97 + index)))"
else
# Get one of the 10 digits (0-9)
printf "$((index - 26))"
fi
}
cargo run --bin bundler_client_cli -- --url=https://blobshare.up.railway.app --priv-key=$PRIVKEY --eth-provider=$ETH_PROVIDER --data to_send.txt --skip-wait --test-data-random='rand_same_alphanumeric,10000' --test-repeat-send 32

# Loop through the first 36 alphanumeric characters (26 letters + 10 digits)
for i in {0..35}; do
char=$(get_char $i)

# Write 10000 bytes of the current character
printf "%.0s$char" {1..10000} > "to_send.txt"

# Run a command (replace this with your actual command)
echo "Running a command for $char"

cargo run --bin bundler_client_cli -- --url=https://blobshare.up.railway.app --priv-key=$PRIVKEY --eth-provider=$ETH_PROVIDER --data to_send.txt --skip-wait

# Example: Uncomment the next line to list the current directory (replace `ls` with your command)
# ls
done

0 comments on commit 299e14b

Please sign in to comment.