Skip to content

Commit

Permalink
[fix] genesis to have 1 transaction: executor
Browse files Browse the repository at this point in the history
fix tests
change test_env.py to reflect the changes

Signed-off-by: VAmuzing <amuzik95@gmail.com>
  • Loading branch information
VAmuzing committed Apr 2, 2024
1 parent fea4e86 commit ace1afa
Show file tree
Hide file tree
Showing 28 changed files with 88 additions and 155 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Expand Up @@ -58,8 +58,8 @@ tracing = { workspace = true }
tokio = { workspace = true, features = ["macros", "signal"] }
once_cell = { workspace = true }
owo-colors = { workspace = true, features = ["supports-colors"] }
parity-scale-codec = { workspace = true }
supports-color = { workspace = true }
serde_json = { workspace = true }

thread-local-panic-hook = { version = "0.1.0", optional = true }

Expand Down
6 changes: 2 additions & 4 deletions cli/src/lib.rs
Expand Up @@ -36,8 +36,6 @@ use tokio::{
task,
};

use parity_scale_codec::Decode;

// FIXME: move from CLI
pub mod samples;

Expand Down Expand Up @@ -520,9 +518,9 @@ pub fn read_config_and_genesis<P: AsRef<Path>>(
.wrap_err("failed to load configuration")?;

let genesis = if let Genesis::Full { public_key: _, file } = &config.genesis {
let encoded_block = fs::read(file)?;
let signed_json_block = fs::read(file)?;

Some(GenesisNetwork::decode(&mut encoded_block.as_slice())?)
Some(serde_json::from_slice(signed_json_block.as_slice())?)
} else {
None
};
Expand Down
1 change: 0 additions & 1 deletion config/iroha_test_config.toml
Expand Up @@ -7,7 +7,6 @@ address = "127.0.0.1:1337"

[genesis]
public_key = "ed01204CFFD0EE429B1BDD36B3910EC570852B8BB63F18750341772FB46BC856C5CAAF"
public_key_algorithm = "ed25519"
file = "./genesis.json"

[torii]
Expand Down
33 changes: 6 additions & 27 deletions config/src/parameters/user.rs
Expand Up @@ -146,14 +146,7 @@ impl Root {
None
}, Some);

let genesis = self.genesis.parse().map_or_else(
|err| {
// FIXME
emitter.emit(eyre!("{err}"));
None
},
Some,
);
let genesis = self.genesis.parse();

let kura = self.kura.parse();

Expand Down Expand Up @@ -197,7 +190,6 @@ impl Root {
p2p_address,
};
let (telemetry, dev_telemetry) = telemetries.unwrap();
let genesis = genesis.unwrap();
let sumeragi = {
let mut x = sumeragi.unwrap();
x.trusted_peers.push(peer.peer_id());
Expand All @@ -223,7 +215,6 @@ impl Root {
}
}

