Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ impl StateInfo for Client {
}

impl EngineInfo for Client {
fn network_id(&self) -> NetworkId {
self.common_params(BlockId::Earliest).expect("Genesis state must exist").network_id()
}

fn common_params(&self, block_id: BlockId) -> Option<CommonParams> {
self.state_info(block_id.into()).map(|state| {
state
Expand Down Expand Up @@ -518,7 +522,7 @@ impl EngineInfo for Client {
}

fn possible_authors(&self, block_number: Option<u64>) -> Result<Option<Vec<PlatformAddress>>, EngineError> {
let network_id = self.common_params(BlockId::Latest).unwrap().network_id();
let network_id = self.network_id();
if block_number == Some(0) {
let genesis_author = self.block_header(&0.into()).expect("genesis block").author();
return Ok(Some(vec![PlatformAddress::new_v1(network_id, genesis_author)]))
Expand Down Expand Up @@ -572,8 +576,7 @@ impl BlockChainTrait for Client {
}

fn genesis_accounts(&self) -> Vec<PlatformAddress> {
// XXX: What should we do if the network id has been changed
let network_id = self.common_params(BlockId::Latest).unwrap().network_id();
let network_id = self.network_id();
self.genesis_accounts.iter().map(|addr| PlatformAddress::new_v1(network_id, *addr)).collect()
}

Expand Down Expand Up @@ -887,10 +890,6 @@ impl MiningBlockChainClient for Client {
fn register_immune_users(&self, immune_user_vec: Vec<Address>) {
self.importer.miner.register_immune_users(immune_user_vec)
}

fn get_network_id(&self) -> NetworkId {
self.common_params(BlockId::Latest).unwrap().network_id()
}
}

impl ChainTimeInfo for Client {
Expand Down
4 changes: 1 addition & 3 deletions core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub trait BlockChainTrait {
}

pub trait EngineInfo: Send + Sync {
fn network_id(&self) -> NetworkId;
fn common_params(&self, block_id: BlockId) -> Option<CommonParams>;
fn metadata_seq(&self, block_id: BlockId) -> Option<u64>;
fn block_reward(&self, block_number: u64) -> u64;
Expand Down Expand Up @@ -279,9 +280,6 @@ pub trait MiningBlockChainClient: BlockChainClient + BlockProducer + FindActionH

/// Append designated users to the immune user list.
fn register_immune_users(&self, immune_user_vec: Vec<Address>);

/// Returns network id.
fn get_network_id(&self) -> NetworkId;
}

/// Provides methods to access database.
Expand Down
8 changes: 4 additions & 4 deletions core/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,6 @@ impl MiningBlockChainClient for TestBlockChainClient {
fn register_immune_users(&self, immune_user_vec: Vec<Address>) {
self.miner.register_immune_users(immune_user_vec)
}

fn get_network_id(&self) -> NetworkId {
NetworkId::default()
}
}

impl AccountData for TestBlockChainClient {
Expand Down Expand Up @@ -632,6 +628,10 @@ impl super::EngineClient for TestBlockChainClient {
}

impl EngineInfo for TestBlockChainClient {
fn network_id(&self) -> NetworkId {
self.scheme.engine.machine().genesis_common_params().network_id()
}

fn common_params(&self, _block_id: BlockId) -> Option<CommonParams> {
Some(*self.scheme.engine.machine().genesis_common_params())
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/consensus/stake/action_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ impl CurrentValidators {
pub fn update(&mut self, validators: Vec<Validator>) {
self.0 = validators;
}

pub fn addresses(&self) -> Vec<Address> {
self.0.iter().rev().map(|v| public_to_address(&v.pubkey)).collect()
}
}

impl Deref for CurrentValidators {
Expand Down
14 changes: 9 additions & 5 deletions core/src/consensus/tendermint/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl ConsensusEngine for Tendermint {

if block_number == metadata.last_term_finished_block_num() + 1 {
match term {
0 => {}
0 | 1 => {}
_ => {
let rewards = stake::drain_current_rewards(block.state_mut())?;
let banned = stake::Banned::load_from_state(block.state())?;
Expand Down Expand Up @@ -389,20 +389,24 @@ fn aggregate_work_info(
end_of_the_last_term + 1
};
let mut header = start_of_the_next_term_header;
let mut parent_validators = validators.current_addresses(&header.parent_hash());
// FIXME: Use method from `ValidatorSet` interface
let mut current_validators = {
let state = chain.state_at(header.parent_hash().into()).expect("The block's state must exist");
stake::CurrentValidators::load_from_state(&state)?.addresses()
};
while start_of_the_current_term != header.number() {
for index in TendermintSealView::new(&header.seal()).bitset()?.true_index_iter() {
let signer = *parent_validators.get(index).expect("The seal must be the signature of the validator");
let signer = *current_validators.get(index).expect("The seal must be the signature of the validator");
work_info.entry(signer).or_default().signed += 1;
}

header = chain.block_header(&header.parent_hash().into()).unwrap();
parent_validators = validators.current_addresses(&header.parent_hash());
current_validators = validators.current_addresses(&header.parent_hash());

let author = header.author();
let info = work_info.entry(author).or_default();
info.proposed += 1;
info.missed += parent_validators.len() - TendermintSealView::new(&header.seal()).bitset()?.count();
info.missed += current_validators.len() - TendermintSealView::new(&header.seal()).bitset()?.count();
}

Ok(work_info)
Expand Down
17 changes: 9 additions & 8 deletions core/src/consensus/tendermint/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1219,13 +1219,14 @@ impl Worker {
};

let mut voted_validators = BitSet::new();
let grand_parent_hash = self
.client()
.block_header(&(*header.parent_hash()).into())
.expect("The parent block must exist")
.parent_hash();
let parent_hash = header.parent_hash();
let grand_parent_hash =
self.client().block_header(&(*parent_hash).into()).expect("The parent block must exist").parent_hash();
for (bitset_index, signature) in seal_view.signatures()? {
let public = self.validators.get(&grand_parent_hash, bitset_index);
let public = match self.validators.get_current(header.parent_hash(), bitset_index) {
Some(p) => p,
None => self.validators.get(&grand_parent_hash, bitset_index),
};
if !verify_schnorr(&public, &signature, &precommit_vote_on.hash())? {
let address = public_to_address(&public);
return Err(EngineError::BlockNotAuthorized(address.to_owned()).into())
Expand All @@ -1238,7 +1239,7 @@ impl Worker {
if header.number() == 1 {
return Ok(())
}
self.validators.check_enough_votes(&grand_parent_hash, &voted_validators)?;
self.validators.check_enough_votes_with_current(&parent_hash, &voted_validators)?;
Ok(())
}

Expand Down Expand Up @@ -1446,7 +1447,7 @@ impl Worker {
}

fn report_double_vote(&self, double: &DoubleVote) {
let network_id = self.client().common_params(BlockId::Latest).unwrap().network_id();
let network_id = self.client().network_id();
let seq = match self.signer.address() {
Some(address) => self.client().latest_seq(address),
None => {
Expand Down
38 changes: 38 additions & 0 deletions core/src/consensus/validator_set/dynamic_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,44 @@ impl DynamicValidator {
(prev_proposer_index + proposed_view + 1) % num_validators
}
}

pub fn get_current(&self, hash: &BlockHash, index: usize) -> Option<Public> {
let validators = self.current_validators_pubkey(*hash)?;
let n_validators = validators.len();
Some(*validators.get(index % n_validators).unwrap())
}

pub fn check_enough_votes_with_current(&self, hash: &BlockHash, votes: &BitSet) -> Result<(), EngineError> {
if let Some(validators) = self.current_validators(*hash) {
let mut voted_delegation = 0u64;
let n_validators = validators.len();
for index in votes.true_index_iter() {
assert!(index < n_validators);
let validator = validators.get(index).ok_or_else(|| {
EngineError::ValidatorNotExist {
height: 0, // FIXME
index,
}
})?;
voted_delegation += validator.delegation();
}
let total_delegation: u64 = validators.iter().map(Validator::delegation).sum();
if voted_delegation * 3 > total_delegation * 2 {
Ok(())
} else {
let threshold = total_delegation as usize * 2 / 3;
Err(EngineError::BadSealFieldSize(OutOfBounds {
min: Some(threshold),
max: Some(total_delegation as usize),
found: voted_delegation as usize,
}))
}
} else {
let client = self.client.read().as_ref().and_then(Weak::upgrade).expect("Client is not initialized");
let header = client.block_header(&(*hash).into()).unwrap();
self.check_enough_votes(&header.parent_hash(), votes)
}
}
}

impl ValidatorSet for DynamicValidator {
Expand Down
2 changes: 1 addition & 1 deletion foundry/run_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ pub fn run_node(matches: &ArgMatches<'_>) -> Result<(), String> {
let network_config = config.network_config()?;
// XXX: What should we do if the network id has been changed.
let c = client.client();
let network_id = c.common_params(BlockId::Latest).unwrap().network_id();
let network_id = c.network_id();
let routing_table = RoutingTable::new();
let service = network_start(network_id, timer_loop, &network_config, Arc::clone(&routing_table))?;

Expand Down
18 changes: 8 additions & 10 deletions rpc/src/v1/impls/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use std::convert::TryInto;
use std::sync::Arc;
use std::time::Duration;

use ccore::{AccountData, AccountProvider, BlockId, EngineInfo, MinerService, MiningBlockChainClient, TermInfo};
use ckey::{NetworkId, Password, PlatformAddress, Signature};
use ccore::{AccountData, AccountProvider, EngineInfo, MinerService, MiningBlockChainClient, TermInfo};
use ckey::{Password, PlatformAddress, Signature};
use ctypes::transaction::IncompleteTransaction;
use jsonrpc_core::Result;
use parking_lot::Mutex;
Expand All @@ -46,11 +46,6 @@ where
miner,
}
}

fn network_id(&self) -> NetworkId {
// XXX: What should we do if the network id has been changed
self.client.common_params(BlockId::Latest).unwrap().network_id()
}
}

impl<C, M> Account for AccountClient<C, M>
Expand All @@ -62,21 +57,24 @@ where
self.account_provider
.get_list()
.map(|addresses| {
addresses.into_iter().map(|address| PlatformAddress::new_v1(self.network_id(), address)).collect()
addresses
.into_iter()
.map(|address| PlatformAddress::new_v1(self.client.network_id(), address))
.collect()
})
.map_err(account_provider)
}

fn create_account(&self, passphrase: Option<Password>) -> Result<PlatformAddress> {
let (address, _) =
self.account_provider.new_account_and_public(&passphrase.unwrap_or_default()).map_err(account_provider)?;
Ok(PlatformAddress::new_v1(self.network_id(), address))
Ok(PlatformAddress::new_v1(self.client.network_id(), address))
}

fn create_account_from_secret(&self, secret: H256, passphrase: Option<Password>) -> Result<PlatformAddress> {
self.account_provider
.insert_account(secret.into(), &passphrase.unwrap_or_default())
.map(|address| PlatformAddress::new_v1(self.network_id(), address))
.map(|address| PlatformAddress::new_v1(self.client.network_id(), address))
.map_err(account_provider)
}

Expand Down
36 changes: 11 additions & 25 deletions rpc/src/v1/impls/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ where
return Ok(None)
}
let block_id = block_number.map(BlockId::from).unwrap_or(BlockId::Latest);
Ok(self.client.get_text(transaction_hash, block_id).map_err(errors::transaction_state)?.map(|text| {
let parent_block_id = block_number.map(|n| (n - 1).into()).unwrap_or(BlockId::ParentOfLatest);
Text::from_core(text, self.client.common_params(parent_block_id).unwrap().network_id())
}))
Ok(self
.client
.get_text(transaction_hash, block_id)
.map_err(errors::transaction_state)?
.map(|text| Text::from_core(text, self.client.network_id())))
}

fn get_asset(
Expand Down Expand Up @@ -178,8 +179,7 @@ where
fn get_regular_key_owner(&self, public: Public, block_number: Option<u64>) -> Result<Option<PlatformAddress>> {
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
Ok(self.client.regular_key_owner(&public_to_address(&public), block_id.into()).and_then(|address| {
let parent_block_id = block_number.map(|n| (n - 1).into()).unwrap_or(BlockId::ParentOfLatest);
let network_id = self.client.common_params(parent_block_id).unwrap().network_id();
let network_id = self.client.network_id();
Some(PlatformAddress::new_v1(network_id, address))
}))
}
Expand All @@ -206,17 +206,15 @@ where
fn get_shard_owners(&self, shard_id: ShardId, block_number: Option<u64>) -> Result<Option<Vec<PlatformAddress>>> {
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
Ok(self.client.shard_owners(shard_id, block_id.into()).map(|owners| {
let parent_block_id = block_number.map(|n| (n - 1).into()).unwrap_or(BlockId::ParentOfLatest);
let network_id = self.client.common_params(parent_block_id).unwrap().network_id();
let network_id = self.client.network_id();
owners.into_iter().map(|owner| PlatformAddress::new_v1(network_id, owner)).collect()
}))
}

fn get_shard_users(&self, shard_id: ShardId, block_number: Option<u64>) -> Result<Option<Vec<PlatformAddress>>> {
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
Ok(self.client.shard_users(shard_id, block_id.into()).map(|users| {
let parent_block_id = block_number.map(|n| (n - 1).into()).unwrap_or(BlockId::ParentOfLatest);
let network_id = self.client.common_params(parent_block_id).unwrap().network_id();
let network_id = self.client.network_id();
users.into_iter().map(|user| PlatformAddress::new_v1(network_id, user)).collect()
}))
}
Expand All @@ -239,26 +237,14 @@ where

fn get_block_by_number(&self, block_number: u64) -> Result<Option<Block>> {
let id = BlockId::Number(block_number);
Ok(self.client.block(&id).map(|block| {
let block_id_to_read_params = if block_number == 0 {
0.into()
} else {
(block_number - 1).into()
};
Block::from_core(block.decode(), self.client.common_params(block_id_to_read_params).unwrap().network_id())
}))
Ok(self.client.block(&id).map(|block| Block::from_core(block.decode(), self.client.network_id())))
}

fn get_block_by_hash(&self, block_hash: BlockHash) -> Result<Option<Block>> {
let id = BlockId::Hash(block_hash);
Ok(self.client.block(&id).map(|block| {
let block = block.decode();
let block_id_to_read_params = if block.header.number() == 0 {
0.into()
} else {
(*block.header.parent_hash()).into()
};
Block::from_core(block, self.client.common_params(block_id_to_read_params).unwrap().network_id())
Block::from_core(block, self.client.network_id())
}))
}

Expand Down Expand Up @@ -301,7 +287,7 @@ where
}

fn get_network_id(&self) -> Result<NetworkId> {
Ok(self.client.common_params(BlockId::Latest).unwrap().network_id())
Ok(self.client.network_id())
}

fn get_common_params(&self, block_number: Option<u64>) -> Result<Option<Params>> {
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/v1/impls/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ where
Ok(None)
} else {
// XXX: What should we do if the network id has been changed
let network_id = self.client.common_params(BlockId::Latest).unwrap().network_id();
let network_id = self.client.network_id();
Ok(Some(PlatformAddress::new_v1(network_id, author)))
}
}
Expand Down
Loading