Skip to content

Commit

Permalink
rm chain_id and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sergerad committed Aug 29, 2024
1 parent 3f5095b commit f91c8ad
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
6 changes: 6 additions & 0 deletions rollup/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ use crate::{Signature, SignedTransaction, Signer};
/// A block header containing metadata about the block.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BlockHeader {
/// The address of the sequencer that sealed the block.
pub sequencer: Address,
/// The number of the block.
pub number: u64,
/// The timestamp at the time the block was sealed.
pub timestamp: u64,
/// The hash of the parent block. None if this is the genesis block.
pub parent_digest: Option<String>,
/// The root digest of the withdrawals Merkle tree.
pub withdrawals_root: String,
/// The root digest of the transactions Merkle tree.
pub transactions_root: String,
}

Expand Down
3 changes: 3 additions & 0 deletions rollup/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use crate::{transaction::DynamicTxData, Block, WithdrawalTxData};

/// A blockchain containing a list of blocks and an incremental Merkle tree of withdrawals.
pub(crate) struct Blockchain {
/// The chain of blocks in the blockchain.
pub(crate) blocks: Vec<Block>,
/// The incremental Merkle tree of withdrawals.
pub(crate) withdrawals_tree: imt::Tree<sha2::Sha256>,
/// The incremental Merkle tree of transactions.
pub(crate) transactions_tree: imt::Tree<sha2::Sha256>,
}

Expand Down
20 changes: 12 additions & 8 deletions rollup/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ use crate::{
/// Permissioned entity responsible for maintaining the canonical [Blockchain].
/// Receives transactions directly and seals them into blocks.
pub struct Sequencer {
/// The sequencer's signer used to sign blocks.
signer: Signer,
/// The blockchain maintained by the sequencer.
blockchain: Blockchain,
pool: Vec<SignedTransaction>,
withdrawals: Vec<SignedTransaction>,
/// The pool of transactions to be included in the next block.
transactions_pool: Vec<SignedTransaction>,
/// The pool of withdrawal transactions to be included in the next block.
withdrawals_pool: Vec<SignedTransaction>,
}

impl Sequencer {
Expand All @@ -17,8 +21,8 @@ impl Sequencer {
Sequencer {
signer: signer.into(),
blockchain: Blockchain::default(),
pool: vec![],
withdrawals: vec![],
transactions_pool: vec![],
withdrawals_pool: vec![],
}
}

Expand All @@ -27,11 +31,11 @@ impl Sequencer {
match &transaction.transaction {
Transaction::Withdrawal(tx) => {
self.blockchain.withdraw(tx);
self.withdrawals.push(transaction);
self.withdrawals_pool.push(transaction);
}
Transaction::Dynamic(tx) => {
self.blockchain.transact(tx);
self.pool.push(transaction);
self.transactions_pool.push(transaction);
}
}
}
Expand All @@ -51,9 +55,9 @@ impl Sequencer {
// Drain the transaction pools and construct the block.
let block = Block::new(
SignedBlockHeader::new(header, &self.signer),
self.pool
self.transactions_pool
.drain(..)
.chain(self.withdrawals.drain(..))
.chain(self.withdrawals_pool.drain(..))
.collect(),
);
self.blockchain.push(block.clone());
Expand Down
3 changes: 3 additions & 0 deletions rollup/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ use std::str::FromStr;
/// A recoverable seckp256k1 signature.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Signature {
/// The r component of the signature.
pub r: U256,
/// The s component of the signature.
pub s: U256,
/// The recovery id of the signature.
pub recovery_id: i32,
}

Expand Down
11 changes: 8 additions & 3 deletions rollup/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ use crate::signer::{Signature, Signer};
/// A transaction header containing metadata about the transaction.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TransactionHeader {
chain_id: u64,
/// The address of the sender of the transaction.
sender: Address,
/// The address of the recipient of the transaction.
recipient: Address,
/// The amount of value transferred by the transaction.
amount: u64,
}

/// A dynamic transaction containing a transaction header and dynamic fee data.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DynamicTxData {
/// The transaction header.
header: TransactionHeader,
/// The maximum fee per gas that the sender is willing to pay.
max_fee_per_gas: u64,
/// The maximum priority fee per gas that the sender is willing to pay.
max_priority_fee_per_gas: u64,
}

Expand All @@ -32,7 +37,9 @@ impl DynamicTxData {
/// A withdrawal transaction containing a transaction header and destination.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct WithdrawalTxData {
/// The transaction header.
header: TransactionHeader,
/// The destination chain of the withdrawal.
dest_chain: u64,
}

Expand All @@ -58,7 +65,6 @@ impl Transaction {
pub fn dynamic(sender: Address, amount: u64) -> Self {
Transaction::Dynamic(DynamicTxData {
header: TransactionHeader {
chain_id: 1,
sender,
recipient: Address::random(),
amount,
Expand All @@ -72,7 +78,6 @@ impl Transaction {
pub fn withdrawal(sender: Address, amount: u64, dest_chain: u64) -> Self {
Transaction::Withdrawal(WithdrawalTxData {
header: TransactionHeader {
chain_id: 1,
sender,
recipient: sender,
amount,
Expand Down

0 comments on commit f91c8ad

Please sign in to comment.