Skip to content

Commit

Permalink
Removed redundant InnerTx usages.
Browse files Browse the repository at this point in the history
  • Loading branch information
Murisi Tarusenga committed Apr 8, 2023
1 parent 27a4a40 commit 1f35ef4
Show file tree
Hide file tree
Showing 54 changed files with 60 additions and 252 deletions.
3 changes: 1 addition & 2 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::io::{self, Write};
use std::iter::Iterator;
use std::str::FromStr;

use namada::proto::InnerTx;
use async_std::fs;
use async_std::path::PathBuf;
use async_std::prelude::*;
Expand All @@ -36,7 +35,7 @@ use namada::ledger::pos::{
};
use namada::ledger::queries::{self, RPC};
use namada::ledger::storage::ConversionState;
use namada::proto::{SignedTxData, Tx};
use namada::proto::{Tx};
use namada::types::address::{masp, tokens, Address};
use namada::types::governance::{
OfflineProposal, OfflineVote, ProposalResult, ProposalVote, TallyResult,
Expand Down
1 change: 0 additions & 1 deletion apps/src/lib/client/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use namada::types::storage::Epoch;
use namada::types::token;
use namada::types::token::Amount;
use namada::types::transaction::{hash_tx, Fee, WrapperTx, MIN_FEE};
use namada::proto::InnerTx;
use namada::types::transaction::TxType;
use namada::types::transaction::decrypted::DecryptedTx;
use namada::types::hash::Hash;
Expand Down
2 changes: 1 addition & 1 deletion apps/src/lib/client/tendermint_rpc_types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::convert::TryFrom;

use namada::ledger::events::Event;
use namada::proto::{InnerTx, Tx};
use namada::proto::Tx;
use namada::types::address::Address;
use serde::Serialize;

Expand Down
1 change: 0 additions & 1 deletion apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ use rand_core::{CryptoRng, OsRng, RngCore};
use rust_decimal::Decimal;
use sha2::{Sha256, Digest};
use tokio::time::{Duration, Instant};
use namada::proto::{InnerTx, SignedTxData};

use super::rpc;
use super::types::ShieldedTransferContext;
Expand Down
3 changes: 1 addition & 2 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,7 @@ mod test_finalize_block {
use namada::types::transaction::encrypted::EncryptedTx;
use namada::core::types::hash::Hash;
use namada::types::transaction::{EncryptionKey, Fee, WrapperTx, MIN_FEE};
use namada::proto::{Data, Code, Signature, Section, InnerTx};
use namada::proto::SignedTxData;
use namada::proto::{Data, Code, Signature, Section};

use super::*;
use crate::node::ledger::shell::test_utils::*;
Expand Down
2 changes: 1 addition & 1 deletion apps/src/lib/node/ledger/shell/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use namada::types::address::Address;
use namada::types::governance::TallyResult;
use namada::types::storage::Epoch;
use namada::types::token;
use namada::proto::{SignedTxData, Data, Code};
use namada::proto::{Data, Code};
use namada::types::hash::Hash;

use super::*;
Expand Down
5 changes: 2 additions & 3 deletions apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use namada::ledger::storage::{
DBIter, Sha256Hasher, Storage, StorageHasher, DB,
};
use namada::ledger::{ibc, pos, protocol};
use namada::proto::{self, InnerTx, Tx};
use namada::proto::{self, Tx};
use namada::types::address;
use namada::types::address::{masp, masp_tx_key, Address};
use namada::types::chain::ChainId;
Expand Down Expand Up @@ -770,8 +770,7 @@ mod test_utils {
use namada::types::transaction::{Fee, WrapperTx};
use tempfile::tempdir;
use tokio::sync::mpsc::UnboundedReceiver;
use namada::proto::InnerTx;
use namada::proto::{SignedOuterTxData, SignedTxData, Code, Data};
use namada::proto::{Code, Data};

use super::*;
use crate::facade::tendermint_proto::abci::{
Expand Down
3 changes: 1 addition & 2 deletions apps/src/lib/node/ledger/shell/prepare_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ mod test_prepare_proposal {
use borsh::BorshSerialize;
use namada::types::storage::Epoch;
use namada::types::transaction::{Fee, WrapperTx};
use namada::proto::InnerTx;
use namada::proto::{SignedOuterTxData, SignedTxData, Code, Data, Section, Signature};
use namada::proto::{Code, Data, Section, Signature};

use super::*;
use crate::node::ledger::shell::test_utils::{gen_keypair, TestShell};
Expand Down
3 changes: 1 addition & 2 deletions apps/src/lib/node/ledger/shell/process_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,13 @@ where
#[cfg(test)]
mod test_process_proposal {
use borsh::BorshDeserialize;
use namada::proto::{SignedOuterTxData, SignedTxData};
use namada::types::hash::Hash;
use namada::types::key::*;
use namada::types::storage::Epoch;
use namada::types::token::Amount;
use namada::types::transaction::encrypted::EncryptedTx;
use namada::types::transaction::{EncryptionKey, Fee, WrapperTx, RawHeader};
use namada::proto::{InnerTx, Code, Data, Section, Signature};
use namada::proto::{Code, Data, Section, Signature};

use super::*;
use crate::facade::tendermint_proto::abci::RequestInitChain;
Expand Down
3 changes: 1 addition & 2 deletions core/src/ledger/vp_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use borsh::BorshDeserialize;
use super::storage_api::{self, StorageRead};
use crate::types::address::Address;
use crate::types::hash::Hash;
use crate::types::key::common;
use crate::types::storage::{BlockHash, BlockHeight, Epoch, Key, TxIndex};
use crate::proto::{Tx, SignedTxData};
use crate::proto::{Tx};

/// Validity predicate's environment is available for native VPs and WASM VPs
pub trait VpEnv<'view>
Expand Down
2 changes: 1 addition & 1 deletion core/src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pub mod generated;
mod types;

pub use types::{Dkg, Error, Signed, SignedTxData, SignedOuterTxData, Tx, InnerTx, Data, Code, Signature, Section};
pub use types::{Dkg, Error, Signed, Tx, Data, Code, Signature, Section};

#[cfg(test)]
mod tests {
Expand Down
156 changes: 0 additions & 156 deletions core/src/proto/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,6 @@ pub enum Error {

pub type Result<T> = std::result::Result<T, Error>;

/// This can be used to sign an arbitrary tx. The signature is produced and
/// verified on the tx data concatenated with the tx code, however the tx code
/// itself is not part of this structure.
///
/// Because the signature is not checked by the ledger, we don't inline it into
/// the `Tx` type directly. Instead, the signature is attached to the `tx.data`,
/// which can then be checked by a validity predicate wasm.
#[derive(Clone, Debug, BorshSerialize, BorshDeserialize, BorshSchema, PartialEq, Eq, Default, Deserialize, Serialize)]
pub struct SignedTxData {
/// The original tx data bytes, if any
pub data: Option<Vec<u8>>,
/// The signature is produced on the tx data concatenated with the tx code
/// and the timestamp.
pub sig: Option<common::Signature>,
}

#[derive(Clone, Debug, BorshSerialize, BorshDeserialize, BorshSchema, Deserialize, Serialize)]
pub struct SignedOuterTxData {
/// The original tx data bytes, if any
pub data: TxType,
/// The signature is produced on the tx data concatenated with the tx code
/// and the timestamp.
pub sig: Option<common::Signature>,
}

/// A generic signed data wrapper for Borsh encode-able data.
#[derive(
Clone, Debug, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
Expand Down Expand Up @@ -465,25 +440,11 @@ pub struct Tx {
pub outer_timestamp: DateTimeUtc,
pub outer_extra: Vec<u8>,
pub code: Vec<u8>,
pub data: Option<SignedTxData>,
pub timestamp: DateTimeUtc,
pub extra: Vec<u8>,
pub sections: Vec<Section>,
}

/// A SigningTx but with the full code embedded. This structure will almost
/// certainly be bigger than SigningTxs and contains enough information to
/// execute the transaction.
#[derive(
Clone, Debug, BorshSerialize, BorshDeserialize, BorshSchema, PartialEq, Eq, Serialize, Deserialize,
)]
pub struct InnerTx {
pub code: Vec<u8>,
pub data: Option<SignedTxData>,
pub timestamp: DateTimeUtc,
pub extra: Vec<u8>,
}

impl TryFrom<&[u8]> for Tx {
type Error = Error;

Expand Down Expand Up @@ -583,7 +544,6 @@ impl Tx {
outer_timestamp: DateTimeUtc::now(),
outer_extra: vec![],
code: vec![],
data: None,
timestamp: DateTimeUtc::now(),
extra: vec![],
sections: vec![],
Expand Down Expand Up @@ -808,122 +768,6 @@ impl Tx {
}
}

impl From<InnerTx> for types::InnerTx {
fn from(tx: InnerTx) -> Self {
let timestamp = Some(tx.timestamp.into());
let data = tx.data.map(|x| {
x.try_to_vec()
.expect("Unable to serialize encrypted transaction")
});
types::InnerTx {
code: tx.code,
data,
extra: tx.extra,
timestamp,
}
}
}

impl InnerTx {
pub fn new(code: Vec<u8>, data: Option<SignedTxData>) -> Self {
InnerTx {
code,
data,
timestamp: DateTimeUtc::now(),
extra: vec![],
}
}

pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = vec![];
let tx: types::InnerTx = self.clone().into();
tx.encode(&mut bytes)
.expect("encoding a transaction failed");
bytes
}

/// Produce a reduced version of this transaction that is sufficient for
/// signing. Specifically replaces code and extra with their hashes, and
/// leaves out inner tx.
pub fn signing_tx(&self) -> types::InnerTx {
let timestamp = Some(self.timestamp.into());
let data = self.data.as_ref().map(|x| {
x.try_to_vec()
.expect("Unable to serialize encrypted transaction")
});
types::InnerTx {
code: hash_tx(&self.code).0.to_vec(),
extra: hash_tx(&self.extra).0.to_vec(),
data,
timestamp,
}
}

/// Hash this transaction leaving out the inner tx, but instead of including
/// the transaction code and extra data in the hash, include their hashes
/// instead.
pub fn partial_hash(&self) -> [u8; 32] {
let mut bytes = vec![];
self.signing_tx()
.encode(&mut bytes)
.expect("encoding a transaction failed");
hash_tx(&bytes).0
}

/// Get the hash of this transaction's code
pub fn code_hash(&self) -> [u8; 32] {
hash_tx(&self.code).0
}

/// Get the hash of this transaction's extra data
pub fn extra_hash(&self) -> [u8; 32] {
hash_tx(&self.extra).0
}

/// Sign a transaction using [`SignedTxData`].
pub fn sign(self, keypair: &common::SecretKey) -> Self {
let to_sign = self.partial_hash();
let sig = common::SigScheme::sign(keypair, to_sign);
let signed = SignedTxData {
data: self.data.and_then(|x| x.data),
sig: Some(sig),
};
InnerTx {
code: self.code,
data: Some(signed),
extra: self.extra,
timestamp: self.timestamp,
}
}

/// Verify that the transaction has been signed by the secret key
/// counterpart of the given public key.
pub fn verify_sig(
&self,
pk: &common::PublicKey,
sig: &common::Signature,
) -> std::result::Result<(), VerifySigError> {
// Try to get the transaction data from decoded `SignedTxData`
let signed_tx_data = self.data.clone().ok_or(VerifySigError::MissingData)?;
let mut data = signed_tx_data.clone();
data.sig = None;
let tx = InnerTx {
code: self.code.clone(),
extra: self.extra.clone(),
data: Some(data),
timestamp: self.timestamp,
};
let signed_data = tx.partial_hash();
common::SigScheme::verify_signature_raw(pk, &signed_data, sig)
}

/// A validity check on the ciphertext.
#[cfg(feature = "ferveo-tpke")]
pub fn validate_ciphertext(&self) -> bool {
true
}
}

#[allow(dead_code)]
#[derive(Clone, Debug, PartialEq)]
pub struct DkgGossipMessage {
Expand Down
2 changes: 1 addition & 1 deletion core/src/types/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod tx_queue {
use borsh::{BorshDeserialize, BorshSerialize};

use crate::types::transaction::encrypted::EncryptedTx;
use crate::proto::{Tx, InnerTx};
use crate::proto::Tx;

/// A wrapper for `crate::types::transaction::WrapperTx` to conditionally
/// add `has_valid_pow` flag for only used in testnets.
Expand Down
2 changes: 0 additions & 2 deletions core/src/types/transaction/decrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub mod decrypted_tx {
#[cfg(feature = "ferveo-tpke")]
use crate::types::transaction::encrypted::EncryptedTx;
use crate::types::transaction::{Hash, TxType, WrapperTx};
use crate::proto::InnerTx;
use crate::proto::{SignedTxData, SignedOuterTxData};
use sha2::{Digest, Sha256};

#[derive(Clone, Debug, BorshSerialize, BorshDeserialize, BorshSchema, serde::Serialize, serde::Deserialize)]
Expand Down
4 changes: 2 additions & 2 deletions core/src/types/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub mod tx_types {

use super::*;
use sha2::{Sha256, Digest};
use crate::proto::{SignedTxData, SignedOuterTxData, InnerTx, Tx, Code, Data, Section, Signature};
use crate::proto::{Tx, Code, Data, Section, Signature};
#[cfg(feature = "ferveo-tpke")]
use crate::types::transaction::protocol::ProtocolTx;

Expand Down Expand Up @@ -320,7 +320,7 @@ pub mod tx_types {
use super::*;
use crate::types::address::nam;
use crate::types::storage::Epoch;
use crate::proto::{Code, Data, InnerTx};
use crate::proto::{Code, Data};

fn gen_keypair() -> common::SecretKey {
use rand::prelude::ThreadRng;
Expand Down
3 changes: 1 addition & 2 deletions core/src/types/transaction/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ mod protocol_txs {
use serde_json;

use super::*;
use crate::proto::{InnerTx, Tx, Data, Code, Signature, Section};
use crate::proto::{Tx, Data, Code, Signature, Section};
use crate::types::key::*;
use crate::types::transaction::{EllipticCurve, TxError, TxType};
use crate::proto::{SignedTxData, SignedOuterTxData};
use crate::types::hash::Hash;
use crate::types::transaction::{Digest, Sha256};

Expand Down
4 changes: 1 addition & 3 deletions core/src/types/transaction/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ pub mod wrapper_tx {
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::proto::{Tx, InnerTx, Code, Data, Signature, Section};
use crate::proto::{Tx, Code, Data, Signature, Section};
use crate::types::address::Address;
use crate::types::key::*;
use crate::types::storage::Epoch;
use crate::types::token::Amount;
#[cfg(feature = "ferveo-tpke")]
use crate::types::transaction::encrypted::EncryptedTx;
use crate::types::transaction::{Hash, TxError, TxType};
use crate::proto::{SignedTxData, SignedOuterTxData};
use sha2::{Digest, Sha256};

/// Minimum fee amount in micro NAMs
Expand Down Expand Up @@ -288,7 +287,6 @@ pub mod wrapper_tx {
#[cfg(test)]
mod test_wrapper_tx {
use super::*;
use crate::proto::SignedTxData;
use crate::types::address::nam;
use crate::types::transaction::EncryptionKey;

Expand Down
2 changes: 1 addition & 1 deletion core/src/types/validity_predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use crate::proto::{Tx, SignedTxData};
use crate::proto::{Tx};

/// A validity predicate with an input that is intended to be invoked via `eval`
/// host function.
Expand Down
Loading

0 comments on commit 1f35ef4

Please sign in to comment.