loom_types_blockchain/
state_update.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use std::collections::BTreeMap;

use alloy_primitives::{Address, TxHash};
use alloy_provider::ext::DebugApi;
use alloy_provider::{Network, Provider};
use alloy_rpc_types::{BlockId, TransactionRequest};
use alloy_rpc_types_trace::common::TraceResult;
use alloy_rpc_types_trace::geth::GethDebugBuiltInTracerType::PreStateTracer;
use alloy_rpc_types_trace::geth::GethDebugTracerType::BuiltInTracer;
use alloy_rpc_types_trace::geth::{
    AccountState, GethDebugBuiltInTracerType, GethDebugTracerConfig, GethDebugTracerType, GethDebugTracingCallOptions,
    GethDebugTracingOptions, GethDefaultTracingOptions, GethTrace, PreStateConfig, PreStateFrame,
};
use alloy_transport::Transport;
use eyre::Result;
use lazy_static::lazy_static;
use tracing::{debug, trace};

use loom_node_debug_provider::DebugProviderExt;

pub type GethStateUpdate = BTreeMap<Address, AccountState>;

pub type GethStateUpdateVec = Vec<BTreeMap<Address, AccountState>>;

lazy_static! {
    pub static ref TRACING_OPTS: GethDebugTracingOptions = GethDebugTracingOptions {
        tracer: Some(GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::PreStateTracer,)),
        tracer_config: GethDebugTracerConfig::default(),
        config: GethDefaultTracingOptions::default().disable_storage().disable_stack().disable_memory().disable_return_data(),
        timeout: None,
    };
    pub static ref TRACING_CALL_OPTS: GethDebugTracingCallOptions =
        GethDebugTracingCallOptions { tracing_options: TRACING_OPTS.clone(), state_overrides: None, block_overrides: None };
}

pub fn debug_log_geth_state_update(state_update: &GethStateUpdate) {
    for (address, state) in state_update {
        debug!("{} nonce {:?} balance {:?} is_code {}", address, state.nonce, state.balance, state.code.is_some())
    }
}

pub async fn debug_trace_block<T: Transport + Clone, N: Network, P: Provider<T, N> + DebugProviderExt<T, N>>(
    client: P,
    block_id: BlockId,
    diff_mode: bool,
) -> eyre::Result<(GethStateUpdateVec, GethStateUpdateVec)> {
    let tracer_opts = GethDebugTracingOptions { config: GethDefaultTracingOptions::default(), ..GethDebugTracingOptions::default() }
        .with_tracer(BuiltInTracer(PreStateTracer))
        .with_prestate_config(PreStateConfig { diff_mode: Some(diff_mode), disable_code: Some(false), disable_storage: Some(false) });

    let trace_result_vec = match block_id {
        BlockId::Number(block_number) => client.geth_debug_trace_block_by_number(block_number, tracer_opts).await?,
        BlockId::Hash(rpc_block_hash) => {
            //client.debug_trace_block_by_number(BlockNumber::from(19776525u32), tracer_opts).await?
            client.geth_debug_trace_block_by_hash(rpc_block_hash.block_hash, tracer_opts).await?
        }
    };

    trace!("block trace {}", trace_result_vec.len());

    let mut pre: Vec<BTreeMap<Address, AccountState>> = Vec::new();
    let mut post: Vec<BTreeMap<Address, AccountState>> = Vec::new();

    for trace_result in trace_result_vec.into_iter() {
        if let TraceResult::Success { result, .. } = trace_result {
            match result {
                GethTrace::PreStateTracer(geth_trace_frame) => match geth_trace_frame {
                    PreStateFrame::Diff(diff_frame) => {
                        pre.push(diff_frame.pre);
                        post.push(diff_frame.post);
                    }
                    PreStateFrame::Default(diff_frame) => {
                        pre.push(diff_frame.0.into_iter().collect());
                    }
                },
                _ => {
                    return Err(eyre::eyre!("TRACE_RESULT_FAILED"));
                }
            }
        }
    }
    Ok((pre, post))
}

async fn debug_trace_call<T: Transport + Clone, N: Network, C: DebugProviderExt<T, N>, TR: Into<TransactionRequest> + Send + Sync>(
    client: C,
    req: TR,
    block: BlockId,
    opts: Option<GethDebugTracingCallOptions>,
    diff_mode: bool,
) -> Result<(GethStateUpdate, GethStateUpdate)> {
    let tracer_opts = GethDebugTracingOptions { config: GethDefaultTracingOptions::default(), ..GethDebugTracingOptions::default() }
        .with_tracer(BuiltInTracer(PreStateTracer))
        .with_prestate_config(PreStateConfig { diff_mode: Some(diff_mode), disable_code: Some(false), disable_storage: Some(false) });

    let tracer_call_opts = GethDebugTracingCallOptions {
        tracing_options: tracer_opts.clone(),
        state_overrides: opts.clone().and_then(|x| x.state_overrides),
        block_overrides: opts.and_then(|x| x.block_overrides),
    };

    let trace_result = client.geth_debug_trace_call(req.into(), block, tracer_call_opts.clone()).await?;
    trace!(
        "{} {} {:?} {:?}",
        tracer_opts.config.is_stack_enabled(),
        tracer_opts.config.is_storage_enabled(),
        tracer_call_opts.clone(),
        trace_result
    );

    match trace_result {
        GethTrace::PreStateTracer(geth_trace_frame) => match geth_trace_frame {
            PreStateFrame::Diff(diff_frame) => Ok((diff_frame.pre, diff_frame.post)),
            PreStateFrame::Default(diff_frame) => Ok((diff_frame.0, BTreeMap::new())),
        },
        _ => Err(eyre::eyre!("TRACE_RESULT_FAILED")),
    }
}

