-
Notifications
You must be signed in to change notification settings - Fork 133
feat: integrate revm + impl address recovery #73
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
Merged
Changes from 15 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
af59cf4
Draft
fmoletta 9830005
Create BlockEnv from BlockHeader
fmoletta f568b4c
Add state cache
fmoletta 19b47e1
Merge branch 'main' of https://github.com/lambdaclass/ethrex into add…
fmoletta 4d82b04
Fill more tx env fields
fmoletta 9c8f150
Move `Transaction` to its own module
fmoletta d48b3f6
Update crate name + add access list
fmoletta ff36e10
Merge branch 'main' of https://github.com/lambdaclass/ethrex into add…
fmoletta 0423796
Add nonce and external handler
fmoletta 0e7739e
Fill some todos
fmoletta e854ae5
Convert eftests header to ethrex block header
fmoletta 639b74e
Convert eftests tx to ethrex tx
fmoletta ff7297f
Convert eftests account to ethrex account
fmoletta 5e1d06b
Replace evm code in eftests with ethrex-evm import
fmoletta c1f7f53
WIP: add address recovery
fmoletta f29a030
Make ef tests run: Set chain id to mainnet + use current block instea…
fmoletta 98b9edd
Update address calculation
fmoletta 2d27e43
Use y parity
fmoletta f25208f
Fix parity calc
fmoletta 890cff8
Fix parity calc
fmoletta 937e1a9
Add another ef test example
fmoletta 7c663df
Comment quick fix
fmoletta 6ca3811
Differentiate between legacy and eip tx when converting from eftests …
fmoletta 9748e86
Filter by tx_type
fmoletta f0b02a0
execute_tx return ExecutionResult
fmoletta fe64b2c
Handle y_parity in protected legacy txs
fmoletta 3875895
Use rlp encoded tx fields instead of tx data to calc pub key
fmoletta 5e52b86
Move quick fix for empty data
fmoletta 184d56b
Add todo comment
fmoletta e8bd271
Fix tx data desrialization for eftests
fmoletta 9e4b141
Remove comment
fmoletta 1f2a194
Fix code deser
fmoletta 9549014
Use correct data for EIP1555 addr calc too
fmoletta 1dbc191
Remove unused dep
fmoletta 4da9018
clippy
fmoletta 69caf65
Remove uneeded feature
fmoletta 625fc21
Remove dbg print
fmoletta 08de5e2
Move evm crate inside core
fmoletta 0908a4a
Fix desr
fmoletta 87dab3d
map error
fmoletta 6636cfc
Merge branch 'main' of https://github.com/lambdaclass/ethrex into add…
fmoletta 54ffef5
Remove dbg print
fmoletta 480a2fc
Merge branch 'main' of https://github.com/lambdaclass/ethrex into add…
fmoletta 1aff2a4
Fix EOF
fmoletta d3fb8fc
Merge branch 'main' of https://github.com/lambdaclass/ethrex into add…
fmoletta d3f52b2
Add ExecutionResult type
fmoletta f4ced4a
Push fn
fmoletta 4368db0
Move ExecutionResult to its own module
fmoletta 769550f
Merge branch 'main' of https://github.com/lambdaclass/ethrex into add…
fmoletta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| mod account; | ||
| mod block; | ||
| mod genesis; | ||
| mod transaction; | ||
|
|
||
| pub use account::*; | ||
| pub use block::*; | ||
| pub use genesis::*; | ||
| pub use transaction::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| use bytes::Bytes; | ||
| use ethereum_types::{Address, H256, U256}; | ||
| use k256::ecdsa::{RecoveryId, Signature, VerifyingKey}; | ||
| use sha3::{Digest, Keccak256}; | ||
|
|
||
| use crate::rlp::encode::RLPEncode; | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub enum Transaction { | ||
| LegacyTransaction(LegacyTransaction), | ||
| EIP1559Transaction(EIP1559Transaction), | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub struct LegacyTransaction { | ||
| pub nonce: u64, | ||
| pub gas_price: u64, | ||
| pub gas: u64, | ||
| pub to: Address, | ||
| pub value: U256, | ||
| pub data: Bytes, | ||
| pub v: U256, | ||
| pub r: U256, | ||
| pub s: U256, | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub struct EIP1559Transaction { | ||
| pub chain_id: u64, | ||
| pub signer_nonce: u64, | ||
| pub max_priority_fee_per_gas: u64, | ||
| pub max_fee_per_gas: u64, | ||
| pub gas_limit: u64, | ||
| pub destination: Address, | ||
| pub amount: U256, | ||
| pub payload: Bytes, | ||
| pub access_list: Vec<(Address, Vec<H256>)>, | ||
| pub signature_y_parity: bool, | ||
| pub signature_r: U256, | ||
| pub signature_s: U256, | ||
| } | ||
|
|
||
| impl RLPEncode for Transaction { | ||
| fn encode(&self, buf: &mut dyn bytes::BufMut) { | ||
| match self { | ||
| Transaction::LegacyTransaction(t) => t.encode(buf), | ||
| Transaction::EIP1559Transaction(t) => t.encode(buf), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| impl RLPEncode for LegacyTransaction { | ||
juanbono marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fn encode(&self, buf: &mut dyn bytes::BufMut) { | ||
| self.nonce.encode(buf); | ||
| self.gas_price.encode(buf); | ||
| self.gas.encode(buf); | ||
| self.to.encode(buf); | ||
| self.value.encode(buf); | ||
| self.data.encode(buf); | ||
| self.v.encode(buf); | ||
| self.r.encode(buf); | ||
| self.s.encode(buf); | ||
| } | ||
| } | ||
|
|
||
| impl RLPEncode for EIP1559Transaction { | ||
juanbono marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fn encode(&self, buf: &mut dyn bytes::BufMut) { | ||
| self.chain_id.encode(buf); | ||
| self.signer_nonce.encode(buf); | ||
| self.max_priority_fee_per_gas.encode(buf); | ||
| self.max_fee_per_gas.encode(buf); | ||
| self.gas_limit.encode(buf); | ||
| self.destination.encode(buf); | ||
| self.amount.encode(buf); | ||
| self.payload.encode(buf); | ||
| self.access_list.encode(buf); | ||
| self.signature_y_parity.encode(buf); | ||
| self.signature_r.encode(buf); | ||
| self.signature_s.encode(buf); | ||
| } | ||
| } | ||
|
|
||
| impl Transaction { | ||
| pub fn sender(&self) -> Address { | ||
| match self { | ||
| Transaction::LegacyTransaction(tx) => recover_address(&tx.r, &tx.s, &tx.data), | ||
| Transaction::EIP1559Transaction(tx) => { | ||
| dbg!(recover_address( | ||
| &tx.signature_r, | ||
| &tx.signature_s, | ||
| &tx.payload | ||
| )) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn gas_limit(&self) -> u64 { | ||
| match self { | ||
| Transaction::LegacyTransaction(tx) => tx.gas_price, | ||
| Transaction::EIP1559Transaction(tx) => tx.gas_limit, | ||
| } | ||
| } | ||
|
|
||
| pub fn gas_price(&self) -> u64 { | ||
| match self { | ||
| Transaction::LegacyTransaction(tx) => tx.gas_price, | ||
| Transaction::EIP1559Transaction(tx) => tx.max_fee_per_gas, | ||
| } | ||
| } | ||
|
|
||
| pub fn to(&self) -> Address { | ||
| match self { | ||
| Transaction::LegacyTransaction(tx) => tx.to, | ||
| Transaction::EIP1559Transaction(tx) => tx.destination, | ||
| } | ||
| } | ||
|
|
||
| pub fn value(&self) -> U256 { | ||
| match self { | ||
| Transaction::LegacyTransaction(tx) => tx.value, | ||
| Transaction::EIP1559Transaction(tx) => tx.amount, | ||
| } | ||
| } | ||
|
|
||
| pub fn max_priority_fee(&self) -> Option<u64> { | ||
| match self { | ||
| Transaction::LegacyTransaction(_tx) => None, | ||
| Transaction::EIP1559Transaction(tx) => Some(tx.max_priority_fee_per_gas), | ||
| } | ||
| } | ||
|
|
||
| pub fn chain_id(&self) -> Option<u64> { | ||
| match self { | ||
| Transaction::LegacyTransaction(_tx) => None, | ||
| Transaction::EIP1559Transaction(tx) => Some(tx.chain_id), | ||
| } | ||
| } | ||
|
|
||
| pub fn access_list(&self) -> Vec<(Address, Vec<H256>)> { | ||
| match self { | ||
| Transaction::LegacyTransaction(_tx) => Vec::new(), | ||
| Transaction::EIP1559Transaction(tx) => tx.access_list.clone(), | ||
| } | ||
| } | ||
|
|
||
| pub fn nonce(&self) -> u64 { | ||
| match self { | ||
| Transaction::LegacyTransaction(tx) => tx.nonce, | ||
| Transaction::EIP1559Transaction(tx) => tx.signer_nonce, | ||
| } | ||
| } | ||
|
|
||
| pub fn data(&self) -> &Bytes { | ||
| match self { | ||
| Transaction::LegacyTransaction(tx) => &tx.data, | ||
| Transaction::EIP1559Transaction(tx) => &tx.payload, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO: Fix | ||
| fn recover_address(signature_r: &U256, signature_s: &U256, message: &Bytes) -> Address { | ||
| let mut r_bytes = [0; 32]; | ||
| let mut s_bytes = [0; 32]; | ||
| signature_r.to_big_endian(&mut r_bytes); | ||
| signature_s.to_big_endian(&mut s_bytes); | ||
| let signature = Signature::from_scalars(r_bytes, s_bytes).unwrap(); | ||
| let recid = RecoveryId::try_from(1u8).unwrap(); | ||
| let recovered_key = VerifyingKey::recover_from_digest( | ||
| Keccak256::new_with_prefix(message.as_ref()), | ||
| &signature, | ||
| recid, | ||
| ) | ||
| .unwrap(); | ||
| let state = &mut recovered_key.to_sec1_bytes()[1..]; | ||
| keccak_hash::keccak256(state); | ||
| Address::from_slice(&state[12..]) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.