Skip to content

Commit

Permalink
refactor saya using the new by block method instead of pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed May 20, 2024
1 parent 6d70126 commit 2440af6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 62 deletions.
4 changes: 2 additions & 2 deletions crates/saya/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ impl Saya {
.body
.iter()
.filter_map(|t| match &t.transaction {
Tx::L1Handler(tx) => Some(tx),
Tx::L1Handler(tx) => Some((t.hash, tx)),

Check warning on line 185 in crates/saya/core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/saya/core/src/lib.rs#L185

Added line #L185 was not covered by tests
_ => None,
})
.collect::<Vec<_>>();

let (message_to_starknet_segment, message_to_appchain_segment) =
extract_messages(&exec_infos, transactions);
extract_messages(&exec_infos, &transactions);

Check warning on line 191 in crates/saya/core/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/saya/core/src/lib.rs#L191

Added line #L191 was not covered by tests

let new_program_input = ProgramInput {
prev_state_root: prev_block.header.header.state_root,
Expand Down
71 changes: 30 additions & 41 deletions crates/saya/core/src/prover/program_input.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use katana_primitives::contract::ContractAddress;
use katana_primitives::state::StateUpdates;
use katana_primitives::trace::{CallInfo, EntryPointType, TxExecInfo};
use katana_primitives::transaction::L1HandlerTx;
use katana_primitives::utils::transaction::compute_l1_message_hash;
use katana_primitives::trace::{CallInfo, EntryPointType};
use katana_primitives::transaction::{L1HandlerTx, TxHash};
use katana_rpc_types::trace::TxExecutionInfo;
use starknet::core::types::FieldElement;

use super::state_diff::state_updates_to_json_like;
Expand Down Expand Up @@ -42,50 +42,39 @@ fn get_messages_recursively(info: &CallInfo) -> Vec<MessageToStarknet> {
}

pub fn extract_messages(
exec_infos: &[TxExecInfo],
mut transactions: Vec<&L1HandlerTx>,
exec_infos: &[TxExecutionInfo],
transactions: &[(TxHash, &L1HandlerTx)],

Check warning on line 46 in crates/saya/core/src/prover/program_input.rs

View check run for this annotation

Codecov / codecov/patch

crates/saya/core/src/prover/program_input.rs#L45-L46

Added lines #L45 - L46 were not covered by tests
) -> (Vec<MessageToStarknet>, Vec<MessageToAppchain>) {
// extract messages to starknet (ie l2 -> l1)

Check warning on line 48 in crates/saya/core/src/prover/program_input.rs

View check run for this annotation

Codecov / codecov/patch

crates/saya/core/src/prover/program_input.rs#L48

Added line #L48 was not covered by tests
let message_to_starknet_segment = exec_infos
.iter()
.flat_map(|t| t.execute_call_info.iter().chain(t.validate_call_info.iter()).chain(t.fee_transfer_call_info.iter())) // Take into account both validate and execute calls.
.flat_map(|t| t.trace.execute_call_info.iter().chain(t.trace.validate_call_info.iter()).chain(t.trace.fee_transfer_call_info.iter())) // Take into account both validate and execute calls.

Check warning on line 51 in crates/saya/core/src/prover/program_input.rs

View check run for this annotation

Codecov / codecov/patch

crates/saya/core/src/prover/program_input.rs#L51

Added line #L51 was not covered by tests
.flat_map(get_messages_recursively)
.collect();

let message_to_appchain_segment = exec_infos
.iter()
.flat_map(|t| t.execute_call_info.iter())
.filter(|c| c.entry_point_type == EntryPointType::L1Handler)
.map(|c| {
let message_hash =
compute_l1_message_hash(*c.caller_address, *c.contract_address, &c.calldata[..]);

// Matching execution to a transaction to extract nonce.
let matching = transactions
.iter()
.enumerate()
.find(|(_, &t)| {
t.message_hash == message_hash
&& c.contract_address == t.contract_address
&& c.calldata == t.calldata
})
.unwrap_or_else(|| {
panic!("No matching transaction found for message hash: {}", message_hash)
})
.0;

// Removing, to have different nonces, even for the same message content.
let removed = transactions.remove(matching);

(c, removed)
})
.map(|(c, t)| MessageToAppchain {
from_address: c.caller_address,
to_address: c.contract_address,
nonce: t.nonce,
selector: c.entry_point_selector,
payload: c.calldata.clone(),
})
.collect();
// extract messages to appchain (ie l1 -> l2)
let message_to_appchain_segment = {
// get the call infos only
let calls = exec_infos.iter().filter_map(|t| {
let calls = t.trace.execute_call_info.as_ref()?;
let tx = transactions.iter().find(|tx| tx.0 == t.hash).expect("qed; tx must exist");
Some((tx.1, calls))
});

// filter only the l1 handler tx
let l1_handlers = calls.filter(|(_, c)| c.entry_point_type == EntryPointType::L1Handler);

// build messages
l1_handlers
.map(|(t, c)| MessageToAppchain {
nonce: t.nonce,
payload: c.calldata.clone(),
from_address: c.caller_address,
to_address: c.contract_address,
selector: c.entry_point_selector,
})
.collect()
};

Check warning on line 77 in crates/saya/core/src/prover/program_input.rs

View check run for this annotation

Codecov / codecov/patch

crates/saya/core/src/prover/program_input.rs#L55-L77

Added lines #L55 - L77 were not covered by tests

(message_to_starknet_segment, message_to_appchain_segment)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/saya/provider/src/provider.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use katana_primitives::block::{BlockNumber, SealedBlock};
use katana_primitives::state::StateUpdatesWithDeclaredClasses;
use katana_primitives::trace::TxExecInfo;
use katana_rpc_types::trace::TxExecutionInfo;
use starknet::core::types::FieldElement;

use crate::ProviderResult;
Expand Down Expand Up @@ -40,5 +40,5 @@ pub trait Provider {
async fn fetch_transactions_executions(
&self,
block_number: BlockNumber,
) -> ProviderResult<Vec<TxExecInfo>>;
) -> ProviderResult<Vec<TxExecutionInfo>>;
}
23 changes: 6 additions & 17 deletions crates/saya/provider/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ use katana_primitives::block::{
use katana_primitives::chain::ChainId;
use katana_primitives::conversion::rpc as rpc_converter;
use katana_primitives::state::StateUpdatesWithDeclaredClasses;
use katana_primitives::trace::TxExecInfo;
use katana_primitives::transaction::TxWithHash;
use katana_primitives::version::Version;
use katana_rpc_api::saya::SayaApiClient;
use katana_rpc_types::transaction::{TransactionsExecutionsPage, TransactionsPageCursor};
use katana_rpc_types::trace::TxExecutionInfo;
use starknet::core::types::{
ContractClass, FieldElement, MaybePendingBlockWithTxs, MaybePendingStateUpdate,
};
Expand Down Expand Up @@ -155,25 +154,15 @@ impl Provider for JsonRpcProvider {

async fn fetch_transactions_executions(
&self,
block_number: u64,
) -> ProviderResult<Vec<TxExecInfo>> {
block_number: BlockNumber,
) -> ProviderResult<Vec<TxExecutionInfo>> {

Check warning on line 158 in crates/saya/provider/src/rpc/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/saya/provider/src/rpc/mod.rs#L157-L158

Added lines #L157 - L158 were not covered by tests
trace!(target: LOG_TARGET, block_number = %block_number, "Fetching transactions executions.");
let cursor = TransactionsPageCursor { block_number, chunk_size: 50, ..Default::default() };

let client = HttpClientBuilder::default().build(&self.rpc_url).unwrap();
let mut executions = vec![];

loop {
let rsp: TransactionsExecutionsPage =
client.get_transactions_executions(cursor).await.unwrap();

executions.extend(rsp.transactions_executions);

if rsp.cursor.block_number > block_number {
break;
}
}
let block_id = BlockIdOrTag::Number(block_number);
let traces = client.transaction_executions_by_block(block_id).await.unwrap();

Check warning on line 164 in crates/saya/provider/src/rpc/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/saya/provider/src/rpc/mod.rs#L163-L164

Added lines #L163 - L164 were not covered by tests

Ok(executions)
Ok(traces)

Check warning on line 166 in crates/saya/provider/src/rpc/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/saya/provider/src/rpc/mod.rs#L166

Added line #L166 was not covered by tests
}
}

0 comments on commit 2440af6

Please sign in to comment.