Skip to content

Commit

Permalink
Moving inner_tx from WrapperTx to Tx.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Jan 27, 2023
1 parent 075553f commit 69deef8
Show file tree
Hide file tree
Showing 26 changed files with 330 additions and 147 deletions.
6 changes: 3 additions & 3 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,12 @@ fn extract_payload(
wrapper: &mut Option<WrapperTx>,
transfer: &mut Option<Transfer>,
) {
match process_tx(tx) {
match process_tx(tx.clone()) {
Ok(TxType::Wrapper(wrapper_tx)) => {
let privkey = <EllipticCurve as PairingEngine>::G2Affine::prime_subgroup_generator();
extract_payload(
Tx::from(match wrapper_tx.decrypt(privkey) {
Ok(tx) => DecryptedTx::Decrypted {
Tx::from(match tx.inner_tx.and_then(|inner_tx| wrapper_tx.decrypt(privkey, inner_tx).ok()) {
Some(tx) => DecryptedTx::Decrypted {
tx,
#[cfg(not(feature = "mainnet"))]
has_valid_pow: false,
Expand Down
28 changes: 17 additions & 11 deletions apps/src/lib/client/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ pub async fn sign_wrapper(
None
}
};

let tx = {
// This object governs how the payload will be processed
let wrapper_tx = {
WrapperTx::new(
Fee {
amount: fee_amount,
Expand All @@ -246,23 +246,29 @@ pub async fn sign_wrapper(
keypair,
epoch,
args.gas_limit.clone(),
tx,
// TODO: Actually use the fetched encryption key
Default::default(),
#[cfg(not(feature = "mainnet"))]
pow_solution,
)
// Bind the inner transaction to the wrapper
.bind(tx.clone())
};

// Then sign over the bound wrapper
let stx = wrapper_tx
.sign(keypair)
.expect("Wrapper tx signing keypair should be correct")
// Then encrypt and attach the payload to the wrapper
.attach_inner_tx(
&tx,
// TODO: Actually use the fetched encryption key
Default::default(),
);
// We use this to determine when the wrapper tx makes it on-chain
let wrapper_hash = hash_tx(&tx.try_to_vec().unwrap()).to_string();
let wrapper_hash = hash_tx(&wrapper_tx.try_to_vec().unwrap()).to_string();
// We use this to determine when the decrypted inner tx makes it
// on-chain
let decrypted_hash = tx.tx_hash.to_string();
let decrypted_hash = wrapper_tx.tx_hash.to_string();
TxBroadcastData::Wrapper {
tx: tx
.sign(keypair)
.expect("Wrapper tx signing keypair should be correct"),
tx: stx,
wrapper_hash,
decrypted_hash,
}
Expand Down
42 changes: 22 additions & 20 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ where
continue;
}

let tx_type = if let Ok(tx_type) = process_tx(tx) {
let tx_type = if let Ok(tx_type) = process_tx(tx.clone()) {
tx_type
} else {
tracing::error!(
Expand Down Expand Up @@ -217,6 +217,8 @@ where

self.storage.tx_queue.push(WrapperTxInQueue {
tx: wrapper.clone(),
inner_tx: tx.inner_tx,
inner_tx_code: tx.inner_tx_code,
#[cfg(not(feature = "mainnet"))]
has_valid_pow,
});
Expand Down Expand Up @@ -454,6 +456,7 @@ where
mod test_finalize_block {
use namada::types::storage::Epoch;
use namada::types::transaction::{EncryptionKey, Fee, WrapperTx, MIN_FEE};
use namada::types::transaction::encrypted::EncryptedTx;

use super::*;
use crate::node::ledger::shell::test_utils::*;
Expand Down Expand Up @@ -495,12 +498,13 @@ mod test_finalize_block {
&keypair,
Epoch(0),
0.into(),
raw_tx.clone(),
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
);
let tx = wrapper.sign(&keypair).expect("Test failed");
).bind(raw_tx.clone());
let tx = wrapper
.sign(&keypair)
.expect("Test failed")
.attach_inner_tx(&raw_tx, Default::default());
if i > 1 {
processed_txs.push(ProcessedTx {
tx: tx.to_bytes(),
Expand All @@ -511,7 +515,7 @@ mod test_finalize_block {
},
});
} else {
shell.enqueue_tx(wrapper.clone());
shell.enqueue_tx(wrapper.clone(), tx.inner_tx, tx.inner_tx_code);
}

if i != 3 {
Expand Down Expand Up @@ -560,6 +564,7 @@ mod test_finalize_block {
"wasm_code".as_bytes().to_owned(),
Some(String::from("transaction data").as_bytes().to_owned()),
);
let encrypted_raw_tx = EncryptedTx::encrypt(&raw_tx.to_bytes(), Default::default());
let wrapper = WrapperTx::new(
Fee {
amount: 0.into(),
Expand All @@ -568,11 +573,9 @@ mod test_finalize_block {
&keypair,
Epoch(0),
0.into(),
raw_tx.clone(),
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
);
).bind(raw_tx.clone());

let processed_tx = ProcessedTx {
tx: Tx::from(TxType::Decrypted(DecryptedTx::Decrypted {
Expand All @@ -586,7 +589,7 @@ mod test_finalize_block {
info: "".into(),
},
};
shell.enqueue_tx(wrapper);
shell.enqueue_tx(wrapper, Some(encrypted_raw_tx), None);

// check that the decrypted tx was not applied
for event in shell
Expand Down Expand Up @@ -626,7 +629,6 @@ mod test_finalize_block {
pk: keypair.ref_to(),
epoch: Epoch(0),
gas_limit: 0.into(),
inner_tx,
tx_hash: hash_tx(&tx),
#[cfg(not(feature = "mainnet"))]
pow_solution: None,
Expand All @@ -642,7 +644,7 @@ mod test_finalize_block {
},
};

shell.enqueue_tx(wrapper);
shell.enqueue_tx(wrapper, Some(inner_tx), None);

// check that correct error message is returned
for event in shell
Expand Down Expand Up @@ -696,6 +698,7 @@ mod test_finalize_block {
.to_owned(),
),
);
let encrypted_raw_tx = EncryptedTx::encrypt(&raw_tx.to_bytes(), Default::default());
let wrapper_tx = WrapperTx::new(
Fee {
amount: MIN_FEE.into(),
Expand All @@ -704,12 +707,10 @@ mod test_finalize_block {
&keypair,
Epoch(0),
0.into(),
raw_tx.clone(),
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
);
shell.enqueue_tx(wrapper_tx);
).bind(raw_tx.clone());
shell.enqueue_tx(wrapper_tx, Some(encrypted_raw_tx), None);
processed_txs.push(ProcessedTx {
tx: Tx::from(TxType::Decrypted(DecryptedTx::Decrypted {
tx: raw_tx,
Expand Down Expand Up @@ -741,12 +742,13 @@ mod test_finalize_block {
&keypair,
Epoch(0),
0.into(),
raw_tx.clone(),
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
);
let wrapper = wrapper_tx.sign(&keypair).expect("Test failed");
).bind(raw_tx.clone());
let wrapper = wrapper_tx
.sign(&keypair)
.expect("Test failed")
.attach_inner_tx(&raw_tx, Default::default());
valid_txs.push(wrapper_tx);
processed_txs.push(ProcessedTx {
tx: wrapper.to_bytes(),
Expand Down
17 changes: 13 additions & 4 deletions apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,7 @@ mod test_utils {
use namada::types::key::*;
use namada::types::storage::{BlockHash, BlockResults, Epoch, Header};
use namada::types::transaction::{Fee, WrapperTx};
use namada::types::transaction::encrypted::EncryptedTx;
use tempfile::tempdir;
use tokio::sync::mpsc::UnboundedReceiver;

Expand Down Expand Up @@ -921,9 +922,16 @@ mod test_utils {
/// Add a wrapper tx to the queue of txs to be decrypted
/// in the current block proposal
#[cfg(test)]
pub fn enqueue_tx(&mut self, wrapper: WrapperTx) {
pub fn enqueue_tx(
&mut self,
wrapper: WrapperTx,
inner_tx: Option<EncryptedTx>,
inner_tx_code: Option<EncryptedTx>,
) {
self.shell.storage.tx_queue.push(WrapperTxInQueue {
tx: wrapper,
inner_tx,
inner_tx_code,
#[cfg(not(feature = "mainnet"))]
has_valid_pow: false,
});
Expand Down Expand Up @@ -992,6 +1000,7 @@ mod test_utils {
"wasm_code".as_bytes().to_owned(),
Some("transaction data".as_bytes().to_owned()),
);
let encrypted_tx = EncryptedTx::encrypt(&tx.to_bytes(), Default::default());
let wrapper = WrapperTx::new(
Fee {
amount: 0.into(),
Expand All @@ -1000,13 +1009,13 @@ mod test_utils {
&keypair,
Epoch(0),
0.into(),
tx,
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
);
).bind(tx);
shell.storage.tx_queue.push(WrapperTxInQueue {
tx: wrapper,
inner_tx: Some(encrypted_tx),
inner_tx_code: None,
#[cfg(not(feature = "mainnet"))]
has_valid_pow: false,
});
Expand Down
31 changes: 19 additions & 12 deletions apps/src/lib/node/ledger/shell/prepare_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ where
// decrypt the wrapper txs included in the previous block
let decrypted_txs = self.storage.tx_queue.iter().map(
|WrapperTxInQueue {
tx,
tx,
inner_tx,
inner_tx_code,
#[cfg(not(feature = "mainnet"))]
has_valid_pow,
}| {
Tx::from(match tx.decrypt(privkey) {
Ok(tx) => DecryptedTx::Decrypted {
Tx::from(match inner_tx.clone().and_then(|x| tx.decrypt(privkey, x).ok()) {
Some(tx) => DecryptedTx::Decrypted {
tx,
#[cfg(not(feature = "mainnet"))]
has_valid_pow: *has_valid_pow,
Expand Down Expand Up @@ -230,16 +232,16 @@ mod test_prepare_proposal {
&keypair,
Epoch(0),
0.into(),
tx,
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
)
.try_to_vec()
.expect("Test failed"),
.bind(tx.clone())
.try_to_vec()
.expect("Test failed"),
),
)
.to_bytes();
.attach_inner_tx(&tx, Default::default())
.to_bytes();
#[allow(clippy::redundant_clone)]
let req = RequestPrepareProposal {
txs: vec![wrapper.clone()],
Expand Down Expand Up @@ -290,13 +292,18 @@ mod test_prepare_proposal {
&keypair,
Epoch(0),
0.into(),
tx,
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
).bind(tx.clone());
let wrapper = wrapper_tx
.sign(&keypair)
.expect("Test failed")
.attach_inner_tx(&tx, Default::default());
shell.enqueue_tx(
wrapper_tx,
wrapper.inner_tx.clone(),
wrapper.inner_tx_code.clone(),
);
let wrapper = wrapper_tx.sign(&keypair).expect("Test failed");
shell.enqueue_tx(wrapper_tx);
expected_wrapper.push(wrapper.clone());
req.txs.push(wrapper.to_bytes());
}
Expand Down
Loading

0 comments on commit 69deef8

Please sign in to comment.