-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): git init installs genesis file
kindelia_core: * genesis.kdl file removed. (moved into kindelia/genesis/networks) * genesis Statements are passed to kindelia_core api, not network_id * added kindelia_core/genesis-tests.kdl for core test cases * updated test cases * remove empty constants.rs * remove genesis_path(). (moved into kindelia/src/genesis.rs) kindelia: * all genesis files get compiled into kindelia executable * latest (by name) genesis file gets installed by kindelia init * add kindelia/genesis/README.md * add genesis.rs and move some util fn into it * cargo add include_dir * cargo fmt fixes * parse genesis statements in 'node start' and 'test' * update test(s)
- Loading branch information
Showing
16 changed files
with
330 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,18 @@ | ||
# About this directory | ||
|
||
The `genesis/networks` directory holds genesis block files, one per network. | ||
|
||
Important! All files inside the `networks` sub-directory get compiled into the | ||
`kindelia` executable. So please do not put any other type of file inside and | ||
observe the naming convention. | ||
|
||
The files are named to match hexadecimal network identifiers as specified | ||
in the config file with extension `.kdl`. For example, `network/0xCAFE0006.kdl` corresponds to network `0xCAFE0006`. | ||
|
||
Typically a new network is created by bumping this value, eg creating the | ||
file `networks/0xCAFE0007.kdl`. Also `../default.toml` should be updated to match. | ||
|
||
The genesis file with the highest hex value is installed into the | ||
user's home directory when `kindelia init` is run. | ||
|
||
|
File renamed without changes.
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,79 @@ | ||
use include_dir::{include_dir, Dir, File}; | ||
use std::path::{Path, PathBuf}; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum GenesisPathError { | ||
#[error("Home directory not found")] | ||
HomeDirNotFound, | ||
#[error("File not found in {0}")] | ||
FileNotFound(PathBuf), | ||
} | ||
|
||
pub fn genesis_path(network_id: u32) -> Result<PathBuf, GenesisPathError> { | ||
let path = dirs::home_dir() | ||
.ok_or(GenesisPathError::HomeDirNotFound)? | ||
.join(".kindelia") | ||
.join("genesis") | ||
.join(format!("{:#02X}.kdl", network_id)); | ||
match path.exists() { | ||
true => Ok(path), | ||
false => Err(GenesisPathError::FileNotFound(path)), | ||
} | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum GenesisCodeError { | ||
#[error(transparent)] | ||
PathError(#[from] GenesisPathError), | ||
|
||
#[error("Genesis block could not be read from {path:?}.")] | ||
ReadError { path: PathBuf, cause: std::io::Error }, | ||
} | ||
|
||
pub fn genesis_code(network_id: u32) -> Result<String, GenesisCodeError> { | ||
let path = genesis_path(network_id)?; | ||
std::fs::read_to_string(&path) | ||
.map_err(|e| GenesisCodeError::ReadError { path, cause: e }) | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum InitGenesisError { | ||
#[error("Could not create directory: {path:?}")] | ||
DirNotCreated { path: std::path::PathBuf, cause: std::io::Error }, | ||
#[error("Unable to write genesis file: {path:?}")] | ||
FileNotWritten { path: std::path::PathBuf, cause: std::io::Error }, | ||
#[error("Genesis file is missing from the executable")] | ||
Missing, | ||
} | ||
|
||
static GENESIS_DIR: Dir<'_> = | ||
include_dir!("$CARGO_MANIFEST_DIR/genesis/networks"); | ||
|
||
/// Copies latest file from kindelia_core/genesis to <homedir>/.kindelia/genesis | ||
/// Creates target dir if not existing. | ||
/// | ||
/// The way this works is that all the files in kindelia_core/genesis get compiled | ||
/// into the executable by the include_dir!() macro. With this trick we are able | ||
/// to include files dynamically, whereas include_str!() requires a static str. | ||
/// | ||
/// note: we could copy over all files from kindelia_core/genesis instead. | ||
pub fn init_genesis(dir_path: &Path) -> Result<(), InitGenesisError> { | ||
let mut files: Vec<&File> = GENESIS_DIR.files().collect(); | ||
|
||
// files should be named as hex values, so we sort case insensitively | ||
// The goal here is to find highest numeric (hex) value. | ||
files.sort_by_cached_key(|f| f.path().as_os_str().to_ascii_uppercase()); | ||
|
||
let file = files.last().ok_or(InitGenesisError::Missing)?; | ||
let fname = file.path().file_name().ok_or(InitGenesisError::Missing)?; | ||
let fpath = dir_path.join(fname); | ||
|
||
let default_content = file.contents(); | ||
std::fs::create_dir_all(dir_path).map_err(|e| { | ||
InitGenesisError::DirNotCreated { path: dir_path.to_path_buf(), cause: e } | ||
})?; | ||
|
||
std::fs::write(&fpath, default_content) | ||
.map_err(|e| InitGenesisError::FileNotWritten { path: fpath, cause: e }) | ||
} |
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
Oops, something went wrong.