Skip to content

Commit

Permalink
use reference gas price in cluster test (MystenLabs#10123)
Browse files Browse the repository at this point in the history
## Description 

This PR modifies our cluster test and some other necessary places so
that we use the reference gas price for transactions in cluster test
instead of dummy gas price.

## Test Plan 

How did you test the new or updated feature?

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
emmazzz authored Mar 31, 2023
1 parent c8fa325 commit 7f105b7
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 15 deletions.
12 changes: 10 additions & 2 deletions crates/sui-cluster-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use sui_json_rpc_types::{
use sui_types::base_types::TransactionDigest;
use sui_types::messages::ExecuteTransactionRequestType;
use sui_types::object::Owner;
use test_utils::messages::make_transactions_with_wallet_context;
use test_utils::messages::make_transactions_with_wallet_context_and_rgp;

use shared_crypto::intent::Intent;
use sui_sdk::SuiClient;
Expand Down Expand Up @@ -107,10 +107,18 @@ impl TestContext {
self.client.get_wallet_address()
}

async fn get_reference_gas_price(&self) -> u64 {
self.get_fullnode_client()
.governance_api()
.get_reference_gas_price()
.await
.expect("failed to get reference gas price")
}

/// See `make_transactions_with_wallet_context` for potential caveats
/// of this helper function.
pub async fn make_transactions(&mut self, max_txn_num: usize) -> Vec<VerifiedTransaction> {
make_transactions_with_wallet_context(self.get_wallet_mut(), max_txn_num).await
make_transactions_with_wallet_context_and_rgp(self.get_wallet_mut(), max_txn_num).await
}

pub async fn build_transaction_remotely(
Expand Down
18 changes: 16 additions & 2 deletions crates/sui-cluster-test/src/test_case/coin_merge_split_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ impl CoinMergeSplitTest {
coin_to_merge: ObjectID,
gas_obj_id: ObjectID,
) -> SuiTransactionBlockResponse {
let params = rpc_params![signer, primary_coin, coin_to_merge, Some(gas_obj_id), 2000];
let gas_price = ctx.get_reference_gas_price().await;
let params = rpc_params![
signer,
primary_coin,
coin_to_merge,
Some(gas_obj_id),
2000 * gas_price
];

let data = ctx
.build_transaction_remotely("unsafe_mergeCoins", params)
Expand All @@ -135,7 +142,14 @@ impl CoinMergeSplitTest {
amounts: Vec<u64>,
gas_obj_id: ObjectID,
) -> SuiTransactionBlockResponse {
let params = rpc_params![signer, primary_coin, amounts, Some(gas_obj_id), 2000];
let gas_price = ctx.get_reference_gas_price().await;
let params = rpc_params![
signer,
primary_coin,
amounts,
Some(gas_obj_id),
2000 * gas_price
];

let data = ctx
.build_transaction_remotely("unsafe_splitCoin", params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ impl TestCaseImpl for FullNodeBuildPublishTransactionTest {
compiled_package.get_package_base64(/* with_unpublished_deps */ false);
let dependencies = compiled_package.get_dependency_original_package_ids();

let gas_price = ctx.get_reference_gas_price().await;
let params = rpc_params![
ctx.get_wallet_address(),
all_module_bytes,
dependencies,
None::<ObjectID>,
10000
10000 * gas_price
];

let data = ctx
Expand Down
12 changes: 9 additions & 3 deletions crates/sui-cluster-test/src/test_case/native_transfer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ impl TestCaseImpl for NativeTransferTest {
let gas_obj = sui_objs.swap_remove(0);
let signer = ctx.get_wallet_address();
let (recipient_addr, _): (_, AccountKeyPair) = get_key_pair();

let gas_price = ctx.get_reference_gas_price().await;
// Test transfer object
let obj_to_transfer = *sui_objs.swap_remove(0).id();
let params = rpc_params![
signer,
obj_to_transfer,
Some(*gas_obj.id()),
5000,
5000 * gas_price,
recipient_addr
];
let data = ctx
Expand All @@ -54,7 +54,13 @@ impl TestCaseImpl for NativeTransferTest {

// Test transfer sui
let obj_to_transfer = *sui_objs.swap_remove(0).id();
let params = rpc_params![signer, obj_to_transfer, 5000, recipient_addr, None::<u64>];
let params = rpc_params![
signer,
obj_to_transfer,
5000 * gas_price,
recipient_addr,
None::<u64>
];
let data = ctx
.build_transaction_remotely("unsafe_transferSui", params)
.await?;
Expand Down
1 change: 1 addition & 0 deletions crates/sui-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub const AUTHORITIES_DB_NAME: &str = "authorities_db";
pub const CONSENSUS_DB_NAME: &str = "consensus_db";
pub const FULL_NODE_DB_PATH: &str = "full_node_db";

// TODO: change this to a more realistic value like 1000.
const DEFAULT_GAS_PRICE: u64 = 1;
const DEFAULT_COMMISSION_RATE: u64 = 0;

Expand Down
1 change: 1 addition & 0 deletions crates/sui-types/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use tap::Pipe;
use thiserror::Error;
use tracing::trace;

// TODO: use RGP instead.
pub const DUMMY_GAS_PRICE: u64 = 1;

const BLOCKED_MOVE_FUNCTIONS: [(ObjectID, &str, &str); 0] = [];
Expand Down
6 changes: 6 additions & 0 deletions crates/sui/src/client_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,12 @@ impl WalletContext {
))
}

pub async fn get_reference_gas_price(&self) -> Result<u64, anyhow::Error> {
let client = self.get_client().await?;
let gas_price = client.governance_api().get_reference_gas_price().await?;
Ok(gas_price)
}

pub async fn execute_transaction_block(
&self,
tx: VerifiedTransaction,
Expand Down
39 changes: 39 additions & 0 deletions crates/test-utils/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,45 @@ pub async fn make_transactions_with_wallet_context(
res
}

pub async fn make_transactions_with_wallet_context_and_rgp(
context: &mut WalletContext,
max_txn_num: usize,
) -> Vec<VerifiedTransaction> {
let recipient = get_key_pair::<AuthorityKeyPair>().0;
let accounts_and_objs = get_account_and_gas_objects(context).await;
let mut res = Vec::with_capacity(max_txn_num);
let client = context.get_client().await.unwrap();
let gas_price = client
.governance_api()
.get_reference_gas_price()
.await
.unwrap();
for (address, objs) in &accounts_and_objs {
for obj in objs {
if res.len() >= max_txn_num {
return res;
}
let data = TransactionData::new_transfer_sui(
recipient,
*address,
Some(2),
obj.clone()
.into_object()
.expect("Gas coin could not be converted to object ref.")
.object_ref(),
MAX_GAS * gas_price,
gas_price,
);
let tx = to_sender_signed_transaction(
data,
context.config.keystore.get_key(address).unwrap(),
);
res.push(tx);
}
}
res
}

pub async fn make_counter_increment_transaction_with_wallet_context(
context: &WalletContext,
sender: SuiAddress,
Expand Down
26 changes: 19 additions & 7 deletions crates/test-utils/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::messages::{
make_tx_certs_and_signed_effects_with_committee, MAX_GAS,
};

const GAS_BUDGET: u64 = 10000;
const GAS_BUDGET_IN_UNIT: u64 = 10000;

pub fn make_publish_package(gas_object: Object, path: PathBuf) -> VerifiedTransaction {
let (sender, keypair) = deterministic_random_account_key();
Expand Down Expand Up @@ -118,10 +118,17 @@ pub async fn publish_package_with_wallet(
dep_ids: Vec<ObjectID>,
) -> (ObjectRef, TransactionDigest) {
let client = context.get_client().await.unwrap();
let gas_price = context.get_reference_gas_price().await.unwrap();
let transaction = {
let data = client
.transaction_builder()
.publish(sender, all_module_bytes, dep_ids, None, GAS_BUDGET)
.publish(
sender,
all_module_bytes,
dep_ids,
None,
GAS_BUDGET_IN_UNIT * gas_price,
)
.await
.unwrap();

Expand Down Expand Up @@ -171,6 +178,7 @@ pub async fn submit_move_transaction(
) -> SuiTransactionBlockResponse {
debug!(?package_id, ?arguments, "move_transaction");
let client = context.get_client().await.unwrap();
let gas_price = context.get_reference_gas_price().await.unwrap();
let data = client
.transaction_builder()
.move_call(
Expand All @@ -181,7 +189,7 @@ pub async fn submit_move_transaction(
vec![], // type_args
arguments,
gas_object,
GAS_BUDGET,
GAS_BUDGET_IN_UNIT * gas_price,
)
.await
.unwrap();
Expand Down Expand Up @@ -270,6 +278,7 @@ pub async fn create_devnet_nft(
) -> Result<(SuiAddress, ObjectID, TransactionDigest), anyhow::Error> {
let (sender, gas_objects) = get_account_and_gas_coins(context).await?.swap_remove(0);
let gas_object = gas_objects.get(0).unwrap().id();
let gas_price = context.get_reference_gas_price().await?;

let args_json = json!([
"example_nft_name",
Expand All @@ -288,7 +297,7 @@ pub async fn create_devnet_nft(
type_args: vec![],
args,
gas: Some(*gas_object),
gas_budget: GAS_BUDGET,
gas_budget: GAS_BUDGET_IN_UNIT * gas_price,
}
.execute(context)
.await?;
Expand Down Expand Up @@ -318,6 +327,7 @@ pub async fn transfer_sui(
sender: Option<SuiAddress>,
receiver: Option<SuiAddress>,
) -> Result<(ObjectID, SuiAddress, SuiAddress, TransactionDigest), anyhow::Error> {
let gas_price = context.get_reference_gas_price().await?;
let sender = match sender {
None => context.config.keystore.addresses().get(0).cloned().unwrap(),
Some(addr) => addr,
Expand All @@ -334,7 +344,7 @@ pub async fn transfer_sui(
to: receiver,
amount: None,
sui_coin_object_id: gas_ref.0,
gas_budget: GAS_BUDGET,
gas_budget: GAS_BUDGET_IN_UNIT * gas_price,
}
.execute(context)
.await?;
Expand All @@ -361,6 +371,7 @@ pub async fn transfer_coin(
),
anyhow::Error,
> {
let gas_price = context.get_reference_gas_price().await?;
let sender = context.config.keystore.addresses().get(0).cloned().unwrap();
let receiver = context.config.keystore.addresses().get(1).cloned().unwrap();
let client = context.get_client().await.unwrap();
Expand Down Expand Up @@ -393,7 +404,7 @@ pub async fn transfer_coin(
to: receiver,
object_id: object_to_send,
gas: None,
gas_budget: GAS_BUDGET,
gas_budget: GAS_BUDGET_IN_UNIT * gas_price,
}
.execute(context)
.await?;
Expand Down Expand Up @@ -427,12 +438,13 @@ pub async fn transfer_coin(
}

pub async fn split_coin_with_wallet_context(context: &mut WalletContext, coin_id: ObjectID) {
let gas_price = context.get_reference_gas_price().await.unwrap();
SuiClientCommands::SplitCoin {
coin_id,
amounts: None,
count: Some(2),
gas: None,
gas_budget: MAX_GAS,
gas_budget: GAS_BUDGET_IN_UNIT * gas_price,
}
.execute(context)
.await
Expand Down

0 comments on commit 7f105b7

Please sign in to comment.