// TODO: FIX?
pub(crate) fn private_key_from_env<E: Error>(
emitter: &mut Emitter<Report>,
env: &impl ReadEnv<E>,
Expand Down Expand Up @@ -296,34 +287,22 @@ pub struct Genesis {
}

impl Genesis {
fn parse(self) -> Result<actual::Genesis, GenesisConfigError> {
fn parse(self) -> actual::Genesis {
match self.file {
None => Ok(actual::Genesis::Partial {
None => actual::Genesis::Partial {
public_key: self.public_key,
}),
Some(file) => Ok(actual::Genesis::Full {
},
Some(file) => actual::Genesis::Full {
public_key: self.public_key,
file,
}),
},
// (Some(_), Some(_), false) => Err(GenesisConfigError::GenesisWithoutSubmit),
// (None, None, true) => Err(GenesisConfigError::SubmitWithoutGenesis),
// _ => Err(GenesisConfigError::Inconsistent),
}
}
}

#[derive(Debug, displaydoc::Display, thiserror::Error)]
pub enum GenesisConfigError {
/// `genesis.file` and `genesis.private_key` are presented, but `--submit-genesis` was not set
GenesisWithoutSubmit,
/// `--submit-genesis` was set, but `genesis.file` and `genesis.private_key` are not presented
SubmitWithoutGenesis,
/// `genesis.file` and `genesis.private_key` should be set together
Inconsistent,
/// failed to construct the genesis's keypair using `genesis.public_key` and `genesis.private_key` configuration parameters
KeyPair(#[from] iroha_crypto::error::Error),
}

#[derive(Debug)]
pub struct Kura {
pub init_mode: KuraInitMode,
Expand Down
9 changes: 3 additions & 6 deletions config/tests/fixtures.rs
Expand Up @@ -179,6 +179,7 @@ fn config_with_genesis() -> Result<()> {
}

#[test]
#[ignore = "--submit-genesis was removed, more in #4225"]
fn minimal_with_genesis_but_no_cli_arg_fails() -> Result<()> {
let error = RootPartial::from_toml(fixtures_dir().join("minimal_alone_with_genesis.toml"))?
.unwrap_partial()?
Expand All @@ -194,6 +195,7 @@ fn minimal_with_genesis_but_no_cli_arg_fails() -> Result<()> {
}

#[test]
#[ignore = "--submit-genesis was removed, more in #4225"]
fn minimal_without_genesis_but_with_submit_fails() -> Result<()> {
let error = RootPartial::from_toml(fixtures_dir().join("minimal_with_trusted_peers.toml"))?
.unwrap_partial()?
Expand Down Expand Up @@ -250,6 +252,7 @@ fn extra_fields() {
}

#[test]
#[ignore = "temporarily, more in #4225"]
fn inconsistent_genesis_config() -> Result<()> {
let error = RootPartial::from_toml(fixtures_dir().join("inconsistent_genesis.toml"))?
.unwrap_partial()
Expand Down Expand Up @@ -304,11 +307,6 @@ fn full_envs_set_is_consumed() -> Result<()> {
),
),
),
private_key: Some(
ed25519(
"8F4C15E5D664DA3F13778801D23D4E89B76E94C1B94B389544168B6CB894F84F8BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB",
),
),
file: None,
},
kura: KuraPartial {
Expand Down Expand Up @@ -422,7 +420,6 @@ fn multiple_env_parsing_errors() {

let expected = expect_test::expect![[r#"
`PRIVATE_KEY_PAYLOAD` env was provided, but `PRIVATE_KEY_ALGORITHM` was not
failed to parse `genesis.private_key.algorithm` field from `GENESIS_PRIVATE_KEY_ALGORITHM` env variable
failed to parse `kura.debug.output_new_blocks` field from `KURA_DEBUG_OUTPUT_NEW_BLOCKS` env variable
failed to parse `logger.format` field from `LOG_FORMAT` env variable
failed to parse `torii.address` field from `API_ADDRESS` env variable"#]];
Expand Down
2 changes: 1 addition & 1 deletion config/tests/fixtures/absolute_paths.toml
Expand Up @@ -11,4 +11,4 @@ out_file = "/telemetry/file.json"

[genesis]
file = "/oh/my/genesis.json"
private_key = { algorithm = "ed25519", payload = "8f4c15e5d664da3f13778801d23d4e89b76e94c1b94b389544168b6cb894f84f8ba62848cf767d72e7f7f4b9d2d7ba07fee33760f79abe5597a51520e292a0cb" }
public_key = "ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB"
2 changes: 1 addition & 1 deletion config/tests/fixtures/bad.extends_nowhere.toml
@@ -1 +1 @@
extends = "nowhere.toml"
extends = "nowhere.toml"
2 changes: 1 addition & 1 deletion config/tests/fixtures/bad.extra_fields.toml
@@ -1,4 +1,4 @@
# Iroha should not silently ignore extra fields
i_am_unknown = true
foo = false
bar = 0.5
bar = 0.5
2 changes: 1 addition & 1 deletion config/tests/fixtures/bad.missing_fields.toml
@@ -1 +1 @@
# all fields are missing
# all fields are missing
2 changes: 1 addition & 1 deletion config/tests/fixtures/base.toml
Expand Up @@ -10,4 +10,4 @@ address = "127.0.0.1:1337"
public_key = "ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB"

[torii]
address = "127.0.0.1:8080"
address = "127.0.0.1:8080"
2 changes: 1 addition & 1 deletion config/tests/fixtures/empty_ok_genesis.json
@@ -1,4 +1,4 @@
{
"transactions": [],
"executor_file": "./executor.wasm"
}
}
2 changes: 0 additions & 2 deletions config/tests/fixtures/full.env
Expand Up @@ -4,8 +4,6 @@ PRIVATE_KEY_ALGORITHM=ed25519
PRIVATE_KEY_PAYLOAD=8f4c15e5d664da3f13778801d23d4e89b76e94c1b94b389544168b6cb894f84f8ba62848cf767d72e7f7f4b9d2d7ba07fee33760f79abe5597a51520e292a0cb
P2P_ADDRESS=127.0.0.1:5432
GENESIS_PUBLIC_KEY=ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB
GENESIS_PRIVATE_KEY_ALGORITHM=ed25519
GENESIS_PRIVATE_KEY_PAYLOAD=8f4c15e5d664da3f13778801d23d4e89b76e94c1b94b389544168b6cb894f84f8ba62848cf767d72e7f7f4b9d2d7ba07fee33760f79abe5597a51520e292a0cb
API_ADDRESS=127.0.0.1:8080
KURA_INIT_MODE=strict
KURA_STORE_DIR=/store/path/from/env
Expand Down
1 change: 0 additions & 1 deletion config/tests/fixtures/full.toml
Expand Up @@ -7,7 +7,6 @@ private_key = { algorithm = "ed25519", payload = "8f4c15e5d664da3f13778801d23d4e
[genesis]
file = "genesis.json"
public_key = "ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB"
private_key = { algorithm = "ed25519", payload = "8f4c15e5d664da3f13778801d23d4e89b76e94c1b94b389544168b6cb894f84f8ba62848cf767d72e7f7f4b9d2d7ba07fee33760f79abe5597a51520e292a0cb" }

[network]
address = "localhost:3840"
Expand Down
3 changes: 1 addition & 2 deletions config/tests/fixtures/inconsistent_genesis.toml
@@ -1,7 +1,6 @@
extends = "base.toml"

[genesis]
private_key.algorithm = "ed25519"
private_key.payload = "8f4c15e5d664da3f13778801d23d4e89b76e94c1b94b389544168b6cb894f84f8ba62848cf767d72e7f7f4b9d2d7ba07fee33760f79abe5597a51520e292a0cb"
public_key = "ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB"
# should fail without it:
# file = ...
3 changes: 1 addition & 2 deletions config/tests/fixtures/minimal_alone_with_genesis.toml
Expand Up @@ -2,5 +2,4 @@ extends = "base.toml"

[genesis]
file = "./empty_ok_genesis.json"
private_key.algorithm = "ed25519"
private_key.payload = "8f4c15e5d664da3f13778801d23d4e89b76e94c1b94b389544168b6cb894f84f8ba62848cf767d72e7f7f4b9d2d7ba07fee33760f79abe5597a51520e292a0cb"
public_key = "ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB"
2 changes: 1 addition & 1 deletion config/tests/fixtures/minimal_file_and_env.env
@@ -1 +1 @@
API_ADDRESS=127.0.0.1:8080
API_ADDRESS=127.0.0.1:8080
2 changes: 1 addition & 1 deletion config/tests/fixtures/multiple_extends.1.toml
@@ -1,2 +1,2 @@
[logger]
format = "pretty"
format = "pretty"
2 changes: 1 addition & 1 deletion config/tests/fixtures/multiple_extends.2.toml
Expand Up @@ -2,4 +2,4 @@
extends = "multiple_extends.2a.toml"

[logger]
format = "compact"
format = "compact"
2 changes: 1 addition & 1 deletion config/tests/fixtures/multiple_extends.2a.toml
@@ -1,2 +1,2 @@
[logger]
level = "DEBUG"
level = "DEBUG"
2 changes: 1 addition & 1 deletion config/tests/fixtures/multiple_extends.toml
Expand Up @@ -3,4 +3,4 @@ extends = ["multiple_extends.1.toml", "multiple_extends.2.toml"]

[logger]
# final value
level = "ERROR"
level = "ERROR"
1 change: 0 additions & 1 deletion configs/peer.template.toml
Expand Up @@ -16,7 +16,6 @@
[genesis]
# file =
# public_key =
# private_key = { algorithm = "", payload = "" }

[network]
# address =
Expand Down
11 changes: 1 addition & 10 deletions core/test_network/src/lib.rs
Expand Up @@ -130,16 +130,7 @@ impl TestGenesis for GenesisNetwork {
GenesisNetwork::new(
genesis.try_into().expect("genesis should load fine"),
&cfg.common.chain_id,
// TODO: FIX
&KeyPair::random()
// {
// use iroha_config::parameters::actual::Genesis;
// if let Genesis::Full { key_pair, .. } = &cfg.genesis {
// key_pair
// } else {
// unreachable!("test config should contain full genesis config (or it is a bug)")
// }
// },
&cfg.common.key_pair,
)
}
}
Expand Down
23 changes: 21 additions & 2 deletions genesis/src/lib.rs
Expand Up @@ -43,13 +43,32 @@ impl GenesisNetwork {
raw_block: RawGenesisBlock,
chain_id: &ChainId,
genesis_key_pair: &KeyPair,
) -> GenesisNetwork {
Self::new_with_executor_and_txs(raw_block.executor, raw_block.transactions, chain_id, genesis_key_pair)
}

/// Construct from configuration with only [`Executor`] from genesis block
pub fn new_without_transactions(
raw_block: RawGenesisBlock,
chain_id: &ChainId,
genesis_key_pair: &KeyPair,
) -> GenesisNetwork {
Self::new_with_executor_and_txs(raw_block.executor, Vec::new(), chain_id, genesis_key_pair)
}

/// Construct from configuration with explicit separation between [`Executor`] and transactions
fn new_with_executor_and_txs(
executor: Executor,
transactions: Vec<GenesisTransactionBuilder>,
chain_id: &ChainId,
genesis_key_pair: &KeyPair,
) -> GenesisNetwork {
// The first instruction should be Executor upgrade.
// This makes it possible to grant permissions to users in genesis.
let transactions_iter = std::iter::once(GenesisTransactionBuilder {
isi: vec![Upgrade::new(raw_block.executor).into()],
isi: vec![Upgrade::new(executor).into()],
})
.chain(raw_block.transactions);
.chain(transactions);

let transactions = transactions_iter
.map(|raw_transaction| raw_transaction.sign(chain_id.clone(), genesis_key_pair))
Expand Down
6 changes: 3 additions & 3 deletions hooks/pre-commit.sample
Expand Up @@ -4,6 +4,6 @@ cargo +nightly fmt --all -- --check
cargo +nightly lints clippy --workspace --benches --tests --examples --all-features
cargo run --bin kagami -- genesis >configs/swarm/genesis.json
cargo run --bin kagami -- schema >docs/source/references/schema.json
cargo run --bin kagami -- crypto key-pair-generation -j >configs/swarm/keypair.json
cargo run --bin kagami -- crypto --keypair-path configs/swarm/keypair.json --genesis-path configs/swarm/genesis.json --chain-id 01234 -o configs/swarm/signes_genesis.scale
git add configs/peer/genesis.json configs/swarm/keypair.json configs/swarm/signes_genesis.scale docs/source/references/schema.json
cargo run --bin kagami -- crypto generate-key-pair -j >configs/swarm/keypair.json
cargo run --bin kagami -- crypto sign-transaction --keypair-file configs/swarm/keypair.json --genesis-file configs/swarm/genesis.json --chain-id 00000000-0000-0000-0000-000000000000 --out-file configs/swarm/signed_genesis.json
git add configs/peer/genesis.json configs/swarm/keypair.json configs/swarm/signed_genesis.json docs/source/references/schema.json

0 comments on commit ace1afa

Please sign in to comment.