Skip to content

Commit

Permalink
add crate to update transaction fixtures and also update them
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed Nov 7, 2024
1 parent 2fdd75d commit 60ef78e
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AAQBAAEAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADCvRQiTAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAA
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AAIAAAAAAAAAAAMAAAAAAAAAAPSuRQiTAQAAIMqkGXCaiwgO7vW+HxGGRJKqK/XG/PDv0XQvYDPtrP9BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAA==

Large diffs are not rendered by default.

This file was deleted.

1 change: 1 addition & 0 deletions crates/iota-rust-sdk/src/types/transaction/fixtures/ptb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AAACAQBd73FNj10Qajf+57GswFchYNMadHool5oWwooqv2OsHQEAAAAAAAAAIMywMsW0Eq8kxUMvPriunI4/5MFevTlujsluEO2eq3sBAAkBQEIPAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACA3BheQlzcGxpdF92ZWMBBwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACBGlvdGEESU9UQQACAQAAAQEAD3roDxNW8al/e8qSMX1VsrnQKhFCf2d00KrpSeBUxd4BX2H7GBo8Dz687+cs2Zz0jgm7eTVBr9REodZ6XRGb9GcBAAAAAAAAACBUwR888cuuNQritn9jaBy8dbKeUZ0CwB8kRLqKHQMgig966A8TVvGpf3vKkjF9VbK50CoRQn9ndNCq6UngVMXe6AMAAAAAAABAQg8AAAAAAAA=
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[workspace]

[package]
name = "update_transaction_fixtures"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.71"
bcs = "0.1.4"
fastcrypto = { git = "https://github.com/MystenLabs/fastcrypto", rev = "5f2c63266a065996d53f98156f0412782b468597" }
iota-json-rpc-types = { path = "../../../../../../../../iota/crates/iota-json-rpc-types" }
iota-sdk = { path = "../../../../../../../../iota/crates/iota-sdk" }
iota-keys = { path = "../../../../../../../../iota/crates/iota-keys" }
iota-types = { path = "../../../../../../../../iota/crates/iota-types" }
test-cluster = { path = "../../../../../../../../iota/crates/test-cluster" }
tokio = "1.39.2"
futures = "0.3.28"
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! Update the fixtures for the transaction_fixtures() test.
//! For this to work, have the `iota` repo next to `iota-rust-sdk` and in
//! the iota repo checkout to the branch for which the fixtures should be
//! created.
//!
//! cargo run
use std::{fs::OpenOptions, io::Write};

use fastcrypto::encoding::{Base64, Encoding};
use futures::StreamExt;
use iota_json_rpc_types::{
IotaEndOfEpochTransactionKind, IotaTransactionBlockDataAPI, IotaTransactionBlockKind,
IotaTransactionBlockResponseOptions, IotaTransactionBlockResponseQuery,
};
use iota_types::transaction::SenderSignedData;
use test_cluster::TestClusterBuilder;

const BASE_PATH: &str = "../";

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let test_cluster = TestClusterBuilder::new().build().await;
let client = test_cluster.wallet.get_client().await?;

// Build a PTB
let address = test_cluster.get_address_0();
let object_refs = client
.read_api()
.get_owned_objects(address, None, None, None)
.await?;
let gas_coin = object_refs.data.first().unwrap().object()?.object_id;
let ptb_tx_data = client
.transaction_builder()
.split_coin(address, gas_coin, vec![1_000_000], None, 1_000_000)
.await?;
write_bs64_tx_to_file(&bcs::to_bytes(&ptb_tx_data)?, "ptb")?;

// Force new epoch so we get a change epoch tx
test_cluster.force_new_epoch().await;

let mut txs = client
.read_api()
.get_transactions_stream(
IotaTransactionBlockResponseQuery::new(
None,
Some(
IotaTransactionBlockResponseOptions::new()
.with_raw_input()
.with_input(),
),
),
None,
// Starts with the genesis tx
false,
)
.boxed();

let mut got_consensus_commit_prologue_v1 = false;
let mut got_epoch_change = false;
let mut got_genesis = false;

