Skip to content

Commit

Permalink
refactor: use dedicated keypairs and consider epoch progression from …
Browse files Browse the repository at this point in the history
…other tests
  • Loading branch information
samuel-rufi committed Oct 29, 2024
1 parent 20e6f46 commit 136d05f
Showing 1 changed file with 73 additions and 100 deletions.
173 changes: 73 additions & 100 deletions crates/iota-indexer/tests/rpc-tests/governance_api.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
// Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use iota_json_rpc_api::{
CoinReadApiClient, GovernanceReadApiClient, IndexerApiClient, TransactionBuilderClient,
WriteApiClient,
};
use iota_json_rpc_types::{
CoinPage, DelegatedStake, IotaObjectDataOptions, IotaObjectResponseQuery,
IotaTransactionBlockResponseOptions, ObjectsPage, StakeStatus, TransactionBlockBytes,
};
use iota_json_rpc_api::{GovernanceReadApiClient, TransactionBuilderClient};
use iota_json_rpc_types::{DelegatedStake, StakeStatus, TransactionBlockBytes};
use iota_test_transaction_builder::TestTransactionBuilder;
use iota_types::{
IOTA_FRAMEWORK_ADDRESS, IOTA_SYSTEM_ADDRESS,
Expand All @@ -17,7 +11,6 @@ use iota_types::{
crypto::{AccountKeyPair, get_key_pair},
gas_coin::GAS,
programmable_transaction_builder::ProgrammableTransactionBuilder,
quorum_driver_types::ExecuteTransactionRequestType,
transaction::{CallArg, ObjectArg},
utils::to_sender_signed_transaction,
};
Expand All @@ -40,26 +33,30 @@ fn test_staking() {
runtime.block_on(async move {
indexer_wait_for_checkpoint(store, 1).await;

let address = cluster.get_address_0();

let objects: ObjectsPage = client
.get_owned_objects(
address,
Some(IotaObjectResponseQuery::new_with_options(
IotaObjectDataOptions::new()
.with_type()
.with_owner()
.with_previous_transaction(),
)),
None,
None,
let (sender, keypair): (_, AccountKeyPair) = get_key_pair();

let gas = cluster
.fund_address_and_return_gas(
cluster.get_reference_gas_price().await,
Some(10_000_000_000),
sender,
)
.await
.unwrap();
assert_eq!(5, objects.data.len());
.await;

indexer_wait_for_object(client, gas.0, gas.1).await;

let iota_coin_ref = cluster
.fund_address_and_return_gas(
cluster.get_reference_gas_price().await,
Some(10_000_000_000),
sender,
)
.await;

indexer_wait_for_object(client, iota_coin_ref.0, iota_coin_ref.1).await;

// Check StakedIota object before test
let staked_iota: Vec<DelegatedStake> = client.get_stakes(address).await.unwrap();
let staked_iota: Vec<DelegatedStake> = client.get_stakes(sender).await.unwrap();
assert!(staked_iota.is_empty());

let validator = client
Expand All @@ -69,40 +66,29 @@ fn test_staking() {
.active_validators[0]
.iota_address;

let coin = objects.data[0].object().unwrap().object_id;
// Delegate some IOTA
let transaction_bytes: TransactionBlockBytes = client
.request_add_stake(
address,
vec![coin],
sender,
vec![iota_coin_ref.0],
Some(1000000000.into()),
validator,
None,
Some(gas.0),
100_000_000.into(),
)
.await
.unwrap();
let tx = cluster
.wallet
.sign_transaction(&transaction_bytes.to_data().unwrap());

let (tx_bytes, signatures) = tx.to_tx_bytes_and_signatures();
let txn = to_sender_signed_transaction(transaction_bytes.to_data().unwrap(), &keypair);

client
.execute_transaction_block(
tx_bytes,
signatures,
Some(IotaTransactionBlockResponseOptions::new()),
Some(ExecuteTransactionRequestType::WaitForLocalExecution),
)
.await
.unwrap();
let res = cluster.wallet.execute_transaction_must_succeed(txn).await;
indexer_wait_for_transaction(res.digest, store, client).await;

cluster.force_new_epoch().await;
indexer_wait_for_latest_checkpoint(store, cluster).await;

// Check DelegatedStake object after epoch transition
let staked_iota: Vec<DelegatedStake> = client.get_stakes(address).await.unwrap();
let staked_iota: Vec<DelegatedStake> = client.get_stakes(sender).await.unwrap();
assert_eq!(1, staked_iota.len());
assert_eq!(1000000000, staked_iota[0].stakes[0].principal);
assert!(matches!(
Expand All @@ -128,16 +114,30 @@ fn test_unstaking() {
runtime.block_on(async move {
indexer_wait_for_checkpoint(store, 1).await;

let address = cluster.get_address_0();
let (sender, keypair): (_, AccountKeyPair) = get_key_pair();

let coins: CoinPage = indexer_client
.get_coins(address, None, None, None)
.await
.unwrap();
assert_eq!(5, coins.data.len());
let gas = cluster
.fund_address_and_return_gas(
cluster.get_reference_gas_price().await,
Some(10_000_000_000),
sender,
)
.await;

indexer_wait_for_object(client, gas.0, gas.1).await;

let iota_coin_ref = cluster
.fund_address_and_return_gas(
cluster.get_reference_gas_price().await,
Some(10_000_000_000),
sender,
)
.await;

indexer_wait_for_object(client, iota_coin_ref.0, iota_coin_ref.1).await;

// Check StakedIota object before test
let staked_iota: Vec<DelegatedStake> = indexer_client.get_stakes(address).await.unwrap();
let staked_iota: Vec<DelegatedStake> = indexer_client.get_stakes(sender).await.unwrap();
assert!(staked_iota.is_empty());

let validator = indexer_client
Expand All @@ -150,36 +150,26 @@ fn test_unstaking() {
// Delegate some IOTA
let transaction_bytes: TransactionBlockBytes = indexer_client
.request_add_stake(
address,
vec![coins.data[0].coin_object_id],
sender,
vec![iota_coin_ref.0],
Some(1000000000.into()),
validator,
None,
Some(gas.0),
100_000_000.into(),
)
.await
.unwrap();
let tx = cluster
.wallet
.sign_transaction(&transaction_bytes.to_data().unwrap());

let (tx_bytes, signatures) = tx.to_tx_bytes_and_signatures();
let txn = to_sender_signed_transaction(transaction_bytes.to_data().unwrap(), &keypair);

indexer_client
.execute_transaction_block(
tx_bytes,
signatures,
Some(IotaTransactionBlockResponseOptions::new()),
Some(ExecuteTransactionRequestType::WaitForLocalExecution),
)
.await
.unwrap();
let res = cluster.wallet.execute_transaction_must_succeed(txn).await;
indexer_wait_for_transaction(res.digest, store, client).await;

cluster.force_new_epoch().await;
indexer_wait_for_latest_checkpoint(store, cluster).await;

// Check DelegatedStake object
let staked_iota: Vec<DelegatedStake> = indexer_client.get_stakes(address).await.unwrap();
let staked_iota: Vec<DelegatedStake> = indexer_client.get_stakes(sender).await.unwrap();
assert_eq!(1, staked_iota.len());
assert_eq!(1000000000, staked_iota[0].stakes[0].principal);
assert!(matches!(
Expand All @@ -191,18 +181,18 @@ fn test_unstaking() {

let transaction_bytes: TransactionBlockBytes = indexer_client
.request_withdraw_stake(
address,
sender,
staked_iota[0].stakes[0].staked_iota_id,
None,
Some(gas.0),
100_000_000.into(),
)
.await
.unwrap();
let tx = cluster
.wallet
.sign_transaction(&transaction_bytes.to_data().unwrap());

let _ = cluster.wallet.execute_transaction_must_succeed(tx).await;
let txn = to_sender_signed_transaction(transaction_bytes.to_data().unwrap(), &keypair);

let res = cluster.wallet.execute_transaction_must_succeed(txn).await;
indexer_wait_for_transaction(res.digest, store, client).await;

cluster.force_new_epoch().await;
indexer_wait_for_latest_checkpoint(store, cluster).await;
Expand All @@ -224,7 +214,7 @@ fn test_unstaking() {
.unwrap();
assert_eq!(0, indexer_response.len());

let staked_iota: Vec<DelegatedStake> = indexer_client.get_stakes(address).await.unwrap();
let staked_iota: Vec<DelegatedStake> = indexer_client.get_stakes(sender).await.unwrap();
assert!(staked_iota.is_empty());
});
}
Expand Down Expand Up @@ -316,7 +306,7 @@ fn test_timelocked_staking() {
let context = &cluster.wallet;
let gas_price = context.get_reference_gas_price().await.unwrap();

let tx_builder = TestTransactionBuilder::new(sender, gas, gas_price);
let tx_builder = TestTransactionBuilder::new(sender, gas, gas_price * 2);
let txn = to_sender_signed_transaction(tx_builder.programmable(pt).build(), &keypair);

let res = context.execute_transaction_must_succeed(txn).await;
Expand Down Expand Up @@ -504,10 +494,9 @@ fn get_latest_iota_system_state() {
runtime.block_on(async move {
indexer_wait_for_checkpoint(store, 1).await;

let response = client.get_latest_iota_system_state().await.unwrap();
assert_eq!(response.epoch, 0);
assert_eq!(response.protocol_version, 1);
assert_eq!(response.system_state_version, 1);
let system_state = client.get_latest_iota_system_state().await.unwrap();
assert_eq!(system_state.protocol_version, 1);
assert_eq!(system_state.system_state_version, 1);
});
}

Expand All @@ -517,7 +506,7 @@ fn get_committee_info() {
runtime,
store,
client,
cluster,
..
} = ApiTestSetup::get_or_init();

runtime.block_on(async move {
Expand All @@ -526,10 +515,7 @@ fn get_committee_info() {
// Test with no specified epoch
let response = client.get_committee_info(None).await.unwrap();

let (epoch_id, validators) = (response.epoch, response.validators);

assert!(epoch_id == 0);
assert_eq!(validators.len(), 4);
assert_eq!(response.validators.len(), 4);

// Test with specified epoch 0
let response = client.get_committee_info(Some(0.into())).await.unwrap();
Expand All @@ -540,20 +526,9 @@ fn get_committee_info() {
assert_eq!(validators.len(), 4);

// Test with non-existent epoch
let response = client.get_committee_info(Some(1.into())).await;
let response = client.get_committee_info(Some(u64::MAX.into())).await;

assert!(response.is_err());

// Sleep for 5 seconds
cluster.force_new_epoch().await;

// Test with specified epoch 1
let response = client.get_committee_info(Some(1.into())).await.unwrap();

let (epoch_id, validators) = (response.epoch, response.validators);

assert!(epoch_id == 1);
assert_eq!(validators.len(), 4);
});
}

Expand Down Expand Up @@ -586,10 +561,8 @@ fn get_validators_apy() {
runtime.block_on(async move {
indexer_wait_for_checkpoint(store, 1).await;

let response = client.get_validators_apy().await.unwrap();
let (apys, epoch) = (response.apys, response.epoch);
let apys = client.get_validators_apy().await.unwrap().apys;

assert_eq!(epoch, 0);
assert_eq!(apys.len(), 4);
assert!(apys.iter().any(|apy| apy.apy == 0.0));
});
Expand Down

0 comments on commit 136d05f

Please sign in to comment.