Skip to content

Commit d59017a

Browse files
committed
add crate to update transaction fixtures and also update them
1 parent 2fdd75d commit d59017a

File tree

9 files changed

+165
-24
lines changed

9 files changed

+165
-24
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AAQBAAEAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADCvRQiTAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAA
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AAIAAAAAAAAAAAMAAAAAAAAAAPSuRQiTAQAAIMqkGXCaiwgO7vW+HxGGRJKqK/XG/PDv0XQvYDPtrP9BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAA==

crates/iota-rust-sdk/src/types/transaction/fixtures/genesis

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

crates/iota-rust-sdk/src/types/transaction/fixtures/genesis-transaction

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AAACAQBd73FNj10Qajf+57GswFchYNMadHool5oWwooqv2OsHQEAAAAAAAAAIMywMsW0Eq8kxUMvPriunI4/5MFevTlujsluEO2eq3sBAAkBQEIPAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACA3BheQlzcGxpdF92ZWMBBwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACBGlvdGEESU9UQQACAQAAAQEAD3roDxNW8al/e8qSMX1VsrnQKhFCf2d00KrpSeBUxd4BX2H7GBo8Dz687+cs2Zz0jgm7eTVBr9REodZ6XRGb9GcBAAAAAAAAACBUwR888cuuNQritn9jaBy8dbKeUZ0CwB8kRLqKHQMgig966A8TVvGpf3vKkjF9VbK50CoRQn9ndNCq6UngVMXe6AMAAAAAAABAQg8AAAAAAAA=
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[workspace]
2+
3+
[package]
4+
name = "update_transaction_fixtures"
5+
version = "0.1.0"
6+
edition = "2021"
7+
8+
[dependencies]
9+
anyhow = "1.0.71"
10+
bcs = "0.1.4"
11+
fastcrypto = { git = "https://github.com/MystenLabs/fastcrypto", rev = "5f2c63266a065996d53f98156f0412782b468597" }
12+
iota-json-rpc-types = { path = "../../../../../../../../iota/crates/iota-json-rpc-types" }
13+
iota-sdk = { path = "../../../../../../../../iota/crates/iota-sdk" }
14+
iota-keys = { path = "../../../../../../../../iota/crates/iota-keys" }
15+
iota-types = { path = "../../../../../../../../iota/crates/iota-types" }
16+
test-cluster = { path = "../../../../../../../../iota/crates/test-cluster" }
17+
tokio = "1.39.2"
18+
futures = "0.3.28"
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
}

crates/iota-rust-sdk/src/types/transaction/serialization.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,29 +1253,11 @@ mod test {
12531253

12541254
#[test]
12551255
fn transaction_fixtures() {
1256-
// To update the fixtures use the iota-sdk, request a corresponding transaction
1257-
// and convert it to base64 like the following:
1258-
// let tx_response = client
1259-
// .read_api()
1260-
// .get_transaction_with_options(
1261-
// checkpoint.transactions[0],
1262-
// IotaTransactionBlockResponseOptions::new().with_raw_input(),
1263-
// )
1264-
// .await?;
1265-
// let sender_signed_data: iota_types::transaction::SenderSignedData =
1266-
// bcs::from_bytes(&tx_response.raw_transaction).unwrap();
1267-
// let tx_data = sender_signed_data.transaction_data();
1268-
// println!(
1269-
// "{}",
1270-
// <fastcrypto::encoding::Base64 as
1271-
// fastcrypto::encoding::Encoding>::encode(bcs::to_bytes(
1272-
// tx_data
1273-
// )?)
1274-
// );
1275-
const GENESIS_TRANSACTION: &str = include_str!("fixtures/genesis-transaction");
1276-
const CONSENSUS_PROLOGUE: &str = "AAJDAAAAAAAAAI4CAAAAAAAAAFGk2N2SAQAAIOILX2b6jMcm6eMh2PLilUlPkMHHA70qjw5m6HUBU45+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAA==";
1277-
const EPOCH_CHANGE: &str = "AAQBAEAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAML+1N2SAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAA";
1278-
const PTB: &str = "AAACAAgA0O2QLgAAAAAgERERERUE6TUOY11lzTjM0sApQ0xqOkgNiUepumoVshUCAgAFAQAAAQAAAQAAAQAAAQAAAQUDAAAAAAMAAAEAAwAAAgADAAADAAMAAAQAAQEA1dk1p8euO6O+qDrVqmJNGhat5thMVunhMP8bmDfnfiIBCOfaLD6XvxOgV5xdw7DDiE+wHdmBj3/e7d1XnRnMkj8BAAAAAAAAACBemVndc8ApfzVuUeaArMKKgsPMl8VFeGWYAzD8Hb5cfdXZNafHrjujvqg61apiTRoWrebYTFbp4TD/G5g3534i6AMAAAAAAAAA5AtUAgAAAAA=";
1256+
// Look in the fixtures folder to see how to update them
1257+
const GENESIS_TRANSACTION: &str = include_str!("fixtures/genesis");
1258+
const CONSENSUS_PROLOGUE: &str = include_str!("fixtures/consensus-commit-prologue-v1");
1259+
const EPOCH_CHANGE: &str = include_str!("fixtures/change-epoch");
1260+
const PTB: &str = include_str!("fixtures/ptb");
12791261

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

0 commit comments

Comments
 (0)