Skip to content

Commit

Permalink
Merge remote-tracking branch 'namada/tiago/main/fix-ledger-params-mat…
Browse files Browse the repository at this point in the history
…ch' (#903) into main

* namada/tiago/main/fix-ledger-params-match:
  changelog: add #903
  wasm: update checksums.json
  Remove boilerplate code in favor of StorageKeys macro
  Update Cargo deps
  Define Keys struct with all ledger param key types
  Fix #882
  • Loading branch information
juped committed Jan 10, 2023
2 parents 4d3440a + bfb8ae8 commit 3850c3a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Binary search ledger storage keys to match faster.
([#903](https://github.com/anoma/namada/pull/903))
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.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ testing = [
]

[dependencies]
namada_macros = {path = "../macros"}
ark-bls12-381 = {version = "0.3"}
ark-ec = {version = "0.3", optional = true}
ark-serialize = {version = "0.3"}
Expand Down
82 changes: 48 additions & 34 deletions core/src/ledger/parameters/storage.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
//! Parameters storage
use namada_macros::StorageKeys;

use super::ADDRESS;
use crate::types::storage::{DbKeySeg, Key};

const EPOCH_DURATION_KEY: &str = "epoch_duration";
const VP_WHITELIST_KEY: &str = "vp_whitelist";
const TX_WHITELIST_KEY: &str = "tx_whitelist";
const MAX_EXPECTED_TIME_PER_BLOCK_KEY: &str = "max_expected_time_per_block";
const IMPLICIT_VP_KEY: &str = "implicit_vp";
const EPOCHS_PER_YEAR_KEY: &str = "epochs_per_year";
const POS_GAIN_P_KEY: &str = "pos_gain_p";
const POS_GAIN_D_KEY: &str = "pos_gain_d";
const STAKED_RATIO_KEY: &str = "staked_ratio_key";
const POS_INFLATION_AMOUNT_KEY: &str = "pos_inflation_amount_key";
/// Storage keys for ledger parameters.
#[derive(StorageKeys)]
struct Keys {
epoch_duration: &'static str,
epochs_per_year: &'static str,
implicit_vp: &'static str,
max_expected_time_per_block: &'static str,
pos_gain_d: &'static str,
pos_gain_p: &'static str,
pos_inflation_amount: &'static str,
staked_ratio: &'static str,
tx_whitelist: &'static str,
vp_whitelist: &'static str,
}

/// Returns if the key is a parameter key.
pub fn is_parameter_key(key: &Key) -> bool {
Expand All @@ -20,98 +27,103 @@ pub fn is_parameter_key(key: &Key) -> bool {

/// Returns if the key is a protocol parameter key.
pub fn is_protocol_parameter_key(key: &Key) -> bool {
is_epoch_duration_storage_key(key)
|| is_max_expected_time_per_block_key(key)
|| is_tx_whitelist_key(key)
|| is_vp_whitelist_key(key)
let segment = match &key.segments[..] {
[DbKeySeg::AddressSeg(addr), DbKeySeg::StringSeg(segment)]
if addr == &ADDRESS =>
{
segment.as_str()
}
_ => return false,
};
Keys::ALL.binary_search(&segment).is_ok()
}

/// Returns if the key is an epoch storage key.
pub fn is_epoch_duration_storage_key(key: &Key) -> bool {
matches!(&key.segments[..], [
DbKeySeg::AddressSeg(addr),
DbKeySeg::StringSeg(epoch_duration),
] if addr == &ADDRESS && epoch_duration == EPOCH_DURATION_KEY)
] if addr == &ADDRESS && epoch_duration == Keys::VALUES.epoch_duration)
}

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

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

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

/// Returns if the key is the implicit VP key.
pub fn is_implicit_vp_key(key: &Key) -> bool {
matches!(&key.segments[..], [
DbKeySeg::AddressSeg(addr),
DbKeySeg::StringSeg(sub_key),
] if addr == &ADDRESS && sub_key == IMPLICIT_VP_KEY)
] if addr == &ADDRESS && sub_key == Keys::VALUES.implicit_vp)
}

