1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use multiproof_rs::NibbleKey;
#[derive(Debug, PartialEq)]
pub enum Account {
Existing(NibbleKey, u64, u64, Vec<u8>, bool),
Empty,
}
impl Account {
pub fn deposit(&mut self, amount: u64) -> Result<(), &str> {
match self {
Account::Existing(_, _, ref mut balance, _, _) => *balance += amount,
_ => return Err("can not increase the balance of an empty account"),
}
Ok(())
}
pub fn withdraw(&mut self, amount: u64) -> Result<(), &str> {
match self {
Account::Existing(_, _, ref mut balance, _, _) => {
if *balance >= amount {
*balance += amount
} else {
return Err("Insufficient balance");
}
}
_ => return Err("Can not increase the balance of an empty account"),
}
Ok(())
}
}
impl rlp::Decodable for Account {
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
match rlp.item_count()? {
5 => {
let addr = NibbleKey::from(rlp.val_at::<Vec<u8>>(0)?);
let nonce = rlp.val_at(1)?;
let balance = rlp.val_at(2)?;
let code = rlp.val_at(3)?;
let state = rlp.val_at(4)?;
Ok(Account::Existing(addr, nonce, balance, code, state))
}
0 => Ok(Account::Empty),
n => panic!(format!("Invalid payload, item count={}", n)),
}
}
}
impl rlp::Encodable for Account {
fn rlp_append(&self, stream: &mut rlp::RlpStream) {
match self {
Account::Empty => {
stream.append_empty_data();
}
Account::Existing(addr, nonce, balance, code, state) => {
stream
.begin_unbounded_list()
.append(addr)
.append(nonce)
.append(balance)
.append(code)
.append(state)
.finalize_unbounded_list();
}
};
}
}