Skip to content

Commit

Permalink
patch v5 parsing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Paoletta Saint Diminuendo committed Jan 28, 2025
1 parent e432c3a commit 1229154
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 101 deletions.
212 changes: 115 additions & 97 deletions src/json_rescue_v5_extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ pub fn extract_v5_json_rescue(
}

decode_transaction_args(&mut wtxs, &t.bytes)?;

dbg!(&wtxs);
// TODO:
// wtxs.events
// TODO:
wtxs.block_timestamp = timestamp;

// TODO: create arg to exclude tx without counter party
match &wtxs.relation_label {
RelationLabel::Tx => {}
RelationLabel::Unknown => {}
RelationLabel::Transfer(_) => tx_vec.push(wtxs),
RelationLabel::Onboarding(_) => tx_vec.push(wtxs),
RelationLabel::Vouch(_) => tx_vec.push(wtxs),
Expand All @@ -88,7 +88,7 @@ pub fn extract_v5_json_rescue(
_ => {}
}
}

dbg!(&tx_vec);
Ok((tx_vec, event_vec, unique_functions))
}

Expand All @@ -103,115 +103,133 @@ pub fn decode_transaction_args(wtx: &mut WarehouseTxMaster, tx_bytes: &[u8]) ->
})?;

if let TransactionV5::UserTransaction(u) = &t {
// check this is actually a ScriptFunction
if let TransactionPayload::ScriptFunction(_) = &u.raw_txn.payload {
if let Some(sf) = &ScriptFunctionCallGenesis::decode(&u.raw_txn.payload) {
wtx.entry_function = Some(EntryFunctionArgs::V5(sf.to_owned()));
// TODO: some script functions have very large payloads which clog the e.g. Miner. So those are only added for the catch-all txs which don't fall into categories we are interested in.
match sf {
ScriptFunctionCallGenesis::BalanceTransfer { destination, .. } => {
wtx.relation_label =
RelationLabel::Transfer(cast_legacy_account(destination)?);
maybe_decode_v5_genesis_function(wtx, &u.raw_txn.payload)?;
// if still unknown TX try again with v5.2.0
if let RelationLabel::Unknown = wtx.relation_label {
maybe_decode_v520_function(wtx, &u.raw_txn.payload)?;
}
}
}
Ok(())
}

wtx.entry_function = Some(EntryFunctionArgs::V5(sf.to_owned()));
}
ScriptFunctionCallGenesis::AutopayCreateInstruction { payee, .. } => {
wtx.relation_label = RelationLabel::Transfer(cast_legacy_account(payee)?);
wtx.entry_function = Some(EntryFunctionArgs::V5(sf.to_owned()));
}
ScriptFunctionCallGenesis::CreateAccUser { .. } => {
// onboards self
wtx.relation_label = RelationLabel::Onboarding(wtx.sender);
}
ScriptFunctionCallGenesis::CreateAccVal { .. } => {
// onboards self
wtx.relation_label = RelationLabel::Onboarding(wtx.sender);
}
fn maybe_decode_v5_genesis_function(
wtx: &mut WarehouseTxMaster,
payload: &TransactionPayload,
) -> Result<()> {
if let Some(sf) = &ScriptFunctionCallGenesis::decode(payload) {
dbg!(&sf);
wtx.entry_function = Some(EntryFunctionArgs::V5(sf.to_owned()));
// TODO: some script functions have very large payloads which clog the e.g. Miner. So those are only added for the catch-all txs which don't fall into categories we are interested in.
match sf {
ScriptFunctionCallGenesis::BalanceTransfer { destination, .. } => {
wtx.relation_label = RelationLabel::Transfer(cast_legacy_account(destination)?);

ScriptFunctionCallGenesis::CreateUserByCoinTx { account, .. } => {
wtx.relation_label =
RelationLabel::Onboarding(cast_legacy_account(account)?);
}
ScriptFunctionCallGenesis::CreateValidatorAccount {
sliding_nonce: _,
new_account_address,
..
} => {
wtx.relation_label =
RelationLabel::Onboarding(cast_legacy_account(new_account_address)?);
}
ScriptFunctionCallGenesis::CreateValidatorOperatorAccount {
sliding_nonce: _,
new_account_address,
..
} => {
wtx.relation_label =
RelationLabel::Onboarding(cast_legacy_account(new_account_address)?);
}
wtx.entry_function = Some(EntryFunctionArgs::V5(sf.to_owned()));
}
ScriptFunctionCallGenesis::AutopayCreateInstruction { payee, .. } => {
wtx.relation_label = RelationLabel::Transfer(cast_legacy_account(payee)?);
wtx.entry_function = Some(EntryFunctionArgs::V5(sf.to_owned()));
}
ScriptFunctionCallGenesis::CreateAccUser { .. } => {
// onboards self
wtx.relation_label = RelationLabel::Onboarding(wtx.sender);
}
ScriptFunctionCallGenesis::CreateAccVal { .. } => {
// onboards self
wtx.relation_label = RelationLabel::Onboarding(wtx.sender);
}

ScriptFunctionCallGenesis::MinerstateCommit { .. } => {
wtx.relation_label = RelationLabel::Miner;
}
ScriptFunctionCallGenesis::MinerstateCommitByOperator { .. } => {
wtx.relation_label = RelationLabel::Miner;
}
_ => {
wtx.relation_label = RelationLabel::Configuration;
ScriptFunctionCallGenesis::CreateUserByCoinTx { account, .. } => {
dbg!(&account);
wtx.relation_label = RelationLabel::Onboarding(cast_legacy_account(account)?);
}
ScriptFunctionCallGenesis::CreateValidatorAccount {
sliding_nonce: _,
new_account_address,
..
} => {
wtx.relation_label =
RelationLabel::Onboarding(cast_legacy_account(new_account_address)?);
}
ScriptFunctionCallGenesis::CreateValidatorOperatorAccount {
sliding_nonce: _,
new_account_address,
..
} => {
wtx.relation_label =
RelationLabel::Onboarding(cast_legacy_account(new_account_address)?);
}

wtx.entry_function = Some(EntryFunctionArgs::V5(sf.to_owned()));
}
}
ScriptFunctionCallGenesis::MinerstateCommit { .. } => {
wtx.relation_label = RelationLabel::Miner;
}
ScriptFunctionCallGenesis::MinerstateCommitByOperator { .. } => {
wtx.relation_label = RelationLabel::Miner;
}
_ => {
wtx.relation_label = RelationLabel::Unknown;

if let Some(sf) = &ScriptFunctionCallV520::decode(&u.raw_txn.payload) {
wtx.entry_function = Some(EntryFunctionArgs::V520(sf.to_owned()));
wtx.entry_function = Some(EntryFunctionArgs::V5(sf.to_owned()));
}
}
}
Ok(())
}

match sf {
ScriptFunctionCallV520::BalanceTransfer { destination, .. } => {
wtx.relation_label =
RelationLabel::Transfer(cast_legacy_account(destination)?);
fn maybe_decode_v520_function(
wtx: &mut WarehouseTxMaster,
payload: &TransactionPayload,
) -> Result<()> {
if let Some(sf) = &ScriptFunctionCallV520::decode(payload) {
wtx.entry_function = Some(EntryFunctionArgs::V520(sf.to_owned()));
match sf {
// NOTE: This balanceTransfer likely de/encodes to the same
// bytes as v5 genesis
ScriptFunctionCallV520::BalanceTransfer { destination, .. } => {
wtx.relation_label = RelationLabel::Transfer(cast_legacy_account(destination)?);

wtx.entry_function = Some(EntryFunctionArgs::V520(sf.to_owned()));
}
ScriptFunctionCallV520::CreateAccUser { .. } => {
wtx.relation_label = RelationLabel::Onboarding(wtx.sender);
}
ScriptFunctionCallV520::CreateAccVal { .. } => {
wtx.relation_label = RelationLabel::Onboarding(wtx.sender);
}
wtx.entry_function = Some(EntryFunctionArgs::V520(sf.to_owned()));
}
ScriptFunctionCallV520::CreateAccUser { .. } => {
wtx.relation_label = RelationLabel::Onboarding(wtx.sender);
}
ScriptFunctionCallV520::CreateAccVal { .. } => {
wtx.relation_label = RelationLabel::Onboarding(wtx.sender);
}

ScriptFunctionCallV520::CreateValidatorAccount {
sliding_nonce: _,
new_account_address,
..
} => {
wtx.relation_label =
RelationLabel::Onboarding(cast_legacy_account(new_account_address)?);
}
ScriptFunctionCallV520::CreateValidatorOperatorAccount {
sliding_nonce: _,
new_account_address,
..
} => {
wtx.relation_label =
RelationLabel::Onboarding(cast_legacy_account(new_account_address)?);
}
ScriptFunctionCallV520::MinerstateCommit { .. } => {
wtx.relation_label = RelationLabel::Miner;
}
ScriptFunctionCallV520::MinerstateCommitByOperator { .. } => {
wtx.relation_label = RelationLabel::Miner;
}
_ => {
wtx.relation_label = RelationLabel::Configuration;
wtx.entry_function = Some(EntryFunctionArgs::V520(sf.to_owned()));
}
}
ScriptFunctionCallV520::CreateValidatorAccount {
sliding_nonce: _,
new_account_address,
..
} => {
wtx.relation_label =
RelationLabel::Onboarding(cast_legacy_account(new_account_address)?);
}
ScriptFunctionCallV520::CreateValidatorOperatorAccount {
sliding_nonce: _,
new_account_address,
..
} => {
wtx.relation_label =
RelationLabel::Onboarding(cast_legacy_account(new_account_address)?);
}
ScriptFunctionCallV520::MinerstateCommit { .. } => {
wtx.relation_label = RelationLabel::Miner;
}
ScriptFunctionCallV520::MinerstateCommitByOperator { .. } => {
wtx.relation_label = RelationLabel::Miner;
}
_ => {
wtx.relation_label = RelationLabel::Unknown;
wtx.entry_function = Some(EntryFunctionArgs::V520(sf.to_owned()));
}
}
}
Ok(())
}

/// from a tgz file unwrap to temp path
/// NOTE: we return the Temppath object for the directory
/// for the enclosing function to handle
Expand Down
6 changes: 3 additions & 3 deletions src/schema_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum RelationLabel {
Tx, // undefined tx
Unknown, // undefined tx
Transfer(AccountAddress),
Onboarding(AccountAddress),
Vouch(AccountAddress),
Expand All @@ -30,7 +30,7 @@ pub enum RelationLabel {
impl RelationLabel {
pub fn to_cypher_label(&self) -> String {
match self {
RelationLabel::Tx => "Tx".to_owned(),
RelationLabel::Unknown => "Tx".to_owned(),
RelationLabel::Transfer(_) => "Tx".to_owned(),
RelationLabel::Onboarding(_) => "Onboarding".to_owned(),
RelationLabel::Vouch(_) => "Vouch".to_owned(),
Expand All @@ -41,7 +41,7 @@ impl RelationLabel {

pub fn get_recipient(&self) -> Option<AccountAddress> {
match &self {
RelationLabel::Tx => None,
RelationLabel::Unknown => None,
RelationLabel::Transfer(account_address) => Some(*account_address),
RelationLabel::Onboarding(account_address) => Some(*account_address),
RelationLabel::Vouch(account_address) => Some(*account_address),
Expand Down
1 change: 1 addition & 0 deletions tests/test_json_rescue_v5_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async fn test_load_queue() -> anyhow::Result<()> {
let path = fixtures::v5_json_tx_path();

let tx_count = json_rescue_v5_load::rip_concurrent_limited(&path, &pool, None).await?;
dbg!(&tx_count);
assert!(tx_count == 13);

let tx_count = json_rescue_v5_load::rip_concurrent_limited(&path, &pool, None).await?;
Expand Down
3 changes: 2 additions & 1 deletion tests/test_json_rescue_v5_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ fn test_json_format_example() -> anyhow::Result<()> {
let p = fixtures::v5_json_tx_path().join("example_create_user.json");

let (tx, _, _) = extract_v5_json_rescue(&p)?;
let first = tx.first().unwrap();
dbg!(&tx);

let first = tx.first().unwrap();
assert!(first.sender.to_hex_literal() == *"0xecaf65add1b785b0495e3099f4045ec0");
Ok(())
}
Expand Down

0 comments on commit 1229154

Please sign in to comment.