Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Early testnet blocks #75

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea
/target
config.toml
translator-config.toml
*.csv
*.log
*.pid
4 changes: 4 additions & 0 deletions client/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub struct AppConfig {
/// The parent hash of the start_block
pub prev_hash: String,

/// (Optional) For testnet, skip all events until deploy block
pub evm_deploy_block: Option<u32>,

/// Start block to start with, should be at or before the first block of the execution node
pub evm_start_block: u32,

Expand Down Expand Up @@ -75,6 +78,7 @@ impl From<&AppConfig> for TranslatorConfig {
evm_start_block: config.evm_start_block,
evm_stop_block: config.evm_stop_block,
prev_hash: config.prev_hash.clone(),
evm_deploy_block: config.evm_deploy_block,
validate_hash: config.validate_hash.clone(),
http_endpoint: config.chain_endpoint.clone(),
ship_endpoint: config.ship_endpoint.clone(),
Expand Down
1 change: 1 addition & 0 deletions client/tests/ship_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ async fn evm_deploy() {
chain_endpoint: format!("http://localhost:{port_8888}"),
batch_size: 5,
prev_hash: B256::ZERO.to_string(),
skip_raw_tx_until: None,
evm_start_block: 0,
validate_hash: None,
evm_stop_block: Some(60),
Expand Down
151 changes: 97 additions & 54 deletions translator/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use reth_primitives::ReceiptWithBloom;
use reth_telos_rpc_engine_api::structs::TelosEngineAPIExtraFields;
use reth_trie_common::root::ordered_trie_root_with_encoder;
use std::cmp::{max, Ordering};
use std::collections::HashMap;
use tracing::{debug, warn};

const MINIMUM_FEE_PER_GAS: u128 = 7;
Expand Down Expand Up @@ -65,8 +66,8 @@ impl BasicTrace for ActionTrace {

fn console(&self) -> String {
match self {
ActionTrace::V0(a) => a.console.clone(),
ActionTrace::V1(a) => a.console.clone(),
ActionTrace::V0(a) => String::from_utf8(a.console.clone()).unwrap_or_default(),
ActionTrace::V1(a) => String::from_utf8(a.console.clone()).unwrap_or_default(),
}
}

Expand Down Expand Up @@ -104,6 +105,7 @@ pub struct ProcessingEVMBlock {
pub new_wallets: Vec<WalletEvents>,
pub lib_num: u32,
pub lib_hash: Checksum256,
pub skip_events: bool,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -137,22 +139,40 @@ impl TelosEVMBlock {
}
}

pub fn decode_raw_action(encoded: &[u8]) -> RawAction {
decode::<RawAction>(encoded)
}

pub fn decode<T: Packer + Default>(raw: &[u8]) -> T {
let mut result = T::default();
result.unpack(raw);
result
}

pub struct ProcessingEVMBlockArgs {
pub chain_id: u64,
pub block_num: u32,
pub block_hash: Checksum256,
pub prev_block_hash: Option<Checksum256>,
pub lib_num: u32,
pub lib_hash: Checksum256,
pub result: GetBlocksResultV0,
pub skip_events: bool,
}

impl ProcessingEVMBlock {
pub fn new(
chain_id: u64,
block_num: u32,
block_hash: Checksum256,
prev_block_hash: Option<Checksum256>,
lib_num: u32,
lib_hash: Checksum256,
result: GetBlocksResultV0,
) -> Self {
pub fn new(args: ProcessingEVMBlockArgs) -> Self {
let ProcessingEVMBlockArgs {
chain_id,
block_num,
block_hash,
prev_block_hash,
lib_num,
lib_hash,
result,
skip_events,
} = args;

Self {
block_num,
block_hash,
Expand All @@ -161,6 +181,7 @@ impl ProcessingEVMBlock {
lib_hash,
chain_id,
result,
skip_events,
signed_block: None,
block_traces: None,
contract_rows: None,
Expand Down Expand Up @@ -214,13 +235,13 @@ impl ProcessingEVMBlock {
}

fn find_config_row(&self) -> Option<&EvmContractConfigRow> {
return self.decoded_rows.iter().find_map(|row| {
self.decoded_rows.iter().find_map(|row| {
if let DecodedRow::Config(config) = row {
Some(config)
} else {
None
}
});
})
}

fn add_transaction(&mut self, transaction: TelosEVMTransaction) {
Expand Down Expand Up @@ -255,7 +276,7 @@ impl ProcessingEVMBlock {
self.new_gas_price = Some((self.transactions.len() as u64, gas_price));
} else if action_account == EOSIO_EVM && action_name == RAW {
// Normally signed EVM transaction
let raw: RawAction = decode(&action.data());
let raw: RawAction = decode_raw_action(&action.data());
let printed_receipt =
PrintedReceipt::from_console(action.console()).ok_or_else(|| {
eyre::eyre!(
Expand Down Expand Up @@ -289,7 +310,7 @@ impl ProcessingEVMBlock {
self.add_transaction(transaction);
} else if action_account == EOSIO_TOKEN
&& action_name == TRANSFER
&& action_receiver == EOSIO_EVM
&& action_receiver == EOSIO_TOKEN
{
// Deposit/transfer to EVM
let transfer_action: TransferAction = decode(&action.data());
Expand Down Expand Up @@ -355,52 +376,74 @@ impl ProcessingEVMBlock {

let row_deltas = self.contract_rows.clone().unwrap_or_default();

for delta in row_deltas {
match delta.1 {
ContractRow::V0(r) => {
// Global eosio.system table, since block_delta is static
// no need to decode
// if r.table == Name::new_from_str("global") {
// let mut decoder = Decoder::new(r.value.as_slice());
// let decoded_row = &mut GlobalTable::default();
// decoder.unpack(decoded_row);
// info!("Global table: {:?}", decoded_row);
// }
if r.code == Name::new_from_str("eosio.evm") {
// delta.0 is "present" and if false, the row was removed
let removed = !delta.0;
if r.table == Name::new_from_str("config") {
if removed {
panic!(
"Config row removed, this should never happen: {}",
self.block_num
);
let mut deduped_accstate_deltas = HashMap::new();

if !self.skip_events {
for delta in row_deltas {
match delta.1 {
ContractRow::V0(r) => {
// Global eosio.system table, since block_delta is static
// no need to decode
// if r.table == Name::new_from_str("global") {
// let mut decoder = Decoder::new(r.value.as_slice());
// let decoded_row = &mut GlobalTable::default();
// decoder.unpack(decoded_row);
// info!("Global table: {:?}", decoded_row);
// }
if r.code == Name::new_from_str("eosio.evm") {
// delta.0 is "present" and if false, the row was removed
let removed = !delta.0;
if r.table == Name::new_from_str("config") {
if removed {
panic!(
"Config row removed, this should never happen: {}",
self.block_num
);
}
self.decoded_rows.push(DecodedRow::Config(decode(&r.value)));
} else if r.table == Name::new_from_str("account") {
self.decoded_rows
.push(DecodedRow::Account(removed, decode(&r.value)));
} else if r.table == Name::new_from_str("accountstate") {
let decoded_row: AccountStateRow = decode(&r.value);
let complex_key = (r.scope.n, decoded_row.key.data);
match deduped_accstate_deltas.get(&complex_key) {
Some(prev_acc_state) => {
match prev_acc_state {
DecodedRow::AccountState(_vrem, prev_row, _scope) => {
if prev_row.index < decoded_row.index {
deduped_accstate_deltas.insert(complex_key, DecodedRow::AccountState(removed, decoded_row, r.scope));
}
},
_ => {
panic!("Not suposed to happen");
}
}
},
None => {
deduped_accstate_deltas.insert(complex_key, DecodedRow::AccountState(removed, decoded_row, r.scope));
}
}
}
self.decoded_rows.push(DecodedRow::Config(decode(&r.value)));
} else if r.table == Name::new_from_str("account") {
self.decoded_rows
.push(DecodedRow::Account(removed, decode(&r.value)));
} else if r.table == Name::new_from_str("accountstate") {
self.decoded_rows.push(DecodedRow::AccountState(
removed,
decode(&r.value),
r.scope,
));
}
}
}
}
}

let traces = self.block_traces.clone().unwrap_or_default();
for row in deduped_accstate_deltas.values() {
self.decoded_rows.push(row.clone());
}

for TransactionTrace::V0(t) in traces {
for action in t.action_traces {
if let Err(e) = self
.handle_action(Box::new(action), native_to_evm_cache)
.await
{
return Err(eyre!("Error handling the action. {}", e));
let traces = self.block_traces.clone().unwrap_or_default();

for TransactionTrace::V0(t) in traces {
for action in t.action_traces {
if let Err(e) = self
.handle_action(Box::new(action), native_to_evm_cache)
.await
{
return Err(eyre!("Error handling the action. {}", e));
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion translator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tracing::error;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(long, default_value = "config.toml")]
#[arg(long, default_value = "translator-config.toml")]
config: String,
}

Expand Down
Loading
Loading