/// Returns if the key is the epoch_per_year key.
pub fn is_epochs_per_year_key(key: &Key) -> bool {
matches!(&key.segments[..], [
DbKeySeg::AddressSeg(addr),
DbKeySeg::StringSeg(epochs_per_year),
] if addr == &ADDRESS && epochs_per_year == EPOCHS_PER_YEAR_KEY)
] if addr == &ADDRESS && epochs_per_year == Keys::VALUES.epochs_per_year)
}

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

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

/// Returns if the key is the staked ratio key.
pub fn is_staked_ratio_key(key: &Key) -> bool {
matches!(&key.segments[..], [
DbKeySeg::AddressSeg(addr),
DbKeySeg::StringSeg(staked_ratio),
] if addr == &ADDRESS && staked_ratio == STAKED_RATIO_KEY)
] if addr == &ADDRESS && staked_ratio == Keys::VALUES.staked_ratio)
}

/// Returns if the key is the PoS reward rate key.
pub fn is_pos_inflation_amount_key(key: &Key) -> bool {
matches!(&key.segments[..], [
DbKeySeg::AddressSeg(addr),
DbKeySeg::StringSeg(pos_inflation_amount),
] if addr == &ADDRESS && pos_inflation_amount == POS_INFLATION_AMOUNT_KEY)
] if addr == &ADDRESS && pos_inflation_amount == Keys::VALUES.pos_inflation_amount)
}

/// Storage key used for epoch parameter.
pub fn get_epoch_duration_storage_key() -> Key {
Key {
segments: vec![
DbKeySeg::AddressSeg(ADDRESS),
DbKeySeg::StringSeg(EPOCH_DURATION_KEY.to_string()),
DbKeySeg::StringSeg(Keys::VALUES.epoch_duration.to_string()),
],
}
}
Expand All @@ -121,7 +133,7 @@ pub fn get_vp_whitelist_storage_key() -> Key {
Key {
segments: vec![
DbKeySeg::AddressSeg(ADDRESS),
DbKeySeg::StringSeg(VP_WHITELIST_KEY.to_string()),
DbKeySeg::StringSeg(Keys::VALUES.vp_whitelist.to_string()),
],
}
}
Expand All @@ -131,7 +143,7 @@ pub fn get_tx_whitelist_storage_key() -> Key {
Key {
segments: vec![
DbKeySeg::AddressSeg(ADDRESS),
DbKeySeg::StringSeg(TX_WHITELIST_KEY.to_string()),
DbKeySeg::StringSeg(Keys::VALUES.tx_whitelist.to_string()),
],
}
}
Expand All @@ -141,7 +153,9 @@ pub fn get_max_expected_time_per_block_key() -> Key {
Key {
segments: vec![
DbKeySeg::AddressSeg(ADDRESS),
DbKeySeg::StringSeg(MAX_EXPECTED_TIME_PER_BLOCK_KEY.to_string()),
DbKeySeg::StringSeg(
Keys::VALUES.max_expected_time_per_block.to_string(),
),
],
}
}
Expand All @@ -151,7 +165,7 @@ pub fn get_implicit_vp_key() -> Key {
Key {
segments: vec![
DbKeySeg::AddressSeg(ADDRESS),
DbKeySeg::StringSeg(IMPLICIT_VP_KEY.to_string()),
DbKeySeg::StringSeg(Keys::VALUES.implicit_vp.to_string()),
],
}
}
Expand All @@ -161,7 +175,7 @@ pub fn get_epochs_per_year_key() -> Key {
Key {
segments: vec![
DbKeySeg::AddressSeg(ADDRESS),
DbKeySeg::StringSeg(EPOCHS_PER_YEAR_KEY.to_string()),
DbKeySeg::StringSeg(Keys::VALUES.epochs_per_year.to_string()),
],
}
}
Expand All @@ -171,7 +185,7 @@ pub fn get_pos_gain_p_key() -> Key {
Key {
segments: vec![
DbKeySeg::AddressSeg(ADDRESS),
DbKeySeg::StringSeg(POS_GAIN_P_KEY.to_string()),
DbKeySeg::StringSeg(Keys::VALUES.pos_gain_p.to_string()),
],
}
}
Expand All @@ -181,7 +195,7 @@ pub fn get_pos_gain_d_key() -> Key {
Key {
segments: vec![
DbKeySeg::AddressSeg(ADDRESS),
DbKeySeg::StringSeg(POS_GAIN_D_KEY.to_string()),
DbKeySeg::StringSeg(Keys::VALUES.pos_gain_d.to_string()),
],
}
}
Expand All @@ -191,7 +205,7 @@ pub fn get_staked_ratio_key() -> Key {
Key {
segments: vec![
DbKeySeg::AddressSeg(ADDRESS),
DbKeySeg::StringSeg(STAKED_RATIO_KEY.to_string()),
DbKeySeg::StringSeg(Keys::VALUES.staked_ratio.to_string()),
],
}
}
Expand All @@ -201,7 +215,7 @@ pub fn get_pos_inflation_amount_key() -> Key {
Key {
segments: vec![
DbKeySeg::AddressSeg(ADDRESS),
DbKeySeg::StringSeg(POS_INFLATION_AMOUNT_KEY.to_string()),
DbKeySeg::StringSeg(Keys::VALUES.pos_inflation_amount.to_string()),
],
}
}
1 change: 1 addition & 0 deletions wasm/Cargo.lock

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

