Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Nov 26, 2024
1 parent 6d7ec8a commit 4b3be0c
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 49 deletions.
151 changes: 120 additions & 31 deletions src/json_rescue_v5_extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ use crate::{
schema_transaction::{EntryFunctionArgs, RelationLabel, WarehouseEvent, WarehouseTxMaster},
unzip_temp::decompress_tar_archive,
};
use diem_crypto::HashValue;
use libra_backwards_compatibility::{
sdk::v5_0_0_genesis_transaction_script_builder::ScriptFunctionCall as ScriptFunctionCallGenesis,
sdk::v5_2_0_transaction_script_builder::ScriptFunctionCall as ScriptFunctionCallV520,
sdk::{
v5_0_0_genesis_transaction_script_builder::ScriptFunctionCall as ScriptFunctionCallGenesis,
v5_2_0_transaction_script_builder::ScriptFunctionCall as ScriptFunctionCallV520,
},
version_five::{
legacy_address_v5::LegacyAddressV5,
transaction_type_v5::{TransactionPayload, TransactionV5},
transaction_view_v5::{ScriptView, TransactionDataView, TransactionViewV5},
},
Expand Down Expand Up @@ -35,19 +39,20 @@ pub fn extract_v5_json_rescue(
let mut wtxs = WarehouseTxMaster::default();
match &t.transaction {
TransactionDataView::UserTransaction { sender, script, .. } => {
// dbg!(&t);
wtxs.sender = AccountAddress::from_hex_literal(&sender.to_hex_literal())?;

// wtxs.tx_hash = HashValue::from_str(&t.hash.to_hex_literal())?;
wtxs.sender = cast_legacy_account(&sender)?;

// must cast from V5 Hashvalue buffer layout
wtxs.tx_hash = HashValue::from_slice(&t.hash.to_vec())?;

wtxs.function = make_function_name(script);
info!("function: {}", &wtxs.function);

wtxs.relation_label = guess_relation(&wtxs.function);
decode_transaction_args(&mut wtxs, &t.bytes)?;

// TODO:
// wtxs.events
// wtxs.block_timestamp
wtxs.entry_function = decode_transaction_args(&t.bytes);

tx_vec.push(wtxs);
}
Expand All @@ -66,27 +71,120 @@ pub fn extract_v5_json_rescue(
Ok((tx_vec, event_vec))
}

pub fn decode_transaction_args(tx_bytes: &[u8]) -> Option<EntryFunctionArgs> {
pub fn decode_transaction_args(wtx: &mut WarehouseTxMaster, tx_bytes: &[u8]) -> Result<()> {
// test we can bcs decode to the transaction object
let t: TransactionV5 = bcs::from_bytes(tx_bytes).unwrap();

if let TransactionV5::UserTransaction(u) = &t {
match &u.raw_txn.payload {
TransactionPayload::ScriptFunction(_) => {
info!("ScriptFunction");

if let Some(sf) = ScriptFunctionCallGenesis::decode(&u.raw_txn.payload) {
dbg!("genesis", &sf);
Some(EntryFunctionArgs::V5(sf))
} else if let Some(sf) = ScriptFunctionCallV520::decode(&u.raw_txn.payload) {
dbg!("520", &sf);
Some(EntryFunctionArgs::V520(sf))
if let TransactionPayload::ScriptFunction(_) = &u.raw_txn.payload {
info!("ScriptFunction");

if let Some(sf) = &ScriptFunctionCallGenesis::decode(&u.raw_txn.payload) {
dbg!("genesis", &sf);
wtx.entry_function = Some(EntryFunctionArgs::V5(sf.to_owned()));

match sf {
ScriptFunctionCallGenesis::BalanceTransfer {
destination,
..
} => {
wtx.relation_label =
RelationLabel::Transfer(cast_legacy_account(&destination)?);
}
ScriptFunctionCallGenesis::CreateAccUser {
..
} => {
// onboards self
wtx.relation_label = RelationLabel::Onboarding(wtx.sender.clone());
}
ScriptFunctionCallGenesis::CreateAccVal {
..
} => {
// onboards self
wtx.relation_label = RelationLabel::Onboarding(wtx.sender.clone());
}

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)?);
}

ScriptFunctionCallGenesis::MinerstateCommit { .. } => {
wtx.relation_label = RelationLabel::Miner;
}
ScriptFunctionCallGenesis::MinerstateCommitByOperator {
..
} => {
wtx.relation_label = RelationLabel::Miner;
}
_ => {
wtx.relation_label = RelationLabel::Configuration;
}
}
}

if let Some(sf) = &ScriptFunctionCallV520::decode(&u.raw_txn.payload) {
dbg!("520", &sf);
wtx.entry_function = Some(EntryFunctionArgs::V520(sf.to_owned()));

match sf {
ScriptFunctionCallV520::CreateAccUser { .. } => {
wtx.relation_label = RelationLabel::Onboarding(wtx.sender.clone());
}
ScriptFunctionCallV520::CreateAccVal { .. } => {
wtx.relation_label = RelationLabel::Onboarding(wtx.sender.clone());
}

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;
}
}
}
_ => None,
}
}
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 Expand Up @@ -126,16 +224,7 @@ fn make_function_name(script: &ScriptView) -> String {
)
}

fn guess_relation(script_name: &str) -> RelationLabel {
if script_name.contains("minerstate_commit") {
RelationLabel::Miner
} else if script_name.contains("create_user_by_coin_tx") {
// TODO: get the address
RelationLabel::Onboarding(AccountAddress::ZERO)
} else if script_name.contains("set_wallet_type") {
RelationLabel::Configuration
} else {
dbg!(&script_name);
RelationLabel::Tx
}

fn cast_legacy_account(legacy: &LegacyAddressV5) -> Result<AccountAddress> {
Ok(AccountAddress::from_hex_literal(&legacy.to_hex_literal())?)
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ pub mod warehouse_cli;

use std::sync::Once;

use env_logger::Env;

static LOGGER: Once = Once::new();

/// Setup function that is only run once, even if called multiple times.
pub fn log_setup() {
LOGGER.call_once(|| {
env_logger::init();
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
});
}
8 changes: 8 additions & 0 deletions src/warehouse_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ pub enum Sub {
/// file with owner map
owner_json: PathBuf,
},
VersionFiveTx {
#[clap(long)]
/// starting path for v5 .tgz files
archive_dir: PathBuf,
}
}

impl WarehouseCli {
Expand Down Expand Up @@ -165,6 +170,9 @@ impl WarehouseCli {

println!("SUCCESS: {} owner accounts linked", owners_merged);
}
VersionFiveTx { archive_dir } => {

}
};
Ok(())
}
Expand Down
26 changes: 9 additions & 17 deletions tests/test_json_rescue_v5_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ fn test_rescue_v5_genesis_create_account() -> anyhow::Result<()> {
assert!(script_function.function().as_str() == "create_user_by_coin_tx");

let sf = ScriptFunctionCall::decode(&u.raw_txn.payload).unwrap();
if let ScriptFunctionCall::CreateUserByCoinTx {
account,
authkey_prefix,
unscaled_value,
} = sf
{
if let ScriptFunctionCall::CreateUserByCoinTx { account, .. } = sf {
assert!(&account.to_string().to_uppercase() == "F605FE7F787551EEA808EE9ACDB98897");
}
}
Expand All @@ -57,16 +52,11 @@ fn test_rescue_v5_parse_miner_tx() -> anyhow::Result<()> {
let t: TransactionV5 = bcs::from_bytes(&first.bytes).unwrap();

if let TransactionV5::UserTransaction(u) = &t {
match &u.raw_txn.payload {
TransactionPayload::WriteSet(write_set_payload) => println!("writeset"),
TransactionPayload::Script(script) => println!("script"),
TransactionPayload::ModuleBundle(module_bundle) => println!("modulebundle"),
TransactionPayload::ScriptFunction(script_function) => {
println!("ScriptFunction");
dbg!(&u.raw_txn.payload);
let sf = ScriptFunctionCall::decode(&u.raw_txn.payload);
dbg!(&sf);
}
if let TransactionPayload::ScriptFunction(_) = &u.raw_txn.payload {
println!("ScriptFunction");
dbg!(&u.raw_txn.payload);
let sf = ScriptFunctionCall::decode(&u.raw_txn.payload);
dbg!(&sf);
}
}

Expand All @@ -91,7 +81,9 @@ fn test_json_full_file() -> anyhow::Result<()> {
let p = fixtures::v5_json_tx_path().join("0-999.json");

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

let first = tx.first().unwrap();
dbg!(&first.entry_function);

Ok(())
}
Expand Down

0 comments on commit 4b3be0c

Please sign in to comment.