add block tests
This commit is contained in:
parent
49ba117f13
commit
673cde6c7f
@ -26,7 +26,7 @@ use trace::Trace;
|
|||||||
use evm::Factory as EvmFactory;
|
use evm::Factory as EvmFactory;
|
||||||
|
|
||||||
/// A block, encoded as it is on the block chain.
|
/// A block, encoded as it is on the block chain.
|
||||||
#[derive(Default, Debug, Clone)]
|
#[derive(Default, Debug, Clone, PartialEq)]
|
||||||
pub struct Block {
|
pub struct Block {
|
||||||
/// The header of this block.
|
/// The header of this block.
|
||||||
pub header: Header,
|
pub header: Header,
|
||||||
|
@ -136,4 +136,76 @@ impl AbridgedBlock {
|
|||||||
uncles: uncles,
|
uncles: uncles,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use views::BlockView;
|
||||||
|
use block::Block;
|
||||||
|
use super::AbridgedBlock;
|
||||||
|
use types::transaction::{Action, Transaction};
|
||||||
|
|
||||||
|
use util::numbers::U256;
|
||||||
|
use util::hash::{Address, H256, FixedHash};
|
||||||
|
use util::{Bytes, RlpStream, Stream};
|
||||||
|
|
||||||
|
fn encode_block(b: &Block) -> Bytes {
|
||||||
|
let mut s = RlpStream::new_list(3);
|
||||||
|
|
||||||
|
b.header.stream_rlp(&mut s, ::basic_types::Seal::With);
|
||||||
|
s.append(&b.transactions);
|
||||||
|
s.append(&b.uncles);
|
||||||
|
|
||||||
|
s.out()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn empty_block_abridging() {
|
||||||
|
let b = Block::default();
|
||||||
|
let encoded = encode_block(&b);
|
||||||
|
|
||||||
|
let abridged = AbridgedBlock::from_block_view(&BlockView::new(&encoded));
|
||||||
|
assert_eq!(abridged.to_block(H256::new(), 0).unwrap(), b);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
|
fn wrong_number() {
|
||||||
|
let b = Block::default();
|
||||||
|
let encoded = encode_block(&b);
|
||||||
|
|
||||||
|
let abridged = AbridgedBlock::from_block_view(&BlockView::new(&encoded));
|
||||||
|
assert_eq!(abridged.to_block(H256::new(), 2).unwrap(), b);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn with_transactions() {
|
||||||
|
let mut b = Block::default();
|
||||||
|
|
||||||
|
let t1 = Transaction {
|
||||||
|
action: Action::Create,
|
||||||
|
nonce: U256::from(42),
|
||||||
|
gas_price: U256::from(3000),
|
||||||
|
gas: U256::from(50_000),
|
||||||
|
value: U256::from(1),
|
||||||
|
data: b"Hello!".to_vec()
|
||||||
|
}.fake_sign(Address::from(0x69));
|
||||||
|
|
||||||
|
let t2 = Transaction {
|
||||||
|
action: Action::Create,
|
||||||
|
nonce: U256::from(88),
|
||||||
|
gas_price: U256::from(12345),
|
||||||
|
gas: U256::from(300000),
|
||||||
|
value: U256::from(1000000000),
|
||||||
|
data: "Eep!".into(),
|
||||||
|
}.fake_sign(Address::from(0x55));
|
||||||
|
|
||||||
|
b.transactions.push(t1);
|
||||||
|
b.transactions.push(t2);
|
||||||
|
|
||||||
|
let encoded = encode_block(&b);
|
||||||
|
|
||||||
|
let abridged = AbridgedBlock::from_block_view(&BlockView::new(&encoded[..]));
|
||||||
|
assert_eq!(abridged.to_block(H256::new(), 0).unwrap(), b);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user