Skip to content

Commit

Permalink
fixup! Derive inner tx hash using wrapper hash
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Jun 17, 2024
1 parent 1305d90 commit 15477e0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
2 changes: 2 additions & 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 crates/apps_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ config.workspace = true
data-encoding.workspace = true
derivative.workspace = true
directories.workspace = true
either.workspace = true
eyre.workspace = true
fd-lock.workspace = true
flate2.workspace = true
Expand Down
34 changes: 29 additions & 5 deletions crates/apps_lib/src/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use namada::governance::cli::onchain::{
};
use namada::io::Io;
use namada::state::EPOCH_SWITCH_BLOCKS_DELAY;
use namada::tx::data::compute_inner_tx_hash;
use namada::tx::{CompressedAuthorization, Section, Signer, Tx};
use namada_sdk::args::TxBecomeValidator;
use namada_sdk::rpc::{InnerTxResult, TxBroadcastData, TxResponse};
Expand Down Expand Up @@ -298,8 +299,11 @@ where
sign(namada, &mut tx, &args.tx, signing_data).await?;

let cmt = tx.first_commitments().unwrap().to_owned();
let wrapper_hash = tx.wrapper_hash();
let response = namada.submit(tx, &args.tx).await?;
if let Some(result) = response.is_applied_and_valid(&cmt) {
if let Some(result) =
response.is_applied_and_valid(wrapper_hash.as_ref(), &cmt)
{
return Ok(result.initialized_accounts.first().cloned());
}
}
Expand Down Expand Up @@ -377,10 +381,14 @@ pub async fn submit_change_consensus_key(
} else {
sign(namada, &mut tx, &args.tx, signing_data).await?;
let cmt = tx.first_commitments().unwrap().to_owned();
let wrapper_hash = tx.wrapper_hash();
let resp = namada.submit(tx, &args.tx).await?;

if !args.tx.dry_run {
if resp.is_applied_and_valid(&cmt).is_some() {
if resp
.is_applied_and_valid(wrapper_hash.as_ref(), &cmt)
.is_some()
{
namada.wallet_mut().await.save().unwrap_or_else(|err| {
edisplay_line!(namada.io(), "{}", err)
});
Expand Down Expand Up @@ -571,6 +579,7 @@ pub async fn submit_become_validator(
} else {
sign(namada, &mut tx, &args.tx, signing_data).await?;
let cmt = tx.first_commitments().unwrap().to_owned();
let wrapper_hash = tx.wrapper_hash();
let resp = namada.submit(tx, &args.tx).await?;

if args.tx.dry_run {
Expand All @@ -581,7 +590,10 @@ pub async fn submit_become_validator(
safe_exit(0)
}

if resp.is_applied_and_valid(&cmt).is_none() {
if resp
.is_applied_and_valid(wrapper_hash.as_ref(), &cmt)
.is_none()
{
display_line!(
namada.io(),
"Transaction failed. No key or addresses have been saved."
Expand Down Expand Up @@ -787,11 +799,18 @@ pub async fn submit_shielding_transfer(
} else {
sign(namada, &mut tx, &args.tx, signing_data).await?;
let cmt_hash = tx.first_commitments().unwrap().get_hash();
let wrapper_hash = tx.wrapper_hash();
let result = namada.submit(tx, &args.tx).await?;
match result {
ProcessTxResponse::Applied(resp) if
// If a transaction is rejected by a VP
matches!(resp.batch_result().get(&cmt_hash), Some(InnerTxResult::VpsRejected(_))) =>
matches!(
resp.batch_result().get(&compute_inner_tx_hash(
wrapper_hash.as_ref(),
either::Left(&cmt_hash)
)),
Some(InnerTxResult::VpsRejected(_))
) =>
{
let submission_masp_epoch = rpc::query_and_print_masp_epoch(namada).await;
// And its submission epoch doesn't match construction epoch
Expand Down Expand Up @@ -1107,9 +1126,14 @@ where
} else {
sign(namada, &mut tx, &args.tx, signing_data).await?;
let cmt = tx.first_commitments().unwrap().to_owned();
let wrapper_hash = tx.wrapper_hash();
let resp = namada.submit(tx, &args.tx).await?;

if !args.tx.dry_run && resp.is_applied_and_valid(&cmt).is_some() {
if !args.tx.dry_run
&& resp
.is_applied_and_valid(wrapper_hash.as_ref(), &cmt)
.is_some()
{
tx::query_unbonds(namada, args.clone(), latest_withdrawal_pre)
.await?;
}
Expand Down
1 change: 1 addition & 0 deletions crates/sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ circular-queue.workspace = true
data-encoding.workspace = true
derivation-path.workspace = true
duration-str.workspace = true
either.workspace = true
ethbridge-bridge-contract.workspace = true
ethers.workspace = true
eyre.workspace = true
Expand Down
24 changes: 17 additions & 7 deletions crates/sdk/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ use namada_token::storage_key::balance_key;
use namada_token::DenominatedAmount;
use namada_tx::data::pgf::UpdateStewardCommission;
use namada_tx::data::pos::{BecomeValidator, ConsensusKeyChange};
use namada_tx::data::{pos, BatchedTxResult, ResultCode, TxResult};
use namada_tx::data::{
compute_inner_tx_hash, pos, BatchedTxResult, ResultCode, TxResult,
};
pub use namada_tx::{Authorization, *};
use num_traits::Zero;
use rand_core::{OsRng, RngCore};
Expand Down Expand Up @@ -159,13 +161,17 @@ impl ProcessTxResponse {
/// all VPs. Note that this always returns false for dry-run transactions.
pub fn is_applied_and_valid(
&self,
wrapper_hash: Option<&Hash>,
cmt: &TxCommitments,
) -> Option<&BatchedTxResult> {
match self {
ProcessTxResponse::Applied(resp) => {
if resp.code == ResultCode::Ok {
if let Some(InnerTxResult::Success(result)) =
resp.batch_result().get(&cmt.get_hash())
resp.batch_result().get(&compute_inner_tx_hash(
wrapper_hash,
either::Right(cmt),
))
{
return Some(result);
}
Expand Down Expand Up @@ -241,6 +247,7 @@ pub async fn process_tx(
// We use this to determine when the wrapper tx makes it on-chain
let tx_hash = tx.header_hash().to_string();
let cmts = tx.commitments().clone();
let wrapper_hash = tx.wrapper_hash();
// We use this to determine when the decrypted inner tx makes it
// on-chain
let to_broadcast = TxBroadcastData::Live { tx, tx_hash };
Expand All @@ -253,7 +260,10 @@ pub async fn process_tx(
Ok(resp) => {
for cmt in cmts {
if let Some(InnerTxResult::Success(result)) =
resp.batch_result().get(&cmt.get_hash())
resp.batch_result().get(&compute_inner_tx_hash(
wrapper_hash.as_ref(),
either::Right(&cmt),
))
{
save_initialized_accounts(
context,
Expand Down Expand Up @@ -400,13 +410,13 @@ pub async fn submit_tx(

/// Display a result of a tx batch.
pub fn display_batch_resp(context: &impl Namada, resp: &TxResponse) {
for (cmt_hash, result) in resp.batch_result() {
for (inner_hash, result) in resp.batch_result() {
match result {
InnerTxResult::Success(_) => {
display_line!(
context.io(),
"Transaction {} was successfully applied at height {}.",
cmt_hash,
inner_hash,
resp.height,
);
}
Expand All @@ -420,7 +430,7 @@ pub fn display_batch_resp(context: &impl Namada, resp: &TxResponse) {
context.io(),
"Transaction {} was rejected by VPs: {}\nErrors: \
{}\nChanged keys: {}",
cmt_hash,
inner_hash,
serde_json::to_string_pretty(
&inner.vps_result.rejected_vps
)
Expand All @@ -434,7 +444,7 @@ pub fn display_batch_resp(context: &impl Namada, resp: &TxResponse) {
edisplay_line!(
context.io(),
"Transaction {} failed.\nDetails: {}",
cmt_hash,
inner_hash,
serde_json::to_string_pretty(&resp).unwrap()
);
}
Expand Down
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.

1 change: 1 addition & 0 deletions wasm_for_tests/Cargo.lock

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

0 comments on commit 15477e0

Please sign in to comment.