@@ -162,82 +162,92 @@ impl ConsensusEngine for Tendermint {
162162 let block_author_reward = total_reward - total_min_fee + distributor. remaining_fee ( ) ;
163163
164164 let metadata = block. state ( ) . metadata ( ) ?. expect ( "Metadata must exist" ) ;
165- if metadata. current_term_id ( ) == 0 {
166- self . machine . add_balance ( block, & author, block_author_reward) ?;
165+ let term = metadata. current_term_id ( ) ;
166+ let term_seconds = match term {
167+ 0 => parent_common_params. term_seconds ( ) ,
168+ _ => term_common_params. expect ( "TermCommonParams should exist" ) . term_seconds ( ) ,
169+ } ;
167170
168- if is_term_changed ( block. header ( ) , parent_header, parent_common_params) {
169- // First term change
170- stake:: on_term_close ( block. state_mut ( ) , block_number, & [ ] ) ?;
171+ match term {
172+ 0 => {
173+ self . machine . add_balance ( block, & author, block_author_reward) ?;
174+ }
175+ _ => {
176+ stake:: update_validator_weights ( & mut block. state_mut ( ) , & author) ?;
177+ stake:: add_intermediate_rewards ( block. state_mut ( ) , author, block_author_reward) ?;
171178 }
172- return Ok ( ( ) )
173179 }
174180
175- stake:: update_validator_weights ( & mut block. state_mut ( ) , & author) ?;
176-
177- stake:: add_intermediate_rewards ( block. state_mut ( ) , author, block_author_reward) ?;
178-
179- let term_common_params = term_common_params. expect ( "TermCommonParams should exist" ) ;
180- if !is_term_changed ( block. header ( ) , parent_header, term_common_params) {
181+ if !is_term_changed ( block. header ( ) , parent_header, term_seconds) {
181182 return Ok ( ( ) )
182183 }
183- let rewards = stake:: drain_previous_rewards ( & mut block. state_mut ( ) ) ?;
184184
185- let start_of_the_current_term = metadata. last_term_finished_block_num ( ) + 1 ;
186- let client = self
187- . client
188- . read ( )
189- . as_ref ( )
190- . ok_or ( EngineError :: CannotOpenBlock ) ?
191- . upgrade ( )
192- . ok_or ( EngineError :: CannotOpenBlock ) ?;
193-
194- let inactive_validators = if metadata. current_term_id ( ) == 1 {
195- assert ! ( rewards. is_empty( ) ) ;
196-
197- let validators = stake:: Validators :: load_from_state ( block. state ( ) ) ?
198- . into_iter ( )
199- . map ( |val| public_to_address ( val. pubkey ( ) ) )
200- . collect ( ) ;
201- inactive_validators ( & * client, start_of_the_current_term, block. header ( ) , validators)
202- } else {
203- let start_of_the_previous_term = {
204- let end_of_the_two_level_previous_term =
205- client. last_term_finished_block_num ( ( metadata. last_term_finished_block_num ( ) - 1 ) . into ( ) ) . unwrap ( ) ;
206-
207- end_of_the_two_level_previous_term + 1
208- } ;
209-
210- let banned = stake:: Banned :: load_from_state ( block. state ( ) ) ?;
211- let start_of_the_current_term_header = if block_number == start_of_the_current_term {
212- encoded:: Header :: new ( block. header ( ) . clone ( ) . rlp_bytes ( ) . to_vec ( ) )
213- } else {
214- client. block_header ( & start_of_the_current_term. into ( ) ) . unwrap ( )
215- } ;
216-
217- let pending_rewards = calculate_pending_rewards_of_the_previous_term (
218- & * client,
219- & * self . validators ,
220- rewards,
221- start_of_the_current_term,
222- start_of_the_current_term_header,
223- start_of_the_previous_term,
224- & banned,
225- ) ?;
226-
227- for ( address, reward) in pending_rewards {
228- self . machine . add_balance ( block, & address, reward) ?;
185+ match term {
186+ 0 => {
187+ // First term change
188+ stake:: on_term_close ( block. state_mut ( ) , block_number, & [ ] ) ?;
229189 }
230-
231- let validators = stake:: Validators :: load_from_state ( block. state ( ) ) ?
232- . into_iter ( )
233- . map ( |val| public_to_address ( val. pubkey ( ) ) )
234- . collect ( ) ;
235- inactive_validators ( & * client, start_of_the_current_term, block. header ( ) , validators)
236- } ;
237-
238- stake:: move_current_to_previous_intermediate_rewards ( & mut block. state_mut ( ) ) ?;
239- stake:: on_term_close ( block. state_mut ( ) , block_number, & inactive_validators) ?;
240-
190+ _ => {
191+ let rewards = stake:: drain_previous_rewards ( & mut block. state_mut ( ) ) ?;
192+
193+ let start_of_the_current_term = metadata. last_term_finished_block_num ( ) + 1 ;
194+ let client = self
195+ . client
196+ . read ( )
197+ . as_ref ( )
198+ . ok_or ( EngineError :: CannotOpenBlock ) ?
199+ . upgrade ( )
200+ . ok_or ( EngineError :: CannotOpenBlock ) ?;
201+
202+ let inactive_validators = if term == 1 {
203+ assert ! ( rewards. is_empty( ) ) ;
204+
205+ let validators = stake:: Validators :: load_from_state ( block. state ( ) ) ?
206+ . into_iter ( )
207+ . map ( |val| public_to_address ( val. pubkey ( ) ) )
208+ . collect ( ) ;
209+ inactive_validators ( & * client, start_of_the_current_term, block. header ( ) , validators)
210+ } else {
211+ let start_of_the_previous_term = {
212+ let end_of_the_two_level_previous_term = client
213+ . last_term_finished_block_num ( ( metadata. last_term_finished_block_num ( ) - 1 ) . into ( ) )
214+ . unwrap ( ) ;
215+
216+ end_of_the_two_level_previous_term + 1
217+ } ;
218+
219+ let banned = stake:: Banned :: load_from_state ( block. state ( ) ) ?;
220+ let start_of_the_current_term_header = if block_number == start_of_the_current_term {
221+ encoded:: Header :: new ( block. header ( ) . clone ( ) . rlp_bytes ( ) . to_vec ( ) )
222+ } else {
223+ client. block_header ( & start_of_the_current_term. into ( ) ) . unwrap ( )
224+ } ;
225+
226+ let pending_rewards = calculate_pending_rewards_of_the_previous_term (
227+ & * client,
228+ & * self . validators ,
229+ rewards,
230+ start_of_the_current_term,
231+ start_of_the_current_term_header,
232+ start_of_the_previous_term,
233+ & banned,
234+ ) ?;
235+
236+ for ( address, reward) in pending_rewards {
237+ self . machine . add_balance ( block, & address, reward) ?;
238+ }
239+
240+ let validators = stake:: Validators :: load_from_state ( block. state ( ) ) ?
241+ . into_iter ( )
242+ . map ( |val| public_to_address ( val. pubkey ( ) ) )
243+ . collect ( ) ;
244+ inactive_validators ( & * client, start_of_the_current_term, block. header ( ) , validators)
245+ } ;
246+
247+ stake:: move_current_to_previous_intermediate_rewards ( & mut block. state_mut ( ) ) ?;
248+ stake:: on_term_close ( block. state_mut ( ) , block_number, & inactive_validators) ?;
249+ }
250+ }
241251 Ok ( ( ) )
242252 }
243253
@@ -337,8 +347,7 @@ impl ConsensusEngine for Tendermint {
337347 }
338348}
339349
340- fn is_term_changed ( header : & Header , parent : & Header , common_params : & CommonParams ) -> bool {
341- let term_seconds = common_params. term_seconds ( ) ;
350+ fn is_term_changed ( header : & Header , parent : & Header , term_seconds : u64 ) -> bool {
342351 if term_seconds == 0 {
343352 return false
344353 }
0 commit comments