Skip to content

Commit

Permalink
[fix] fix compile error; add custom serialization for tests
Browse files Browse the repository at this point in the history
Signed-off-by: VAmuzing <amuzik95@gmail.com>
  • Loading branch information
VAmuzing committed Apr 24, 2024
1 parent d15bda1 commit d3b579e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
7 changes: 1 addition & 6 deletions cli/src/lib.rs
Expand Up @@ -670,13 +670,10 @@ mod tests {
fn config_to_toml_value(config: PartialUserConfig) -> Result<toml::Value> {
use iroha_crypto::ExposedPrivateKey;
let private_key = config.private_key.as_ref().unwrap().clone();
let genesis_private_key = config.genesis.private_key.as_ref().unwrap().clone();
let mut result = toml::Value::try_from(config)?;

// private key will be serialized as "[REDACTED PrivateKey]" so need to restore it
result["private_key"] = toml::Value::try_from(ExposedPrivateKey(private_key))?;
result["genesis"]["private_key"] =
toml::Value::try_from(ExposedPrivateKey(genesis_private_key))?;

Ok(result)
}
Expand All @@ -688,9 +685,7 @@ mod tests {
let dir = tempfile::tempdir()?;

let genesis = RawGenesisBlockBuilder::default()
.executor_file(PathBuf::from(
dir.path().join("config/genesis/executor.wasm"),
))
.executor_file(dir.path().join("config/genesis/executor.wasm"))
.build();

let genesis_path = dir.path().join("config/genesis/gen.json");
Expand Down
19 changes: 8 additions & 11 deletions client/examples/register_1000_triggers.rs
Expand Up @@ -8,8 +8,7 @@ use iroha_data_model::trigger::TriggerId;
use iroha_genesis::{GenesisNetwork, RawGenesisBlock, RawGenesisBlockBuilder};
use iroha_primitives::unique_vec;
use test_network::{
get_chain_id, wait_for_genesis_committed_with_max_retries, Peer as TestPeer, PeerBuilder,
TestClient, TestRuntime,
get_chain_id, get_genesis_signature, wait_for_genesis_committed_with_max_retries, Peer as TestPeer, PeerBuilder, TestClient, TestRuntime
};
use tokio::runtime::Runtime;

Expand Down Expand Up @@ -57,25 +56,23 @@ fn generate_genesis(num_triggers: u32) -> Result<RawGenesisBlock, Box<dyn std::e
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut peer = <TestPeer>::new().expect("Failed to create peer");

let peer_key_pair = &peer.key_pair;
let chain_id = get_chain_id();
let mut configuration = get_config(
&unique_vec![peer.id.clone()],
Some(chain_id.clone()),
Some(peer.key_pair.clone()),
Some(peer_key_pair.clone()),
&get_genesis_signature(),
);

let raw_block = generate_genesis(1_000_u32)?;
let signature = GenesisNetwork::new_genesis_signature(raw_block.clone(), &chain_id, peer_key_pair);

// Increase executor limits for large genesis
configuration.chain_wide.executor_runtime.fuel_limit = u64::MAX;
configuration.chain_wide.executor_runtime.max_memory_bytes = u32::MAX;

let genesis = GenesisNetwork::new(
generate_genesis(1_000_u32)?,
&chain_id,
configuration
.genesis
.key_pair()
.expect("should be available in the config; probably a bug"),
);
let genesis = GenesisNetwork::try_parse(raw_block, signature)?;

let builder = PeerBuilder::new()
.with_into_genesis(genesis)
Expand Down
9 changes: 6 additions & 3 deletions core/src/sumeragi/main_loop.rs
Expand Up @@ -271,9 +271,12 @@ impl Sumeragi {
assert_eq!(state_view.latest_block_hash(), None);
}

let genesis_accepted_tx =
AcceptedTransaction::accept_genesis(genesis_network.into_transaction(), &self.chain_id)
.expect("Genesis invalid");
let genesis_accepted_tx = AcceptedTransaction::accept_genesis(
genesis_network.into_transaction(),
&self.chain_id,
genesis_public_key,
)
.expect("Genesis invalid");
let transactions = Vec::from([genesis_accepted_tx]);

let mut state_block = state.block();
Expand Down
12 changes: 11 additions & 1 deletion genesis/src/lib.rs
Expand Up @@ -58,13 +58,23 @@ pub enum GenesisSignatureParseError {
impl std::error::Error for GenesisSignatureParseError {}

/// [`SignedGenesisConfig`] contains data that is used for loading signed genesis from config.
#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode)]
pub struct GenesisSignature {
chain_id: ChainId,
creation_time: Duration,
signatures: SignaturesOf<TransactionPayload>,
}

impl Serialize for GenesisSignature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let hex_encoded_string = self.to_hex_string();
hex_encoded_string.serialize(serializer)
}
}

struct GenesisSignatureVisitor;

impl de::Visitor<'_> for GenesisSignatureVisitor {
Expand Down
6 changes: 3 additions & 3 deletions tools/kagami/src/genesis.rs
Expand Up @@ -272,6 +272,9 @@ fn generate_synthetic(
#[command(group = ArgGroup::new("private_key").required(true))]
#[command(group = ArgGroup::new("public_key").required(true))]
pub struct SignArgs {
/// Path to genesis json file
#[clap(long)]
genesis_file: PathBuf,
/// The algorithm of the provided keypair
#[clap(default_value_t, long, short)]
algorithm: crypto::AlgorithmArg,
Expand All @@ -293,9 +296,6 @@ pub struct SignArgs {
/// Unique id of blockchain
#[clap(long)]
chain_id: ChainId,
/// Path to genesis json file
#[clap(long, short)]
genesis_file: PathBuf,
/// Encode signed genesis block with SCALE (it is only supported with file output)
#[clap(long, short, default_value_t = false)]
binary: bool,
Expand Down
4 changes: 2 additions & 2 deletions tools/swarm/src/compose.rs
Expand Up @@ -407,8 +407,8 @@ impl DockerComposeBuilder<'_> {

let mut peers_iter = peers.iter();

let signature = self.signature.clone().map_or(
generate_hex_string_signature(&chain_id, &genesis_key_pair),
let signature = self.signature.clone().map_or_else(
|| generate_hex_string_signature(&chain_id, &genesis_key_pair),
|sign| sign,
);

Expand Down

0 comments on commit d3b579e

Please sign in to comment.