Skip to content

Commit

Permalink
Mpt trie panic refactor (#118)
Browse files Browse the repository at this point in the history
* refactor: refactoring mpt_trie to use more Results

* fix: replace anyhow with this-error for mpt_trie

* style: formatting

* fix: fix results

* fix: pr fixes

* fix: fix error message

* fix: format

* fix: fix unusefull return type

* fix: fix formatting

* fix: pr fixes

* fix: pr fixes

* fix: pr fixes

* tests: refactor some tests

---------

Co-authored-by: Vladimir Trifonov <trifonov.vp@gmail.com>
  • Loading branch information
2 people authored and BGluth committed Apr 22, 2024
1 parent 521de06 commit 4e1cc6d
Show file tree
Hide file tree
Showing 22 changed files with 495 additions and 336 deletions.
14 changes: 7 additions & 7 deletions evm_arithmetization/tests/add11_yml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ fn add11_yml() -> anyhow::Result<()> {
state_trie_before.insert(
beneficiary_nibbles,
rlp::encode(&beneficiary_account_before).to_vec(),
);
state_trie_before.insert(sender_nibbles, rlp::encode(&sender_account_before).to_vec());
state_trie_before.insert(to_nibbles, rlp::encode(&to_account_before).to_vec());
)?;
state_trie_before.insert(sender_nibbles, rlp::encode(&sender_account_before).to_vec())?;
state_trie_before.insert(to_nibbles, rlp::encode(&to_account_before).to_vec())?;

let tries_before = TrieInputs {
state_trie: state_trie_before,
Expand Down Expand Up @@ -119,10 +119,10 @@ fn add11_yml() -> anyhow::Result<()> {
expected_state_trie_after.insert(
beneficiary_nibbles,
rlp::encode(&beneficiary_account_after).to_vec(),
);
)?;
expected_state_trie_after
.insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec());
expected_state_trie_after.insert(to_nibbles, rlp::encode(&to_account_after).to_vec());
.insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec())?;
expected_state_trie_after.insert(to_nibbles, rlp::encode(&to_account_after).to_vec())?;
expected_state_trie_after
};

