Skip to content

Commit b0147b9

Browse files
committed
Merge branch 'grarco/refactor-get-fee-unshield' (#1877)
2 parents 63dc84b + 78a23f4 commit b0147b9

File tree

6 files changed

+31
-62
lines changed

6 files changed

+31
-62
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Refactored retrieval of `Transaction` object for fee unshielding.
2+
([\#1877](https://github.com/anoma/namada/pull/1877))

apps/src/lib/node/ledger/shell/mod.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ use namada::ledger::pos::namada_proof_of_stake::types::{
3838
ConsensusValidator, ValidatorSetUpdate,
3939
};
4040
use namada::ledger::protocol::{
41-
apply_wasm_tx, get_transfer_hash_from_storage, ShellParams,
41+
apply_wasm_tx, get_fee_unshielding_transaction,
42+
get_transfer_hash_from_storage, ShellParams,
4243
};
4344
use namada::ledger::storage::write_log::WriteLog;
4445
use namada::ledger::storage::{
@@ -1262,21 +1263,10 @@ where
12621263
return response;
12631264
}
12641265

1265-
let fee_unshield = wrapper
1266-
.unshield_section_hash
1267-
.and_then(|ref hash| tx.get_section(hash))
1268-
.and_then(|section| {
1269-
if let Section::MaspTx(transaction) = section.as_ref() {
1270-
Some(transaction.to_owned())
1271-
} else {
1272-
None
1273-
}
1274-
});
1275-
12761266
// Validate wrapper fees
12771267
if let Err(e) = self.wrapper_fee_check(
12781268
&wrapper,
1279-
fee_unshield,
1269+
get_fee_unshielding_transaction(&tx, &wrapper),
12801270
&mut TempWlStorage::new(&self.wl_storage.storage),
12811271
&mut self.vp_wasm_cache.clone(),
12821272
&mut self.tx_wasm_cache.clone(),

apps/src/lib/node/ledger/shell/prepare_proposal.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use namada::core::ledger::gas::TxGasMeter;
55
#[cfg(feature = "abcipp")]
66
use namada::ledger::eth_bridge::{EthBridgeQueries, SendValsetUpd};
77
use namada::ledger::pos::PosQueries;
8+
use namada::ledger::protocol::get_fee_unshielding_transaction;
89
use namada::ledger::storage::{DBIter, StorageHasher, TempWlStorage, DB};
910
use namada::proof_of_stake::find_validator_by_raw_hash;
10-
use namada::proto::{Section, Tx};
11+
use namada::proto::Tx;
1112
use namada::types::address::Address;
1213
use namada::types::internal::TxInQueue;
1314
use namada::types::key::tm_raw_hash_to_string;
@@ -249,20 +250,9 @@ where
249250
.map_err(|_| ())?;
250251

251252
// Check fees
252-
let fee_unshield =
253-
wrapper.unshield_section_hash.and_then(|ref hash| {
254-
tx.get_section(hash).and_then(|section| {
255-
if let Section::MaspTx(transaction) = section.as_ref() {
256-
Some(transaction.to_owned())
257-
} else {
258-
None
259-
}
260-
})
261-
});
262-
263253
match self.wrapper_fee_check(
264254
&wrapper,
265-
fee_unshield,
255+
get_fee_unshielding_transaction(&tx, &wrapper),
266256
temp_wl_storage,
267257
vp_wasm_cache,
268258
tx_wasm_cache,

apps/src/lib/node/ledger/shell/process_proposal.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use namada::core::hints;
66
use namada::core::ledger::storage::WlStorage;
77
use namada::ledger::eth_bridge::{EthBridgeQueries, SendValsetUpd};
88
use namada::ledger::pos::PosQueries;
9+
use namada::ledger::protocol::get_fee_unshielding_transaction;
910
use namada::ledger::storage::TempWlStorage;
1011
use namada::proof_of_stake::find_validator_by_raw_hash;
1112
use namada::types::internal::TxInQueue;
@@ -889,22 +890,9 @@ where
889890
}
890891

891892
// Check that the fee payer has sufficient balance.
892-
let fee_unshield =
893-
wrapper.unshield_section_hash.and_then(|ref hash| {
894-
tx.get_section(hash).and_then(|section| {
895-
if let Section::MaspTx(transaction) =
896-
section.as_ref()
897-
{
898-
Some(transaction.to_owned())
899-
} else {
900-
None
901-
}
902-
})
903-
});
904-
905893
match self.wrapper_fee_check(
906894
&wrapper,
907-
fee_unshield,
895+
get_fee_unshielding_transaction(&tx, &wrapper),
908896
temp_wl_storage,
909897
vp_wasm_cache,
910898
tx_wasm_cache,

core/src/proto/types.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,30 +1546,22 @@ impl Tx {
15461546
/// signatures over it
15471547
#[cfg(feature = "ferveo-tpke")]
15481548
pub fn encrypt(&mut self, pubkey: &EncryptionKey) -> &mut Self {
1549-
use crate::types::hash::Hash;
15501549
let header_hash = self.header_hash();
15511550
let mut plaintexts = vec![];
15521551
// Iterate backwrds to sidestep the effects of deletion on indexing
15531552
for i in (0..self.sections.len()).rev() {
15541553
match &self.sections[i] {
15551554
Section::Signature(sig)
15561555
if sig.targets.contains(&header_hash) => {}
1557-
Section::MaspTx(_) => {
1556+
masp_section @ Section::MaspTx(_) => {
15581557
// Do NOT encrypt the fee unshielding transaction
15591558
if let Some(unshield_section_hash) = self
15601559
.header()
15611560
.wrapper()
15621561
.expect("Tried to encrypt a non-wrapper tx")
15631562
.unshield_section_hash
15641563
{
1565-
if unshield_section_hash
1566-
== Hash(
1567-
self.sections[i]
1568-
.hash(&mut Sha256::new())
1569-
.finalize_reset()
1570-
.into(),
1571-
)
1572-
{
1564+
if unshield_section_hash == masp_section.get_hash() {
15731565
continue;
15741566
}
15751567
}

shared/src/ledger/protocol/mod.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,9 @@ where
169169
apply_protocol_tx(protocol_tx.tx, tx.data(), wl_storage)
170170
}
171171
TxType::Wrapper(ref wrapper) => {
172-
let masp_transaction =
173-
wrapper.unshield_section_hash.and_then(|ref hash| {
174-
tx.get_section(hash).and_then(|section| {
175-
if let Section::MaspTx(transaction) = section.as_ref() {
176-
Some(transaction.to_owned())
177-
} else {
178-
None
179-
}
180-
})
181-
});
182-
183172
let changed_keys = apply_wrapper_tx(
184173
wrapper,
185-
masp_transaction,
174+
get_fee_unshielding_transaction(&tx, wrapper),
186175
tx_bytes,
187176
ShellParams {
188177
tx_gas_meter,
@@ -280,6 +269,24 @@ where
280269
Ok(changed_keys)
281270
}
282271

272+
/// Retrieve the Masp `Transaction` for fee unshielding from the provided
273+
/// transaction, if present
274+
pub fn get_fee_unshielding_transaction(
275+
tx: &Tx,
276+
wrapper: &WrapperTx,
277+
) -> Option<Transaction> {
278+
wrapper
279+
.unshield_section_hash
280+
.and_then(|ref hash| tx.get_section(hash))
281+
.and_then(|section| {
282+
if let Section::MaspTx(transaction) = section.as_ref() {
283+
Some(transaction.to_owned())
284+
} else {
285+
None
286+
}
287+
})
288+
}
289+
283290
/// Charge fee for the provided wrapper transaction. In ABCI returns an error if
284291
/// the balance of the block proposer overflows. In ABCI plus returns error if:
285292
/// - The unshielding fails

0 commit comments

Comments
 (0)