Skip to content

Commit 3e3012e

Browse files
author
Joon-Mo Yang
committed
Add score_to_target method to ConsensusEngine
1 parent 9d7ce99 commit 3e3012e

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

core/src/consensus/cuckoo/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ impl ConsensusEngine<CodeChainMachine> for Cuckoo {
126126
if !self.verifier.verify(&message, &seal.proof) {
127127
return Err(From::from(BlockError::InvalidProofOfWork))
128128
}
129-
let max_hash = (U256::max_value() - *header.score()) / *header.score();
130-
if U256::from(header.hash()) > max_hash {
129+
if U256::from(header.hash()) > self.score_to_target(header.score()) {
131130
return Err(From::from(BlockError::InvalidScore(OutOfBounds {
132131
min: Some(*header.score()),
133132
max: Some(*header.score()),
@@ -167,4 +166,8 @@ impl ConsensusEngine<CodeChainMachine> for Cuckoo {
167166
let author = *block.header().author();
168167
self.machine.add_balance(block, &author, &self.params.block_reward)
169168
}
169+
170+
fn score_to_target(&self, score: &U256) -> U256 {
171+
(U256::max_value() - *score) / *score
172+
}
170173
}

core/src/consensus/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use std::sync::{Arc, Weak};
3737

3838
use ckey::ECDSASignature;
3939
use cnetwork::NetworkExtension;
40-
use ctypes::{Address, Bytes, H256};
40+
use ctypes::{Address, Bytes, H256, U256};
4141
use unexpected::{Mismatch, OutOfBounds};
4242

4343
use self::epoch::{EpochVerifier, NoOp, PendingTransition};
@@ -212,6 +212,10 @@ pub trait ConsensusEngine<M: Machine>: Sync + Send {
212212
fn network_extension(&self) -> Option<Arc<NetworkExtension>> {
213213
None
214214
}
215+
216+
fn score_to_target(&self, _score: &U256) -> U256 {
217+
U256::zero()
218+
}
215219
}
216220

217221
/// Results of a query of whether an epoch change occurred at the given block.

0 commit comments

Comments
 (0)