2019-01-24 12:04:04 -08:00
|
|
|
//! The `genesis_block` module is a library for generating the chain's genesis block.
|
|
|
|
|
2019-05-07 11:16:22 -07:00
|
|
|
use crate::account::Account;
|
2019-05-07 20:28:41 -07:00
|
|
|
use crate::fee_calculator::FeeCalculator;
|
2019-02-18 22:26:22 -08:00
|
|
|
use crate::hash::{hash, Hash};
|
2019-06-11 21:42:31 -07:00
|
|
|
use crate::inflation::Inflation;
|
2019-05-18 14:01:36 -07:00
|
|
|
use crate::poh_config::PohConfig;
|
2019-02-18 22:26:22 -08:00
|
|
|
use crate::pubkey::Pubkey;
|
|
|
|
use crate::signature::{Keypair, KeypairUtil};
|
2019-05-07 11:16:22 -07:00
|
|
|
use crate::system_program;
|
2019-02-21 16:01:10 -08:00
|
|
|
use crate::timing::{DEFAULT_SLOTS_PER_EPOCH, DEFAULT_TICKS_PER_SLOT};
|
2019-01-24 12:04:04 -08:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct GenesisBlock {
|
2019-05-07 20:28:41 -07:00
|
|
|
pub accounts: Vec<(Pubkey, Account)>,
|
|
|
|
pub fee_calculator: FeeCalculator,
|
2019-04-02 07:57:00 -07:00
|
|
|
pub native_instruction_processors: Vec<(String, Pubkey)>,
|
2019-06-11 17:04:13 -07:00
|
|
|
pub ticks_per_slot: u64,
|
2019-05-07 20:28:41 -07:00
|
|
|
pub slots_per_epoch: u64,
|
|
|
|
pub stakers_slot_offset: u64,
|
2019-06-11 17:04:13 -07:00
|
|
|
pub epoch_warmup: bool,
|
2019-05-18 14:01:36 -07:00
|
|
|
pub poh_config: PohConfig,
|
2019-06-11 21:42:31 -07:00
|
|
|
pub inflation: Inflation,
|
2019-01-24 12:04:04 -08:00
|
|
|
}
|
|
|
|
|
2019-05-07 11:16:22 -07:00
|
|
|
// useful for basic tests
|
|
|
|
pub fn create_genesis_block(lamports: u64) -> (GenesisBlock, Keypair) {
|
|
|
|
let mint_keypair = Keypair::new();
|
|
|
|
(
|
|
|
|
GenesisBlock::new(
|
|
|
|
&[(
|
|
|
|
mint_keypair.pubkey(),
|
|
|
|
Account::new(lamports, 0, &system_program::id()),
|
|
|
|
)],
|
|
|
|
&[],
|
|
|
|
),
|
|
|
|
mint_keypair,
|
|
|
|
)
|
|
|
|
}
|
2019-01-24 12:04:04 -08:00
|
|
|
|
2019-06-11 17:04:13 -07:00
|
|
|
impl Default for GenesisBlock {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
accounts: Vec::new(),
|
|
|
|
epoch_warmup: true,
|
|
|
|
fee_calculator: FeeCalculator::default(),
|
|
|
|
native_instruction_processors: Vec::new(),
|
|
|
|
slots_per_epoch: DEFAULT_SLOTS_PER_EPOCH,
|
|
|
|
stakers_slot_offset: DEFAULT_SLOTS_PER_EPOCH,
|
|
|
|
ticks_per_slot: DEFAULT_TICKS_PER_SLOT,
|
|
|
|
poh_config: PohConfig::default(),
|
2019-06-11 21:42:31 -07:00
|
|
|
inflation: Inflation::default(),
|
2019-06-11 17:04:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 11:16:22 -07:00
|
|
|
impl GenesisBlock {
|
|
|
|
pub fn new(
|
|
|
|
accounts: &[(Pubkey, Account)],
|
|
|
|
native_instruction_processors: &[(String, Pubkey)],
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
2019-05-07 20:28:41 -07:00
|
|
|
accounts: accounts.to_vec(),
|
2019-05-07 11:16:22 -07:00
|
|
|
native_instruction_processors: native_instruction_processors.to_vec(),
|
2019-06-11 17:04:13 -07:00
|
|
|
..GenesisBlock::default()
|
2019-05-07 11:16:22 -07:00
|
|
|
}
|
2019-01-24 12:04:04 -08:00
|
|
|
}
|
|
|
|
|
2019-03-01 09:49:37 -08:00
|
|
|
pub fn hash(&self) -> Hash {
|
2019-01-24 12:04:04 -08:00
|
|
|
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)?;
|
2019-05-07 11:16:22 -07:00
|
|
|
|
|
|
|
let dir = Path::new(ledger_path);
|
|
|
|
std::fs::create_dir_all(&dir)?;
|
|
|
|
|
|
|
|
let mut file = File::create(&dir.join("genesis.json"))?;
|
2019-01-24 12:04:04 -08:00
|
|
|
file.write_all(&serialized.into_bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-05-07 11:16:22 -07:00
|
|
|
use crate::signature::{Keypair, KeypairUtil};
|
2019-01-24 12:04:04 -08:00
|
|
|
|
2019-05-07 11:16:22 -07:00
|
|
|
fn make_tmp_path(name: &str) -> String {
|
|
|
|
let out_dir = std::env::var("OUT_DIR").unwrap_or_else(|_| "target".to_string());
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
|
|
|
|
let path = format!("{}/tmp/{}-{}", out_dir, name, keypair.pubkey());
|
|
|
|
|
|
|
|
// whack any possible collision
|
|
|
|
let _ignored = std::fs::remove_dir_all(&path);
|
|
|
|
// whack any possible collision
|
|
|
|
let _ignored = std::fs::remove_file(&path);
|
|
|
|
|
|
|
|
path
|
2019-01-24 12:04:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-05-07 11:16:22 -07:00
|
|
|
fn test_genesis_block() {
|
|
|
|
let mint_keypair = Keypair::new();
|
|
|
|
let block = GenesisBlock::new(
|
|
|
|
&[
|
|
|
|
(
|
|
|
|
mint_keypair.pubkey(),
|
|
|
|
Account::new(10_000, 0, &Pubkey::default()),
|
|
|
|
),
|
|
|
|
(Pubkey::new_rand(), Account::new(1, 0, &Pubkey::default())),
|
|
|
|
],
|
|
|
|
&[("hi".to_string(), Pubkey::new_rand())],
|
|
|
|
);
|
|
|
|
assert_eq!(block.accounts.len(), 2);
|
|
|
|
assert!(block.accounts.iter().any(
|
|
|
|
|(pubkey, account)| *pubkey == mint_keypair.pubkey() && account.lamports == 10_000
|
|
|
|
));
|
|
|
|
|
|
|
|
let path = &make_tmp_path("genesis_block");
|
|
|
|
block.write(&path).expect("write");
|
|
|
|
let loaded_block = GenesisBlock::load(&path).expect("load");
|
|
|
|
assert_eq!(block.hash(), loaded_block.hash());
|
|
|
|
let _ignored = std::fs::remove_file(&path);
|
2019-01-24 12:04:04 -08:00
|
|
|
}
|
2019-05-07 11:16:22 -07:00
|
|
|
|
2019-01-24 12:04:04 -08:00
|
|
|
}
|