36 changes: 18 additions & 18 deletions wasm/checksums.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"tx_bond.wasm": "tx_bond.263cf9433c3eeec3b6a53e885544e0fea19724f9680b14a15491f33ff122d402.wasm",
"tx_change_validator_commission.wasm": "tx_change_validator_commission.541f44f500255891db7e4ae79710159213d30599cf40062d59db2faea6122560.wasm",
"tx_ibc.wasm": "tx_ibc.62d9499a23842240f97f6caeed88fe28ff4e97eb2ff0db5dd837eb6b67d2a066.wasm",
"tx_init_account.wasm": "tx_init_account.a0a334950daa21e64fec0a98609268137eb2e35e5161cfa1da534ad71843c239.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.fc1a3347c4e978d579b074db77bea63db8f9ba450215bf556378fc6601c63f79.wasm",
"tx_init_validator.wasm": "tx_init_validator.63a13e7aef346e1e48c8e848cb4c5d59ab8ce5a88bba2ef67b00837faa2350cb.wasm",
"tx_reveal_pk.wasm": "tx_reveal_pk.05dfde69e33f0833fc25ad653066da73ef04a2b2f7894ba9fa509d2dd759ae8b.wasm",
"tx_transfer.wasm": "tx_transfer.f10da787df300b60e1a9ff8e4bd51fc5246646e40c4c82f2245fc6c132b7f8f5.wasm",
"tx_unbond.wasm": "tx_unbond.34b123ce66a5204a209a08da14403bd72bdb64ccc0066a7deaee48a5c26a4a42.wasm",
"tx_update_vp.wasm": "tx_update_vp.ee2e9b882c4accadf4626e87d801c9ac8ea8c61ccea677e0532fc6c1ee7db6a2.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.7764e840c0208e33c1d36248d90bbac1b46b3895da856f3b43023e4514452f7f.wasm",
"tx_withdraw.wasm": "tx_withdraw.804fb484757e1b8fba5032f413eb3ac8e8ec15f51a157c6b54840f00c040a5c0.wasm",
"vp_implicit.wasm": "vp_implicit.fea78a8683921be216d0fbe65ec8ca3e8821e67d1899f44bd2291e95c6744a95.wasm",
"vp_masp.wasm": "vp_masp.566dbb24f3ac9845cb8e62dc6542e805e43727861bc4503f748449b17a369772.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.60f93d2c2569490b63196f7c279ffbdb3cd71b785670a314b18e07f62c7c7c0d.wasm",
"vp_token.wasm": "vp_token.28a74e6898ce458b725783e060b949d336503a3ddb663fffe79bc21a0d0c1c00.wasm",
"vp_user.wasm": "vp_user.bd548ff4346b0633708596949c394eb34af5b4221d26df2483c993aef36d7abe.wasm",
"vp_validator.wasm": "vp_validator.b989ef2caa1fcae7e719dcce01186000cd23f96a111632574b4a07dfc27fcd7e.wasm"
"tx_bond.wasm": "tx_bond.72f3c77794fcddf39ecd173612050b4bf66cfd85a321629132772afe83880cdf.wasm",
"tx_change_validator_commission.wasm": "tx_change_validator_commission.9a48083f02ed36020b25af8c994c59fb95959e8e74636799807ac459ab797b25.wasm",
"tx_ibc.wasm": "tx_ibc.b2b17b54485dc124110d9e4d6debf6d849c014dd1360b35e53a77d80338acc06.wasm",
"tx_init_account.wasm": "tx_init_account.5d49dece8c29bcd7af0b984bb299b4e72a893b47bf2f1e7cdaa7ee4aa706ee65.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.37e431fad8c795f88eb14615e385e10b0cc0b31830ffa7e63d1a269c237752de.wasm",
"tx_init_validator.wasm": "tx_init_validator.43e98a3535f24162ab815473ff4b53193a982a5ab4625d2e99179e001107a99b.wasm",
"tx_reveal_pk.wasm": "tx_reveal_pk.d0cb24001c6fd9085ad307647ab4b25cd497aac117a66e726ac293f66c8e8d3e.wasm",
"tx_transfer.wasm": "tx_transfer.9b25f232df89a470568e6c222a323c1aaf288c3a108c1dfd1d38e819dbcee085.wasm",
"tx_unbond.wasm": "tx_unbond.ca48d7e50abb02c9c868af72bb970196b7f7950f74af47658043f425f31357ef.wasm",
"tx_update_vp.wasm": "tx_update_vp.3453db3b3c02252c0d2020dba038cbb9b66fed7c99243723a028c13c407a0936.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.f23a08f22f5f39218c04033aa7a6d87d560b5274591632b3ac8dd437d8d45e44.wasm",
"tx_withdraw.wasm": "tx_withdraw.0f36ae0f61d7e9ae367f92ee055125120184ad594848f2ae53ba84a543a57f45.wasm",
"vp_implicit.wasm": "vp_implicit.5b2617f6cbafb5d8c54132caa3e0cd6fdaf9e1bc6f73907810f4681be6515dbe.wasm",
"vp_masp.wasm": "vp_masp.a7d43a5e9f5aa43761f9a04c5228f8f0114fe790035ced8df75a0368b8a21273.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.ca6b9dbbe35fb2b3412bbc0031fc5c2924fa015aa0cdf1250f0012581b464703.wasm",
"vp_token.wasm": "vp_token.ac2383054b75fa5cffd8037ffc684e546483b1fb5f1cd2c86a82722475cd71e6.wasm",
"vp_user.wasm": "vp_user.5a4ae8fa16a5ac8ca4e905360f1bab016eaa920825d4e21e616baf80c20fc015.wasm",
"vp_validator.wasm": "vp_validator.c4c5734167df5576645b43a4240baceaec414cd75440d736727c02546e245ff7.wasm"
}
2 changes: 2 additions & 0 deletions wasm_for_tests/wasm_source/Cargo.lock

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

0 comments on commit 3850c3a

Please sign in to comment.