Skip to content

Commit 9ccb60b

Browse files
itegulovperekopskiyzksync-era-bot
authored
feat: migrate to zksync-os-interface (#274)
## What ❔ Use types and implement traits from `zksync-os-interface`. ## Why ❔ Needed for multivm. ## Is this a breaking change? - [ ] Yes - [ ] No ## Checklist <!-- Check your PR fulfills the following items. --> <!-- For draft PRs check the boxes as you complete them. --> - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted. --------- Co-authored-by: perekopskiy <mikeson.dp@gmail.com> Co-authored-by: zksync-era-bot <zksync-era-bot@users.noreply.github.com> Co-authored-by: perekopskiy <53865202+perekopskiy@users.noreply.github.com>
1 parent ca1ce6e commit 9ccb60b

File tree

23 files changed

+739
-7745
lines changed

23 files changed

+739
-7745
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ keywords = ["blockchain", "zksync", "zk", "risc-v"]
6060
categories = ["cryptography"]
6161

6262
[workspace.dependencies]
63+
zksync_os_interface = { git = "https://github.com/matter-labs/zksync-os-interface", tag = "v0.0.2" }
64+
6365
risc_v_simulator = { git = "https://github.com/matter-labs/zksync-airbender", tag = "v0.4.3"}
6466
blake2s_u32 = { git = "https://github.com/matter-labs/zksync-airbender", tag = "v0.4.3"}
6567
prover_examples = { git = "https://github.com/matter-labs/zksync-airbender", tag = "v0.4.3"}

forward_system/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ zk_ee = { path = "../zk_ee", default-features = false }
2020
evm_interpreter = { path = "../evm_interpreter", default-features = false }
2121
basic_system = { path = "../basic_system", default-features = false }
2222
basic_bootloader = { path = "../basic_bootloader", default-features = false }
23+
zksync_os_interface = { workspace = true, default-features = false }
24+
2325
ruint = { version = "1.12.3", default-features = false, features = ["alloc"] }
26+
alloy = { version = "1", default-features = false }
2427
arrayvec = { version = "0.7.4", default-features = false }
2528
hex = { version = "*", optional = true }
2629
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }

