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

Update data types #18

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions libs/data_types/include/zkevm_framework/data_types/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ namespace data_types {
class Block {
public:
MPTNode<AccountBlock> m_accountBlocks;
InMsg m_inputMsg;
OutMsg m_outputMsg;
MPTNode<InMsg> m_inputMsgs;
MPTNode<OutMsg> m_outputMsgs;
BlockHeader m_previousBlock;
BlockHeader m_currentBlock;

Block(const MPTNode<AccountBlock> &accountBlocks, const InMsg &inputMsg,
const OutMsg &outputMsg, const BlockHeader &previousBlock,
Block(const MPTNode<AccountBlock> &accountBlocks, const MPTNode<InMsg> &inputMsgs,
const MPTNode<OutMsg> &outputMsgs, const BlockHeader &previousBlock,
const BlockHeader &currentBlock)
: m_accountBlocks(accountBlocks),
m_inputMsg(inputMsg),
m_outputMsg(outputMsg),
m_inputMsgs(inputMsgs),
m_outputMsgs(outputMsgs),
m_previousBlock(previousBlock),
m_currentBlock(currentBlock) {}

Expand All @@ -41,12 +41,12 @@ namespace data_types {
private:
struct Serializable : ssz::ssz_variable_size_container {
ssz::list<AccountBlock::Serializable, 100> m_accountBlocks;
InMsg::Serializable m_inputMsg;
OutMsg::Serializable m_outputMsg;
ssz::list<InMsg::Serializable, 100> m_inputMsgs;
ssz::list<OutMsg::Serializable, 100> m_outputMsgs;
BlockHeader::Serializable m_previousBlock;
BlockHeader::Serializable m_currentBlock;

SSZ_CONT(m_accountBlocks, m_inputMsg, m_outputMsg, m_previousBlock, m_currentBlock)
SSZ_CONT(m_accountBlocks, m_inputMsgs, m_outputMsgs, m_previousBlock, m_currentBlock)

Serializable() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,29 @@ namespace data_types {
size_t m_number;
size_t m_gasLimit;
size_t m_gasUsed;
Address m_coinbase;
size_t m_prevrandao;
size_t m_chain_id;
size_t m_basefee;
size_t m_blob_basefee;
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, size_t number, size_t gasLimit, size_t gasUsed,
const bytes& extraData, size_t timestamp = Invalid256)
Address coinbase, size_t prevrandao, size_t chain_id, size_t basefee,
size_t blob_basefee, const bytes& extraData, size_t timestamp = Invalid256)
: m_parentHash(parentHash),
m_number(number),
m_gasLimit(gasLimit),
m_gasUsed(gasUsed),
m_coinbase(coinbase),
m_prevrandao(prevrandao),
m_chain_id(chain_id),
m_basefee(basefee),
m_blob_basefee(blob_basefee),
m_extraData(extraData),
m_timestamp(timestamp) {}

Expand All @@ -52,10 +63,16 @@ namespace data_types {
size_t m_number;
size_t m_gasLimit;
size_t m_gasUsed;
Address m_coinbase;
size_t m_prevrandao;
size_t m_chain_id;
size_t m_basefee;
size_t m_blob_basefee;
ssz::list<std::byte, 100> m_extraData;
size_t m_timestamp;

SSZ_CONT(m_parentHash, m_number, m_gasLimit, m_gasUsed, m_extraData, m_timestamp)
SSZ_CONT(m_parentHash, m_number, m_gasLimit, m_gasUsed, m_coinbase, m_prevrandao,
m_chain_id, m_basefee, m_blob_basefee, m_extraData, m_timestamp)

Serializable() {}

Expand All @@ -64,17 +81,27 @@ namespace data_types {
m_number(header.m_number),
m_gasLimit(header.m_gasLimit),
m_gasUsed(header.m_gasUsed),
m_timestamp(header.m_timestamp),
m_extraData(header.m_extraData) {}
m_coinbase(header.m_coinbase),
m_prevrandao(header.m_prevrandao),
m_chain_id(header.m_chain_id),
m_basefee(header.m_basefee),
m_blob_basefee(header.m_basefee),
m_extraData(header.m_extraData),
m_timestamp(header.m_timestamp) {}
};

BlockHeader(const Serializable& s)
: m_parentHash(s.m_parentHash),
m_number(s.m_number),
m_gasLimit(s.m_gasLimit),
m_gasUsed(s.m_gasUsed),
m_timestamp(s.m_timestamp),
m_extraData(s.m_extraData.begin(), s.m_extraData.end()) {}
m_coinbase(s.m_coinbase),
m_prevrandao(s.m_prevrandao),
m_chain_id(s.m_chain_id),
m_basefee(s.m_basefee),
m_blob_basefee(s.m_basefee),
m_extraData(s.m_extraData.begin(), s.m_extraData.end()),
m_timestamp(s.m_timestamp) {}
};
} // namespace data_types

Expand Down
28 changes: 18 additions & 10 deletions libs/data_types/include/zkevm_framework/data_types/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

namespace data_types {
class CommonMsgInfo {
Expand Down Expand Up @@ -50,8 +51,10 @@ namespace data_types {
friend class Block;

CommonMsgInfo m_info;
Transaction m_transaction;

InMsg(CommonMsgInfo info) : m_info(info) {}
InMsg(CommonMsgInfo info, Transaction transaction)
: m_info(info), m_transaction(transaction) {}

/// @returns the SSZ serialisation
bytes serialize() const;
Expand All @@ -60,17 +63,18 @@ namespace data_types {
static InMsg deserialize(const bytes& src);

private:
struct Serializable : ssz::ssz_container {
struct Serializable : ssz::ssz_variable_size_container {
CommonMsgInfo::Serializable m_info;
Transaction::Serializable m_transaction;

SSZ_CONT(m_info)
SSZ_CONT(m_info, m_transaction)

Serializable() {}

Serializable(const InMsg& info) : m_info(info.m_info) {}
Serializable(const InMsg& msg) : m_info(msg.m_info), m_transaction(msg.m_transaction) {}
};

InMsg(const Serializable& s) : m_info(s.m_info) {}
InMsg(const Serializable& s) : m_info(s.m_info), m_transaction(s.m_transaction) {}
};

class OutMsg {
Expand All @@ -79,8 +83,10 @@ namespace data_types {
friend class State;

CommonMsgInfo m_info;
Transaction m_transaction;

OutMsg(CommonMsgInfo info) : m_info(info) {}
OutMsg(CommonMsgInfo info, Transaction transaction)
: m_info(info), m_transaction(transaction) {}

/// @returns the SSZ serialisation
bytes serialize() const;
Expand All @@ -89,17 +95,19 @@ namespace data_types {
static OutMsg deserialize(const bytes& src);

private:
struct Serializable : ssz::ssz_container {
struct Serializable : ssz::ssz_variable_size_container {
CommonMsgInfo::Serializable m_info;
Transaction::Serializable m_transaction;

SSZ_CONT(m_info)
SSZ_CONT(m_info, m_transaction)

Serializable() {}

Serializable(const OutMsg& info) : m_info(info.m_info) {}
Serializable(const OutMsg& msg)
: m_info(msg.m_info), m_transaction(msg.m_transaction) {}
};

OutMsg(const Serializable& s) : m_info(s.m_info) {}
OutMsg(const Serializable& s) : m_info(s.m_info), m_transaction(s.m_transaction) {}
};
} // namespace data_types

Expand Down
19 changes: 13 additions & 6 deletions libs/data_types/include/zkevm_framework/data_types/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ namespace data_types {
class Transaction {
public:
friend class AccountBlock;
friend class InMsg;
friend class OutMsg;

/// @brief Transaction type
enum Type : uint8_t { NullTransaction, ContractCreation, MessageCall };

size_t m_id;
Type m_type;
size_t m_nonce;
size_t m_value;
Expand All @@ -30,9 +33,10 @@ namespace data_types {
Address m_sender;

/// @brief an unsigned contract-creation transaction
Transaction(Type type, size_t nonce, size_t value, Address receiveAddress, size_t gasPrice,
size_t gas, bytes const& data, Address sender)
: m_value(value),
Transaction(size_t id, Type type, size_t nonce, size_t value, Address receiveAddress,
size_t gasPrice, size_t gas, bytes const& data, Address sender)
: m_id(id),
m_value(value),
m_gasPrice(gasPrice),
m_gas(gas),
m_data(data),
Expand All @@ -49,6 +53,7 @@ namespace data_types {

private:
struct Serializable : ssz::ssz_variable_size_container {
size_t m_id;
uint8_t m_type;
size_t m_nonce;
size_t m_value;
Expand All @@ -58,13 +63,14 @@ namespace data_types {
ssz::list<std::byte, 100> m_data;
Address m_sender;

SSZ_CONT(m_type, m_nonce, m_value, m_receiveAddress, m_gasPrice, m_gas, m_data,
SSZ_CONT(m_id, m_type, m_nonce, m_value, m_receiveAddress, m_gasPrice, m_gas, m_data,
m_sender)

Serializable() {}

Serializable(const Transaction& transaction)
: m_nonce(transaction.m_nonce),
: m_id(transaction.m_id),
m_nonce(transaction.m_nonce),
m_value(transaction.m_value),
m_receiveAddress(transaction.m_receiveAddress),
m_gasPrice(transaction.m_gasPrice),
Expand All @@ -75,7 +81,8 @@ namespace data_types {
};

Transaction(const Serializable& s)
: m_nonce(s.m_nonce),
: m_id(s.m_id),
m_nonce(s.m_nonce),
m_value(s.m_value),
m_receiveAddress(s.m_receiveAddress),
m_gasPrice(s.m_gasPrice),
Expand Down
22 changes: 14 additions & 8 deletions libs/data_types/src/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@

namespace data_types {
Block::Serializable::Serializable(const Block& block)
: m_previousBlock(block.m_previousBlock),
m_currentBlock(block.m_currentBlock),
m_inputMsg(block.m_inputMsg),
m_outputMsg(block.m_outputMsg) {
: m_previousBlock(block.m_previousBlock), m_currentBlock(block.m_currentBlock) {
for (const AccountBlock& account_block : block.m_accountBlocks) {
m_accountBlocks.push_back(AccountBlock::Serializable(account_block));
}
for (const InMsg& in_msg : block.m_inputMsgs) {
m_inputMsgs.push_back(InMsg::Serializable(in_msg));
}
for (const OutMsg& out_msg : block.m_outputMsgs) {
m_outputMsgs.push_back(OutMsg::Serializable(out_msg));
}
}

Block::Block(const Block::Serializable& s)
: m_previousBlock(s.m_previousBlock),
m_currentBlock(s.m_currentBlock),
m_inputMsg(s.m_inputMsg),
m_outputMsg(s.m_outputMsg) {
: m_previousBlock(s.m_previousBlock), m_currentBlock(s.m_currentBlock) {
for (const AccountBlock::Serializable& account_block : s.m_accountBlocks) {
m_accountBlocks.push_back(AccountBlock(account_block));
}
for (const InMsg& in_msg : s.m_inputMsgs) {
m_inputMsgs.push_back(InMsg(in_msg));
}
for (const OutMsg& out_msg : s.m_outputMsgs) {
m_outputMsgs.push_back(OutMsg(out_msg));
}
}

bytes Block::serialize() const { return ssz::serialize<Block::Serializable>(*this); }
Expand Down
3 changes: 2 additions & 1 deletion tests/libs/data_types/test_account_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TEST(DataTypesAccountTests, SerializeDeserializeAccount) {
Address addr = {0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A};
bytes data = {std::byte{0xAA}, std::byte{0xBB}, std::byte{0xCC}};
Transaction transaction(Transaction::Type::ContractCreation, 1, 2, addr, 3, 4, data, addr);
Transaction transaction(0, Transaction::Type::ContractCreation, 1, 2, addr, 3, 4, data, addr);
MPTNode<Transaction> transactions = {};
transactions.push_back(transaction);
AccountBlock acc(addr, transactions);
Expand All @@ -23,6 +23,7 @@ TEST(DataTypesAccountTests, SerializeDeserializeAccount) {
EXPECT_EQ(result.m_transactions.size(), 1);

Transaction t = result.m_transactions[0];
EXPECT_EQ(t.m_id, 0);
EXPECT_EQ(t.m_type, Transaction::Type::ContractCreation);
EXPECT_EQ(t.m_nonce, 1);
EXPECT_EQ(t.m_value, 2);
Expand Down
Loading
Loading