Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit 673cde6

Browse files
committed
add block tests
1 parent 49ba117 commit 673cde6

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

ethcore/src/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use trace::Trace;
2626
use evm::Factory as EvmFactory;
2727

2828
/// A block, encoded as it is on the block chain.
29-
#[derive(Default, Debug, Clone)]
29+
#[derive(Default, Debug, Clone, PartialEq)]
3030
pub struct Block {
3131
/// The header of this block.
3232
pub header: Header,

ethcore/src/snapshot/block.rs

+72
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,76 @@ impl AbridgedBlock {
136136
uncles: uncles,
137137
})
138138
}
139+
}
140+
141+
#[cfg(test)]
142+
mod tests {
143+
use views::BlockView;
144+
use block::Block;
145+
use super::AbridgedBlock;
146+
use types::transaction::{Action, Transaction};
147+
148+
use util::numbers::U256;
149+
use util::hash::{Address, H256, FixedHash};
150+
use util::{Bytes, RlpStream, Stream};
151+
152+
fn encode_block(b: &Block) -> Bytes {
153+
let mut s = RlpStream::new_list(3);
154+
155+
b.header.stream_rlp(&mut s, ::basic_types::Seal::With);
156+
s.append(&b.transactions);
157+
s.append(&b.uncles);
158+
159+
s.out()
160+
}
161+
162+
#[test]
163+
fn empty_block_abridging() {
164+
let b = Block::default();
165+
let encoded = encode_block(&b);
166+
167+
let abridged = AbridgedBlock::from_block_view(&BlockView::new(&encoded));
168+
assert_eq!(abridged.to_block(H256::new(), 0).unwrap(), b);
169+
}
170+
171+
#[test]
172+
#[should_panic]
173+
fn wrong_number() {
174+
let b = Block::default();
175+
let encoded = encode_block(&b);
176+
177+
let abridged = AbridgedBlock::from_block_view(&BlockView::new(&encoded));
178+
assert_eq!(abridged.to_block(H256::new(), 2).unwrap(), b);
179+
}
180+
181+
#[test]
182+
fn with_transactions() {
183+
let mut b = Block::default();
184+
185+
let t1 = Transaction {
186+
action: Action::Create,
187+
nonce: U256::from(42),
188+
gas_price: U256::from(3000),
189+
gas: U256::from(50_000),
190+
value: U256::from(1),
191+
data: b"Hello!".to_vec()
192+
}.fake_sign(Address::from(0x69));
193+
194+
let t2 = Transaction {
195+
action: Action::Create,
196+
nonce: U256::from(88),
197+
gas_price: U256::from(12345),
198+
gas: U256::from(300000),
199+
value: U256::from(1000000000),
200+
data: "Eep!".into(),
201+
}.fake_sign(Address::from(0x55));
202+
203+
b.transactions.push(t1);
204+
b.transactions.push(t2);
205+
206+
let encoded = encode_block(&b);
207+
208+
let abridged = AbridgedBlock::from_block_view(&BlockView::new(&encoded[..]));
209+
assert_eq!(abridged.to_block(H256::new(), 0).unwrap(), b);
210+
}
139211
}

0 commit comments

Comments
 (0)