Skip to content

Commit

Permalink
[fix] change how signed genesis is represented; minor fixes
Browse files Browse the repository at this point in the history
Signed-off-by: VAmuzing <amuzik95@gmail.com>
  • Loading branch information
VAmuzing committed Apr 5, 2024
1 parent 0e57f34 commit 3ea17e9
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 124 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

17 changes: 12 additions & 5 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! should be constructed externally: (see `main.rs`).
#[cfg(debug_assertions)]
use core::sync::atomic::{AtomicBool, Ordering};
use std::{fs, path::Path, sync::Arc};
use std::{path::Path, sync::Arc};

use color_eyre::eyre::{eyre, Result, WrapErr};
use iroha_config::parameters::{actual::Root as Config, user::CliContext};
Expand All @@ -27,7 +27,7 @@ use iroha_core::{
IrohaNetwork,
};
use iroha_data_model::prelude::*;
use iroha_genesis::GenesisNetwork;
use iroha_genesis::{GenesisNetwork, RawGenesisBlock, SignedGenesisConfig};
use iroha_logger::actor::LoggerHandle;
use iroha_torii::Torii;
use tokio::{
Expand Down Expand Up @@ -518,10 +518,17 @@ pub fn read_config_and_genesis<P: AsRef<Path>>(
let config = Config::load(path, CliContext { submit_genesis })
.wrap_err("failed to load configuration")?;

let genesis = if let Genesis::Full { public_key: _, file } = &config.genesis {
let signed_json_block = fs::read(file)?;
let genesis = if let Genesis::Full {
public_key: _,
file,
encoded_config: encoded_genesis_config,
} = &config.genesis
{
let raw_genesis = RawGenesisBlock::from_path(file)?;

Some(serde_json::from_slice(signed_json_block.as_slice())?)
let signed_genesis_config = SignedGenesisConfig::from_hex_string(encoded_genesis_config)?;

Some(signed_genesis_config.validate(raw_genesis)?)
} else {
None
};
Expand Down
19 changes: 9 additions & 10 deletions client/examples/million_accounts_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ fn generate_genesis(num_domains: u32) -> (RawGenesisBlock, KeyPair) {
.finish_domain();
}

(builder
.executor_blob(
construct_executor("../default_executor").expect("Failed to construct executor"),
)
.build(), key_pair)
(
builder
.executor_blob(
construct_executor("../default_executor").expect("Failed to construct executor"),
)
.build(),
key_pair,
)
}

fn main_genesis() {
Expand All @@ -49,11 +52,7 @@ fn main_genesis() {
);
let rt = Runtime::test();
let (genesis, key_pair) = generate_genesis(1_000_000_u32);
let genesis = GenesisNetwork::new(
genesis,
&chain_id,
&key_pair
);
let genesis = GenesisNetwork::new(genesis, &chain_id, &key_pair);

let builder = PeerBuilder::new()
.with_into_genesis(genesis)
Expand Down
7 changes: 4 additions & 3 deletions config/src/parameters/actual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,18 @@ pub enum Genesis {
Full {
/// Genesis account public key
public_key: PublicKey,
/// Path to the signed genesis block
/// Path to the genesis file
file: PathBuf,
/// Hex-encoded genesis config
encoded_config: String,
},
}

impl Genesis {
/// Access the public key, which is always present in the genesis config
pub fn public_key(&self) -> &PublicKey {
match self {
Self::Partial { public_key } => public_key,
Self::Full { public_key, .. } => public_key,
Self::Full { public_key, .. } | Self::Partial { public_key } => public_key,
}
}
}
Expand Down
30 changes: 9 additions & 21 deletions config/src/parameters/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,43 +309,31 @@ pub(crate) fn private_key_from_env<E: Error>(
pub struct Genesis {
pub public_key: PublicKey,
pub file: Option<PathBuf>,
pub encoded_config: Option<String>,
}

impl Genesis {
// fn parse(self, cli: CliContext) -> actual::Genesis {
// match self.file {
// None => actual::Genesis::Partial {
// public_key: self.public_key,
// },
// 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),
// }
// }
fn parse(self, cli: CliContext) -> Result<actual::Genesis, GenesisConfigError> {
match (self.file, cli.submit_genesis) {
(None, false) => Ok(actual::Genesis::Partial {
match (self.file, self.encoded_config, cli.submit_genesis) {
(None, None, false) => Ok(actual::Genesis::Partial {
public_key: self.public_key,
}),
(Some(file), true) => Ok(actual::Genesis::Full {
(Some(file), Some(encoded_config), true) => Ok(actual::Genesis::Full {
public_key: self.public_key,
file,
encoded_config,
}),
(Some(_), false) => Err(GenesisConfigError::GenesisWithoutSubmit),
(None, true) => Err(GenesisConfigError::SubmitWithoutGenesis),
(_, _, false) => Err(GenesisConfigError::GenesisWithoutSubmit),
(_, _, true) => Err(GenesisConfigError::SubmitWithoutGenesis),
}
}
}

#[derive(Debug, displaydoc::Display, thiserror::Error)]
#[derive(Copy, Clone, Debug, displaydoc::Display, thiserror::Error)]
pub enum GenesisConfigError {
/// `genesis.file` and `genesis.public_key` are presented, but `--submit-genesis` was not set
GenesisWithoutSubmit,
/// `--submit-genesis` was set, but `genesis.file` and `genesis.public_key` are not presented
/// `--submit-genesis` was set, but `genesis.file`, `genesis.public_key` and `genesis.encoded_config` are not presented
SubmitWithoutGenesis,
}

Expand Down
13 changes: 13 additions & 0 deletions config/src/parameters/user/boilerplate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl FromEnv for RootPartial {
pub struct GenesisPartial {
pub public_key: UserField<PublicKey>,
pub file: UserField<PathBuf>,
pub encoded_config: UserField<String>,
}

impl UnwrapPartial for GenesisPartial {
Expand All @@ -198,9 +199,12 @@ impl UnwrapPartial for GenesisPartial {

let file = self.file.get();

let encoded_config = self.encoded_config.get();

Ok(Genesis {
public_key,
file,
encoded_config,
})
}
}
Expand All @@ -223,11 +227,20 @@ impl FromEnv for GenesisPartial {
let file =
ParseEnvResult::parse_simple(&mut emitter, env, "GENESIS_FILE", "genesis.file").into();

let encoded_config = ParseEnvResult::parse_simple(
&mut emitter,
env,
"ENCODED_CONFIG",
"genesis.encoded_config",
)
.into();

emitter.finish()?;

Ok(Self {
public_key,
file,
encoded_config,
})
}
}
Expand Down
6 changes: 1 addition & 5 deletions config/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
use eyre::Result;
use iroha_config::parameters::{
actual::{Genesis, Root},
user::RootPartial,
user::{CliContext, RootPartial},
};
use iroha_config_base::{FromEnv, TestEnv, UnwrapPartial as _};

Expand Down Expand Up @@ -183,9 +183,7 @@ fn config_with_genesis() -> Result<()> {
Ok(())
}

// TODO: Verify
#[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 @@ -203,7 +201,6 @@ 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 @@ -264,7 +261,6 @@ 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
1 change: 1 addition & 0 deletions config/tests/fixtures/minimal_alone_with_genesis.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ extends = "base.toml"
[genesis]
file = "./empty_ok_genesis.json"
public_key = "ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB"
encoded_config = "9030303030303030302d303030302d303030302d303030302d303030303030303030303030ea9933ab8e0100000400804c390aef5d919d5506756ac51f7dd7e768da13fa60413fd5dc136851e9117213010145dd0ec90d6982851cdac085b54085a0e78689f14844742dcfc7da15b6ef78055584658cad5b9d69da200fa3997e490bfea2d7c417be9f6221d5ca89ba650008"
4 changes: 2 additions & 2 deletions core/test_network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ impl TestGenesis for GenesisNetwork {
upgrade_executor_permission,
] {
first_transaction
.append_instruction(Grant::permission(permission, alice_id.clone()).into());
.push_instruction(Grant::permission(permission, alice_id.clone()).into());
}

for isi in extra_isi.into_iter() {
first_transaction.append_instruction(isi);
first_transaction.push_instruction(isi);
}

GenesisNetwork::new(
Expand Down
23 changes: 23 additions & 0 deletions data_model/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,29 @@ impl SignedTransaction {
}
}

#[cfg(feature = "transparent_api")]
impl TryFrom<(SignaturesOf<TransactionPayload>, TransactionPayload)> for SignedTransaction {
type Error = &'static str;

fn try_from(
value: (SignaturesOf<TransactionPayload>, TransactionPayload),
) -> Result<Self, Self::Error> {
let (signatures, payload) = value;
if let Executable::Instructions(isi) = &payload.instructions {
if isi.is_empty() {
return Err("Transaction is empty");
}
}
signatures
.verify(&payload)
.map_err(|_| "Transaction contains invalid signatures")?;
Ok(SignedTransaction::V1(SignedTransactionV1 {
signatures,
payload,
}))
}
}

#[cfg(feature = "transparent_api")]
impl From<SignedTransaction> for (AccountId, Executable) {
fn from(source: SignedTransaction) -> Self {
Expand Down
3 changes: 2 additions & 1 deletion genesis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ workspace = true

[dependencies]
iroha_crypto = { workspace = true }
iroha_data_model = { workspace = true, features = ["http"] }
iroha_data_model = { workspace = true, features = ["http", "transparent_api"] }
iroha_schema = { workspace = true }

parity-scale-codec = { workspace = true, features = ["derive"] }
Expand All @@ -22,6 +22,7 @@ serde_json = { workspace = true }
once_cell = { workspace = true }
tracing = { workspace = true }
eyre = { workspace = true }
hex = { workspace = true }

[dev-dependencies]
iroha_crypto = { workspace = true, features = ["rand"] }
Loading

0 comments on commit 3ea17e9

Please sign in to comment.