Skip to content

Commit 1a9605b

Browse files
author
SeungMin Lee
committed
Remove the score except it of the Header and the Block
1 parent ee3abea commit 1a9605b

14 files changed

Lines changed: 50 additions & 54 deletions

File tree

core/src/block.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ impl<'x> OpenBlock<'x> {
143143
r.block.header.set_extra_data(extra_data);
144144
r.block.header.note_dirty();
145145

146-
engine.machine().populate_from_parent(&mut r.block.header, parent);
147146
engine.populate_from_parent(&mut r.block.header, parent);
148147

149148
Ok(r)
@@ -208,7 +207,6 @@ impl<'x> OpenBlock<'x> {
208207

209208
/// Populate self from a header.
210209
fn populate_from(&mut self, header: &Header) {
211-
self.block.header.set_score(*header.score());
212210
self.block.header.set_timestamp(header.timestamp());
213211
self.block.header.set_author(*header.author());
214212
self.block.header.set_extra_data(header.extra_data().clone());

core/src/blockchain/blockchain.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ impl BlockChain {
210210
}
211211
}
212212

213+
/// Compare the number and the view of current block with these of best block
214+
fn is_new_header_eligible_to_be_best(&self, new_header: &HeaderView<'_>) -> bool {
215+
let details_of_best_block = self.best_proposal_block_detail();
216+
217+
(new_header.number(), details_of_best_block.view) > (details_of_best_block.number, new_header.view())
218+
}
219+
213220
/// Calculate how best block is changed
214221
fn best_block_changed(&self, new_block: &BlockView<'_>, engine: &dyn CodeChainEngine) -> BestBlockChanged {
215222
let new_header = new_block.header_view();
@@ -218,7 +225,7 @@ impl BlockChain {
218225
let grandparent_hash_of_new_block = parent_details_of_new_block.parent;
219226
let prev_best_hash = self.best_block_hash();
220227

221-
if parent_details_of_new_block.total_score + new_header.score() > self.best_proposal_block_detail().total_score
228+
if self.is_new_header_eligible_to_be_best(&new_header)
222229
&& engine.can_change_canon_chain(parent_hash_of_new_block, grandparent_hash_of_new_block, prev_best_hash)
223230
{
224231
cinfo!(

core/src/blockchain/extras.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use crate::db::Key;
1818
use crate::types::TransactionId;
1919
use ctypes::{BlockHash, BlockNumber, Tracker, TxHash};
20-
use primitives::{H256, H264, U256};
20+
use primitives::{H256, H264};
2121
use std::ops::{Add, AddAssign, Deref, Sub, SubAssign};
2222

2323
/// Represents index of extra data in database
@@ -93,10 +93,10 @@ impl Key<TransactionAddresses> for Tracker {
9393
/// Familial details concerning a block
9494
#[derive(Debug, Clone, RlpEncodable, RlpDecodable)]
9595
pub struct BlockDetails {
96+
/// Block view
97+
pub view: u64,
9698
/// Block number
9799
pub number: BlockNumber,
98-
/// Total score of the block and all its parents
99-
pub total_score: U256,
100100
/// Parent block hash
101101
pub parent: BlockHash,
102102
}

core/src/blockchain/headerchain.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl HeaderChain {
7272

7373
let details = BlockDetails {
7474
number: genesis.number(),
75-
total_score: genesis.score(),
75+
view: genesis.view(),
7676
parent: genesis.parent_hash(),
7777
};
7878

@@ -134,7 +134,7 @@ impl HeaderChain {
134134
let mut new_details = HashMap::new();
135135
new_details.insert(hash, BlockDetails {
136136
number: header.number(),
137-
total_score: 0.into(),
137+
view: header.view(),
138138
parent: header.parent_hash(),
139139
});
140140

@@ -252,12 +252,11 @@ impl HeaderChain {
252252
/// Uses the given parent details or attempts to load them from the database.
253253
fn new_detail_entries(&self, header: &HeaderView<'_>) -> HashMap<BlockHash, BlockDetails> {
254254
let parent_hash = header.parent_hash();
255-
let parent_details = self.block_details(&parent_hash).expect("Invalid parent hash");
256255

257256
// create current block details.
258257
let details = BlockDetails {
259258
number: header.number(),
260-
total_score: parent_details.total_score + header.score(),
259+
view: header.view(),
261260
parent: parent_hash,
262261
};
263262

@@ -267,14 +266,22 @@ impl HeaderChain {
267266
block_details
268267
}
269268

269+
/// Compare the number and the view of current block with these of best block
270+
fn is_new_header_eligible_to_be_best(&self, new_header: &HeaderView<'_>) -> bool {
271+
let best_proposal_block_hash = self.best_header_hash();
272+
let best_proposal_block_detail =
273+
self.block_details(&best_proposal_block_hash).expect("Best proposal block always exists");
274+
275+
(new_header.number(), best_proposal_block_detail.view) > (best_proposal_block_detail.number, new_header.view())
276+
}
277+
270278
/// Calculate how best block is changed
271279
fn best_header_changed(&self, new_header: &HeaderView<'_>, engine: &dyn CodeChainEngine) -> BestHeaderChanged {
272280
let parent_hash_of_new_header = new_header.parent_hash();
273281
let parent_details_of_new_header = self.block_details(&parent_hash_of_new_header).expect("Invalid parent hash");
274282
let grandparent_hash_of_new_header = parent_details_of_new_header.parent;
275283
let prev_best_hash = self.best_header_hash();
276-
let is_new_best = parent_details_of_new_header.total_score + new_header.score()
277-
> self.best_proposal_header_detail().total_score
284+
let is_new_best = self.is_new_header_eligible_to_be_best(new_header)
278285
&& engine.can_change_canon_chain(parent_hash_of_new_header, grandparent_hash_of_new_header, prev_best_hash);
279286

280287
if is_new_best {
@@ -364,10 +371,6 @@ impl HeaderChain {
364371
pub fn best_proposal_header(&self) -> encoded::Header {
365372
self.block_header_data(&self.best_proposal_header_hash()).expect("Highest header always exists")
366373
}
367-
368-
pub fn best_proposal_header_detail(&self) -> BlockDetails {
369-
self.block_details(&self.best_proposal_header_hash()).expect("Best Proposal header always exists")
370-
}
371374
}
372375

373376
/// Interface for querying blocks by hash and by number.

core/src/codechain_machine.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,6 @@ impl CodeChainMachine {
7878
Ok(())
7979
}
8080

81-
/// Populate a header's fields based on its parent's header.
82-
/// Usually implements the chain scoring rule based on weight.
83-
pub fn populate_from_parent(&self, header: &mut Header, parent: &Header) {
84-
header.set_score(*parent.score());
85-
}
86-
8781
pub fn min_cost(params: &CommonParams, action: &Action) -> u64 {
8882
match action {
8983
Action::Pay {

core/src/encoded.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::views;
2929
use ccrypto::blake256;
3030
use ckey::Address;
3131
use ctypes::{BlockHash, BlockNumber, Header as FullHeader, TxHash};
32-
use primitives::{H256, U256};
32+
use primitives::H256;
3333
use rlp::Rlp;
3434

3535
/// Owning header view.
@@ -99,11 +99,6 @@ impl Header {
9999
self.view().next_validator_set_hash()
100100
}
101101

102-
/// Score of this block
103-
pub fn score(&self) -> U256 {
104-
self.view().score()
105-
}
106-
107102
/// Number of this block.
108103
pub fn number(&self) -> BlockNumber {
109104
self.view().number()
@@ -258,11 +253,6 @@ impl Block {
258253
self.header_view().transactions_root()
259254
}
260255

261-
/// Score of this block
262-
pub fn score(&self) -> U256 {
263-
self.header_view().score()
264-
}
265-
266256
/// Number of this block.
267257
pub fn number(&self) -> BlockNumber {
268258
self.header_view().number()

core/src/scheme/genesis.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ use ccrypto::BLAKE_NULL_RLP;
1919
use cjson;
2020
use ckey::{Address, PlatformAddress};
2121
use ctypes::BlockHash;
22-
use primitives::{Bytes, H256, U256};
22+
use primitives::{Bytes, H256};
2323

2424
/// Genesis components.
2525
pub struct Genesis {
2626
/// Seal.
2727
pub seal: Seal,
28-
/// Score.
29-
pub score: U256,
3028
/// Author.
3129
pub author: Address,
3230
/// Timestamp.
@@ -47,7 +45,6 @@ impl From<cjson::scheme::Genesis> for Genesis {
4745
fn from(g: cjson::scheme::Genesis) -> Self {
4846
Genesis {
4947
seal: From::from(g.seal),
50-
score: g.score.into(),
5148
author: g.author.map_or_else(Address::default, PlatformAddress::into_address),
5249
timestamp: g.timestamp.map_or(0, Into::into),
5350
parent_hash: g.parent_hash.map_or_else(H256::zero, Into::into).into(),

core/src/scheme/scheme.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use ctypes::errors::SyntaxError;
3030
use ctypes::{BlockHash, CommonParams, Header, ShardId};
3131
use merkle_trie::{TrieFactory, TrieMut};
3232
use parking_lot::RwLock;
33-
use primitives::{Bytes, H256, U256};
33+
use primitives::{Bytes, H256};
3434
use rlp::{Encodable, Rlp, RlpStream};
3535
use std::io::Read;
3636
use std::sync::Arc;
@@ -52,8 +52,6 @@ pub struct Scheme {
5252
pub parent_hash: BlockHash,
5353
/// The genesis block's author field.
5454
pub author: Address,
55-
/// The genesis block's score field.
56-
pub score: U256,
5755
/// The genesis block's timestamp field.
5856
pub timestamp: u64,
5957
/// Transactions root of the genesis block. Should be BLAKE_NULL_RLP.
@@ -267,7 +265,6 @@ impl Scheme {
267265
header.set_extra_data(blake256(&self.genesis_params().rlp_bytes()).to_vec());
268266
header.set_state_root(self.state_root());
269267
header.set_next_validator_set_hash(BLAKE_NULL_RLP /* This will be calculated from state after https://github.com/CodeChain-io/foundry/issues/142*/);
270-
header.set_score(self.score);
271268
header.set_seal({
272269
let r = Rlp::new(&self.seal_rlp);
273270
r.iter().map(|f| f.as_raw().to_vec()).collect()
@@ -308,7 +305,6 @@ fn load_from(s: cjson::scheme::Scheme) -> Result<Scheme, Error> {
308305
parent_hash: g.parent_hash,
309306
transactions_root: g.transactions_root,
310307
author: g.author,
311-
score: g.score,
312308
timestamp: g.timestamp,
313309
extra_data: g.extra_data,
314310
seal_rlp,

core/src/tests/helpers.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::scheme::Scheme;
1818
use crate::transaction::SignedTransaction;
1919
use cstate::StateDB;
2020
use ctypes::{BlockHash, Header};
21-
use primitives::{Bytes, U256};
21+
use primitives::Bytes;
2222
use rlp::{self, RlpStream};
2323

2424
pub fn create_test_block(header: &Header) -> Bytes {
@@ -48,7 +48,6 @@ pub fn get_good_dummy_block() -> Bytes {
4848
pub fn get_good_dummy_block_hash() -> (BlockHash, Bytes) {
4949
let mut block_header = Header::new();
5050
let test_scheme = Scheme::new_test();
51-
block_header.set_score(U256::from(0x20000));
5251
block_header.set_timestamp(40);
5352
block_header.set_number(1);
5453
block_header.set_parent_hash(test_scheme.genesis_header().hash());

core/src/views/header.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,14 @@ impl<'a> HeaderView<'a> {
112112
let seal = self.seal();
113113
seal.into_iter().map(|s| rlp::Rlp::new(&s).data().map(|x| x.to_vec())).collect()
114114
}
115+
116+
/// Get view in the seal field of the header.
117+
pub fn view(&self) -> u64 {
118+
let seal = self.seal();
119+
if let Some(rlp_view) = seal.get(1) {
120+
Rlp::new(rlp_view.as_slice()).as_val().unwrap()
121+
} else {
122+
0
123+
}
124+
}
115125
}

0 commit comments

Comments
 (0)