forward_system/src/run/convert.rs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
use alloy::consensus::{Header, Sealed};
2+
use alloy::primitives::Log;
3+
use basic_bootloader::bootloader::block_header::BlockHeader;
4+
use ruint::aliases::B160;
5+
use zk_ee::common_structs::GenericEventContent;
6+
use zk_ee::system::metadata::{BlockHashes, BlockMetadataFromOracle};
7+
use zk_ee::types_config::EthereumIOTypesConfig;
8+
use zksync_os_interface::error::{AAMethod, InvalidTransaction};
9+
use zksync_os_interface::types::{BlockContext, L2ToL1Log};
10+
11+
pub trait FromInterface<T> {
12+
fn from_interface(value: T) -> Self;
13+
}
14+
15+
pub trait IntoInterface<T> {
16+
fn into_interface(self) -> T;
17+
}
18+
19+
impl FromInterface<BlockContext> for BlockMetadataFromOracle {
20+
fn from_interface(value: BlockContext) -> Self {
21+
BlockMetadataFromOracle {
22+
chain_id: value.chain_id,
23+
block_number: value.block_number,
24+
block_hashes: BlockHashes(value.block_hashes.0),
25+
timestamp: value.timestamp,
26+
eip1559_basefee: value.eip1559_basefee,
27+
gas_per_pubdata: value.gas_per_pubdata,
28+
native_price: value.native_price,
29+
coinbase: B160::from_be_bytes(value.coinbase.0 .0),
30+
gas_limit: value.gas_limit,
31+
pubdata_limit: value.pubdata_limit,
32+
mix_hash: value.mix_hash,
33+
}
34+
}
35+
}
36+
37+
impl IntoInterface<InvalidTransaction>
38+
for basic_bootloader::bootloader::errors::InvalidTransaction
39+
{
40+
fn into_interface(self) -> InvalidTransaction {
41+
match self {
42+
basic_bootloader::bootloader::errors::InvalidTransaction::InvalidEncoding => { InvalidTransaction::InvalidEncoding }
43+
basic_bootloader::bootloader::errors::InvalidTransaction::InvalidStructure => { InvalidTransaction::InvalidStructure }
44+
basic_bootloader::bootloader::errors::InvalidTransaction::PriorityFeeGreaterThanMaxFee => { InvalidTransaction::PriorityFeeGreaterThanMaxFee }
45+
basic_bootloader::bootloader::errors::InvalidTransaction::BaseFeeGreaterThanMaxFee => { InvalidTransaction::BaseFeeGreaterThanMaxFee }
46+
basic_bootloader::bootloader::errors::InvalidTransaction::GasPriceLessThanBasefee => { InvalidTransaction::GasPriceLessThanBasefee }
47+
basic_bootloader::bootloader::errors::InvalidTransaction::CallerGasLimitMoreThanBlock => { InvalidTransaction::CallerGasLimitMoreThanBlock }
48+
basic_bootloader::bootloader::errors::InvalidTransaction::CallGasCostMoreThanGasLimit => { InvalidTransaction::CallGasCostMoreThanGasLimit }
49+
basic_bootloader::bootloader::errors::InvalidTransaction::RejectCallerWithCode => { InvalidTransaction::RejectCallerWithCode }
50+
basic_bootloader::bootloader::errors::InvalidTransaction::LackOfFundForMaxFee { fee, balance } => { InvalidTransaction::LackOfFundForMaxFee { fee, balance } }
51+
basic_bootloader::bootloader::errors::InvalidTransaction::OverflowPaymentInTransaction => { InvalidTransaction::OverflowPaymentInTransaction }
52+
basic_bootloader::bootloader::errors::InvalidTransaction::NonceOverflowInTransaction => { InvalidTransaction::NonceOverflowInTransaction }
53+
basic_bootloader::bootloader::errors::InvalidTransaction::NonceTooHigh { tx, state } => { InvalidTransaction::NonceTooHigh { tx, state } }
54+
basic_bootloader::bootloader::errors::InvalidTransaction::NonceTooLow { tx, state } => { InvalidTransaction::NonceTooLow { tx, state } }
55+
basic_bootloader::bootloader::errors::InvalidTransaction::MalleableSignature => { InvalidTransaction::MalleableSignature }
56+
basic_bootloader::bootloader::errors::InvalidTransaction::IncorrectFrom { tx, recovered } => { InvalidTransaction::IncorrectFrom { tx: tx.to_be_bytes().into(), recovered: recovered.to_be_bytes().into() } }
57+
basic_bootloader::bootloader::errors::InvalidTransaction::CreateInitCodeSizeLimit => { InvalidTransaction::CreateInitCodeSizeLimit }
58+
basic_bootloader::bootloader::errors::InvalidTransaction::InvalidChainId => { InvalidTransaction::InvalidChainId }
59+
basic_bootloader::bootloader::errors::InvalidTransaction::AccessListNotSupported => { InvalidTransaction::AccessListNotSupported }
60+
basic_bootloader::bootloader::errors::InvalidTransaction::GasPerPubdataTooHigh => { InvalidTransaction::GasPerPubdataTooHigh }
61+
basic_bootloader::bootloader::errors::InvalidTransaction::BlockGasLimitTooHigh => { InvalidTransaction::BlockGasLimitTooHigh }
62+
basic_bootloader::bootloader::errors::InvalidTransaction::UpgradeTxNotFirst => { InvalidTransaction::UpgradeTxNotFirst }
63+
basic_bootloader::bootloader::errors::InvalidTransaction::Revert { method, output } => { InvalidTransaction::Revert { method: match method {
64+
basic_bootloader::bootloader::errors::AAMethod::AccountValidate => { AAMethod::AccountValidate}
65+
basic_bootloader::bootloader::errors::AAMethod::AccountPayForTransaction => { AAMethod::AccountPayForTransaction}
66+
basic_bootloader::bootloader::errors::AAMethod::AccountPrePaymaster => {AAMethod::AccountPrePaymaster}
67+
basic_bootloader::bootloader::errors::AAMethod::PaymasterValidateAndPay => {AAMethod::PaymasterValidateAndPay}
68+
}, output } }
69+
basic_bootloader::bootloader::errors::InvalidTransaction::ReceivedInsufficientFees { received, required } => { InvalidTransaction::ReceivedInsufficientFees { received, required } }
70+
basic_bootloader::bootloader::errors::InvalidTransaction::InvalidMagic => { InvalidTransaction::InvalidMagic }
71+
basic_bootloader::bootloader::errors::InvalidTransaction::InvalidReturndataLength => { InvalidTransaction::InvalidReturndataLength }
72+
basic_bootloader::bootloader::errors::InvalidTransaction::OutOfGasDuringValidation => { InvalidTransaction::OutOfGasDuringValidation }
73+
basic_bootloader::bootloader::errors::InvalidTransaction::OutOfNativeResourcesDuringValidation => { InvalidTransaction::OutOfNativeResourcesDuringValidation }
74+
basic_bootloader::bootloader::errors::InvalidTransaction::NonceUsedAlready => { InvalidTransaction::NonceUsedAlready }
75+
basic_bootloader::bootloader::errors::InvalidTransaction::NonceNotIncreased => { InvalidTransaction::NonceNotIncreased }
76+
basic_bootloader::bootloader::errors::InvalidTransaction::PaymasterReturnDataTooShort => { InvalidTransaction::PaymasterReturnDataTooShort }
77+
basic_bootloader::bootloader::errors::InvalidTransaction::PaymasterInvalidMagic => { InvalidTransaction::PaymasterInvalidMagic }
78+
basic_bootloader::bootloader::errors::InvalidTransaction::PaymasterContextInvalid => { InvalidTransaction::PaymasterContextInvalid }
79+
basic_bootloader::bootloader::errors::InvalidTransaction::PaymasterContextOffsetTooLong => { InvalidTransaction::PaymasterContextOffsetTooLong }
80+
basic_bootloader::bootloader::errors::InvalidTransaction::BlockGasLimitReached => { InvalidTransaction::BlockGasLimitReached }
81+
basic_bootloader::bootloader::errors::InvalidTransaction::BlockNativeLimitReached => { InvalidTransaction::BlockNativeLimitReached }
82+
basic_bootloader::bootloader::errors::InvalidTransaction::BlockPubdataLimitReached => { InvalidTransaction::BlockPubdataLimitReached }
83+
basic_bootloader::bootloader::errors::InvalidTransaction::BlockL2ToL1LogsLimitReached => { InvalidTransaction::BlockL2ToL1LogsLimitReached }
84+
}
85+
}
86+
}
87+
88+
impl IntoInterface<Log> for &GenericEventContent<4, EthereumIOTypesConfig> {
89+
fn into_interface(self) -> Log {
90+
Log::new(
91+
self.address.to_be_bytes().into(),
92+
self.topics.iter().map(|t| t.as_u8_array().into()).collect(),
93+
self.data.as_slice().to_vec().into(),
94+
)
95+
.unwrap()
96+
}
97+
}
98+
99+
impl IntoInterface<L2ToL1Log> for zk_ee::common_structs::L2ToL1Log {
100+
fn into_interface(self) -> L2ToL1Log {
101+
L2ToL1Log {
102+
l2_shard_id: self.l2_shard_id,
103+
is_service: self.is_service,
104+
tx_number_in_block: self.tx_number_in_block,
105+
sender: self.sender.to_be_bytes().into(),
106+
key: self.key.as_u8_array().into(),
107+
value: self.value.as_u8_array().into(),
108+
}
109+
}
110+
}
111+
112+
impl IntoInterface<Sealed<Header>> for BlockHeader {
113+
fn into_interface(self) -> Sealed<Header> {
114+
let hash = self.hash();
115+
let header = Header {
116+
parent_hash: self.parent_hash.as_u8_array().into(),
117+
ommers_hash: self.ommers_hash.as_u8_array().into(),
118+
beneficiary: self.beneficiary.to_be_bytes().into(),
119+
state_root: self.state_root.as_u8_array().into(),
120+
transactions_root: self.transactions_root.as_u8_array().into(),
121+
receipts_root: self.receipts_root.as_u8_array().into(),
122+
logs_bloom: self.logs_bloom.into(),
123+
difficulty: self.difficulty,
124+
number: self.number,
125+
gas_limit: self.gas_limit,
126+
gas_used: self.gas_used,
127+
timestamp: self.timestamp,
128+
extra_data: self.extra_data.to_vec().into(),
129+
mix_hash: self.mix_hash.as_u8_array().into(),
130+
nonce: self.nonce.into(),
131+
base_fee_per_gas: Some(self.base_fee_per_gas),
132+
withdrawals_root: None,
133+
blob_gas_used: None,
134+
excess_blob_gas: None,
135+
parent_beacon_block_root: None,
136+
requests_hash: None,
137+
};
138+
Sealed::new_unchecked(header, hash.into())
139+
}
140+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use crate::run::convert::FromInterface;
2+
use crate::run::errors::ForwardSubsystemError;
3+
use crate::run::output::TxResult;
4+
use crate::run::{run_block, simulate_tx};
5+
use zk_ee::system::metadata::BlockMetadataFromOracle;
6+
use zk_ee::system::tracer::NopTracer;
7+
use zksync_os_interface::traits::{
8+
PreimageSource, ReadStorage, RunBlock, SimulateTx, TxResultCallback, TxSource,
9+
};
10+
use zksync_os_interface::types::BlockContext;
11+
use zksync_os_interface::types::BlockOutput;
12+
13+
pub struct RunBlockForward {
14+
// Empty struct for now, but it can contain some configuration in the future.
15+
// For example, a flag to enable/disable specific behavior for subversions of the system.
16+
// These flags can then be used inside `run_block`/`simulate_tx` below to control the execution flow.
17+
}
18+
19+
impl RunBlock for RunBlockForward {
20+
type Config = ();
21+
type Error = ForwardSubsystemError;
22+
23+
fn run_block<T: ReadStorage, PS: PreimageSource, TS: TxSource, TR: TxResultCallback>(
24+
&self,
25+
_config: (),
26+
block_context: BlockContext,
27+
storage: T,
28+
preimage_source: PS,
29+
tx_source: TS,
30+
tx_result_callback: TR,
31+
) -> Result<BlockOutput, Self::Error> {
32+
run_block(
33+
BlockMetadataFromOracle::from_interface(block_context),
34+
storage,
35+
preimage_source,
36+
tx_source,
37+
tx_result_callback,
38+
&mut NopTracer::default(),
39+
)
40+
}
41+
}
42+
43+
impl SimulateTx for RunBlockForward {
44+
type Config = ();
45+
type Error = ForwardSubsystemError;
46+
47+
fn simulate_tx<S: ReadStorage, PS: PreimageSource>(
48+
&self,
49+
_config: (),
50+
transaction: Vec<u8>,
51+
block_context: BlockContext,
52+
storage: S,
53+
preimage_source: PS,
54+
) -> Result<TxResult, Self::Error> {
55+
simulate_tx(
56+
transaction,
57+
BlockMetadataFromOracle::from_interface(block_context),
58+
storage,
59+
preimage_source,
60+
&mut NopTracer::default(),
61+
)
62+
}
63+
}

forward_system/src/run/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ mod tree;
66
mod tx_result_callback;
77
mod tx_source;
88

9+
pub mod convert;
10+
mod interface_impl;
911
pub mod result_keeper;
1012
pub mod test_impl;
1113

@@ -23,6 +25,7 @@ pub use oracle::ForwardRunningOracle;
2325
use zk_ee::common_structs::ProofData;
2426
use zk_ee::system::tracer::Tracer;
2527

28+
pub use interface_impl::RunBlockForward;
2629
pub use tree::LeafProof;
2730
pub use tree::ReadStorage;
2831
pub use tree::ReadStorageTree;
@@ -38,12 +41,7 @@ pub use tx_result_callback::TxResultCallback;
3841
pub use tx_source::NextTxResponse;
3942
pub use tx_source::TxSource;
4043

41-
pub use self::output::BlockOutput;
42-
pub use self::output::ExecutionOutput;
43-
pub use self::output::ExecutionResult;
44-
pub use self::output::Log;
45-
pub use self::output::StorageWrite;
46-
pub use self::output::TxOutput;
44+
use self::output::BlockOutput;
4745
use crate::run::output::TxResult;
4846
use crate::run::test_impl::{NoopTxCallback, TxListSource};
4947
pub use basic_bootloader::bootloader::errors::InvalidTransaction;

0 commit comments

Comments
 (0)