Expand All @@ -136,7 +136,7 @@ fn add11_yml() -> anyhow::Result<()> {
receipts_trie.insert(
Nibbles::from_str("0x80").unwrap(),
rlp::encode(&receipt_0).to_vec(),
);
)?;
let transactions_trie: HashedPartialTrie = Node::Leaf {
nibbles: Nibbles::from_str("0x80").unwrap(),
value: txn.to_vec(),
Expand Down
2 changes: 1 addition & 1 deletion evm_arithmetization/tests/basic_smart_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fn test_basic_smart_contract() -> anyhow::Result<()> {
receipts_trie.insert(
Nibbles::from_str("0x80").unwrap(),
rlp::encode(&receipt_0).to_vec(),
);
)?;
let transactions_trie: HashedPartialTrie = Node::Leaf {
nibbles: Nibbles::from_str("0x80").unwrap(),
value: txn.to_vec(),
Expand Down
63 changes: 32 additions & 31 deletions evm_arithmetization/tests/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ fn test_erc20() -> anyhow::Result<()> {
let token_nibbles = Nibbles::from_bytes_be(token_state_key.as_bytes()).unwrap();

let mut state_trie_before = HashedPartialTrie::from(Node::Empty);
state_trie_before.insert(sender_nibbles, rlp::encode(&sender_account()).to_vec());
state_trie_before.insert(giver_nibbles, rlp::encode(&giver_account()).to_vec());
state_trie_before.insert(token_nibbles, rlp::encode(&token_account()).to_vec());
state_trie_before.insert(sender_nibbles, rlp::encode(&sender_account()).to_vec())?;
state_trie_before.insert(giver_nibbles, rlp::encode(&giver_account()?).to_vec())?;
state_trie_before.insert(token_nibbles, rlp::encode(&token_account()?).to_vec())?;

let storage_tries = vec![
(giver_state_key, giver_storage()),
(token_state_key, token_storage()),
(giver_state_key, giver_storage()?),
(token_state_key, token_storage()?),
];

let tries_before = TrieInputs {
Expand Down Expand Up @@ -107,13 +107,13 @@ fn test_erc20() -> anyhow::Result<()> {
balance: sender_account.balance - gas_used * 0xa,
..sender_account
};
state_trie_after.insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec());
state_trie_after.insert(giver_nibbles, rlp::encode(&giver_account()).to_vec());
state_trie_after.insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec())?;
state_trie_after.insert(giver_nibbles, rlp::encode(&giver_account()?).to_vec())?;
let token_account_after = AccountRlp {
storage_root: token_storage_after().hash(),
..token_account()
storage_root: token_storage_after()?.hash(),
..token_account()?
};
state_trie_after.insert(token_nibbles, rlp::encode(&token_account_after).to_vec());
state_trie_after.insert(token_nibbles, rlp::encode(&token_account_after).to_vec())?;

state_trie_after
};
Expand Down Expand Up @@ -144,7 +144,7 @@ fn test_erc20() -> anyhow::Result<()> {
}],
};
let mut receipts_trie = HashedPartialTrie::from(Node::Empty);
receipts_trie.insert(Nibbles::from_str("0x80").unwrap(), receipt_0.encode(2));
receipts_trie.insert(Nibbles::from_str("0x80").unwrap(), receipt_0.encode(2))?;
let transactions_trie: HashedPartialTrie = Node::Leaf {
nibbles: Nibbles::from_str("0x80").unwrap(),
value: txn.to_vec(),
Expand Down Expand Up @@ -192,71 +192,72 @@ fn token_bytecode() -> Vec<u8> {
hex!("608060405234801561001057600080fd5b50600436106100935760003560e01c8063313ce56711610066578063313ce567146100fe57806370a082311461010d57806395d89b4114610136578063a9059cbb1461013e578063dd62ed3e1461015157600080fd5b806306fdde0314610098578063095ea7b3146100b657806318160ddd146100d957806323b872dd146100eb575b600080fd5b6100a061018a565b6040516100ad919061056a565b60405180910390f35b6100c96100c43660046105d4565b61021c565b60405190151581526020016100ad565b6002545b6040519081526020016100ad565b6100c96100f93660046105fe565b610236565b604051601281526020016100ad565b6100dd61011b36600461063a565b6001600160a01b031660009081526020819052604090205490565b6100a061025a565b6100c961014c3660046105d4565b610269565b6100dd61015f36600461065c565b6001600160a01b03918216600090815260016020908152604080832093909416825291909152205490565b6060600380546101999061068f565b80601f01602080910402602001604051908101604052809291908181526020018280546101c59061068f565b80156102125780601f106101e757610100808354040283529160200191610212565b820191906000526020600020905b8154815290600101906020018083116101f557829003601f168201915b5050505050905090565b60003361022a818585610277565b60019150505b92915050565b600033610244858285610289565b61024f85858561030c565b506001949350505050565b6060600480546101999061068f565b60003361022a81858561030c565b610284838383600161036b565b505050565b6001600160a01b03838116600090815260016020908152604080832093861683529290522054600019811461030657818110156102f757604051637dc7a0d960e11b81526001600160a01b038416600482015260248101829052604481018390526064015b60405180910390fd5b6103068484848403600061036b565b50505050565b6001600160a01b03831661033657604051634b637e8f60e11b8152600060048201526024016102ee565b6001600160a01b0382166103605760405163ec442f0560e01b8152600060048201526024016102ee565b610284838383610440565b6001600160a01b0384166103955760405163e602df0560e01b8152600060048201526024016102ee565b6001600160a01b0383166103bf57604051634a1406b160e11b8152600060048201526024016102ee565b6001600160a01b038085166000908152600160209081526040808320938716835292905220829055801561030657826001600160a01b0316846001600160a01b03167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9258460405161043291815260200190565b60405180910390a350505050565b6001600160a01b03831661046b57806002600082825461046091906106c9565b909155506104dd9050565b6001600160a01b038316600090815260208190526040902054818110156104be5760405163391434e360e21b81526001600160a01b038516600482015260248101829052604481018390526064016102ee565b6001600160a01b03841660009081526020819052604090209082900390555b6001600160a01b0382166104f957600280548290039055610518565b6001600160a01b03821660009081526020819052604090208054820190555b816001600160a01b0316836001600160a01b03167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef8360405161055d91815260200190565b60405180910390a3505050565b600060208083528351808285015260005b818110156105975785810183015185820160400152820161057b565b506000604082860101526040601f19601f8301168501019250505092915050565b80356001600160a01b03811681146105cf57600080fd5b919050565b600080604083850312156105e757600080fd5b6105f0836105b8565b946020939093013593505050565b60008060006060848603121561061357600080fd5b61061c846105b8565b925061062a602085016105b8565b9150604084013590509250925092565b60006020828403121561064c57600080fd5b610655826105b8565b9392505050565b6000806040838503121561066f57600080fd5b610678836105b8565b9150610686602084016105b8565b90509250929050565b600181811c908216806106a357607f821691505b6020821081036106c357634e487b7160e01b600052602260045260246000fd5b50919050565b8082018082111561023057634e487b7160e01b600052601160045260246000fdfea2646970667358221220266a323ae4a816f6c6342a5be431fedcc0d45c44b02ea75f5474eb450b5d45b364736f6c63430008140033").into()
}

fn insert_storage(trie: &mut HashedPartialTrie, slot: U256, value: U256) {
fn insert_storage(trie: &mut HashedPartialTrie, slot: U256, value: U256) -> anyhow::Result<()> {
let mut bytes = [0; 32];
slot.to_big_endian(&mut bytes);
let key = keccak(bytes);
let nibbles = Nibbles::from_bytes_be(key.as_bytes()).unwrap();
let r = rlp::encode(&value);
let r = r.freeze().to_vec();
trie.insert(nibbles, r);
trie.insert(nibbles, r)?;
Ok(())
}

fn sd2u(s: &str) -> U256 {
U256::from_dec_str(s).unwrap()
}

fn giver_storage() -> HashedPartialTrie {
fn giver_storage() -> anyhow::Result<HashedPartialTrie> {
let mut trie = HashedPartialTrie::from(Node::Empty);
insert_storage(
&mut trie,
U256::zero(),
sd2u("546584486846459126461364135121053344201067465379"),
);
trie
)?;
Ok(trie)
}

fn token_storage() -> HashedPartialTrie {
fn token_storage() -> anyhow::Result<HashedPartialTrie> {
let mut trie = HashedPartialTrie::from(Node::Empty);
insert_storage(
&mut trie,
sd2u("82183438603287090451672504949863617512989139203883434767553028632841710582583"),
sd2u("1000000000000000000000"),
);
trie
)?;
Ok(trie)
}

fn token_storage_after() -> HashedPartialTrie {
fn token_storage_after() -> anyhow::Result<HashedPartialTrie> {
let mut trie = HashedPartialTrie::from(Node::Empty);
insert_storage(
&mut trie,
sd2u("82183438603287090451672504949863617512989139203883434767553028632841710582583"),
sd2u("900000000000000000000"),
);
)?;
insert_storage(
&mut trie,
sd2u("53006154680716014998529145169423020330606407246856709517064848190396281160729"),
sd2u("100000000000000000000"),
);
trie
)?;
Ok(trie)
}

fn giver_account() -> AccountRlp {
AccountRlp {
fn giver_account() -> anyhow::Result<AccountRlp> {
Ok(AccountRlp {
nonce: 1.into(),
balance: 0.into(),
storage_root: giver_storage().hash(),
storage_root: giver_storage()?.hash(),
code_hash: keccak(giver_bytecode()),
}
})
}

fn token_account() -> AccountRlp {
AccountRlp {
fn token_account() -> anyhow::Result<AccountRlp> {
Ok(AccountRlp {
nonce: 1.into(),
balance: 0.into(),
storage_root: token_storage().hash(),
storage_root: token_storage()?.hash(),
code_hash: keccak(token_bytecode()),
}
})
}

fn sender_account() -> AccountRlp {
Expand Down
58 changes: 30 additions & 28 deletions evm_arithmetization/tests/erc721.rs

Large diffs are not rendered by default.

57 changes: 30 additions & 27 deletions evm_arithmetization/tests/log_opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ fn test_log_opcodes() -> anyhow::Result<()> {
state_trie_before.insert(
beneficiary_nibbles,
rlp::encode(&beneficiary_account_before).to_vec(),
);
state_trie_before.insert(sender_nibbles, rlp::encode(&sender_account_before).to_vec());
state_trie_before.insert(to_nibbles, rlp::encode(&to_account_before).to_vec());
)?;
state_trie_before.insert(sender_nibbles, rlp::encode(&sender_account_before).to_vec())?;
state_trie_before.insert(to_nibbles, rlp::encode(&to_account_before).to_vec())?;

// We now add two receipts with logs and data. This updates the receipt trie as
// well.
Expand Down Expand Up @@ -119,7 +119,7 @@ fn test_log_opcodes() -> anyhow::Result<()> {
receipts_trie.insert(
Nibbles::from_str("0x1337").unwrap(),
rlp::encode(&receipt_0).to_vec(),
);
)?;

let tries_before = TrieInputs {
state_trie: state_trie_before,
Expand Down Expand Up @@ -193,16 +193,17 @@ fn test_log_opcodes() -> anyhow::Result<()> {

let receipt_nibbles = Nibbles::from_str("0x80").unwrap(); // RLP(0) = 0x80

receipts_trie.insert(receipt_nibbles, rlp::encode(&receipt).to_vec());
receipts_trie.insert(receipt_nibbles, rlp::encode(&receipt).to_vec())?;

// Update the state trie.
let mut expected_state_trie_after = HashedPartialTrie::from(Node::Empty);
expected_state_trie_after.insert(
beneficiary_nibbles,
rlp::encode(&beneficiary_account_after).to_vec(),
);
expected_state_trie_after.insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec());
expected_state_trie_after.insert(to_nibbles, rlp::encode(&to_account_after).to_vec());
)?;
expected_state_trie_after
.insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec())?;
expected_state_trie_after.insert(to_nibbles, rlp::encode(&to_account_after).to_vec())?;

let transactions_trie: HashedPartialTrie = Node::Leaf {
nibbles: Nibbles::from_str("0x80").unwrap(),
Expand Down Expand Up @@ -319,13 +320,13 @@ fn test_log_with_aggreg() -> anyhow::Result<()> {
state_trie_before.insert(
beneficiary_nibbles,
rlp::encode(&beneficiary_account_before).to_vec(),
);
state_trie_before.insert(sender_nibbles, rlp::encode(&sender_account_before).to_vec());
state_trie_before.insert(to_nibbles, rlp::encode(&to_account_before).to_vec());
)?;
state_trie_before.insert(sender_nibbles, rlp::encode(&sender_account_before).to_vec())?;
state_trie_before.insert(to_nibbles, rlp::encode(&to_account_before).to_vec())?;
state_trie_before.insert(
to_second_nibbles,
rlp::encode(&to_account_second_before).to_vec(),
);
)?;
let checkpoint_state_trie_root = state_trie_before.hash();

let tries_before = TrieInputs {
Expand Down Expand Up @@ -389,13 +390,14 @@ fn test_log_with_aggreg() -> anyhow::Result<()> {
expected_state_trie_after.insert(
beneficiary_nibbles,
rlp::encode(&beneficiary_account_after).to_vec(),
);
expected_state_trie_after.insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec());
expected_state_trie_after.insert(to_nibbles, rlp::encode(&to_account_after).to_vec());
)?;
expected_state_trie_after
.insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec())?;
expected_state_trie_after.insert(to_nibbles, rlp::encode(&to_account_after).to_vec())?;
expected_state_trie_after.insert(
to_second_nibbles,
rlp::encode(&to_account_second_before).to_vec(),
);
)?;

// Compute new receipt trie.
let mut receipts_trie = HashedPartialTrie::from(Node::Empty);
Expand All @@ -408,7 +410,7 @@ fn test_log_with_aggreg() -> anyhow::Result<()> {
receipts_trie.insert(
Nibbles::from_str("0x80").unwrap(),
rlp::encode(&receipt_0).to_vec(),
);
)?;

let mut transactions_trie: HashedPartialTrie = Node::Leaf {
nibbles: Nibbles::from_str("0x80").unwrap(),
Expand Down Expand Up @@ -530,22 +532,23 @@ fn test_log_with_aggreg() -> anyhow::Result<()> {

let receipt_nibbles = Nibbles::from_str("0x01").unwrap(); // RLP(1) = 0x1

receipts_trie.insert(receipt_nibbles, rlp::encode(&receipt).to_vec());
receipts_trie.insert(receipt_nibbles, rlp::encode(&receipt).to_vec())?;

// Update the state trie.
let mut expected_state_trie_after = HashedPartialTrie::from(Node::Empty);
expected_state_trie_after.insert(
beneficiary_nibbles,
rlp::encode(&beneficiary_account_after).to_vec(),
);
expected_state_trie_after.insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec());
expected_state_trie_after.insert(to_nibbles, rlp::encode(&to_account_after).to_vec());
)?;
expected_state_trie_after
.insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec())?;
expected_state_trie_after.insert(to_nibbles, rlp::encode(&to_account_after).to_vec())?;
expected_state_trie_after.insert(
to_second_nibbles,
rlp::encode(&to_account_second_after).to_vec(),
);
)?;

