Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor token crates #2493

Merged
merged 9 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Refactored sub-systems integration in the ABCI FinalizeBlock request handler.
([\#2482](https://github.com/anoma/namada/pull/2482))
1 change: 1 addition & 0 deletions .changelog/unreleased/improvements/2493-refactor-token.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Refactored token crates. ([\#2493](https://github.com/anoma/namada/pull/2493))
3 changes: 2 additions & 1 deletion Cargo.lock

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

11 changes: 6 additions & 5 deletions crates/apps/src/lib/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@
current_epoch + params.pipeline_len,
)
.unwrap();

namada::token::conversion::update_allowed_conversions(
&mut self.wl_storage,
)
.unwrap();

Check warning on line 421 in crates/apps/src/lib/bench_utils.rs

View check run for this annotation

Codecov / codecov/patch

crates/apps/src/lib/bench_utils.rs#L417-L421

Added lines #L417 - L421 were not covered by tests
}

pub fn init_ibc_client_state(&mut self, addr_key: Key) -> ClientId {
Expand Down Expand Up @@ -887,7 +892,7 @@

impl Default for BenchShieldedCtx {
fn default() -> Self {
let mut shell = BenchShell::default();
let shell = BenchShell::default();

Check warning on line 895 in crates/apps/src/lib/bench_utils.rs

View check run for this annotation

Codecov / codecov/patch

crates/apps/src/lib/bench_utils.rs#L895

Added line #L895 was not covered by tests
let base_dir = shell.tempdir.as_ref().canonicalize().unwrap();

// Create a global config and an empty wallet in the chain dir - this is
Expand Down Expand Up @@ -957,10 +962,6 @@
}

crate::wallet::save(&chain_ctx.wallet).unwrap();
namada::token::conversion::update_allowed_conversions(
&mut shell.wl_storage,
)
.unwrap();

Self {
shielded: ShieldedContext::default(),
Expand Down
5 changes: 3 additions & 2 deletions crates/apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ use namada::governance::pgf::parameters::PgfParameters;
use namada::ledger::eth_bridge::EthereumBridgeParams;
use namada::ledger::parameters::EpochDuration;
use namada::ledger::pos::{Dec, GenesisValidator, OwnedPosParams};
use namada::token;
use namada::types::address::{Address, EstablishedAddress};
use namada::types::chain::ProposalBytes;
use namada::types::key::*;
use namada::types::storage;
use namada::types::string_encoding::StringEncoded;
use namada::types::time::{DateTimeUtc, DurationSecs};
use namada::types::token::Denomination;
use namada::types::{storage, token};
use serde::{Deserialize, Serialize};

#[cfg(all(any(test, feature = "benches"), not(feature = "integration")))]
Expand Down Expand Up @@ -228,7 +229,7 @@ pub struct TokenAccount {
#[derivative(PartialOrd = "ignore", Ord = "ignore")]
pub balances: HashMap<Address, token::Amount>,
/// Token parameters
pub masp_params: Option<token::MaspParams>,
pub masp_params: Option<token::ShieldedParams>,
/// Token inflation from the last epoch (read + write for every epoch)
pub last_inflation: token::Amount,
/// Token shielded ratio from the last epoch (read + write for every epoch)
Expand Down
5 changes: 3 additions & 2 deletions crates/apps/src/lib/config/genesis/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use borsh::{BorshDeserialize, BorshSerialize};
use namada::eth_bridge::storage::parameters::{
Contracts, Erc20WhitelistEntry, MinimumConfirmations,
};
use namada::token;
use namada::types::address::Address;
use namada::types::chain::ProposalBytes;
use namada::types::dec::Dec;
use namada::types::ethereum_structs;
use namada::types::token::{
Amount, DenominatedAmount, Denomination, NATIVE_MAX_DECIMAL_PLACES,
};
use namada::types::{ethereum_structs, token};
use serde::{Deserialize, Serialize};

use super::transactions::{self, Transactions};
Expand Down Expand Up @@ -209,7 +210,7 @@ pub struct Tokens {
)]
pub struct TokenConfig {
pub denom: Denomination,
pub masp_params: Option<token::MaspParams>,
pub masp_params: Option<token::ShieldedParams>,
}

#[derive(
Expand Down
168 changes: 28 additions & 140 deletions crates/apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@
use namada::ledger::gas::{GasMetering, TxGasMeter};
use namada::ledger::pos::namada_proof_of_stake;
use namada::ledger::protocol::{self, WrapperArgs};
use namada::proof_of_stake;
use namada::proof_of_stake::storage::{
find_validator_by_raw_hash, read_last_block_proposer_address,
write_last_block_proposer_address,
find_validator_by_raw_hash, write_last_block_proposer_address,
};
use namada::state::wl_storage::WriteLogAndStorage;
use namada::state::write_log::StorageModification;
use namada::state::{
ResultExt, StorageRead, StorageWrite, EPOCH_SWITCH_BLOCKS_DELAY,
};
use namada::token::conversion::update_allowed_conversions;
use namada::tx::data::protocol::ProtocolTxType;
use namada::types::key::tm_raw_hash_to_string;
use namada::types::storage::{BlockHash, BlockResults, Epoch, Header};
use namada::vote_ext::ethereum_events::MultiSignedEthEvent;
use namada::vote_ext::ethereum_tx_data_variants;
use namada_sdk::tx::new_tx_event;

use super::governance::execute_governance_proposals;
use super::*;
use crate::facade::tendermint::abci::types::{Misbehavior, VoteInfo};
use crate::facade::tendermint::abci::types::VoteInfo;
use crate::node::ledger::shell::stats::InternalStats;

impl<D, H> Shell<D, H>
Expand Down Expand Up @@ -61,8 +60,7 @@
let mut response = shim::response::FinalizeBlock::default();

// Begin the new block and check if a new epoch has begun
let (height, new_epoch) =
self.update_state(req.header, req.hash, req.byzantine_validators);
let (height, new_epoch) = self.update_state(req.header, req.hash);

let (current_epoch, _gas) = self.wl_storage.storage.get_current_epoch();
let update_for_tendermint = matches!(
Expand All @@ -88,98 +86,31 @@
.expect("Failed tx hashes finalization")
}

let pos_params =
namada_proof_of_stake::storage::read_pos_params(&self.wl_storage)?;

if new_epoch {
update_allowed_conversions(&mut self.wl_storage)?;

execute_governance_proposals(self, &mut response)?;

// Copy the new_epoch + pipeline_len - 1 validator set into
// new_epoch + pipeline_len
namada_proof_of_stake::validator_set_update::copy_validator_sets_and_positions(
&mut self.wl_storage,
&pos_params,
current_epoch,
current_epoch + pos_params.pipeline_len,
)?;

// Compute the total stake of the consensus validator set and record
// it in storage
namada_proof_of_stake::compute_and_store_total_consensus_stake(
&mut self.wl_storage,
current_epoch,
)?;
}

let emit_events = &mut response.events;
// Get the actual votes from cometBFT in the preferred format
let votes = pos_votes_from_abci(&self.wl_storage, &req.votes);

// Invariant: Has to be applied before `record_slashes_from_evidence`
// because it potentially needs to be able to read validator state from
// previous epoch and jailing validator removes the historical state
if !votes.is_empty() {
self.log_block_rewards(
votes.clone(),
height,
current_epoch,
new_epoch,
)?;
}

// Invariant: This has to be applied after
// `copy_validator_sets_and_positions` and before `self.update_epoch`.
self.record_slashes_from_evidence();
// Invariant: This has to be applied after
// `copy_validator_sets_and_positions` if we're starting a new epoch
if new_epoch {
// Invariant: Process slashes before inflation as they may affect
// the rewards in the current epoch.
self.process_slashes();
self.apply_inflation(current_epoch, &mut response)?;
}

// Consensus set liveness check
if !votes.is_empty() {
let vote_height = height.prev_height();
let epoch_of_votes = self
.wl_storage
.storage
.block
.pred_epochs
.get_epoch(vote_height)
.expect(
"Should always find an epoch when looking up the vote \
height before recording liveness data.",
);
namada_proof_of_stake::record_liveness_data(
&mut self.wl_storage,
&votes,
epoch_of_votes,
vote_height,
&pos_params,
)?;
}

let validator_set_update_epoch =
self.get_validator_set_update_epoch(current_epoch);

// Jail validators for inactivity
namada_proof_of_stake::jail_for_liveness(
// Sub-system updates:
// - Token
token::finalize_block(&mut self.wl_storage, emit_events, new_epoch)?;
// - Governance
governance::finalize_block(self, emit_events, new_epoch)?;
// - PoS
// - Must be applied after governance in case it changes PoS params
proof_of_stake::finalize_block(
&mut self.wl_storage,
&pos_params,
current_epoch,
emit_events,
new_epoch,
validator_set_update_epoch,
votes,
req.byzantine_validators,
)?;

if new_epoch {
// Prune liveness data from validators that are no longer in the
// consensus set
namada_proof_of_stake::prune_liveness_data(
&mut self.wl_storage,
current_epoch,
)?;
// Apply PoS and PGF inflation
self.apply_inflation(current_epoch, &mut response)?;
}

let mut stats = InternalStats::default();
Expand Down Expand Up @@ -216,7 +147,7 @@
{
let mut tx_event = match tx.header().tx_type {
TxType::Wrapper(_) | TxType::Protocol(_) => {
Event::new_tx_event(&tx, height.0)
new_tx_event(&tx, height.0)

Check warning on line 150 in crates/apps/src/lib/node/ledger/shell/finalize_block.rs

View check run for this annotation

Codecov / codecov/patch

crates/apps/src/lib/node/ledger/shell/finalize_block.rs#L150

Added line #L150 was not covered by tests
}
_ => {
tracing::error!(
Expand Down Expand Up @@ -249,7 +180,7 @@
if ResultCode::from_u32(processed_tx.result.code).unwrap()
!= ResultCode::Ok
{
let mut tx_event = Event::new_tx_event(&tx, height.0);
let mut tx_event = new_tx_event(&tx, height.0);
tx_event["code"] = processed_tx.result.code.to_string();
tx_event["info"] =
format!("Tx rejected: {}", &processed_tx.result.info);
Expand Down Expand Up @@ -277,7 +208,7 @@
) = match &tx_header.tx_type {
TxType::Wrapper(wrapper) => {
stats.increment_wrapper_txs();
let tx_event = Event::new_tx_event(&tx, height.0);
let tx_event = new_tx_event(&tx, height.0);
let gas_meter = TxGasMeter::new(wrapper.gas_limit);
(
tx_event,
Expand All @@ -298,7 +229,7 @@
.tx_queue
.pop()
.expect("Missing wrapper tx in queue");
let mut event = Event::new_tx_event(&tx, height.0);
let mut event = new_tx_event(&tx, height.0);

match inner {
DecryptedTx::Decrypted => {
Expand Down Expand Up @@ -345,7 +276,7 @@
| ProtocolTxType::BridgePool
| ProtocolTxType::ValSetUpdateVext
| ProtocolTxType::ValidatorSetUpdate => (
Event::new_tx_event(&tx, height.0),
new_tx_event(&tx, height.0),
None,
TxGasMeter::new_from_sub_limit(0.into()),
None,
Expand All @@ -370,7 +301,7 @@
}
}
(
Event::new_tx_event(&tx, height.0),
new_tx_event(&tx, height.0),
None,
TxGasMeter::new_from_sub_limit(0.into()),
None,
Expand Down Expand Up @@ -398,7 +329,7 @@
}
}
(
Event::new_tx_event(&tx, height.0),
new_tx_event(&tx, height.0),
None,
TxGasMeter::new_from_sub_limit(0.into()),
None,
Expand Down Expand Up @@ -639,7 +570,6 @@
&mut self,
header: Header,
hash: BlockHash,
byzantine_validators: Vec<Misbehavior>,
) -> (BlockHeight, bool) {
let height = self.wl_storage.storage.get_last_block_height() + 1;

Expand All @@ -654,8 +584,6 @@
.set_header(header)
.expect("Setting a header shouldn't fail");

self.byzantine_validators = byzantine_validators;

let new_epoch = self
.wl_storage
.update_epoch(height, header_time)
Expand Down Expand Up @@ -727,47 +655,6 @@
Ok(())
}

// Process the proposer and votes in the block to assign their PoS rewards.
fn log_block_rewards(
&mut self,
votes: Vec<namada_proof_of_stake::types::VoteInfo>,
height: BlockHeight,
current_epoch: Epoch,
new_epoch: bool,
) -> Result<()> {
// Read the block proposer of the previously committed block in storage
// (n-1 if we are in the process of finalizing n right now).
match read_last_block_proposer_address(&self.wl_storage)? {
Some(proposer_address) => {
tracing::debug!(
"Found last block proposer: {proposer_address}"
);
namada_proof_of_stake::rewards::log_block_rewards(
&mut self.wl_storage,
if new_epoch {
current_epoch.prev()
} else {
current_epoch
},
&proposer_address,
votes,
)?;
}
None => {
if height > BlockHeight::default().next_height() {
tracing::error!(
"Can't find the last block proposer at height {height}"
);
} else {
tracing::debug!(
"No last block proposer at height {height}"
);
}
}
}
Ok(())
}

// Write the inner tx hash to storage and remove the corresponding wrapper
// hash since it's redundant (we check the inner tx hash too when validating
// the wrapper). Requires the wrapper transaction as argument to recover
Expand Down Expand Up @@ -902,6 +789,7 @@
liveness_missed_votes_handle, liveness_sum_missed_votes_handle,
read_consensus_validator_set_addresses,
};
use namada_sdk::tendermint::abci::types::MisbehaviorKind;
use namada_test_utils::tx_data::TxWriteData;
use namada_test_utils::TestWasms;
use test_log::test;
Expand Down
Loading
Loading