forked from near/nearcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storage layer (substrate + interfaces) (near#72)
* Added Authority tracking (for now just initial authority). Added Signer trait that can sign hash of data and InMemorySigner implementation * Added staking to the runtime * Add AuthorityChangeSet that Runtime outputs for further consumption of Authority class * Adding PR comments * Added Authority tracking (for now just initial authority). Added Signer trait that can sign hash of data and InMemorySigner implementation * Add AuthorityChangeSet that Runtime outputs for further consumption of Authority class * Together with @mikhailOK worked on integrating substrate state into our storage layer * Cleaning up the usage of Storage, enabling tests in Runtime and Client to work; Rust formatting added. * @bowenwang1996: ganache with rpc server and block producing task * Block production includes blocks into blockchain; Fixup tests to not use default transactions which are incorrect
- Loading branch information
1 parent
08241a7
commit e3744fd
Showing
32 changed files
with
886 additions
and
458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use chain::BlockChain; | ||
use primitives::signature::PublicKey; | ||
use primitives::types::AccountId; | ||
use std::collections::HashMap; | ||
use types::BeaconBlock; | ||
|
||
/// Configure the authority rotation. | ||
pub struct AuthorityConfig { | ||
/// List of initial authorities at genesis block. | ||
pub initial_authorities: Vec<PublicKey>, | ||
/// Authority epoch length. | ||
pub epoch_length: u64, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct AuthorityChangeSet { | ||
pub proposed: HashMap<AccountId, (PublicKey, u64)>, | ||
} | ||
|
||
pub struct Authority { | ||
/// Authority configuation. | ||
authority_config: AuthorityConfig, | ||
/// Cache of current authorities for given index. | ||
_current: HashMap<u64, Vec<PublicKey>>, | ||
} | ||
|
||
impl Authority { | ||
/// Builds authority for given valid blockchain. | ||
/// Starting from best block, figure out current authorities. | ||
pub fn new(authority_config: AuthorityConfig, blockchain: &BlockChain<BeaconBlock>) -> Self { | ||
let authority = Authority { authority_config, _current: HashMap::default() }; | ||
let last_index = blockchain.best_block().header.index; | ||
let _current_epoch = last_index / authority.authority_config.epoch_length; | ||
|
||
authority | ||
} | ||
|
||
/// Returns authorities for given block number. | ||
pub fn get_authorities(&self, _index: u64) -> Vec<PublicKey> { | ||
// TODO: use cache to retrieve current authorities or compute future ones. | ||
self.authority_config.initial_authorities.clone() | ||
} | ||
|
||
/// Updates authority for given block. | ||
pub fn update_authority(&mut self, _change_set: &AuthorityChangeSet) { | ||
// TODO: update cache of current authorities. | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
use chain::ChainConfig; | ||
use primitives::hash::CryptoHash; | ||
use primitives::traits::Block; | ||
use primitives::types::MerkleHash; | ||
use std::sync::Arc; | ||
use storage::test_utils::MemoryStorage; | ||
|
||
fn test_blockchain(num_blocks: u64) -> BlockChain<BeaconBlock> { | ||
let storage = Arc::new(MemoryStorage::default()); | ||
let chain_config = ChainConfig { | ||
extra_col: storage::COL_BEACON_EXTRA, | ||
header_col: storage::COL_BEACON_HEADERS, | ||
block_col: storage::COL_BEACON_BLOCKS, | ||
index_col: storage::COL_BEACON_INDEX, | ||
}; | ||
let mut last_block = | ||
BeaconBlock::new(0, CryptoHash::default(), MerkleHash::default(), vec![]); | ||
let bc = BlockChain::new(chain_config, last_block.clone(), storage); | ||
for i in 1..num_blocks { | ||
let block = BeaconBlock::new(i, last_block.hash(), MerkleHash::default(), vec![]); | ||
bc.insert_block(block.clone()); | ||
last_block = block; | ||
} | ||
bc | ||
} | ||
|
||
#[test] | ||
fn test_authority_genesis() { | ||
let authority_config = AuthorityConfig { initial_authorities: vec![], epoch_length: 2 }; | ||
let bc = test_blockchain(0); | ||
let authority = Authority::new(authority_config, &bc); | ||
assert_eq!(authority.get_authorities(0), vec![]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
extern crate bincode; | ||
extern crate exonum_sodiumoxide; | ||
extern crate heapsize; | ||
extern crate serde; | ||
#[macro_use] | ||
extern crate serde_derive; | ||
extern crate bincode; | ||
extern crate exonum_sodiumoxide; | ||
|
||
pub mod hash; | ||
pub mod signature; | ||
pub mod signer; | ||
pub mod traits; | ||
pub mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.