Skip to content

Commit

Permalink
Use shorter names for serialize consts
Browse files Browse the repository at this point in the history
  • Loading branch information
LogvinovLeon committed May 10, 2024
1 parent 61f6b77 commit e5791fb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
30 changes: 15 additions & 15 deletions ethereum_history_api/circuits/lib/src/serde.nr
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ impl Serde<ADDRESS_LENGTH> for Address {
}
}

global ACCOUNT_SERIALIZED_LEN = 1 + 1 + BYTES32_LENGTH + BYTES32_LENGTH;
global ACCOUNT_LEN = 1 + 1 + BYTES32_LENGTH + BYTES32_LENGTH;

impl Serde<ACCOUNT_SERIALIZED_LEN> for Account {
fn serialize(self) -> [Field; ACCOUNT_SERIALIZED_LEN] {
let mut data: Fragment<ACCOUNT_SERIALIZED_LEN, Field> = Fragment::empty();
impl Serde<ACCOUNT_LEN> for Account {
fn serialize(self) -> [Field; ACCOUNT_LEN] {
let mut data: Fragment<ACCOUNT_LEN, Field> = Fragment::empty();
data.push_back(self.nonce as Field);
data.push_back(self.balance);
data.extend_back(self.storage_root.serialize());
data.extend_back(self.code_hash.serialize());
data.to_array()
}

fn deserialize(data: [Field; ACCOUNT_SERIALIZED_LEN]) -> Self {
fn deserialize(data: [Field; ACCOUNT_LEN]) -> Self {
let mut fragment = Fragment::new_focused(data);
let nonce = fragment.pop_front() as u64;
let balance = fragment.pop_front();
Expand All @@ -67,17 +67,17 @@ impl Serde<ACCOUNT_SERIALIZED_LEN> for Account {
}
}

global ACCOUNT_WITHIN_BLOCK_SERIALIZED_LEN = ACCOUNT_SERIALIZED_LEN + BYTES32_LENGTH;
global ACCOUNT_BLOCK_LEN = ACCOUNT_LEN + BYTES32_LENGTH;

impl Serde<ACCOUNT_WITHIN_BLOCK_SERIALIZED_LEN> for AccountWithinBlock {
fn serialize(self) -> [Field; ACCOUNT_WITHIN_BLOCK_SERIALIZED_LEN] {
let mut data: Fragment<ACCOUNT_WITHIN_BLOCK_SERIALIZED_LEN, Field> = Fragment::empty();
impl Serde<ACCOUNT_BLOCK_LEN> for AccountWithinBlock {
fn serialize(self) -> [Field; ACCOUNT_BLOCK_LEN] {
let mut data: Fragment<ACCOUNT_BLOCK_LEN, Field> = Fragment::empty();
data.extend_back(self.account.serialize());
data.extend_back(self.block_hash.serialize());
data.to_array()
}

fn deserialize(data: [Field; ACCOUNT_WITHIN_BLOCK_SERIALIZED_LEN]) -> Self {
fn deserialize(data: [Field; ACCOUNT_BLOCK_LEN]) -> Self {
let mut fragment = Fragment::new_focused(data);
let account = Account::deserialize(fragment.pop_front_array());
let block_hash = fragment.pop_front_array().deserialize();
Expand All @@ -88,18 +88,18 @@ impl Serde<ACCOUNT_WITHIN_BLOCK_SERIALIZED_LEN> for AccountWithinBlock {
}
}

global STORAGE_WITHIN_BLOCK_1_SERIALIZED_LEN = BYTES32_LENGTH + ACCOUNT_SERIALIZED_LEN + BYTES32_LENGTH;
global STORAGE_BLOCK_LEN = BYTES32_LENGTH + ACCOUNT_LEN + BYTES32_LENGTH;

impl Serde<STORAGE_WITHIN_BLOCK_1_SERIALIZED_LEN> for StorageWithinBlock<1> {
fn serialize(self) -> [Field; STORAGE_WITHIN_BLOCK_1_SERIALIZED_LEN] {
let mut data: Fragment<STORAGE_WITHIN_BLOCK_1_SERIALIZED_LEN, Field> = Fragment::empty();
impl Serde<STORAGE_BLOCK_LEN> for StorageWithinBlock<1> {
fn serialize(self) -> [Field; STORAGE_BLOCK_LEN] {
let mut data: Fragment<STORAGE_BLOCK_LEN, Field> = Fragment::empty();
data.extend_back(self.block_hash.serialize());
data.extend_back(self.account.serialize());
data.extend_back(self.values[0].serialize());
data.to_array()
}

fn deserialize(data: [Field; STORAGE_WITHIN_BLOCK_1_SERIALIZED_LEN]) -> Self {
fn deserialize(data: [Field; STORAGE_BLOCK_LEN]) -> Self {
let mut fragment = Fragment::new_focused(data);
let block_hash = fragment.pop_front_array().deserialize();
let account = Account::deserialize(fragment.pop_front_array());
Expand Down
16 changes: 8 additions & 8 deletions ethereum_history_api/circuits/lib/src/serde_test.nr
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ mod address {

mod account {
use crate::account_with_storage::Account;
use crate::serde::ACCOUNT_SERIALIZED_LEN;
use crate::serde::ACCOUNT_LEN;
use crate::misc::arrays::sub_array_equals;
use crate::fixtures::mainnet::london::vitalik_balance::account::account;

#[test]
fn simple() {
let serialized: [Field; ACCOUNT_SERIALIZED_LEN] = account.serialize();
let serialized: [Field; ACCOUNT_LEN] = account.serialize();

assert_eq(serialized[0], account.nonce as Field);
assert_eq(serialized[1], account.balance);
Expand All @@ -58,38 +58,38 @@ mod account {
}

mod account_within_block {
use crate::serde::{ACCOUNT_WITHIN_BLOCK_SERIALIZED_LEN, ACCOUNT_SERIALIZED_LEN};
use crate::serde::{ACCOUNT_BLOCK_LEN, ACCOUNT_LEN};
use crate::account::AccountWithinBlock;
use crate::fixtures::mainnet::london::vitalik_balance::{account::account, header::hash};
use crate::misc::arrays::sub_array_equals;

#[test]
fn simple() {
let account_within_block = AccountWithinBlock { account, block_hash: hash };
let serialized: [Field; ACCOUNT_WITHIN_BLOCK_SERIALIZED_LEN] = account_within_block.serialize();
let serialized: [Field; ACCOUNT_BLOCK_LEN] = account_within_block.serialize();

assert(sub_array_equals(account_within_block.account.serialize(), serialized, 0));
assert(
sub_array_equals(
account_within_block.block_hash.serialize(),
serialized,
ACCOUNT_SERIALIZED_LEN
ACCOUNT_LEN
)
);
assert_eq(AccountWithinBlock::deserialize(serialized), account_within_block);
}
}

mod storage_within_block {
use crate::serde::{STORAGE_WITHIN_BLOCK_1_SERIALIZED_LEN, ACCOUNT_SERIALIZED_LEN};
use crate::serde::{STORAGE_BLOCK_LEN, ACCOUNT_LEN};
use crate::account_with_storage::StorageWithinBlock;
use crate::fixtures::mainnet::paris::usdc_circle::{account::account, header::hash, storage_proof_new::proofs, storage::values};
use crate::misc::{types::BYTES32_LENGTH, arrays::sub_array_equals};

#[test]
fn simple() {
let storage_within_block = StorageWithinBlock { block_hash: hash, account, values };
let serialized: [Field; STORAGE_WITHIN_BLOCK_1_SERIALIZED_LEN] = storage_within_block.serialize();
let serialized: [Field; STORAGE_BLOCK_LEN] = storage_within_block.serialize();

assert(sub_array_equals(storage_within_block.block_hash.serialize(), serialized, 0));
assert(
Expand All @@ -103,7 +103,7 @@ mod storage_within_block {
sub_array_equals(
storage_within_block.values[0].serialize(),
serialized,
BYTES32_LENGTH + ACCOUNT_SERIALIZED_LEN
BYTES32_LENGTH + ACCOUNT_LEN
)
);
assert_eq(StorageWithinBlock::deserialize(serialized), storage_within_block);
Expand Down

0 comments on commit e5791fb

Please sign in to comment.