Skip to content

Commit

Permalink
refactor: Change parameters format in genesis.json (#5020)
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Murzin <[email protected]>
  • Loading branch information
dima74 authored Sep 9, 2024
1 parent 3e7a77e commit a60a1a0
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 77 deletions.
102 changes: 81 additions & 21 deletions data_model/src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::collections::btree_map;

use iroha_data_model_derive::model;
use iroha_primitives::json::JsonString;
use nonzero_ext::nonzero;

pub use self::model::*;
use crate::{name::Name, Identifiable};
Expand All @@ -17,6 +16,11 @@ pub(crate) type CustomParameters = btree_map::BTreeMap<CustomParameterId, Custom

#[model]
mod model {
#[cfg(not(feature = "std"))]
use alloc::collections::BTreeMap;
#[cfg(feature = "std")]
use std::collections::BTreeMap;

use derive_more::{Constructor, Display, FromStr};
use getset::{CopyGetters, Getters};
use iroha_data_model_derive::IdEqOrdHash;
Expand Down Expand Up @@ -70,16 +74,19 @@ mod model {
///
/// A block is created if this limit or [`BlockParameters::max_transactions`] limit is reached,
/// whichever comes first. Regardless of the limits, an empty block is never created.
#[serde(default = "defaults::sumeragi::block_time_ms")]
pub block_time_ms: u64,
/// Time (in milliseconds) a peer will wait for a block to be committed.
///
/// If this period expires the block will request a view change
#[serde(default = "defaults::sumeragi::commit_time_ms")]
pub commit_time_ms: u64,
/// Maximal allowed random deviation from the nominal rate
///
/// # Warning
///
/// This value should be kept as low as possible to not affect soundness of the consensus
#[serde(default = "defaults::sumeragi::max_clock_drift_ms")]
pub max_clock_drift_ms: u64,
}

Expand Down Expand Up @@ -248,21 +255,28 @@ mod model {
pub struct Parameters {
/// Sumeragi parameters
#[getset(get_copy = "pub")]
#[serde(default)]
pub sumeragi: SumeragiParameters,
/// Block parameters
#[getset(get_copy = "pub")]
#[serde(default)]
pub block: BlockParameters,
/// Transaction parameters
#[getset(get_copy = "pub")]
#[serde(default)]
pub transaction: TransactionParameters,
/// Executor parameters
#[getset(get_copy = "pub")]
#[serde(default)]
pub executor: SmartContractParameters,
/// Smart contract parameters
#[getset(get_copy = "pub")]
#[serde(default)]
pub smart_contract: SmartContractParameters,
/// Collection of blockchain specific parameters
#[getset(get = "pub")]
#[serde(default)]
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub custom: CustomParameters,
}

Expand Down Expand Up @@ -344,45 +358,86 @@ impl SumeragiParameters {
}
}

mod defaults {
pub mod sumeragi {
pub const fn block_time_ms() -> u64 {
2_000
}
pub const fn commit_time_ms() -> u64 {
4_000
}
pub const fn max_clock_drift_ms() -> u64 {
1_000
}
}

pub mod block {
use core::num::NonZeroU64;

use nonzero_ext::nonzero;

/// Default value for [`Parameters::MaxTransactionsInBlock`]
pub const fn max_transactions() -> NonZeroU64 {
nonzero!(2_u64.pow(9))
}
}

pub mod transaction {
use core::num::NonZeroU64;

use nonzero_ext::nonzero;

pub const fn max_instructions() -> NonZeroU64 {
nonzero!(2_u64.pow(12))
}
pub const fn smart_contract_size() -> NonZeroU64 {
nonzero!(4 * 2_u64.pow(20))
}
}

pub mod smart_contract {
use core::num::NonZeroU64;

use nonzero_ext::nonzero;

pub const fn fuel() -> NonZeroU64 {
nonzero!(55_000_000_u64)
}
pub const fn memory() -> NonZeroU64 {
nonzero!(55_000_000_u64)
}
}
}

impl Default for SumeragiParameters {
fn default() -> Self {
pub const DEFAULT_BLOCK_TIME_MS: u64 = 2_000;
pub const DEFAULT_COMMIT_TIME_MS: u64 = 4_000;
pub const DEFAULT_MAX_CLOCK_DRIFT_MS: u64 = 1_000;

use defaults::sumeragi::*;
Self {
block_time_ms: DEFAULT_BLOCK_TIME_MS,
commit_time_ms: DEFAULT_COMMIT_TIME_MS,
max_clock_drift_ms: DEFAULT_MAX_CLOCK_DRIFT_MS,
block_time_ms: block_time_ms(),
commit_time_ms: commit_time_ms(),
max_clock_drift_ms: max_clock_drift_ms(),
}
}
}
impl Default for BlockParameters {
fn default() -> Self {
/// Default value for [`Parameters::MaxTransactionsInBlock`]
pub const DEFAULT_TRANSACTIONS_IN_BLOCK: NonZeroU64 = nonzero!(2_u64.pow(9));

Self::new(DEFAULT_TRANSACTIONS_IN_BLOCK)
Self::new(defaults::block::max_transactions())
}
}

impl Default for TransactionParameters {
fn default() -> Self {
const DEFAULT_INSTRUCTION_NUMBER: NonZeroU64 = nonzero!(2_u64.pow(12));
const DEFAULT_SMART_CONTRACT_SIZE: NonZeroU64 = nonzero!(4 * 2_u64.pow(20));

Self::new(DEFAULT_INSTRUCTION_NUMBER, DEFAULT_SMART_CONTRACT_SIZE)
use defaults::transaction::*;
Self::new(max_instructions(), smart_contract_size())
}
}

impl Default for SmartContractParameters {
fn default() -> Self {
const DEFAULT_FUEL: NonZeroU64 = nonzero!(55_000_000_u64);
const DEFAULT_MEMORY: NonZeroU64 = nonzero!(55_000_000_u64);

use defaults::smart_contract::*;
Self {
fuel: DEFAULT_FUEL,
memory: DEFAULT_MEMORY,
fuel: fuel(),
memory: memory(),
}
}
}
Expand Down Expand Up @@ -516,7 +571,9 @@ mod candidate {

#[derive(Decode, Deserialize)]
struct TransactionParametersCandidate {
#[serde(default = "defaults::transaction::max_instructions")]
max_instructions: NonZeroU64,
#[serde(default = "defaults::transaction::smart_contract_size")]
smart_contract_size: NonZeroU64,
}

Expand All @@ -527,6 +584,7 @@ mod candidate {

#[derive(Decode, Deserialize)]
struct BlockParametersCandidate {
#[serde(default = "super::defaults::block::max_transactions")]
max_transactions: NonZeroU64,
}

Expand All @@ -538,7 +596,9 @@ mod candidate {

#[derive(Decode, Deserialize)]
struct SmartContractParametersCandidate {
#[serde(default = "super::defaults::smart_contract::fuel")]
fuel: NonZeroU64,
#[serde(default = "super::defaults::smart_contract::memory")]
memory: NonZeroU64,
}

Expand Down
72 changes: 21 additions & 51 deletions defaults/genesis.json
Original file line number Diff line number Diff line change
@@ -1,58 +1,28 @@
{
"chain": "00000000-0000-0000-0000-000000000000",
"executor": "./executor.wasm",
"parameters": [
{
"Sumeragi": {
"BlockTimeMs": 2000
}
},
{
"Sumeragi": {
"CommitTimeMs": 4000
}
},
{
"Sumeragi": {
"MaxClockDriftMs": 1000
}
},
{
"Block": {
"MaxTransactions": 512
}
},
{
"Transaction": {
"MaxInstructions": 4096
}
},
{
"Transaction": {
"SmartContractSize": 4194304
}
},
{
"Executor": {
"Fuel": 55000000
}
},
{
"Executor": {
"Memory": 55000000
}
},
{
"SmartContract": {
"Fuel": 55000000
}
},
{
"SmartContract": {
"Memory": 55000000
}
"parameters": {
"sumeragi": {
"block_time_ms": 2000,
"commit_time_ms": 4000,
"max_clock_drift_ms": 1000
},
"block": {
"max_transactions": 512
},
"transaction": {
"max_instructions": 4096,
"smart_contract_size": 4194304
},
"executor": {
"fuel": 55000000,
"memory": 55000000
},
"smart_contract": {
"fuel": 55000000,
"memory": 55000000
}
],
},
"instructions": [
{
"Register": {
Expand Down
5 changes: 4 additions & 1 deletion docs/source/references/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2727,6 +2727,9 @@
"Option<Option<NonZero<u64>>>": {
"Option": "Option<NonZero<u64>>"
},
"Option<Parameters>": {
"Option": "Parameters"
},
"Option<PeerId>": {
"Option": "PeerId"
},
Expand Down Expand Up @@ -3411,7 +3414,7 @@
},
{
"name": "parameters",
"type": "Vec<Parameter>"
"type": "Option<Parameters>"
},
{
"name": "instructions",
Expand Down
Loading

0 comments on commit a60a1a0

Please sign in to comment.