while let Some(tx) = txs.next().await {
let transaction = tx.transaction.as_ref().expect(
"Missing tx in response, add .with_input() to IotaTransactionBlockResponseOptions",
);
match transaction.data.transaction() {
IotaTransactionBlockKind::Genesis(_genesis_transaction) => {
if !got_genesis {
write_bs64_tx_to_file(
&raw_tx_bytes_to_transaction_data_bytes(&tx.raw_transaction)?,
"./genesis",
)?;
got_genesis = true;
}
}
IotaTransactionBlockKind::ConsensusCommitPrologueV1(_consensus_commit_prologue) => {
if !got_consensus_commit_prologue_v1 {
write_bs64_tx_to_file(
&raw_tx_bytes_to_transaction_data_bytes(&tx.raw_transaction)?,
"./consensus-commit-prologue-v1",
)?;
got_consensus_commit_prologue_v1 = true;
}
}
IotaTransactionBlockKind::EndOfEpochTransaction(end_of_epoch_tx) => {
for tx_kind in &end_of_epoch_tx.transactions {
if let IotaEndOfEpochTransactionKind::ChangeEpoch(_change_epoch) = tx_kind {
if !got_epoch_change {
write_bs64_tx_to_file(
&raw_tx_bytes_to_transaction_data_bytes(&tx.raw_transaction)?,
"./change-epoch",
)?;
got_epoch_change = true;
}
}
}
}
_ => {} // We don't care about other types for now
}
// Break if we got all types
if got_consensus_commit_prologue_v1 && got_epoch_change && got_genesis {
break;
}
}

if !(got_consensus_commit_prologue_v1 && got_epoch_change && got_genesis) {
panic!(
"Didn't get all transaction types: consensus_commit_prologue_v1: {got_consensus_commit_prologue_v1}, epoch_change: {got_epoch_change}, genesis: {got_genesis}"
);
}

Ok(())
}

// Write the tx data bytes base64 encoded to a file with the BASE_PATH before
// the provided name
fn write_bs64_tx_to_file(tx_data_bytes: &[u8], name: &str) -> Result<(), anyhow::Error> {
let mut f = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(format!("{BASE_PATH}{name}"))?;
f.write_all(Base64::encode(tx_data_bytes).as_bytes())?;
f.flush()?;
Ok(())
}

fn raw_tx_bytes_to_transaction_data_bytes(raw_tx_bytes: &[u8]) -> Result<Vec<u8>, anyhow::Error> {
let sender_signed_data: SenderSignedData = bcs::from_bytes(raw_tx_bytes)?;
let tx_data = sender_signed_data.transaction_data();
let tx_data_bytes = bcs::to_bytes(tx_data)?;
Ok(tx_data_bytes)
}
28 changes: 5 additions & 23 deletions crates/iota-rust-sdk/src/types/transaction/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,29 +1253,11 @@ mod test {

#[test]
fn transaction_fixtures() {
// To update the fixtures use the iota-sdk, request a corresponding transaction
// and convert it to base64 like the following:
// let tx_response = client
// .read_api()
// .get_transaction_with_options(
// checkpoint.transactions[0],
// IotaTransactionBlockResponseOptions::new().with_raw_input(),
// )
// .await?;
// let sender_signed_data: iota_types::transaction::SenderSignedData =
// bcs::from_bytes(&tx_response.raw_transaction).unwrap();
// let tx_data = sender_signed_data.transaction_data();
// println!(
// "{}",
// <fastcrypto::encoding::Base64 as
// fastcrypto::encoding::Encoding>::encode(bcs::to_bytes(
// tx_data
// )?)
// );
const GENESIS_TRANSACTION: &str = include_str!("fixtures/genesis-transaction");
const CONSENSUS_PROLOGUE: &str = "AAJDAAAAAAAAAI4CAAAAAAAAAFGk2N2SAQAAIOILX2b6jMcm6eMh2PLilUlPkMHHA70qjw5m6HUBU45+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAA==";
const EPOCH_CHANGE: &str = "AAQBAEAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAML+1N2SAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAA";
const PTB: &str = "AAACAAgA0O2QLgAAAAAgERERERUE6TUOY11lzTjM0sApQ0xqOkgNiUepumoVshUCAgAFAQAAAQAAAQAAAQAAAQAAAQUDAAAAAAMAAAEAAwAAAgADAAADAAMAAAQAAQEA1dk1p8euO6O+qDrVqmJNGhat5thMVunhMP8bmDfnfiIBCOfaLD6XvxOgV5xdw7DDiE+wHdmBj3/e7d1XnRnMkj8BAAAAAAAAACBemVndc8ApfzVuUeaArMKKgsPMl8VFeGWYAzD8Hb5cfdXZNafHrjujvqg61apiTRoWrebYTFbp4TD/G5g3534i6AMAAAAAAAAA5AtUAgAAAAA=";
// Look in the fixtures folder to see how to update them
const GENESIS_TRANSACTION: &str = include_str!("fixtures/genesis");
const CONSENSUS_PROLOGUE: &str = include_str!("fixtures/consensus-commit-prologue-v1");
const EPOCH_CHANGE: &str = include_str!("fixtures/change-epoch");
const PTB: &str = include_str!("fixtures/ptb");

for fixture in [GENESIS_TRANSACTION, CONSENSUS_PROLOGUE, EPOCH_CHANGE, PTB] {
let fixture = Base64::decode_vec(fixture.trim()).unwrap();
Expand Down

0 comments on commit 60ef78e

Please sign in to comment.