diff --git a/rollup/src/block.rs b/rollup/src/block.rs index e335d39..452de7b 100644 --- a/rollup/src/block.rs +++ b/rollup/src/block.rs @@ -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()); + } +} diff --git a/rollup/src/signer.rs b/rollup/src/signer.rs index 77aa2b8..955e206 100644 --- a/rollup/src/signer.rs +++ b/rollup/src/signer.rs @@ -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); + } +} diff --git a/rollup/src/transaction.rs b/rollup/src/transaction.rs index 6067f7c..51d0cc2 100644 --- a/rollup/src/transaction.rs +++ b/rollup/src/transaction.rs @@ -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()); + } +}