Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snapshot sync e2e test for Tendermint Dynamic Validator #1892

Open
wants to merge 5 commits into
base: snapshot
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions codechain/run_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use std::time::{SystemTime, UNIX_EPOCH};

use ccore::snapshot_notify;
use ccore::{
AccountProvider, AccountProviderError, BlockId, ChainNotify, Client, ClientConfig, ClientService, EngineInfo,
EngineType, Miner, MinerService, Scheme, Stratum, StratumConfig, StratumError, NUM_COLUMNS,
AccountProvider, AccountProviderError, ChainNotify, Client, ClientConfig, ClientService, EngineInfo, EngineType,
Miner, MinerService, Scheme, Stratum, StratumConfig, StratumError, NUM_COLUMNS,
};
use cdiscovery::{Config, Discovery};
use ckey::{Address, NetworkId, PlatformAddress};
Expand Down Expand Up @@ -286,7 +286,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::Number(0)).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
13 changes: 6 additions & 7 deletions core/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,10 @@ impl StateInfo for Client {
}

impl EngineInfo for Client {
fn network_id(&self) -> NetworkId {
self.common_params(BlockId::Earliest).expect("Genesis state should 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 @@ -528,7 +532,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 @@ -594,8 +598,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 @@ -945,10 +948,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 @@ -89,6 +89,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 @@ -300,9 +301,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
18 changes: 9 additions & 9 deletions core/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use crate::client::{
AccountData, BlockChainClient, BlockChainTrait, BlockProducer, BlockStatus, ConsensusClient, EngineInfo,
ImportBlock, ImportResult, MiningBlockChainClient, StateInfo, StateOrBlock, TermInfo,
};
use crate::consensus::stake::{Validator, Validators};
use crate::consensus::stake::{NextValidators, Validator};
use crate::consensus::EngineError;
use crate::db::{COL_STATE, NUM_COLUMNS};
use crate::encoded;
Expand Down Expand Up @@ -106,7 +106,7 @@ pub struct TestBlockChainClient {
/// Fixed validator keys
pub validator_keys: RwLock<HashMap<Public, Private>>,
/// Fixed validators
pub validators: Validators,
pub validators: NextValidators,
}

impl Default for TestBlockChainClient {
Expand Down Expand Up @@ -160,7 +160,7 @@ impl TestBlockChainClient {
history: RwLock::new(None),
term_id: Some(1),
validator_keys: RwLock::new(HashMap::new()),
validators: Validators::from_vector_to_test(vec![]),
validators: NextValidators::from_vector_to_test(vec![]),
};

// insert genesis hash.
Expand Down Expand Up @@ -325,14 +325,14 @@ impl TestBlockChainClient {
self.validator_keys.write().insert(*key_pair.public(), *key_pair.private());
pubkeys.push(*key_pair.public());
}
let fixed_validators: Validators = Validators::from_vector_to_test(
let fixed_validators: NextValidators = NextValidators::from_vector_to_test(
pubkeys.into_iter().map(|pubkey| Validator::new_for_test(0, 0, pubkey)).collect(),
);

self.validators = fixed_validators;
}

pub fn get_validators(&self) -> &Validators {
pub fn get_validators(&self) -> &NextValidators {
&self.validators
}
}
Expand Down Expand Up @@ -381,10 +381,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 @@ -657,6 +653,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
71 changes: 58 additions & 13 deletions core/src/consensus/stake/action_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ lazy_static! {
pub static ref JAIL_KEY: H256 = ActionDataKeyBuilder::new(CUSTOM_ACTION_HANDLER_ID, 1).append(&"Jail").into_key();
pub static ref BANNED_KEY: H256 =
ActionDataKeyBuilder::new(CUSTOM_ACTION_HANDLER_ID, 1).append(&"Banned").into_key();
pub static ref VALIDATORS_KEY: H256 =
pub static ref NEXT_VALIDATORS_KEY: H256 =
ActionDataKeyBuilder::new(CUSTOM_ACTION_HANDLER_ID, 1).append(&"Validators").into_key();
pub static ref CURRENT_VALIDATORS_KEY: H256 =
ActionDataKeyBuilder::new(CUSTOM_ACTION_HANDLER_ID, 1).append(&"CurrentValidators").into_key();
}

pub fn get_delegation_key(address: &Address) -> H256 {
Expand Down Expand Up @@ -274,17 +276,17 @@ impl Validator {
}

#[derive(Debug)]
pub struct Validators(Vec<Validator>);
impl Validators {
pub struct NextValidators(Vec<Validator>);
impl NextValidators {
pub fn from_vector_to_test(vec: Vec<Validator>) -> Self {
Validators(vec)
Self(vec)
}

pub fn load_from_state(state: &TopLevelState) -> StateResult<Self> {
let key = &*VALIDATORS_KEY;
let key = &*NEXT_VALIDATORS_KEY;
let validators = state.action_data(&key)?.map(|data| decode_list(&data)).unwrap_or_default();

Ok(Validators(validators))
Ok(Self(validators))
}

pub fn elect(state: &TopLevelState) -> StateResult<Self> {
Expand Down Expand Up @@ -335,7 +337,7 @@ impl Validators {


pub fn save_to_state(&self, state: &mut TopLevelState) -> StateResult<()> {
let key = &*VALIDATORS_KEY;
let key = &*NEXT_VALIDATORS_KEY;
if !self.is_empty() {
state.update_action_data(&key, encode_list(&self.0).to_vec())?;
} else {
Expand Down Expand Up @@ -384,21 +386,21 @@ impl Validators {
}
}

impl Deref for Validators {
impl Deref for NextValidators {
type Target = Vec<Validator>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl From<Validators> for Vec<Validator> {
fn from(val: Validators) -> Self {
impl From<NextValidators> for Vec<Validator> {
fn from(val: NextValidators) -> Self {
val.0
}
}

impl IntoIterator for Validators {
impl IntoIterator for NextValidators {
type Item = Validator;
type IntoIter = vec::IntoIter<Self::Item>;

Expand All @@ -407,6 +409,49 @@ impl IntoIterator for Validators {
}
}

#[derive(Debug)]
pub struct CurrentValidators(Vec<Validator>);
impl CurrentValidators {
pub fn load_from_state(state: &TopLevelState) -> StateResult<Self> {
let key = &*CURRENT_VALIDATORS_KEY;
let validators = state.action_data(&key)?.map(|data| decode_list(&data)).unwrap_or_default();

Ok(Self(validators))
}

pub fn save_to_state(&self, state: &mut TopLevelState) -> StateResult<()> {
let key = &*CURRENT_VALIDATORS_KEY;
if !self.is_empty() {
state.update_action_data(&key, encode_list(&self.0).to_vec())?;
} else {
state.remove_action_data(&key);
}
Ok(())
}

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()
}

pub fn get_validator(&self, index: usize) -> &Validator {
let len = self.0.len();
// NOTE: validator list is reversed when reading a validator by index
self.0.iter().nth_back(index % len).unwrap()
}
}

impl Deref for CurrentValidators {
type Target = Vec<Validator>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

pub mod v0 {
use std::mem;

Expand Down Expand Up @@ -603,7 +648,7 @@ impl Candidates {

pub fn renew_candidates(
&mut self,
validators: &Validators,
validators: &NextValidators,
nomination_ends_at: u64,
inactive_validators: &[Address],
banned: &Banned,
Expand Down Expand Up @@ -1868,7 +1913,7 @@ mod tests {
}
candidates.save_to_state(&mut state).unwrap();

let dummy_validators = Validators(
let dummy_validators = NextValidators(
pubkeys[0..5]
.iter()
.map(|pubkey| Validator {
Expand Down
14 changes: 7 additions & 7 deletions core/src/consensus/stake/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use parking_lot::RwLock;
use primitives::{Bytes, H256};
use rlp::{Decodable, Rlp};

pub use self::action_data::{Banned, Validator, Validators};
pub use self::action_data::{Banned, CurrentValidators, NextValidators, Validator};
use self::action_data::{Candidates, Delegation, Jail, ReleaseResult, StakeAccount, Stakeholders};
pub use self::actions::Action;
pub use self::distribute::fee_distribute;
Expand Down Expand Up @@ -317,8 +317,8 @@ pub fn get_stakes(state: &TopLevelState) -> StateResult<HashMap<Address, u64>> {
Ok(result)
}

pub fn get_validators(state: &TopLevelState) -> StateResult<Validators> {
Validators::load_from_state(state)
pub fn get_validators(state: &TopLevelState) -> StateResult<NextValidators> {
NextValidators::load_from_state(state)
}

pub mod v0 {
Expand Down Expand Up @@ -379,7 +379,7 @@ pub mod v1 {
}

pub fn update_validator_weights(state: &mut TopLevelState, block_author: &Address) -> StateResult<()> {
let mut validators = Validators::load_from_state(state)?;
let mut validators = NextValidators::load_from_state(state)?;
validators.update_weight(block_author);
validators.save_to_state(state)
}
Expand Down Expand Up @@ -451,7 +451,7 @@ pub fn on_term_close(

jail(state, inactive_validators, custody_until, kick_at)?;

let validators = Validators::elect(state)?;
let validators = NextValidators::elect(state)?;
validators.save_to_state(state)?;

state.increase_term_id(last_term_finished_block_num)?;
Expand All @@ -469,7 +469,7 @@ fn update_candidates(
let mut candidates = Candidates::load_from_state(state)?;
let nomination_ends_at = current_term + nomination_expiration;

let current_validators = Validators::load_from_state(state)?;
let current_validators = NextValidators::load_from_state(state)?;
candidates.renew_candidates(&current_validators, nomination_ends_at, &inactive_validators, &banned);

let expired = candidates.drain_expired_candidates(current_term);
Expand Down Expand Up @@ -519,7 +519,7 @@ pub fn ban(state: &mut TopLevelState, informant: &Public, criminal: Address) ->

let mut candidates = Candidates::load_from_state(state)?;
let mut jailed = Jail::load_from_state(state)?;
let mut validators = Validators::load_from_state(state)?;
let mut validators = NextValidators::load_from_state(state)?;

let deposit = match (candidates.remove(&criminal), jailed.remove(&criminal)) {
(Some(_), Some(_)) => unreachable!("A candidate that are jailed cannot exist"),
Expand Down
Loading