transactions_trie.insert(Nibbles::from_str("0x01").unwrap(), txn_2.to_vec());
transactions_trie.insert(Nibbles::from_str("0x01").unwrap(), txn_2.to_vec())?;

let block_1_state_root = expected_state_trie_after.hash();

Expand Down Expand Up @@ -687,7 +690,7 @@ fn test_txn_and_receipt_trie_hash() -> anyhow::Result<()> {
example_txn_trie.insert(
Nibbles::from_str("0x80").unwrap(), // RLP(0) = 0x80
rlp::encode(&transaction_0).to_vec(),
);
)?;

let transaction_1 = LegacyTransactionRlp {
nonce: 157824u64.into(),
Expand All @@ -707,7 +710,7 @@ fn test_txn_and_receipt_trie_hash() -> anyhow::Result<()> {
example_txn_trie.insert(
Nibbles::from_str("0x01").unwrap(),
rlp::encode(&transaction_1).to_vec(),
);
)?;

// Receipts:
let mut example_receipt_trie = HashedPartialTrie::from(Node::Empty);
Expand Down Expand Up @@ -735,7 +738,7 @@ fn test_txn_and_receipt_trie_hash() -> anyhow::Result<()> {
example_receipt_trie.insert(
Nibbles::from_str("0x80").unwrap(), // RLP(0) is 0x80
rlp::encode(&receipt_0).to_vec(),
);
)?;

let log_1 = LogRlp {
address: hex!("7ef66b77759e12Caf3dDB3E4AFF524E577C59D8D").into(),
Expand All @@ -760,7 +763,7 @@ fn test_txn_and_receipt_trie_hash() -> anyhow::Result<()> {
example_receipt_trie.insert(
Nibbles::from_str("0x01").unwrap(),
rlp::encode(&receipt_1).to_vec(),
);
)?;

// Check that the trie hashes are correct.
assert_eq!(
Expand Down
14 changes: 7 additions & 7 deletions evm_arithmetization/tests/self_balance_gas_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ fn self_balance_gas_cost() -> anyhow::Result<()> {
state_trie_before.insert(
beneficiary_nibbles,
rlp::encode(&beneficiary_account_before).to_vec(),
);
state_trie_before.insert(sender_nibbles, rlp::encode(&sender_account_before).to_vec());
state_trie_before.insert(to_nibbles, rlp::encode(&to_account_before).to_vec());
)?;
state_trie_before.insert(sender_nibbles, rlp::encode(&sender_account_before).to_vec())?;
state_trie_before.insert(to_nibbles, rlp::encode(&to_account_before).to_vec())?;

let tries_before = TrieInputs {
state_trie: state_trie_before,
Expand Down Expand Up @@ -138,10 +138,10 @@ fn self_balance_gas_cost() -> anyhow::Result<()> {
expected_state_trie_after.insert(
beneficiary_nibbles,
rlp::encode(&beneficiary_account_after).to_vec(),
);
)?;
expected_state_trie_after
.insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec());
expected_state_trie_after.insert(to_nibbles, rlp::encode(&to_account_after).to_vec());
.insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec())?;
expected_state_trie_after.insert(to_nibbles, rlp::encode(&to_account_after).to_vec())?;
expected_state_trie_after
};

Expand All @@ -155,7 +155,7 @@ fn self_balance_gas_cost() -> anyhow::Result<()> {
receipts_trie.insert(
Nibbles::from_str("0x80").unwrap(),
rlp::encode(&receipt_0).to_vec(),
);
)?;
let transactions_trie: HashedPartialTrie = Node::Leaf {
nibbles: Nibbles::from_str("0x80").unwrap(),
value: txn.to_vec(),
Expand Down
Loading

0 comments on commit 4e1cc6d

Please sign in to comment.