pub async fn debug_trace_call_pre_state<
    T: Transport + Clone,
    N: Network,
    C: DebugProviderExt<T, N>,
    TR: Into<TransactionRequest> + Send + Sync,
>(
    client: C,
    req: TR,
    block: BlockId,
    opts: Option<GethDebugTracingCallOptions>,
) -> eyre::Result<GethStateUpdate> {
    Ok(debug_trace_call(client, req, block, opts, false).await?.0)
}

pub async fn debug_trace_call_post_state<
    T: Transport + Clone,
    N: Network,
    C: DebugProviderExt<T, N>,
    TR: Into<TransactionRequest> + Send + Sync,
>(
    client: C,
    req: TR,
    block: BlockId,
    opts: Option<GethDebugTracingCallOptions>,
) -> eyre::Result<GethStateUpdate> {
    Ok(debug_trace_call(client, req, block, opts, true).await?.1)
}

pub async fn debug_trace_call_diff<
    T: Transport + Clone,
    N: Network,
    C: DebugProviderExt<T, N>,
    TR: Into<TransactionRequest> + Send + Sync,
>(
    client: C,
    req: TR,
    block: BlockId,
    call_opts: Option<GethDebugTracingCallOptions>,
) -> eyre::Result<(GethStateUpdate, GethStateUpdate)> {
    debug_trace_call(client, req, block, call_opts, true).await
}

pub async fn debug_trace_transaction<T: Transport + Clone, N: Network, P: Provider<T, N> + DebugApi<N, T>>(
    client: P,
    req: TxHash,
    diff_mode: bool,
) -> Result<(GethStateUpdate, GethStateUpdate)> {
    let tracer_opts = GethDebugTracingOptions { config: GethDefaultTracingOptions::default(), ..GethDebugTracingOptions::default() }
        .with_tracer(BuiltInTracer(PreStateTracer))
        .with_prestate_config(PreStateConfig { diff_mode: Some(diff_mode), disable_code: Some(false), disable_storage: Some(false) });

    let trace_result = client.debug_trace_transaction(req, tracer_opts).await?;
    trace!("{:?}", trace_result);

    match trace_result {
        GethTrace::PreStateTracer(geth_trace_frame) => match geth_trace_frame {
            PreStateFrame::Diff(diff_frame) => Ok((diff_frame.pre.into_iter().collect(), diff_frame.post.into_iter().collect())),
            PreStateFrame::Default(diff_frame) => Ok((diff_frame.0.into_iter().collect(), BTreeMap::new())),
        },
        _ => Err(eyre::eyre!("TRACE_RESULT_FAILED")),
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use alloy_primitives::map::B256HashMap;
    use alloy_primitives::{B256, U256};
    use alloy_provider::network::primitives::BlockTransactionsKind;
    use alloy_provider::ProviderBuilder;
    use alloy_rpc_client::{ClientBuilder, WsConnect};
    use alloy_rpc_types::state::{AccountOverride, StateOverride};
    use env_logger::Env as EnvLog;
    use tracing::{debug, error};

    #[tokio::test]
    async fn test_debug_block() -> Result<()> {
        let node_url = std::env::var("MAINNET_WS")?;

        let _ = env_logger::try_init_from_env(EnvLog::default().default_filter_or("info,tokio_tungstenite=off,tungstenite=off"));
        let node_url = url::Url::parse(node_url.as_str())?;

        let ws_connect = WsConnect::new(node_url);
        let client = ClientBuilder::default().ws(ws_connect).await?;

        let client = ProviderBuilder::new().on_client(client).boxed();

        let blocknumber = client.get_block_number().await?;
        let _block = client.get_block_by_number(blocknumber.into(), BlockTransactionsKind::Hashes).await?.unwrap();

        let _ret = debug_trace_block(client, BlockId::Number(blocknumber.into()), true).await?;

        Ok(())
    }

    #[test]
    fn test_encode_override() {
        let mut state_override: StateOverride = StateOverride::default();
        let address = Address::default();
        let mut account_override: AccountOverride = AccountOverride::default();
        let mut state_update_hashmap: B256HashMap<B256> = B256HashMap::default();
        state_update_hashmap.insert(B256::from(U256::from(1)), B256::from(U256::from(3)));
        account_override.state_diff = Some(state_update_hashmap);

        state_override.insert(address, account_override);

        match serde_json::to_string_pretty(&state_override) {
            Ok(data) => {
                debug!("{}", data);
            }
            Err(e) => {
                error!("{}", e);
                panic!("DESERIALIZATION_ERROR");
            }
        }
    }
}