Skip to content

Commit

Permalink
Add sequencer unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sergerad committed Aug 30, 2024
1 parent aa8c826 commit 4f9208a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion rollup/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};

/// A newtype wrapper around an Ethereum address.
/// Allows conversion from a public key.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Address(AlloyAddress);

impl From<PublicKey> for Address {
Expand Down
8 changes: 4 additions & 4 deletions rollup/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use crate::{Address, Signature, SignedTransaction, Signer};

/// A block header containing metadata about the block.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct BlockHeader {
/// The address of the sequencer that sealed the block.
pub sequencer: Address,
Expand All @@ -30,7 +30,7 @@ impl BlockHeader {
}

/// A signed block header containing a block header and a signature.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct SignedBlockHeader {
header: BlockHeader,
signature: Signature,
Expand All @@ -45,10 +45,10 @@ impl SignedBlockHeader {
}

/// A block containing a header and a list of transactions.
#[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)]
pub struct Block {
pub(crate) signed: SignedBlockHeader,
transactions: Vec<SignedTransaction>,
pub(crate) transactions: Vec<SignedTransaction>,
}

impl std::fmt::Debug for Block {
Expand Down
4 changes: 2 additions & 2 deletions rollup/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ impl Default for Blockchain {

impl Blockchain {
/// Returns the head block of the blockchain.
pub(crate) fn head(&self) -> Option<&Block> {
self.blocks.last()
pub(crate) fn head(&self) -> Option<Block> {
self.blocks.last().cloned()
}

/// Returns the height of the blockchain.
Expand Down
31 changes: 30 additions & 1 deletion rollup/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Sequencer {
}

/// Returns the head block of the blockchain.
pub fn head(&self) -> Option<&Block> {
pub fn head(&self) -> Option<Block> {
self.blockchain.head()
}
}
Expand Down Expand Up @@ -120,3 +120,32 @@ impl Future for ArcSequencer {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_sequencer() {
// Create a sequencer.
let signer = Signer::random();
let sequencer = ArcSequencer::new(signer);
let mut sequencer = sequencer.lock().await;

// Add a transaction to the sequencer.
let transaction = SignedTransaction::new(
Transaction::dynamic(sequencer.signer.address, 100),
&sequencer.signer,
);
sequencer.add_transaction(transaction.clone());

// Seal the block.
let block = sequencer.seal();

// Validate the block.
assert_eq!(block.transactions.len(), 1);
assert_eq!(block.transactions[0], transaction);
assert_eq!(sequencer.head().unwrap(), block);
assert!(block.verify());
}
}
2 changes: 1 addition & 1 deletion rollup/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::str::FromStr;
use crate::Address;

/// A recoverable seckp256k1 signature.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct Signature {
/// The r component of the signature.
pub r: U256,
Expand Down
10 changes: 5 additions & 5 deletions rollup/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::signer::{Signature, Signer};
use crate::{Address, CHAIN_ID};

/// A transaction header containing metadata about the transaction.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct TransactionHeader {
/// The identifier of the chain on which the transaction was intended to be executed.
chain_id: u64,
Expand All @@ -19,7 +19,7 @@ pub struct TransactionHeader {
}

/// A dynamic transaction containing a transaction header and dynamic fee data.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct DynamicTxData {
/// The transaction header.
header: TransactionHeader,
Expand All @@ -38,7 +38,7 @@ impl DynamicTxData {
}

/// A withdrawal transaction containing a transaction header and destination.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct WithdrawalTxData {
/// The transaction header.
header: TransactionHeader,
Expand All @@ -55,7 +55,7 @@ impl WithdrawalTxData {
}

/// A transaction containing either dynamic or withdrawal transaction data.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub enum Transaction {
Dynamic(DynamicTxData),
Withdrawal(WithdrawalTxData),
Expand Down Expand Up @@ -109,7 +109,7 @@ impl Transaction {
}

/// A signed transaction containing a transaction and signature.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct SignedTransaction {
pub transaction: Transaction,
pub signature: Signature,
Expand Down

0 comments on commit 4f9208a

Please sign in to comment.