Skip to content
This repository has been archived by the owner on Feb 17, 2025. It is now read-only.

Commit

Permalink
Update data types
Browse files Browse the repository at this point in the history
Simplified interfaces for future updates
  • Loading branch information
aleasims authored and akokoshn committed Apr 5, 2024
1 parent d7e3e22 commit 5b2814d
Show file tree
Hide file tree
Showing 23 changed files with 416 additions and 371 deletions.
3 changes: 3 additions & 0 deletions libs/data_types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ find_package(sszpp REQUIRED)
set(LIBRARY_NAME zkEVMDataTypes)
set(SOURCES
src/account.cpp
src/account_block.cpp
src/block.cpp
src/block_header.cpp
src/message.cpp
Expand All @@ -20,10 +21,12 @@ add_library(${LIBRARY_NAME} STATIC ${SOURCES})
set(PUBLIC_HEADERS_PREFIX zkevm_framework/data_types)
set(PUBLIC_HEADERS
include/${PUBLIC_HEADERS_PREFIX}/account.hpp
include/${PUBLIC_HEADERS_PREFIX}/account_block.hpp
include/${PUBLIC_HEADERS_PREFIX}/base.hpp
include/${PUBLIC_HEADERS_PREFIX}/block.hpp
include/${PUBLIC_HEADERS_PREFIX}/block_header.hpp
include/${PUBLIC_HEADERS_PREFIX}/message.hpp
include/${PUBLIC_HEADERS_PREFIX}/mpt.hpp
include/${PUBLIC_HEADERS_PREFIX}/state.hpp
include/${PUBLIC_HEADERS_PREFIX}/transaction.hpp
include/${PUBLIC_HEADERS_PREFIX}/transaction_receipt.hpp)
Expand Down
28 changes: 6 additions & 22 deletions libs/data_types/include/zkevm_framework/data_types/account.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#ifndef ZKEMV_FRAMEWORK_LIBS_DATA_TYPES_INCLUDE_ZKEVM_FRAMEWORK_DATA_TYPES_ACCOUNT_HPP_
#define ZKEMV_FRAMEWORK_LIBS_DATA_TYPES_INCLUDE_ZKEVM_FRAMEWORK_DATA_TYPES_ACCOUNT_HPP_

#include <cstddef>
#include <cstdint>

#include "sszpp/ssz++.hpp"
Expand All @@ -18,18 +17,12 @@ namespace data_types {
public:
friend class State;

Account() {}

Account(size_t nonce, size_t balance, AccessStatus access_status, Hash root)
: m_nonce(nonce),
m_balance(balance),
m_accessStatus(access_status),
m_storageRoot(root) {}
size_t m_nonce;
size_t m_balance;
AccessStatus m_accessStatus;

size_t getNonce() const { return m_nonce; }
size_t getBalance() const { return m_balance; }
AccessStatus getAccessStatus() const { return m_accessStatus; }
Hash getStorageRoot() const { return m_storageRoot; }
Account(size_t nonce, size_t balance, AccessStatus access_status)
: m_nonce(nonce), m_balance(balance), m_accessStatus(access_status) {}

/// @returns the SSZ serialization
bytes serialize() const;
Expand All @@ -38,33 +31,24 @@ namespace data_types {
static Account deserialize(const bytes &src);

private:
size_t m_nonce;
size_t m_balance;
AccessStatus m_accessStatus;
Hash m_storageRoot;

/// @brief Serializable mirroring structure of `Account`.
struct Serializable : ssz::ssz_container {
size_t m_nonce;
size_t m_balance;
uint8_t m_accessStatus;
Hash m_storageRoot;

SSZ_CONT(m_nonce, m_balance, m_accessStatus, m_storageRoot)
SSZ_CONT(m_nonce, m_balance, m_accessStatus)

Serializable() {}

Serializable(const Account &account)
: m_nonce(account.m_nonce),
m_balance(account.m_balance),
m_storageRoot(account.m_storageRoot),
m_accessStatus(account.m_accessStatus) {}
};

Account(const Serializable &s)
: m_balance(s.m_balance),
m_nonce(s.m_nonce),
m_storageRoot(s.m_storageRoot),
m_accessStatus(static_cast<AccessStatus>(s.m_accessStatus)) {}
};
} // namespace data_types
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @file account_block.hpp
* @brief This file defines Account Block.
*/

#ifndef ZKEMV_FRAMEWORK_LIBS_DATA_TYPES_INCLUDE_ZKEVM_FRAMEWORK_DATA_TYPES_ACCOUNT_BLOCK_HPP_
#define ZKEMV_FRAMEWORK_LIBS_DATA_TYPES_INCLUDE_ZKEVM_FRAMEWORK_DATA_TYPES_ACCOUNT_BLOCK_HPP_

#include "sszpp/ssz++.hpp"
#include "zkevm_framework/data_types/base.hpp"
#include "zkevm_framework/data_types/mpt.hpp"
#include "zkevm_framework/data_types/transaction.hpp"

namespace data_types {
/// @brief Account block.
class AccountBlock {
public:
friend class Block;

Address m_accountAddress;
MPTNode<Transaction> m_transactions;

AccountBlock(Address accountAddress, MPTNode<Transaction> &transactions)
: m_accountAddress(accountAddress), m_transactions(transactions) {}

/// @returns the SSZ serialisation
bytes serialize() const;

/// @brief deserizalize from SSZ
static AccountBlock deserialize(const bytes &src);

private:
struct Serializable : ssz::ssz_variable_size_container {
Address m_accountAddress;
ssz::list<Transaction::Serializable, 100> m_transactions;

SSZ_CONT(m_accountAddress, m_transactions)

Serializable() {}

Serializable(const AccountBlock &account_block);
};

AccountBlock(const Serializable &s);
};
} // namespace data_types

#endif // ZKEMV_FRAMEWORK_LIBS_DATA_TYPES_INCLUDE_ZKEVM_FRAMEWORK_DATA_TYPES_ACCOUNT_BLOCK_HPP_
41 changes: 17 additions & 24 deletions libs/data_types/include/zkevm_framework/data_types/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@
#ifndef ZKEMV_FRAMEWORK_LIBS_DATA_TYPES_INCLUDE_ZKEVM_FRAMEWORK_DATA_TYPES_BLOCK_HPP_
#define ZKEMV_FRAMEWORK_LIBS_DATA_TYPES_INCLUDE_ZKEVM_FRAMEWORK_DATA_TYPES_BLOCK_HPP_

#include <vector>

#include "sszpp/ssz++.hpp"
#include "zkevm_framework/data_types/account_block.hpp"
#include "zkevm_framework/data_types/base.hpp"
#include "zkevm_framework/data_types/block_header.hpp"
#include "zkevm_framework/data_types/transaction.hpp"
#include "zkevm_framework/data_types/transaction_receipt.hpp"
#include "zkevm_framework/data_types/message.hpp"
#include "zkevm_framework/data_types/mpt.hpp"

namespace data_types {
/// @brief Block.
class Block {
public:
/// @brief empty block
Block() {}
MPTNode<AccountBlock> m_accountBlocks;
InMsg m_inputMsg;
OutMsg m_outputMsg;
BlockHeader m_previousBlock;
BlockHeader m_currentBlock;

Block(const std::vector<Transaction> &transactions,
const std::vector<TransactionReceipt> &receipts, const BlockHeader &previousBlock,
Block(const MPTNode<AccountBlock> &accountBlocks, const InMsg &inputMsg,
const OutMsg &outputMsg, const BlockHeader &previousBlock,
const BlockHeader &currentBlock)
: m_transactions(transactions),
m_receipts(receipts),
: m_accountBlocks(accountBlocks),
m_inputMsg(inputMsg),
m_outputMsg(outputMsg),
m_previousBlock(previousBlock),
m_currentBlock(currentBlock) {}

Expand All @@ -35,25 +38,15 @@ namespace data_types {
/// @brief deserizalize from SSZ
static Block deserialize(const bytes &src);

std::vector<Transaction> const &getTransactions() const { return m_transactions; }
std::vector<TransactionReceipt> const &getReceipts() const { return m_receipts; }
BlockHeader const &getCurrentBlock() const { return m_currentBlock; }
BlockHeader const &getPreviousBlock() const { return m_previousBlock; }

private:
std::vector<Transaction> m_transactions;
std::vector<TransactionReceipt> m_receipts;
BlockHeader m_previousBlock;
BlockHeader m_currentBlock;

/// @brief Serializable mirroring structure of `Block`.
struct Serializable : ssz::ssz_variable_size_container {
ssz::list<Transaction::Serializable, 100> m_transactions;
ssz::list<TransactionReceipt::Serializable, 100> m_receipts;
ssz::list<AccountBlock::Serializable, 100> m_accountBlocks;
InMsg::Serializable m_inputMsg;
OutMsg::Serializable m_outputMsg;
BlockHeader::Serializable m_previousBlock;
BlockHeader::Serializable m_currentBlock;

SSZ_CONT(m_transactions, m_receipts, m_previousBlock, m_currentBlock)
SSZ_CONT(m_accountBlocks, m_inputMsg, m_outputMsg, m_previousBlock, m_currentBlock)

Serializable() {}

Expand Down
62 changes: 12 additions & 50 deletions libs/data_types/include/zkevm_framework/data_types/block_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#ifndef ZKEMV_FRAMEWORK_LIBS_DATA_TYPES_INCLUDE_ZKEVM_FRAMEWORK_DATA_TYPES_BLOCK_HEADER_HPP_
#define ZKEMV_FRAMEWORK_LIBS_DATA_TYPES_INCLUDE_ZKEVM_FRAMEWORK_DATA_TYPES_BLOCK_HEADER_HPP_

#include <cstddef>
#include <cstdint>

#include "sszpp/ssz++.hpp"
Expand All @@ -19,39 +18,27 @@ namespace data_types {
public:
friend class Block;

const static size_t Invalid256 = 0; // TODO: define this

/// @brief Block data type.
enum BlockDataType { HeaderData, BlockData };

/// @brief empty block header
BlockHeader() {}
Hash m_parentHash;
size_t m_number;
size_t m_gasLimit;
size_t m_gasUsed;
bytes m_extraData;
size_t m_timestamp;

static constexpr size_t Invalid256 = 0; // TODO: define this

/// @brief block header from data
BlockHeader(Hash const& parentHash, Hash const& stateRoot, Hash const& transactionsRoot,
Hash const& receiptsRoot, size_t number, size_t gasLimit, size_t gasUsed,
const bytes& extraData, Address const& author, size_t timestamp = Invalid256)
BlockHeader(Hash const& parentHash, size_t number, size_t gasLimit, size_t gasUsed,
const bytes& extraData, size_t timestamp = Invalid256)
: m_parentHash(parentHash),
m_stateRoot(stateRoot),
m_transactionsRoot(transactionsRoot),
m_receiptsRoot(receiptsRoot),
m_number(number),
m_gasLimit(gasLimit),
m_gasUsed(gasUsed),
m_extraData(extraData),
m_timestamp(timestamp),
m_author(author) {}

Hash parentHash() const { return m_parentHash; }
size_t timestamp() const { return m_timestamp; }
Address const& author() const { return m_author; }
Hash stateRoot() const { return m_stateRoot; }
Hash transactionsRoot() const { return m_transactionsRoot; }
Hash receiptsRoot() const { return m_receiptsRoot; }
size_t gasUsed() const { return m_gasUsed; }
size_t number() const { return m_number; }
size_t gasLimit() const { return m_gasLimit; }
bytes extraData() const { return m_extraData; }
m_timestamp(timestamp) {}

/// @returns the SSZ serialization
bytes serialize() const;
Expand All @@ -60,58 +47,33 @@ namespace data_types {
static BlockHeader deserialize(const bytes& src);

private:
Hash m_parentHash;
Hash m_stateRoot;
Hash m_transactionsRoot;
Hash m_receiptsRoot;
size_t m_number;
size_t m_gasLimit;
size_t m_gasUsed;
bytes m_extraData;
size_t m_timestamp;
Address m_author;

/// @brief Serializable mirroring structure of `BlockHeader`.
struct Serializable : ssz::ssz_variable_size_container {
Hash m_parentHash;
Hash m_stateRoot;
Hash m_transactionsRoot;
Hash m_receiptsRoot;
size_t m_number;
size_t m_gasLimit;
size_t m_gasUsed;
ssz::list<std::byte, 100> m_extraData;
size_t m_timestamp;
Address m_author;

SSZ_CONT(m_parentHash, m_stateRoot, m_transactionsRoot, m_receiptsRoot, m_number,
m_gasLimit, m_gasUsed, m_extraData, m_timestamp, m_author)
SSZ_CONT(m_parentHash, m_number, m_gasLimit, m_gasUsed, m_extraData, m_timestamp)

Serializable() {}

Serializable(const BlockHeader& header)
: m_parentHash(header.m_parentHash),
m_stateRoot(header.m_stateRoot),
m_transactionsRoot(header.m_transactionsRoot),
m_receiptsRoot(header.m_receiptsRoot),
m_number(header.m_number),
m_gasLimit(header.m_gasLimit),
m_gasUsed(header.m_gasUsed),
m_timestamp(header.m_timestamp),
m_author(header.m_author),
m_extraData(header.m_extraData) {}
};

BlockHeader(const Serializable& s)
: m_parentHash(s.m_parentHash),
m_stateRoot(s.m_stateRoot),
m_transactionsRoot(s.m_transactionsRoot),
m_receiptsRoot(s.m_receiptsRoot),
m_number(s.m_number),
m_gasLimit(s.m_gasLimit),
m_gasUsed(s.m_gasUsed),
m_timestamp(s.m_timestamp),
m_author(s.m_author),
m_extraData(s.m_extraData.begin(), s.m_extraData.end()) {}
};
} // namespace data_types
Expand Down
Loading

0 comments on commit 5b2814d

Please sign in to comment.