Skip to content

Commit

Permalink
Merge pull request #5 from rainshowerLabs/add-delay-reformat-serializ…
Browse files Browse the repository at this point in the history
…ed-tracker-output

Add delay to replaying blocks, reformat serialized tracker output
  • Loading branch information
makemake-kbo authored Jun 26, 2023
2 parents 4f897c6 + 21493ba commit f69096a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sothis"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
authors = ["makemake <[email protected]>"]
license = "GPL-3.0-or-later"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Options:
Time in ms to check for new blocks. [default: 500]
--entropy_threshold <entropy_threshold>...
Set the percentage of failed transactions to trigger a warning [default: 0.07]
-d, --replay_delay <replay_delay>...
Default delay for block replay in ms [default: 0]
--send_as_raw [<send_as_raw>...]
Exit the program if a transaction fails
-c, --contract_address <contract_address>...
Expand Down Expand Up @@ -86,6 +88,7 @@ The tracking mode is used to track the change in value of a storage slot for a c
The result is saved to a JSON file that looks like this:
```json
{
"address":"0x1c479675ad559DC151F6Ec7ed3FbF8ceE79582B6",
"storage_slot":"0x0",
"state_changes":[
{"block_number":"0x10b7bbc","value":"0x00000000000000000000000000000000000000000000000000000000000e2b18"}
Expand Down
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct AppConfig {
exit_on_tx_fail: bool,
send_as_raw: bool,
entropy_threshold: f32,
replay_delay: u64,
block_listen_time: u64,
path: String,
filename: String,
Expand All @@ -32,7 +33,7 @@ lazy_static! {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let matches = Command::new("sothis")
.version("0.3.0")
.version("0.3.1")
.author("makemake <[email protected]>")
.about("Tool for replaying historical transactions. Designed to be used with anvil or hardhat.")
.arg(Arg::new("source_rpc")
Expand Down Expand Up @@ -73,6 +74,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.num_args(1..)
.default_value("0.07")
.help("Set the percentage of failed transactions to trigger a warning"))
.arg(Arg::new("replay_delay")
.long("replay_delay")
.short('d')
.num_args(1..)
.default_value("0")
.help("Default delay for block replay in ms"))
.arg(Arg::new("send_as_raw")
.long("send_as_raw")
.num_args(0..)
Expand Down Expand Up @@ -112,6 +119,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
app_config.exit_on_tx_fail = matches.get_occurrences::<String>("exit_on_tx_fail").is_some();
app_config.send_as_raw = matches.get_occurrences::<String>("send_as_raw").is_some();
app_config.entropy_threshold = matches.get_one::<String>("entropy_threshold").expect("required").parse::<f32>()?;
app_config.replay_delay = matches.get_one::<String>("replay_delay").expect("required").parse::<u64>()?;
app_config.block_listen_time = matches.get_one::<String>("block_listen_time").expect("required").parse::<u64>()?;
app_config.path = matches.get_one::<String>("path").expect("required").to_string();
app_config.filename = matches.get_one::<String>("filename").expect("required").to_string();
Expand Down
14 changes: 14 additions & 0 deletions src/replay/replay.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::thread::sleep;
use tokio::time::Duration;

use crate::APP_CONFIG;
use crate::replay::send_transaction::send_transactions;
use crate::RpcConnection;
Expand Down Expand Up @@ -37,6 +40,13 @@ pub async fn replay_historic_blocks(
// set insanely high interval for the blocks
replay_rpc.evm_set_interval_mining(std::u32::MAX.into()).await?;

// Get the time we're delaying the replay for
let replay_delay;
{
let app_config = APP_CONFIG.lock()?;
replay_delay = app_config.replay_delay;
}

while until > replay_block {
// we write a bit of illegible code
let hex_block = decimal_to_hex(replay_block + 1);
Expand All @@ -60,6 +70,10 @@ pub async fn replay_historic_blocks(
println!("Successfully replayed block {}", hex_to_decimal(&hex_block)?);

replay_block = hex_to_decimal(&replay_rpc.block_number().await?)?;

// TODO: For some godforsaken reason i cannot do an infinite loop and break here or else it crashes.
// I feel dirty doing 2 checks for the same thing so you have to wait a bit ig.
sleep(Duration::from_millis(replay_delay));
}
println!("Done replaying blocks");
Ok(())
Expand Down
10 changes: 6 additions & 4 deletions src/tracker/tracker.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::hex_to_decimal;
use crate::APP_CONFIG;
use crate::RpcConnection;
use crate::tracker::types::*;
Expand All @@ -25,6 +24,7 @@ pub async fn track_state(
})?;

let mut storage = StateChangeList {
address: contract_address.clone(),
storage_slot: storage_slot,
state_changes: Vec::new(),
};
Expand All @@ -40,24 +40,26 @@ pub async fn track_state(
filename = app_config.filename.clone();
}

let mut block_number = source_rpc.block_number().await?;
loop {
if interrupted.load(Ordering::SeqCst) {
break;
}

let block_number = source_rpc.listen_for_blocks(block_time).await?;
let block_number = hex_to_decimal(&block_number)?; // FIXME: this returns a u64, change this
let block_number_u256: U256 = block_number.parse()?;
let latest_slot = source_rpc.get_storage_at(contract_address.clone(), storage_slot.clone()).await?;

let slot = StateChange {
block_number: block_number.into(),
block_number: block_number_u256,
value: latest_slot,
};

if storage.state_changes.last().map(|change| change.value != slot.value).unwrap_or(true) {
println!("New storage slot value: {:?}", &slot.value);
storage.state_changes.push(slot);
}

block_number = source_rpc.listen_for_blocks(block_time).await?;
}
let json = serde_json::to_string(&storage)?;

Expand Down
4 changes: 2 additions & 2 deletions src/tracker/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use serde::{Deserialize, Serialize};
use ethers::types::U256;

//
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct StateChange {
pub block_number: U256,
Expand All @@ -19,6 +18,7 @@ impl Default for StateChange {

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct StateChangeList {
pub storage_slot: U256,
pub address: String,
pub storage_slot: U256,
pub state_changes: Vec<StateChange>,
}

0 comments on commit f69096a

Please sign in to comment.