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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use ethabi;
use ethabi::ParamType;
use ethereum_types::{H160, Address, U256};
use error::Error;
use machine::WithRewards;
use parity_machine::{Machine, WithBalances};
use trace;
use super::SystemCall;
use_contract!(block_reward_contract, "BlockReward", "res/contracts/block_reward.json");
#[repr(u8)]
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum RewardKind {
Author = 0,
Uncle = 1,
EmptyStep = 2,
External = 3,
}
impl From<RewardKind> for u16 {
fn from(reward_kind: RewardKind) -> Self {
reward_kind as u16
}
}
impl Into<trace::RewardType> for RewardKind {
fn into(self) -> trace::RewardType {
match self {
RewardKind::Author => trace::RewardType::Block,
RewardKind::Uncle => trace::RewardType::Uncle,
RewardKind::EmptyStep => trace::RewardType::EmptyStep,
RewardKind::External => trace::RewardType::External,
}
}
}
pub struct BlockRewardContract {
address: Address,
block_reward_contract: block_reward_contract::BlockReward,
}
impl BlockRewardContract {
pub fn new(address: Address) -> BlockRewardContract {
BlockRewardContract {
address,
block_reward_contract: block_reward_contract::BlockReward::default(),
}
}
pub fn reward(
&self,
benefactors: &[(Address, RewardKind)],
caller: &mut SystemCall,
) -> Result<Vec<(Address, U256)>, Error> {
let reward = self.block_reward_contract.functions().reward();
let input = reward.input(
benefactors.iter().map(|&(address, _)| H160::from(address)),
benefactors.iter().map(|&(_, ref reward_kind)| u16::from(*reward_kind)),
);
let output = caller(self.address, input)
.map_err(Into::into)
.map_err(::engines::EngineError::FailedSystemCall)?;
let types = &[
ParamType::Array(Box::new(ParamType::Address)),
ParamType::Array(Box::new(ParamType::Uint(256))),
];
let tokens = ethabi::decode(types, &output)
.map_err(|err| err.to_string())
.map_err(::engines::EngineError::FailedSystemCall)?;
assert!(tokens.len() == 2);
let addresses = tokens[0].clone().to_array().expect("type checked by ethabi::decode; qed");
let rewards = tokens[1].clone().to_array().expect("type checked by ethabi::decode; qed");
if addresses.len() != rewards.len() {
return Err(::engines::EngineError::FailedSystemCall(
"invalid data returned by reward contract: both arrays must have the same size".into()
).into());
}
let addresses = addresses.into_iter().map(|t| t.to_address().expect("type checked by ethabi::decode; qed"));
let rewards = rewards.into_iter().map(|t| t.to_uint().expect("type checked by ethabi::decode; qed"));
Ok(addresses.zip(rewards).collect())
}
}
pub fn apply_block_rewards<M: Machine + WithBalances + WithRewards>(
rewards: &[(Address, RewardKind, U256)],
block: &mut M::LiveBlock,
machine: &M,
) -> Result<(), M::Error> {
for &(ref author, _, ref block_reward) in rewards {
machine.add_balance(block, author, block_reward)?;
}
let rewards: Vec<_> = rewards.into_iter().map(|&(a, k, r)| (a, k.into(), r)).collect();
machine.note_rewards(block, &rewards)
}
#[cfg(test)]
mod test {
use client::PrepareOpenBlock;
use ethereum_types::U256;
use spec::Spec;
use test_helpers::generate_dummy_client_with_spec_and_accounts;
use super::{BlockRewardContract, RewardKind};
#[test]
fn block_reward_contract() {
let client = generate_dummy_client_with_spec_and_accounts(
Spec::new_test_round_block_reward_contract,
None,
);
let machine = Spec::new_test_machine();
let block_reward_contract = BlockRewardContract::new(
"0000000000000000000000000000000000000042".into(),
);
let mut call = |to, data| {
let mut block = client.prepare_open_block(
"0000000000000000000000000000000000000001".into(),
(3141562.into(), 31415620.into()),
vec![],
);
let result = machine.execute_as_system(
block.block_mut(),
to,
U256::max_value(),
Some(data),
);
result.map_err(|e| format!("{}", e))
};
assert!(block_reward_contract.reward(&vec![], &mut call).unwrap().is_empty());
let benefactors = vec![
("0000000000000000000000000000000000000033".into(), RewardKind::Author),
("0000000000000000000000000000000000000034".into(), RewardKind::Uncle),
("0000000000000000000000000000000000000035".into(), RewardKind::EmptyStep),
];
let rewards = block_reward_contract.reward(&benefactors, &mut call).unwrap();
let expected = vec![
("0000000000000000000000000000000000000033".into(), U256::from(1000)),
("0000000000000000000000000000000000000034".into(), U256::from(1000 + 1)),
("0000000000000000000000000000000000000035".into(), U256::from(1000 + 2)),
];
assert_eq!(expected, rewards);
}
}