Serialize genesis block using bincode (#4687)

* use mmap to read the genesis block, and deserialize
This commit is contained in:
Pankaj Garg 2019-06-14 14:22:52 -07:00 committed by GitHub
parent 3fe5f886d7
commit 02abf422df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 7 deletions

1
Cargo.lock generated
View File

@ -2690,6 +2690,7 @@ dependencies = [
"hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
"num-derive 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "num-derive 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -386,7 +386,7 @@ if [[ -z $CI ]]; then # Skip in CI
fi fi
new_gensis_block() { new_gensis_block() {
! diff -q "$SOLANA_RSYNC_CONFIG_DIR"/ledger/genesis.json "$ledger_config_dir"/genesis.json >/dev/null 2>&1 ! diff -q "$SOLANA_RSYNC_CONFIG_DIR"/ledger/genesis.bin "$ledger_config_dir"/genesis.bin >/dev/null 2>&1
} }
set -e set -e

View File

@ -18,6 +18,7 @@ generic-array = { version = "0.13.0", default-features = false, features = ["ser
hex = "0.3.2" hex = "0.3.2"
itertools = "0.8.0" itertools = "0.8.0"
log = "0.4.2" log = "0.4.2"
memmap = "0.6.2"
num-derive = "0.2" num-derive = "0.2"
num-traits = "0.2" num-traits = "0.2"
rand = "0.6.5" rand = "0.6.5"

View File

@ -9,7 +9,9 @@ use crate::pubkey::Pubkey;
use crate::signature::{Keypair, KeypairUtil}; use crate::signature::{Keypair, KeypairUtil};
use crate::system_program; use crate::system_program;
use crate::timing::{DEFAULT_SLOTS_PER_EPOCH, DEFAULT_TICKS_PER_SLOT}; use crate::timing::{DEFAULT_SLOTS_PER_EPOCH, DEFAULT_TICKS_PER_SLOT};
use std::fs::File; use bincode::{deserialize, serialize};
use memmap::Mmap;
use std::fs::{File, OpenOptions};
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
@ -75,19 +77,27 @@ impl GenesisBlock {
} }
pub fn load(ledger_path: &str) -> Result<Self, std::io::Error> { pub fn load(ledger_path: &str) -> Result<Self, std::io::Error> {
let file = File::open(&Path::new(ledger_path).join("genesis.json"))?; let file = OpenOptions::new()
let genesis_block = serde_json::from_reader(file)?; .read(true)
.open(&Path::new(ledger_path).join("genesis.bin"))
.expect("Unable to open genesis file");
//UNSAFE: Required to create a Mmap
let mem = unsafe { Mmap::map(&file).expect("failed to map the genesis file") };
let genesis_block = deserialize(&mem)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, format!("{:?}", err)))?;
Ok(genesis_block) Ok(genesis_block)
} }
pub fn write(&self, ledger_path: &str) -> Result<(), std::io::Error> { pub fn write(&self, ledger_path: &str) -> Result<(), std::io::Error> {
let serialized = serde_json::to_string(self)?; let serialized = serialize(&self)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, format!("{:?}", err)))?;
let dir = Path::new(ledger_path); let dir = Path::new(ledger_path);
std::fs::create_dir_all(&dir)?; std::fs::create_dir_all(&dir)?;
let mut file = File::create(&dir.join("genesis.json"))?; let mut file = File::create(&dir.join("genesis.bin"))?;
file.write_all(&serialized.into_bytes()) file.write_all(&serialized)
} }
} }