Move Bank to its own crate
Also: * counters.rs to solana_metrics * genesis_block.rs to solana_sdk
This commit is contained in:
110
sdk/src/genesis_block.rs
Normal file
110
sdk/src/genesis_block.rs
Normal file
@ -0,0 +1,110 @@
|
||||
//! The `genesis_block` module is a library for generating the chain's genesis block.
|
||||
|
||||
use crate::hash::{hash, Hash};
|
||||
use crate::pubkey::Pubkey;
|
||||
use crate::signature::{Keypair, KeypairUtil};
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
|
||||
// The default (and minimal) amount of tokens given to the bootstrap leader:
|
||||
// * 1 token for the bootstrap leader ID account
|
||||
// * 1 token for the bootstrap leader vote account
|
||||
pub const BOOTSTRAP_LEADER_TOKENS: u64 = 2;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct GenesisBlock {
|
||||
pub bootstrap_leader_id: Pubkey,
|
||||
pub bootstrap_leader_tokens: u64,
|
||||
pub bootstrap_leader_vote_account_id: Pubkey,
|
||||
pub mint_id: Pubkey,
|
||||
pub tokens: u64,
|
||||
}
|
||||
|
||||
impl GenesisBlock {
|
||||
#[allow(clippy::new_ret_no_self)]
|
||||
pub fn new(tokens: u64) -> (Self, Keypair) {
|
||||
let tokens = tokens
|
||||
.checked_add(BOOTSTRAP_LEADER_TOKENS)
|
||||
.unwrap_or(tokens);
|
||||
let mint_keypair = Keypair::new();
|
||||
let bootstrap_leader_keypair = Keypair::new();
|
||||
let bootstrap_leader_vote_account_keypair = Keypair::new();
|
||||
(
|
||||
Self {
|
||||
bootstrap_leader_id: bootstrap_leader_keypair.pubkey(),
|
||||
bootstrap_leader_tokens: BOOTSTRAP_LEADER_TOKENS,
|
||||
bootstrap_leader_vote_account_id: bootstrap_leader_vote_account_keypair.pubkey(),
|
||||
mint_id: mint_keypair.pubkey(),
|
||||
tokens,
|
||||
},
|
||||
mint_keypair,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_with_leader(
|
||||
tokens: u64,
|
||||
bootstrap_leader_id: Pubkey,
|
||||
bootstrap_leader_tokens: u64,
|
||||
) -> (Self, Keypair) {
|
||||
let mint_keypair = Keypair::new();
|
||||
let bootstrap_leader_vote_account_keypair = Keypair::new();
|
||||
(
|
||||
Self {
|
||||
bootstrap_leader_id,
|
||||
bootstrap_leader_tokens,
|
||||
bootstrap_leader_vote_account_id: bootstrap_leader_vote_account_keypair.pubkey(),
|
||||
mint_id: mint_keypair.pubkey(),
|
||||
tokens,
|
||||
},
|
||||
mint_keypair,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn last_id(&self) -> Hash {
|
||||
let serialized = serde_json::to_string(self).unwrap();
|
||||
hash(&serialized.into_bytes())
|
||||
}
|
||||
|
||||
pub fn load(ledger_path: &str) -> Result<Self, std::io::Error> {
|
||||
let file = File::open(&Path::new(ledger_path).join("genesis.json"))?;
|
||||
let genesis_block = serde_json::from_reader(file)?;
|
||||
Ok(genesis_block)
|
||||
}
|
||||
|
||||
pub fn write(&self, ledger_path: &str) -> Result<(), std::io::Error> {
|
||||
let serialized = serde_json::to_string(self)?;
|
||||
let mut file = File::create(&Path::new(ledger_path).join("genesis.json"))?;
|
||||
file.write_all(&serialized.into_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_genesis_block_new() {
|
||||
let (genesis_block, mint) = GenesisBlock::new(10_000);
|
||||
assert_eq!(genesis_block.tokens, 10_000 + BOOTSTRAP_LEADER_TOKENS);
|
||||
assert_eq!(genesis_block.mint_id, mint.pubkey());
|
||||
assert!(genesis_block.bootstrap_leader_id != Pubkey::default());
|
||||
assert!(genesis_block.bootstrap_leader_vote_account_id != Pubkey::default());
|
||||
assert_eq!(
|
||||
genesis_block.bootstrap_leader_tokens,
|
||||
BOOTSTRAP_LEADER_TOKENS
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_genesis_block_new_with_leader() {
|
||||
let leader_keypair = Keypair::new();
|
||||
let (genesis_block, mint) =
|
||||
GenesisBlock::new_with_leader(20_000, leader_keypair.pubkey(), 123);
|
||||
|
||||
assert_eq!(genesis_block.tokens, 20_000);
|
||||
assert_eq!(genesis_block.mint_id, mint.pubkey());
|
||||
assert_eq!(genesis_block.bootstrap_leader_id, leader_keypair.pubkey());
|
||||
assert_eq!(genesis_block.bootstrap_leader_tokens, 123);
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ pub mod budget_expr;
|
||||
pub mod budget_instruction;
|
||||
pub mod budget_program;
|
||||
pub mod budget_transaction;
|
||||
pub mod genesis_block;
|
||||
pub mod hash;
|
||||
pub mod loader_instruction;
|
||||
pub mod loader_transaction;
|
||||
|
@ -9,6 +9,14 @@ pub const NUM_TICKS_PER_SECOND: usize = 10;
|
||||
pub const DEFAULT_TICKS_PER_SLOT: u64 = 8;
|
||||
pub const DEFAULT_SLOTS_PER_EPOCH: u64 = 64;
|
||||
|
||||
/// The number of most recent `last_id` values that the bank will track the signatures
|
||||
/// of. Once the bank discards a `last_id`, it will reject any transactions that use
|
||||
/// that `last_id` in a transaction. Lowering this value reduces memory consumption,
|
||||
/// but requires clients to update its `last_id` more frequently. Raising the value
|
||||
/// lengthens the time a client must wait to be certain a missing transaction will
|
||||
/// not be processed by the network.
|
||||
pub const MAX_ENTRY_IDS: usize = NUM_TICKS_PER_SECOND * 120;
|
||||
|
||||
pub fn duration_as_us(d: &Duration) -> u64 {
|
||||
(d.as_secs() * 1000 * 1000) + (u64::from(d.subsec_nanos()) / 1_000)
|
||||
}
|
||||
|
Reference in New Issue
Block a user