Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergerad committed Aug 30, 2024
1 parent 4f9208a commit f29ffa8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rollup/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,27 @@ impl Block {
self.signed.header.number
}
}

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

#[test]
fn test_block_verify() {
let signer = Signer::random();
let header = BlockHeader {
sequencer: signer.address,
number: 0,
timestamp: 0,
parent_digest: None,
withdrawals_root: "0".to_string(),
transactions_root: "0".to_string(),
};
let hash = header.hash();
assert_eq!(hash, header.hash());

let signed = SignedBlockHeader::new(header.clone(), &signer);
let block = Block::new(signed, vec![]);
assert!(block.verify());
}
}
18 changes: 18 additions & 0 deletions rollup/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,21 @@ impl Signer {
}
}
}

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

#[test]
fn test_signer() {
// Sign a digest and recover the public key.
let signer = Signer::random();
let digest = [0u8; 32];
let signature = signer.sign(digest);
let secp = Secp256k1::new();
let msg = Message::from_digest(digest);
let pk = secp.recover_ecdsa(&msg, &(&signature).into()).unwrap();
let address = Address::from(pk);
assert_eq!(address, signer.address);
}
}
19 changes: 19 additions & 0 deletions rollup/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,22 @@ impl SignedTransaction {
&& self.transaction.sender() == address
}
}

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

#[test]
fn test_transaction() {
// Create a dynamic transaction and verify.
let signer = Signer::random();
let tx = Transaction::dynamic(signer.address, 100);
let tx = SignedTransaction::new(tx.clone(), &signer);
assert!(tx.verify());

// Create a withdrawal transaction and verify.
let tx = Transaction::withdrawal(signer.address, 100, 1);
let tx = SignedTransaction::new(tx.clone(), &signer);
assert!(tx.verify());
}
}

0 comments on commit f29ffa8

Please sign in to comment.