Skip to content

Commit

Permalink
fixup! governance: fix finalize_block with DI
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Aug 30, 2024
1 parent ae95058 commit 1e319fb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 62 deletions.
68 changes: 11 additions & 57 deletions crates/governance/src/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ use crate::{storage, ProposalVote, ADDRESS as GOV_ADDRESS};
/// Apply governance updates for a block. On a new epoch, this will look for
/// proposals to tally completed proposals and execute accepted proposals.
#[allow(clippy::too_many_arguments)]
pub fn finalize_block<S, Token, PoS, FnTx, VpCache, TxCache, FnIbcTransfer>(
pub fn finalize_block<S, Token, PoS, FnTx, FnIbcTransfer>(
state: &mut S,
vp_wasm_cache: &mut VpCache,
tx_wasm_cache: &mut TxCache,
events: &mut impl EmitEvents,
current_epoch: Epoch,
is_new_epoch: bool,
Expand All @@ -46,7 +44,7 @@ where
S: StateRead + State,
Token: token::Read<S> + token::Write<S> + token::Events<S>,
PoS: proof_of_stake::Read<S>,
FnTx: FnMut(&Tx, &mut S, &mut VpCache, &mut TxCache) -> Result<bool>,
FnTx: FnMut(&Tx, &mut S) -> Result<bool>,
FnIbcTransfer: Fn(&mut S, &Address, &Address, &PGFIbcTarget) -> Result<()>,
{
if is_new_epoch {
Expand All @@ -55,34 +53,14 @@ where
Token,
PoS,
FnTx,
VpCache,
TxCache,
FnIbcTransfer,
>(
state,
vp_wasm_cache,
tx_wasm_cache,
events,
current_epoch,
dispatch_tx,
transfer_over_ibc,
)?;
>(state, events, current_epoch, dispatch_tx, transfer_over_ibc)?;
}
Ok(())
}

fn load_and_execute_governance_proposals<
S,
Token,
PoS,
FnTx,
VpCache,
TxCache,
FnIbcTransfer,
>(
fn load_and_execute_governance_proposals<S, Token, PoS, FnTx, FnIbcTransfer>(
state: &mut S,
vp_wasm_cache: &mut VpCache,
tx_wasm_cache: &mut TxCache,
events: &mut impl EmitEvents,
current_epoch: Epoch,
dispatch_tx: FnTx,
Expand All @@ -92,42 +70,22 @@ where
S: StateRead + State,
Token: token::Read<S> + token::Write<S> + token::Events<S>,
PoS: proof_of_stake::Read<S>,
FnTx: FnMut(&Tx, &mut S, &mut VpCache, &mut TxCache) -> Result<bool>,
FnTx: FnMut(&Tx, &mut S) -> Result<bool>,
FnIbcTransfer: Fn(&mut S, &Address, &Address, &PGFIbcTarget) -> Result<()>,
{
let proposal_ids = load_proposals(state, current_epoch)?;

execute_governance_proposals::<
S,
Token,
PoS,
FnTx,
VpCache,
TxCache,
FnIbcTransfer,
>(
execute_governance_proposals::<S, Token, PoS, FnTx, FnIbcTransfer>(
state,
vp_wasm_cache,
tx_wasm_cache,
events,
proposal_ids,
dispatch_tx,
transfer_over_ibc,
)
}

fn execute_governance_proposals<
S,
Token,
PoS,
FnTx,
VpCache,
TxCache,
FnIbcTransfer,
>(
fn execute_governance_proposals<S, Token, PoS, FnTx, FnIbcTransfer>(
state: &mut S,
vp_wasm_cache: &mut VpCache,
tx_wasm_cache: &mut TxCache,
events: &mut impl EmitEvents,
proposal_ids: BTreeSet<u64>,
mut dispatch_tx: FnTx,
Expand All @@ -137,7 +95,7 @@ where
S: StateRead + State,
Token: token::Read<S> + token::Write<S> + token::Events<S>,
PoS: proof_of_stake::Read<S>,
FnTx: FnMut(&Tx, &mut S, &mut VpCache, &mut TxCache) -> Result<bool>,
FnTx: FnMut(&Tx, &mut S) -> Result<bool>,
FnIbcTransfer: Fn(&mut S, &Address, &Address, &PGFIbcTarget) -> Result<()>,
{
for id in proposal_ids {
Expand Down Expand Up @@ -187,8 +145,6 @@ where
.unwrap_or_default();
let result = execute_default_proposal(
state,
vp_wasm_cache,
tx_wasm_cache,
id,
proposal_code.clone(),
&mut dispatch_tx,
Expand Down Expand Up @@ -405,17 +361,15 @@ where
})
}

fn execute_default_proposal<S, FnTx, VpCache, TxCache>(
fn execute_default_proposal<S, FnTx>(
state: &mut S,
vp_wasm_cache: &mut VpCache,
tx_wasm_cache: &mut TxCache,
id: u64,
proposal_code: Vec<u8>,
dispatch_tx: &mut FnTx,
) -> Result<bool>
where
S: StateRead + State,
FnTx: FnMut(&Tx, &mut S, &mut VpCache, &mut TxCache) -> Result<bool>,
FnTx: FnMut(&Tx, &mut S) -> Result<bool>,
{
let pending_execution_key = keys::get_proposal_execution_key(id);
state.write(&pending_execution_key, ())?;
Expand All @@ -425,7 +379,7 @@ where
tx.set_data(Data::new(encode(&id)));
tx.set_code(Code::new(proposal_code, None));

let dispatch_result = dispatch_tx(&tx, state, vp_wasm_cache, tx_wasm_cache);
let dispatch_result = dispatch_tx(&tx, state);
state
.delete(&pending_execution_key)
.expect("Should be able to delete the storage.");
Expand Down
8 changes: 3 additions & 5 deletions crates/node/src/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,22 +1139,20 @@ where
D: DB + for<'iter> DBIter<'iter> + Sync,
H: StorageHasher + Sync,
{
let vp_wasm_cache = &mut shell.vp_wasm_cache;
let tx_wasm_cache = &mut shell.tx_wasm_cache;
governance::finalize_block::<
_,
token::Store<_>,
proof_of_stake::Store<_>,
_,
_,
_,
_,
>(
&mut shell.state,
&mut shell.vp_wasm_cache,
&mut shell.tx_wasm_cache,
emit_events,
current_epoch,
is_new_epoch,
|tx, state, vp_wasm_cache, tx_wasm_cache| {
|tx, state| {
let dispatch_result = protocol::dispatch_tx(
tx,
protocol::DispatchArgs::Raw {
Expand Down

0 comments on commit 1e319fb

Please sign in to comment.