Skip to content

Commit

Permalink
Merge remote-tracking branch 'namada/tiago/main/max-block-size-genesi…
Browse files Browse the repository at this point in the history
…s-param' (#975) into main

* namada/tiago/main/max-block-size-genesis-param:
  changelog: add #975
  wasm: update checksums.json
  Fix consensus params config for ABCI++
  Add `max_proposal_bytes` ledger param
  • Loading branch information
juped committed Jan 10, 2023
2 parents 3850c3a + 80292e6 commit 614e6ae
Show file tree
Hide file tree
Showing 12 changed files with 308 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/975-max-block-size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add a max_proposal_bytes parameter to the ledger.
([#975](https://github.com/anoma/namada/pull/975))
31 changes: 25 additions & 6 deletions apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use namada::ledger::pos::{GenesisValidator, PosParams};
use namada::types::address::Address;
#[cfg(not(feature = "dev"))]
use namada::types::chain::ChainId;
use namada::types::chain::ProposalBytes;
use namada::types::key::dkg_session_keys::DkgPublicKey;
use namada::types::key::*;
use namada::types::time::{DateTimeUtc, DurationSecs};
Expand All @@ -32,6 +33,7 @@ pub mod genesis_config {
use namada::ledger::parameters::EpochDuration;
use namada::ledger::pos::{GenesisValidator, PosParams};
use namada::types::address::Address;
use namada::types::chain::ProposalBytes;
use namada::types::key::dkg_session_keys::DkgPublicKey;
use namada::types::key::*;
use namada::types::time::Rfc3339String;
Expand Down Expand Up @@ -222,17 +224,30 @@ pub mod genesis_config {

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ParametersConfig {
// Minimum number of blocks per epoch.
/// Max payload size, in bytes, for a tx batch proposal.
///
/// Block proposers may never return a `PrepareProposal`
/// response containing `txs` with a byte length greater
/// than whatever is configured through this parameter.
///
/// Note that this parameter's value will always be strictly
/// smaller than a Tendermint block's `MaxBytes` consensus
/// parameter. Currently, we hard cap `max_proposal_bytes`
/// at 90 MiB in Namada, which leaves at least 10 MiB of
/// room for header data, evidence and protobuf
/// serialization overhead in Tendermint blocks.
pub max_proposal_bytes: ProposalBytes,
/// Minimum number of blocks per epoch.
// XXX: u64 doesn't work with toml-rs!
pub min_num_of_blocks: u64,
// Maximum duration per block (in seconds).
/// Maximum duration per block (in seconds).
// TODO: this is i64 because datetime wants it
pub max_expected_time_per_block: i64,
// Hashes of whitelisted vps array. `None` value or an empty array
// disables whitelisting.
/// Hashes of whitelisted vps array. `None` value or an empty array
/// disables whitelisting.
pub vp_whitelist: Option<Vec<String>>,
// Hashes of whitelisted txs array. `None` value or an empty array
// disables whitelisting.
/// Hashes of whitelisted txs array. `None` value or an empty array
/// disables whitelisting.
pub tx_whitelist: Option<Vec<String>>,
/// Filename of implicit accounts validity predicate WASM code
pub implicit_vp: String,
Expand Down Expand Up @@ -574,6 +589,7 @@ pub mod genesis_config {
parameters.max_expected_time_per_block,
)
.into(),
max_proposal_bytes: parameters.max_proposal_bytes,
vp_whitelist: parameters.vp_whitelist.unwrap_or_default(),
tx_whitelist: parameters.tx_whitelist.unwrap_or_default(),
implicit_vp_code_path,
Expand Down Expand Up @@ -796,6 +812,8 @@ pub struct ImplicitAccount {
BorshDeserialize,
)]
pub struct Parameters {
// Max payload size, in bytes, for a tx batch proposal.
pub max_proposal_bytes: ProposalBytes,
/// Epoch duration
pub epoch_duration: EpochDuration,
/// Maximum expected time per block
Expand Down Expand Up @@ -867,6 +885,7 @@ pub fn genesis() -> Genesis {
min_duration: namada::types::time::Duration::seconds(600).into(),
},
max_expected_time_per_block: namada::types::time::DurationSecs(30),
max_proposal_bytes: Default::default(),
vp_whitelist: vec![],
tx_whitelist: vec![],
implicit_vp_code_path: vp_implicit_path.into(),
Expand Down
2 changes: 2 additions & 0 deletions apps/src/lib/node/ledger/shell/init_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ where
// Initialize protocol parameters
let genesis::Parameters {
epoch_duration,
max_proposal_bytes,
max_expected_time_per_block,
vp_whitelist,
tx_whitelist,
Expand Down Expand Up @@ -98,6 +99,7 @@ where
}
let parameters = Parameters {
epoch_duration,
max_proposal_bytes,
max_expected_time_per_block,
vp_whitelist,
tx_whitelist,
Expand Down
15 changes: 14 additions & 1 deletion apps/src/lib/node/ledger/tendermint_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::process::Command;

use crate::config;
use crate::facade::tendermint::Genesis;
use crate::facade::tendermint::{block, Genesis};
use crate::facade::tendermint_config::net::Address as TendermintAddress;
use crate::facade::tendermint_config::{
Error as TendermintError, TendermintConfig,
Expand Down Expand Up @@ -380,6 +380,19 @@ async fn write_tm_genesis(
genesis.genesis_time = genesis_time
.try_into()
.expect("Couldn't convert DateTimeUtc to Tendermint Time");
let size = block::Size {
// maximum size of a serialized Tendermint block
// cannot go over 100 MiB
max_bytes: (100 << 20) - 1, /* unsure if we are dealing with an open
* range, so it's better to subtract one,
* here */
// gas is metered app-side, so we disable it
// at the Tendermint level
max_gas: -1,
};
#[cfg(not(feature = "abcipp"))]
let size = Some(size);
genesis.consensus_params.block = size;
#[cfg(feature = "abcipp")]
{
genesis.consensus_params.timeout.commit =
Expand Down
57 changes: 47 additions & 10 deletions core/src/ledger/parameters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::storage::types::{decode, encode};
use super::storage::{types, Storage};
use crate::ledger::storage::{self as ledger_storage};
use crate::types::address::{Address, InternalAddress};
use crate::types::chain::ProposalBytes;
use crate::types::storage::Key;
use crate::types::time::DurationSecs;

Expand All @@ -32,6 +33,8 @@ pub struct Parameters {
pub epoch_duration: EpochDuration,
/// Maximum expected time per block (read only)
pub max_expected_time_per_block: DurationSecs,
/// Max payload size, in bytes, for a tx batch proposal.
pub max_proposal_bytes: ProposalBytes,
/// Whitelisted validity predicate hashes (read only)
pub vp_whitelist: Vec<String>,
/// Whitelisted tx hashes (read only)
Expand Down Expand Up @@ -101,6 +104,7 @@ impl Parameters {
let Self {
epoch_duration,
max_expected_time_per_block,
max_proposal_bytes,
vp_whitelist,
tx_whitelist,
implicit_vp,
Expand All @@ -111,6 +115,16 @@ impl Parameters {
pos_inflation_amount,
} = self;

// write max proposal bytes parameter
let max_proposal_bytes_key = storage::get_max_proposal_bytes_key();
let max_proposal_bytes_value = encode(&max_proposal_bytes);
storage
.write(&max_proposal_bytes_key, max_proposal_bytes_value)
.expect(
"Max proposal bytes parameter must be initialized in the \
genesis block",
);

// write epoch parameters
let epoch_key = storage::get_epoch_duration_storage_key();
let epoch_value = encode(epoch_duration);
Expand Down Expand Up @@ -382,6 +396,17 @@ where
DB: ledger_storage::DB + for<'iter> ledger_storage::DBIter<'iter>,
H: ledger_storage::StorageHasher,
{
// read max proposal bytes
let (max_proposal_bytes, gas_proposal_bytes) = {
let key = storage::get_max_proposal_bytes_key();
let (value, gas) =
storage.read(&key).map_err(ReadError::StorageError)?;
let value: ProposalBytes =
decode(value.ok_or(ReadError::ParametersMissing)?)
.map_err(ReadError::StorageTypeError)?;
(value, gas)
};

// read epoch duration
let (epoch_duration, gas_epoch) = read_epoch_duration_parameter(storage)
.expect("Couldn't read epoch duration parameters");
Expand Down Expand Up @@ -464,10 +489,31 @@ where
decode(value.ok_or(ReadError::ParametersMissing)?)
.map_err(ReadError::StorageTypeError)?;

let total_gas_cost = [
gas_epoch,
gas_tx,
gas_vp,
gas_time,
gas_implicit_vp,
gas_epy,
gas_gain_p,
gas_gain_d,
gas_staked,
gas_reward,
gas_proposal_bytes,
]
.into_iter()
.fold(0u64, |accum, gas| {
accum
.checked_add(gas)
.expect("u64 overflow occurred while doing gas arithmetic")
});

Ok((
Parameters {
epoch_duration,
max_expected_time_per_block,
max_proposal_bytes,
vp_whitelist,
tx_whitelist,
implicit_vp,
Expand All @@ -477,15 +523,6 @@ where
staked_ratio,
pos_inflation_amount,
},
gas_epoch
+ gas_tx
+ gas_vp
+ gas_time
+ gas_implicit_vp
+ gas_epy
+ gas_gain_p
+ gas_gain_d
+ gas_staked
+ gas_reward,
total_gas_cost,
))
}
19 changes: 19 additions & 0 deletions core/src/ledger/parameters/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct Keys {
staked_ratio: &'static str,
tx_whitelist: &'static str,
vp_whitelist: &'static str,
max_proposal_bytes: &'static str,
}

/// Returns if the key is a parameter key.
Expand Down Expand Up @@ -118,6 +119,14 @@ pub fn is_pos_inflation_amount_key(key: &Key) -> bool {
] if addr == &ADDRESS && pos_inflation_amount == Keys::VALUES.pos_inflation_amount)
}

/// Returns if the key is the max proposal bytes key.
pub fn is_max_proposal_bytes_key(key: &Key) -> bool {
matches!(&key.segments[..], [
DbKeySeg::AddressSeg(addr),
DbKeySeg::StringSeg(max_proposal_bytes),
] if addr == &ADDRESS && max_proposal_bytes == Keys::VALUES.max_proposal_bytes)
}

/// Storage key used for epoch parameter.
pub fn get_epoch_duration_storage_key() -> Key {
Key {
Expand Down Expand Up @@ -219,3 +228,13 @@ pub fn get_pos_inflation_amount_key() -> Key {
],
}
}

/// Storage key used for the max proposal bytes.
pub fn get_max_proposal_bytes_key() -> Key {
Key {
segments: vec![
DbKeySeg::AddressSeg(ADDRESS),
DbKeySeg::StringSeg(Keys::VALUES.max_proposal_bytes.to_string()),
],
}
}
1 change: 1 addition & 0 deletions core/src/ledger/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,7 @@ mod tests {
..Default::default()
};
let mut parameters = Parameters {
max_proposal_bytes: Default::default(),
epoch_duration: epoch_duration.clone(),
max_expected_time_per_block: Duration::seconds(max_expected_time_per_block).into(),
vp_whitelist: vec![],
Expand Down
Loading

0 comments on commit 614e6ae

Please sign in to comment.