Skip to content

Commit ae69c47

Browse files
kodemartinalexsporn
authored andcommitted
refactor(rosetta): use new system-state summary (#5507)
1 parent 99fb851 commit ae69c47

File tree

3 files changed

+67
-31
lines changed

3 files changed

+67
-31
lines changed

crates/iota-rosetta/src/network.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use axum::{Extension, Json, extract::State};
88
use axum_extra::extract::WithRejection;
99
use fastcrypto::encoding::Hex;
10-
use iota_types::base_types::ObjectID;
10+
use iota_types::{
11+
base_types::ObjectID, iota_system_state::iota_system_state_summary::IotaSystemStateSummary,
12+
};
1113
use serde_json::json;
1214
use strum::IntoEnumIterator;
1315

@@ -50,8 +52,12 @@ pub async fn status(
5052
.get_latest_iota_system_state()
5153
.await?;
5254

53-
let peers = system_state
54-
.active_validators
55+
let active_validators = match system_state {
56+
IotaSystemStateSummary::V1(v1) => v1.active_validators,
57+
IotaSystemStateSummary::V2(v2) => v2.active_validators,
58+
_ => return Err(anyhow::anyhow!("unsupported IotaSystemStateSummary"))?,
59+
};
60+
let peers = active_validators
5561
.iter()
5662
.map(|validator| Peer {
5763
peer_id: ObjectID::from(validator.iota_address).into(),

crates/iota-rosetta/src/unit_tests/balance_changing_tx_tests.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use iota_types::{
2626
TypeTag,
2727
base_types::{IotaAddress, ObjectID, ObjectRef},
2828
gas_coin::GasCoin,
29+
iota_system_state::iota_system_state_summary::IotaSystemStateSummary,
2930
programmable_transaction_builder::ProgrammableTransactionBuilder,
3031
quorum_driver_types::ExecuteTransactionRequestType,
3132
transaction::{
@@ -490,13 +491,17 @@ async fn test_stake_iota() {
490491
let sender = get_random_address(&network.get_addresses(), vec![]);
491492
let coin1 = get_random_iota(&client, sender, vec![]).await;
492493
let coin2 = get_random_iota(&client, sender, vec![coin1.0]).await;
493-
let validator = client
494+
let system_state = client
494495
.governance_api()
495496
.get_latest_iota_system_state()
496497
.await
497-
.unwrap()
498-
.active_validators[0]
499-
.iota_address;
498+
.unwrap();
499+
let active_validators = match system_state {
500+
IotaSystemStateSummary::V1(v1) => v1.active_validators,
501+
IotaSystemStateSummary::V2(v2) => v2.active_validators,
502+
_ => panic!("unsupported IotaSystemStateSummary"),
503+
};
504+
let validator = active_validators[0].iota_address;
500505
let tx = client
501506
.transaction_builder()
502507
.request_add_stake(
@@ -538,13 +543,17 @@ async fn test_stake_iota_with_none_amount() {
538543
let sender = get_random_address(&network.get_addresses(), vec![]);
539544
let coin1 = get_random_iota(&client, sender, vec![]).await;
540545
let coin2 = get_random_iota(&client, sender, vec![coin1.0]).await;
541-
let validator = client
546+
let system_state = client
542547
.governance_api()
543548
.get_latest_iota_system_state()
544549
.await
545-
.unwrap()
546-
.active_validators[0]
547-
.iota_address;
550+
.unwrap();
551+
let active_validators = match system_state {
552+
IotaSystemStateSummary::V1(v1) => v1.active_validators,
553+
IotaSystemStateSummary::V2(v2) => v2.active_validators,
554+
_ => panic!("unsupported IotaSystemStateSummary"),
555+
};
556+
let validator = active_validators[0].iota_address;
548557
let tx = client
549558
.transaction_builder()
550559
.request_add_stake(
@@ -614,13 +623,17 @@ async fn test_delegation_parsing() -> Result<(), anyhow::Error> {
614623
let client = network.wallet.get_client().await.unwrap();
615624
let sender = get_random_address(&network.get_addresses(), vec![]);
616625
let gas = get_random_iota(&client, sender, vec![]).await;
617-
let validator = client
626+
let system_state = client
618627
.governance_api()
619628
.get_latest_iota_system_state()
620629
.await
621-
.unwrap()
622-
.active_validators[0]
623-
.iota_address;
630+
.unwrap();
631+
let active_validators = match system_state {
632+
IotaSystemStateSummary::V1(v1) => v1.active_validators,
633+
IotaSystemStateSummary::V2(v2) => v2.active_validators,
634+
_ => anyhow::bail!("unsupported IotaSystemStateSummary"),
635+
};
636+
let validator = active_validators[0].iota_address;
624637

625638
let ops: Operations = serde_json::from_value(json!(
626639
[{

crates/iota-rosetta/tests/end_to_end_tests.rs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use iota_rosetta::{
1616
use iota_sdk::rpc_types::{IotaExecutionStatus, IotaTransactionBlockEffectsAPI};
1717
use iota_swarm_config::genesis_config::{DEFAULT_GAS_AMOUNT, DEFAULT_NUMBER_OF_OBJECT_PER_ACCOUNT};
1818
use iota_types::{
19+
iota_system_state::iota_system_state_summary::IotaSystemStateSummary,
1920
quorum_driver_types::ExecuteTransactionRequestType, utils::to_sender_signed_transaction,
2021
};
2122
use rosetta_client::start_rosetta_test_server;
@@ -78,13 +79,17 @@ async fn test_get_staked_iota() {
7879
assert_eq!(response.balances[0].value, 0);
7980

8081
// Stake some iota
81-
let validator = client
82+
let system_state = client
8283
.governance_api()
8384
.get_latest_iota_system_state()
8485
.await
85-
.unwrap()
86-
.active_validators[0]
87-
.iota_address;
86+
.unwrap();
87+
let active_validators = match system_state {
88+
IotaSystemStateSummary::V1(v1) => v1.active_validators,
89+
IotaSystemStateSummary::V2(v2) => v2.active_validators,
90+
_ => panic!("unsupported IotaSystemStateSummary"),
91+
};
92+
let validator = active_validators[0].iota_address;
8893
let coins = client
8994
.coin_read_api()
9095
.get_coins(address, None, None, None)
@@ -136,13 +141,17 @@ async fn test_stake() {
136141

137142
let (rosetta_client, _handle) = start_rosetta_test_server(client.clone()).await;
138143

139-
let validator = client
144+
let system_state = client
140145
.governance_api()
141146
.get_latest_iota_system_state()
142147
.await
143-
.unwrap()
144-
.active_validators[0]
145-
.iota_address;
148+
.unwrap();
149+
let active_validators = match system_state {
150+
IotaSystemStateSummary::V1(v1) => v1.active_validators,
151+
IotaSystemStateSummary::V2(v2) => v2.active_validators,
152+
_ => panic!("unsupported IotaSystemStateSummary"),
153+
};
154+
let validator = active_validators[0].iota_address;
146155

147156
let ops = serde_json::from_value(json!(
148157
[{
@@ -197,13 +206,17 @@ async fn test_stake_all() {
197206

198207
let (rosetta_client, _handle) = start_rosetta_test_server(client.clone()).await;
199208

200-
let validator = client
209+
let system_state = client
201210
.governance_api()
202211
.get_latest_iota_system_state()
203212
.await
204-
.unwrap()
205-
.active_validators[0]
206-
.iota_address;
213+
.unwrap();
214+
let active_validators = match system_state {
215+
IotaSystemStateSummary::V1(v1) => v1.active_validators,
216+
IotaSystemStateSummary::V2(v2) => v2.active_validators,
217+
_ => panic!("unsupported IotaSystemStateSummary"),
218+
};
219+
let validator = active_validators[0].iota_address;
207220

208221
let ops = serde_json::from_value(json!(
209222
[{
@@ -263,13 +276,17 @@ async fn test_withdraw_stake() {
263276
let (rosetta_client, _handle) = start_rosetta_test_server(client.clone()).await;
264277

265278
// First add some stakes
266-
let validator = client
279+
let system_state = client
267280
.governance_api()
268281
.get_latest_iota_system_state()
269282
.await
270-
.unwrap()
271-
.active_validators[0]
272-
.iota_address;
283+
.unwrap();
284+
let active_validators = match system_state {
285+
IotaSystemStateSummary::V1(v1) => v1.active_validators,
286+
IotaSystemStateSummary::V2(v2) => v2.active_validators,
287+
_ => panic!("unsupported IotaSystemStateSummary"),
288+
};
289+
let validator = active_validators[0].iota_address;
273290

274291
let ops = serde_json::from_value(json!(
275292
[{

0 commit comments

Comments
 (0)