|
| 1 | +// Copyright (c) 2024 IOTA Stiftung |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Update the fixtures for the transaction_fixtures() test. |
| 5 | +//! For this to work, have the `iota` repo next to `iota-rust-sdk` and in |
| 6 | +//! the iota repo checkout to the branch for which the fixtures should be |
| 7 | +//! created. |
| 8 | +//! |
| 9 | +//! cargo run |
| 10 | +
|
| 11 | +use std::{fs::OpenOptions, io::Write}; |
| 12 | + |
| 13 | +use fastcrypto::encoding::{Base64, Encoding}; |
| 14 | +use futures::StreamExt; |
| 15 | +use iota_json_rpc_types::{ |
| 16 | + IotaEndOfEpochTransactionKind, IotaTransactionBlockDataAPI, IotaTransactionBlockKind, |
| 17 | + IotaTransactionBlockResponseOptions, IotaTransactionBlockResponseQuery, |
| 18 | +}; |
| 19 | +use iota_types::transaction::SenderSignedData; |
| 20 | +use test_cluster::TestClusterBuilder; |
| 21 | + |
| 22 | +const BASE_PATH: &str = "../"; |
| 23 | + |
| 24 | +#[tokio::main] |
| 25 | +async fn main() -> Result<(), anyhow::Error> { |
| 26 | + let test_cluster = TestClusterBuilder::new().build().await; |
| 27 | + let client = test_cluster.wallet.get_client().await?; |
| 28 | + |
| 29 | + // Build a PTB |
| 30 | + let address = test_cluster.get_address_0(); |
| 31 | + let object_refs = client |
| 32 | + .read_api() |
| 33 | + .get_owned_objects(address, None, None, None) |
| 34 | + .await?; |
| 35 | + let gas_coin = object_refs.data.first().unwrap().object()?.object_id; |
| 36 | + let ptb_tx_data = client |
| 37 | + .transaction_builder() |
| 38 | + .split_coin(address, gas_coin, vec![1_000_000], None, 1_000_000) |
| 39 | + .await?; |
| 40 | + write_bs64_tx_to_file(&bcs::to_bytes(&ptb_tx_data)?, "ptb")?; |
| 41 | + |
| 42 | + // Force new epoch so we get a change epoch tx |
| 43 | + test_cluster.force_new_epoch().await; |
| 44 | + |
| 45 | + let mut txs = client |
| 46 | + .read_api() |
| 47 | + .get_transactions_stream( |
| 48 | + IotaTransactionBlockResponseQuery::new( |
| 49 | + None, |
| 50 | + Some( |
| 51 | + IotaTransactionBlockResponseOptions::new() |
| 52 | + .with_raw_input() |
| 53 | + .with_input(), |
| 54 | + ), |
| 55 | + ), |
| 56 | + None, |
| 57 | + // Starts with the genesis tx |
| 58 | + false, |
| 59 | + ) |
| 60 | + .boxed(); |
| 61 | + |
| 62 | + let mut got_consensus_commit_prologue_v1 = false; |
| 63 | + let mut got_epoch_change = false; |
| 64 | + let mut got_genesis = false; |
| 65 | + |
| 66 | + while let Some(tx) = txs.next().await { |
| 67 | + let transaction = tx.transaction.as_ref().expect( |
| 68 | + "Missing tx in response, add .with_input() to IotaTransactionBlockResponseOptions", |
| 69 | + ); |
| 70 | + match transaction.data.transaction() { |
| 71 | + IotaTransactionBlockKind::Genesis(_genesis_transaction) => { |
| 72 | + if !got_genesis { |
| 73 | + write_bs64_tx_to_file( |
| 74 | + &raw_tx_bytes_to_transaction_data_bytes(&tx.raw_transaction)?, |
| 75 | + "genesis", |
| 76 | + )?; |
| 77 | + got_genesis = true; |
| 78 | + } |
| 79 | + } |
| 80 | + IotaTransactionBlockKind::ConsensusCommitPrologueV1(_consensus_commit_prologue) => { |
| 81 | + if !got_consensus_commit_prologue_v1 { |
| 82 | + write_bs64_tx_to_file( |
| 83 | + &raw_tx_bytes_to_transaction_data_bytes(&tx.raw_transaction)?, |
| 84 | + "consensus-commit-prologue-v1", |
| 85 | + )?; |
| 86 | + got_consensus_commit_prologue_v1 = true; |
| 87 | + } |
| 88 | + } |
| 89 | + IotaTransactionBlockKind::EndOfEpochTransaction(end_of_epoch_tx) => { |
| 90 | + for tx_kind in &end_of_epoch_tx.transactions { |
| 91 | + if let IotaEndOfEpochTransactionKind::ChangeEpoch(_change_epoch) = tx_kind { |
| 92 | + if !got_epoch_change { |
| 93 | + write_bs64_tx_to_file( |
| 94 | + &raw_tx_bytes_to_transaction_data_bytes(&tx.raw_transaction)?, |
| 95 | + "change-epoch", |
| 96 | + )?; |
| 97 | + got_epoch_change = true; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + _ => {} // We don't care about other types for now |
| 103 | + } |
| 104 | + // Break if we got all types |
| 105 | + if got_consensus_commit_prologue_v1 && got_epoch_change && got_genesis { |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if !(got_consensus_commit_prologue_v1 && got_epoch_change && got_genesis) { |
| 111 | + panic!( |
| 112 | + "Didn't get all transaction types: consensus_commit_prologue_v1: {got_consensus_commit_prologue_v1}, epoch_change: {got_epoch_change}, genesis: {got_genesis}" |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + Ok(()) |
| 117 | +} |
| 118 | + |
| 119 | +// Write the tx data bytes base64 encoded to a file with the BASE_PATH before |
| 120 | +// the provided name |
| 121 | +fn write_bs64_tx_to_file(tx_data_bytes: &[u8], name: &str) -> Result<(), anyhow::Error> { |
| 122 | + let mut f = OpenOptions::new() |
| 123 | + .create(true) |
| 124 | + .write(true) |
| 125 | + .truncate(true) |
| 126 | + .open(format!("{BASE_PATH}{name}"))?; |
| 127 | + f.write_all(Base64::encode(tx_data_bytes).as_bytes())?; |
| 128 | + f.flush()?; |
| 129 | + Ok(()) |
| 130 | +} |
| 131 | + |
| 132 | +fn raw_tx_bytes_to_transaction_data_bytes(raw_tx_bytes: &[u8]) -> Result<Vec<u8>, anyhow::Error> { |
| 133 | + let sender_signed_data: SenderSignedData = bcs::from_bytes(raw_tx_bytes)?; |
| 134 | + let tx_data = sender_signed_data.transaction_data(); |
| 135 | + let tx_data_bytes = bcs::to_bytes(tx_data)?; |
| 136 | + Ok(tx_data_bytes) |
| 137 | +} |
0 commit comments