Skip to content

Commit

Permalink
added better transfer classification
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraccaman committed Jan 15, 2025
1 parent dae72e9 commit 7b7e9e9
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
SELECT 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Your SQL goes here
ALTER TYPE TRANSACTION_KIND ADD VALUE 'mixed_transfer';
6 changes: 6 additions & 0 deletions orm/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum TransactionKindDb {
ShieldedTransfer,
ShieldingTransfer,
UnshieldingTransfer,
MixedTransfer,
IbcMsgTransfer,
Bond,
Redelegation,
Expand All @@ -41,6 +42,11 @@ impl From<TransactionKind> for TransactionKindDb {
Self::TransparentTransfer
}
TransactionKind::ShieldedTransfer(_) => Self::ShieldedTransfer,
TransactionKind::UnshieldingTransfer(_) => {
Self::UnshieldingTransfer
}
TransactionKind::ShieldingTransfer(_) => Self::ShieldingTransfer,
TransactionKind::MixedTransfer(_) => Self::MixedTransfer,
TransactionKind::IbcMsgTransfer(_) => Self::IbcMsgTransfer,
TransactionKind::Bond(_) => Self::Bond,
TransactionKind::Redelegation(_) => Self::Redelegation,
Expand Down
125 changes: 119 additions & 6 deletions shared/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use namada_ibc::apps::transfer::types::packet::PacketData;
use namada_ibc::core::channel::types::msgs::{MsgRecvPacket, PacketMsg};
use namada_ibc::core::handler::types::msgs::MsgEnvelope;
use namada_ibc::IbcMessage;
use namada_sdk::address::Address;
use namada_sdk::address::{Address, InternalAddress};
use namada_sdk::borsh::BorshDeserialize;
use namada_sdk::token::Transfer;
use subtle_encoding::hex;
Expand Down Expand Up @@ -112,13 +112,12 @@ impl Block {
pub fn from(
block_response: &TendermintBlockResponse,
block_results: &BlockResult,
proposer_address_namada: &Option<Id>, /* Provide the namada address
* of the proposer, if
* available */
proposer_address_namada: &Option<Id>,
checksums: Checksums,
epoch: Epoch,
block_height: BlockHeight,
) -> Self {
let masp_address = Address::Internal(InternalAddress::Masp);
let transactions = block_response
.block
.data
Expand All @@ -131,6 +130,7 @@ impl Block {
block_height,
checksums.clone(),
block_results,
&masp_address,
)
.map_err(|reason| {
tracing::info!("Couldn't deserialize tx due to {}", reason);
Expand Down Expand Up @@ -208,8 +208,121 @@ impl Block {
vec![]
}
}
TransactionKind::ShieldedTransfer(_shielded_transfer) => {
vec![]
TransactionKind::MixedTransfer(transparent_transfer) => {
if let Some(data) = transparent_transfer {
let sources = data
.sources
.0
.keys()
.map(|account| {
TransactionTarget::sent(
tx.tx_id.clone(),
account.owner.to_string(),
)
})
.collect::<Vec<_>>();
let targets = data
.targets
.0
.keys()
.map(|account| {
TransactionTarget::received(
tx.tx_id.clone(),
account.owner.to_string(),
)
})
.collect::<Vec<_>>();
[sources, targets].concat()
} else {
vec![]
}
}
TransactionKind::ShieldedTransfer(transparent_transfer) => {
if let Some(data) = transparent_transfer {
let sources = data
.sources
.0
.keys()
.map(|account| {
TransactionTarget::sent(
tx.tx_id.clone(),
account.owner.to_string(),
)
})
.collect::<Vec<_>>();
let targets = data
.targets
.0
.keys()
.map(|account| {
TransactionTarget::received(
tx.tx_id.clone(),
account.owner.to_string(),
)
})
.collect::<Vec<_>>();
[sources, targets].concat()
} else {
vec![]
}
}
TransactionKind::UnshieldingTransfer(transparent_transfer) => {
if let Some(data) = transparent_transfer {
let sources = data
.sources
.0
.keys()
.map(|account| {
TransactionTarget::sent(
tx.tx_id.clone(),
account.owner.to_string(),
)
})
.collect::<Vec<_>>();
let targets = data
.targets
.0
.keys()
.map(|account| {
TransactionTarget::received(
tx.tx_id.clone(),
account.owner.to_string(),
)
})
.collect::<Vec<_>>();
[sources, targets].concat()
} else {
vec![]
}
}
TransactionKind::ShieldingTransfer(transparent_transfer) => {
if let Some(data) = transparent_transfer {
let sources = data
.sources
.0
.keys()
.map(|account| {
TransactionTarget::sent(
tx.tx_id.clone(),
account.owner.to_string(),
)
})
.collect::<Vec<_>>();
let targets = data
.targets
.0
.keys()
.map(|account| {
TransactionTarget::received(
tx.tx_id.clone(),
account.owner.to_string(),
)
})
.collect::<Vec<_>>();
[sources, targets].concat()
} else {
vec![]
}
}
TransactionKind::IbcMsgTransfer(ibc_message) => {
if let Some(data) = ibc_message {
Expand Down
6 changes: 3 additions & 3 deletions shared/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<'de> Deserialize<'de> for AccountsMap {
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct TransparentTransfer {
pub struct TransferData {
/// Sources of this transfer
pub sources: AccountsMap,
/// Targets of this transfer
Expand All @@ -77,13 +77,13 @@ pub struct TransparentTransfer {
pub shielded_section_hash: Option<MaspTxId>,
}

impl From<NamadaTransfer> for TransparentTransfer {
impl From<NamadaTransfer> for TransferData {
fn from(transfer: NamadaTransfer) -> Self {
let sources = AccountsMap(transfer.sources);
let targets = AccountsMap(transfer.targets);
let shielded_section_hash = transfer.shielded_section_hash;

TransparentTransfer {
TransferData {
sources,
targets,
shielded_section_hash,
Expand Down
75 changes: 62 additions & 13 deletions shared/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use namada_governance::{InitProposalData, VoteProposalData};
use namada_sdk::address::Address;
use namada_sdk::borsh::BorshDeserialize;
use namada_sdk::key::common::PublicKey;
use namada_sdk::masp::ShieldedTransfer;
use namada_sdk::token::Transfer;
use namada_sdk::uint::Uint;
use namada_tx::data::pos::{
Expand All @@ -21,7 +20,7 @@ use crate::block::BlockHeight;
use crate::block_result::{BlockResult, TxEventStatusCode};
use crate::checksums::Checksums;
use crate::id::Id;
use crate::ser::{IbcMessage, TransparentTransfer};
use crate::ser::{IbcMessage, TransferData};

// We wrap public key in a struct so we serialize data as object instead of
// string
Expand All @@ -33,10 +32,11 @@ pub struct RevealPkData {
#[derive(Serialize, Debug, Clone)]
#[serde(untagged)]
pub enum TransactionKind {
TransparentTransfer(Option<TransparentTransfer>),
// TODO: remove once ShieldedTransfer can be serialized
#[serde(skip)]
ShieldedTransfer(Option<ShieldedTransfer>),
TransparentTransfer(Option<TransferData>),
ShieldedTransfer(Option<TransferData>),
ShieldingTransfer(Option<TransferData>),
UnshieldingTransfer(Option<TransferData>),
MixedTransfer(Option<TransferData>),
IbcMsgTransfer(Option<IbcMessage<Transfer>>),
Bond(Option<Bond>),
Redelegation(Option<Redelegation>),
Expand All @@ -60,16 +60,60 @@ impl TransactionKind {
serde_json::to_string(&self).ok()
}

pub fn from(tx_kind_name: &str, data: &[u8]) -> Self {
pub fn from(
tx_kind_name: &str,
data: &[u8],
masp_address: &Address,
) -> Self {
match tx_kind_name {
"tx_transfer" => {
let data = if let Ok(data) = Transfer::try_from_slice(data) {
Some(TransparentTransfer::from(data))
if let Ok(data) = Transfer::try_from_slice(data) {
let has_shielded_section =
data.shielded_section_hash.is_some();
let all_sources_are_masp = data
.sources
.iter()
.all(|(acc, _)| acc.owner.eq(masp_address));
let any_sources_are_masp = data
.sources
.iter()
.all(|(acc, _)| acc.owner.eq(masp_address));
let all_targets_are_masp = data
.targets
.iter()
.all(|(acc, _)| acc.owner.eq(masp_address));
let any_targets_are_masp = data
.targets
.iter()
.all(|(acc, _)| acc.owner.eq(masp_address));
if all_sources_are_masp
&& all_targets_are_masp
&& has_shielded_section
{
TransactionKind::ShieldedTransfer(Some(data.into()))
} else if all_sources_are_masp
&& !any_targets_are_masp
&& has_shielded_section
{
TransactionKind::UnshieldingTransfer(Some(data.into()))
} else if !any_sources_are_masp
&& all_targets_are_masp
&& has_shielded_section
{
TransactionKind::ShieldingTransfer(Some(data.into()))
} else if !any_sources_are_masp
&& !any_targets_are_masp
&& !has_shielded_section
{
TransactionKind::TransparentTransfer(Some(data.into()))
} else {
TransactionKind::MixedTransfer(Some(data.into()))
}
} else {
None
};
TransactionKind::TransparentTransfer(data)
TransactionKind::Unknown
}
}

"tx_bond" => {
let data = if let Ok(data) = Bond::try_from_slice(data) {
Some(data)
Expand Down Expand Up @@ -299,6 +343,7 @@ impl Transaction {
block_height: BlockHeight,
checksums: Checksums,
block_results: &BlockResult,
masp_address: &Address,
) -> Result<(WrapperTransaction, Vec<InnerTransaction>), String> {
let transaction =
Tx::try_from(raw_tx_bytes).map_err(|e| e.to_string())?;
Expand Down Expand Up @@ -379,7 +424,11 @@ impl Transaction {
if let Some(tx_kind_name) =
checksums.get_name_by_id(&id)
{
TransactionKind::from(&tx_kind_name, &tx_data)
TransactionKind::from(
&tx_kind_name,
&tx_data,
masp_address,
)
} else {
TransactionKind::Unknown
}
Expand Down
15 changes: 11 additions & 4 deletions transactions/src/services/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,10 @@ pub fn get_gas_estimates(
&& inner_tx.wrapper_id.eq(&wrapper_tx.tx_id)
})
.for_each(|tx| match tx.kind {
TransactionKind::TransparentTransfer(_) => {
TransactionKind::TransparentTransfer(_)
| TransactionKind::MixedTransfer(_) => {
gas_estimate.increase_transparent_transfer()
}
TransactionKind::ShieldedTransfer(_) => {
gas_estimate.increase_shielded_transfer()
}
TransactionKind::IbcMsgTransfer(_) => {
gas_estimate.increase_ibc_msg_transfer()
}
Expand All @@ -161,6 +159,15 @@ pub fn get_gas_estimates(
TransactionKind::RevealPk(_) => {
gas_estimate.increase_reveal_pk()
}
TransactionKind::ShieldedTransfer(_) => {
gas_estimate.increase_shielded_transfer()
}
TransactionKind::ShieldingTransfer(_) => {
gas_estimate.increase_shielding_transfer()
}
TransactionKind::UnshieldingTransfer(_) => {
gas_estimate.increase_unshielding_transfer()
}
_ => (),
});
gas_estimate
Expand Down
2 changes: 2 additions & 0 deletions webserver/src/response/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum TransactionKind {
ShieldedTransfer,
ShieldingTransfer,
UnshieldingTransfer,
MixedTransfer,
Bond,
Redelegation,
Unbond,
Expand Down Expand Up @@ -99,6 +100,7 @@ impl From<TransactionKindDb> for TransactionKind {
TransactionKindDb::ShieldedTransfer => Self::ShieldedTransfer,
TransactionKindDb::ShieldingTransfer => Self::ShieldingTransfer,
TransactionKindDb::UnshieldingTransfer => Self::UnshieldingTransfer,
TransactionKindDb::MixedTransfer => Self::MixedTransfer,
TransactionKindDb::Bond => Self::Bond,
TransactionKindDb::Redelegation => Self::Redelegation,
TransactionKindDb::Unbond => Self::Unbond,
Expand Down

0 comments on commit 7b7e9e9

Please sign in to comment.