Skip to content

Commit

Permalink
Refactor with framework sdk (#4302)
Browse files Browse the repository at this point in the history
* fix some move entry-function names

* fix some executor tests

* fix executor tests: vm_version, flexidagconfig
* refactor helper functions with auto-generated sdk
* create a account by p2p transfer
* fix building errors

* update genesis to avoid test failure
  • Loading branch information
simonjiao authored Nov 27, 2024
1 parent 3d39449 commit 9451893
Show file tree
Hide file tree
Showing 22 changed files with 78 additions and 147 deletions.
4 changes: 2 additions & 2 deletions chain/tests/block_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use starcoin_logger::prelude::*;
use starcoin_statedb::ChainStateDB;
use starcoin_storage::storage::StorageInstance;
use starcoin_storage::Storage;
use starcoin_transaction_builder::{build_empty_script, DEFAULT_EXPIRATION_TIME};
use starcoin_transaction_builder::{empty_txn_payload, DEFAULT_EXPIRATION_TIME};
use starcoin_types::block::BlockHeaderExtra;
use starcoin_types::proptest_types::{AccountInfoUniverse, Index, SignatureCheckedTransactionGen};
use starcoin_types::transaction::{SignedUserTransaction, Transaction, TransactionPayload};
Expand Down Expand Up @@ -86,7 +86,7 @@ fn gen_header(
}

fn gen_script_payload() -> TransactionPayload {
TransactionPayload::EntryFunction(build_empty_script())
empty_txn_payload()
}

fn txn_transfer(
Expand Down
2 changes: 1 addition & 1 deletion cmd/airdrop/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ async fn main() -> Result<()> {
let script_function = EntryFunction::new(
ModuleId::new(
genesis_address(),
Identifier::new("TransferScripts").unwrap(),
Identifier::new("transfer_scripts").unwrap(),
),
Identifier::new("batch_peer_to_peer_v2").unwrap(),
vec![token_type.clone().into()],
Expand Down
2 changes: 1 addition & 1 deletion cmd/db-exporter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ pub fn create_account_txn_sent_as_association(
TransactionPayload::EntryFunction(EntryFunction::new(
ModuleId::new(
starcoin_vm_types::account_config::core_code_address(),
Identifier::new("Account").unwrap(),
Identifier::new("account").unwrap(),
),
Identifier::new("create_account_with_initial_amount").unwrap(),
vec![stc_type_tag()],
Expand Down
2 changes: 1 addition & 1 deletion cmd/starcoin/src/account/accept_token_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl CommandAction for AcceptTokenCommand {
ctx.state().build_and_execute_transaction(
opt.transaction_opts.clone(),
TransactionPayload::EntryFunction(EntryFunction::new(
ModuleId::new(core_code_address(), Identifier::new("Account").unwrap()),
ModuleId::new(core_code_address(), Identifier::new("account").unwrap()),
Identifier::new("accept_token").unwrap(),
vec![TypeTag::Struct(Box::new(
opt.token_code.clone().try_into().unwrap(),
Expand Down
9 changes: 3 additions & 6 deletions cmd/starcoin/src/account/transfer_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use starcoin_transaction_builder::encode_transfer_script_by_token_code;
use starcoin_types::account_address::AccountAddress;
use starcoin_vm_types::token::stc::G_STC_TOKEN_CODE;
use starcoin_vm_types::token::token_code::TokenCode;
use starcoin_vm_types::transaction::TransactionPayload;

use crate::cli_state::CliState;
use crate::view::{ExecuteResultView, TransactionOptions};
Expand Down Expand Up @@ -60,11 +59,9 @@ impl CommandAction for TransferCommand {
.token_code
.clone()
.unwrap_or_else(|| G_STC_TOKEN_CODE.clone());
let script_function =
let payload =
encode_transfer_script_by_token_code(receiver_address, opt.amount, token_code);
ctx.state().build_and_execute_transaction(
opt.transaction_opts.clone(),
TransactionPayload::EntryFunction(script_function),
)
ctx.state()
.build_and_execute_transaction(opt.transaction_opts.clone(), payload)
}
}
6 changes: 2 additions & 4 deletions cmd/starcoin/src/dev/gen_block_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use crate::StarcoinOpt;
use anyhow::{ensure, Result};
use clap::Parser;
use scmd::{CommandAction, ExecContext};
use starcoin_transaction_builder::build_empty_script;
use starcoin_types::transaction::TransactionPayload;
use starcoin_transaction_builder::empty_txn_payload;

/// Trigger a new block in dev.
#[derive(Debug, Parser)]
Expand All @@ -30,13 +29,12 @@ impl CommandAction for GenBlockCommand {
let cli_state = ctx.state();
let net = cli_state.net();
ensure!(net.is_dev(), "Only dev network support this command");
let empty = build_empty_script();
let txn_opts = TransactionOptions {
blocking: true,
dry_run: false,
..Default::default()
};
ctx.state()
.build_and_execute_transaction(txn_opts, TransactionPayload::EntryFunction(empty))
.build_and_execute_transaction(txn_opts, empty_txn_payload())
}
}
5 changes: 2 additions & 3 deletions cmd/starcoin/src/dev/get_coin_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use starcoin_types::account_config;
use starcoin_types::account_config::STCUnit;
use starcoin_vm_types::account_config::G_STC_TOKEN_CODE;
use starcoin_vm_types::token::token_value::TokenValue;
use starcoin_vm_types::transaction::TransactionPayload;
use std::time::Duration;

/// Get stc to default account.
Expand Down Expand Up @@ -71,11 +70,11 @@ impl CommandAction for GetCoinCommand {
state
.build_and_execute_transaction(
txn_opt,
TransactionPayload::EntryFunction(encode_transfer_script_by_token_code(
encode_transfer_script_by_token_code(
to,
opt.amount.scaling(),
G_STC_TOKEN_CODE.clone(),
)),
),
)?
.get_transaction_info()
} else {
Expand Down
7 changes: 2 additions & 5 deletions cmd/starcoin/src/dev/upgrade_vm_config_proposal_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use starcoin_rpc_client::StateRootOption;
use starcoin_transaction_builder::build_vm_config_upgrade_proposal;
use starcoin_vm_runtime::starcoin_vm::StarcoinVM;
use starcoin_vm_types::on_chain_config::VMConfig;
use starcoin_vm_types::transaction::TransactionPayload;

/// Submit a VM config upgrade proposal
#[derive(Debug, Parser)]
Expand Down Expand Up @@ -62,9 +61,7 @@ impl CommandAction for UpgradeVMConfigProposalCommand {
let min_action_delay = get_dao_config(ctx.state())?.min_action_delay;
let vm_config_upgrade_proposal =
build_vm_config_upgrade_proposal(genesis_config.vm_config, min_action_delay);
ctx.state().build_and_execute_transaction(
opt.transaction_opts.clone(),
TransactionPayload::EntryFunction(vm_config_upgrade_proposal),
)
ctx.state()
.build_and_execute_transaction(opt.transaction_opts.clone(), vm_config_upgrade_proposal)
}
}
9 changes: 4 additions & 5 deletions executor/benchmark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use starcoin_types::{
use starcoin_vm_types::genesis_config::StdlibVersion;
use starcoin_vm_types::token::stc;
use starcoin_vm_types::transaction::authenticator::AuthenticationKey;
use starcoin_vm_types::transaction::EntryFunction;
use std::sync::mpsc;
use std::sync::Arc;

Expand Down Expand Up @@ -128,13 +127,13 @@ impl TransactionGenerator {
for (j, account) in block.iter().enumerate() {
let txn = create_transaction(
self.sequence,
encode_create_account_script_function(
TransactionPayload::EntryFunction(encode_create_account_script_function(
StdlibVersion::Latest,
stc::stc_type_tag(),
&account.address,
AuthenticationKey::ed25519(account.public_key()),
init_account_balance as u128,
),
)),
self.net.time_service().now_secs() + j as u64 + 1,
&self.net,
);
Expand Down Expand Up @@ -285,12 +284,12 @@ pub fn run_benchmark(

fn create_transaction(
sequence_number: u64,
program: EntryFunction,
program: TransactionPayload,
expiration_timestamp_secs: u64,
net: &ChainNetwork,
) -> Transaction {
let signed_txn = create_signed_txn_with_association_account(
TransactionPayload::EntryFunction(program),
program,
sequence_number,
40_000_000,
1,
Expand Down
2 changes: 1 addition & 1 deletion executor/tests/block_reward_test.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::Result;
use starcoin_crypto::HashValue;
use starcoin_transaction_builder::empty_txn_payload;
use starcoin_types::block_metadata::BlockMetadata;
use starcoin_vm_types::account_config::BlockRewardEvent;
use test_helper::executor::expect_decode_event;
use test_helper::{
dao::empty_txn_payload,
executor::{
account_execute_with_output, blockmeta_execute, current_block_number, prepare_genesis,
},
Expand Down
6 changes: 1 addition & 5 deletions executor/tests/error_code_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@ pub fn raw_peer_to_peer_txn_with_non_default_gas_token(
RawUserTransaction::new(
sender,
seq_num,
TransactionPayload::EntryFunction(encode_transfer_script_by_token_code(
receiver,
transfer_amount,
G_STC_TOKEN_CODE.clone(),
)),
encode_transfer_script_by_token_code(receiver, transfer_amount, G_STC_TOKEN_CODE.clone()),
max_gas,
gas_price,
expiration_timestamp_secs,
Expand Down
32 changes: 15 additions & 17 deletions executor/tests/executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use starcoin_config::{BuiltinNetworkID, ChainNetwork};
use starcoin_executor::validate_transaction;
use starcoin_logger::prelude::*;
use starcoin_transaction_builder::{
build_batch_script_function_same_amount, build_transfer_txn,
encode_create_account_script_function, raw_peer_to_peer_txn, DEFAULT_EXPIRATION_TIME,
DEFAULT_MAX_GAS_AMOUNT,
build_batch_payload_same_amount, build_transfer_txn, encode_create_account_script_function,
raw_peer_to_peer_txn, DEFAULT_EXPIRATION_TIME, DEFAULT_MAX_GAS_AMOUNT,
};
use starcoin_types::account::peer_to_peer_txn;
use starcoin_types::identifier::Identifier;
Expand Down Expand Up @@ -76,7 +75,8 @@ impl TStateView for NullStateView {
fn test_vm_version() {
let (chain_state, _net) = prepare_genesis();

let version_module_id = ModuleId::new(genesis_address(), Identifier::new("Version").unwrap());
let version_module_id =
ModuleId::new(genesis_address(), Identifier::new("stc_version").unwrap());
let mut value = starcoin_dev::playground::call_contract(
&chain_state,
version_module_id,
Expand All @@ -103,7 +103,7 @@ fn test_flexidag_config_get() {

let version_module_id = ModuleId::new(
genesis_address(),
Identifier::new("FlexiDagConfig").unwrap(),
Identifier::new("flexi_dag_config").unwrap(),
);
let mut value = starcoin_dev::playground::call_contract(
&chain_state,
Expand Down Expand Up @@ -159,7 +159,7 @@ fn test_consensus_config_get() -> Result<()> {

let module_id = ModuleId::new(
genesis_address(),
Identifier::new("ConsensusConfig").unwrap(),
Identifier::new("consensus_config").unwrap(),
);
let mut rets = starcoin_dev::playground::call_contract(
&chain_state,
Expand All @@ -176,6 +176,7 @@ fn test_consensus_config_get() -> Result<()> {
Ok(())
}

// fixme
#[stest::test]
fn test_batch_transfer() -> Result<()> {
let (chain_state, net) = prepare_genesis();
Expand All @@ -188,12 +189,8 @@ fn test_batch_transfer() -> Result<()> {
addresses.push(*account.address());
});

let script_function = build_batch_script_function_same_amount(addresses, 1);
association_execute_should_success(
&net,
&chain_state,
TransactionPayload::EntryFunction(script_function),
)?;
let payload = build_batch_payload_same_amount(addresses, 1);
association_execute_should_success(&net, &chain_state, payload)?;
Ok(())
}

Expand Down Expand Up @@ -230,10 +227,11 @@ fn test_txn_verify_err_case() -> Result<()> {
Ok(())
}

// fixme
#[stest::test(timeout = 360)]
fn test_package_txn() -> Result<()> {
let (chain_state, net) = prepare_genesis();
let alice = test_helper::Account::new();
let alice = Account::new();
let bob = Account::new();
let pre_mint_amount = net.genesis_config().pre_mine_amount;

Expand Down Expand Up @@ -341,8 +339,8 @@ fn test_package_txn() -> Result<()> {
#[stest::test(timeout = 360)]
fn test_wrong_package_address() -> Result<()> {
let (chain_state, net) = prepare_genesis();
let alice = test_helper::Account::new();
let bob = test_helper::Account::new();
let alice = Account::new();
let bob = Account::new();
let pre_mint_amount = net.genesis_config().pre_mine_amount;

// create alice, bob accounts
Expand Down Expand Up @@ -582,7 +580,7 @@ fn test_validate_txn_args() -> Result<()> {
let action = EntryFunction::new(
ModuleId::new(
core_code_address(),
Identifier::new("TransferScript").unwrap(),
Identifier::new("transfer_scripts").unwrap(),
),
Identifier::new("peer_to_peer").unwrap(),
vec![stc_type_tag()],
Expand All @@ -603,7 +601,7 @@ fn test_validate_txn_args() -> Result<()> {
let action = EntryFunction::new(
ModuleId::new(
core_code_address(),
Identifier::new("TransferScripts").unwrap(),
Identifier::new("transfer_scripts").unwrap(),
),
Identifier::new("peer_to_peer_v2").unwrap(),
vec![stc_type_tag()],
Expand Down
Binary file modified genesis/generated/barnard/genesis
Binary file not shown.
Binary file modified genesis/generated/halley/genesis
Binary file not shown.
Binary file modified genesis/generated/main/genesis
Binary file not shown.
Binary file modified genesis/generated/proxima/genesis
Binary file not shown.
Binary file modified genesis/generated/vega/genesis
Binary file not shown.
5 changes: 0 additions & 5 deletions test-helper/src/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use starcoin_executor::execute_readonly_function;
use starcoin_logger::prelude::*;
use starcoin_state_api::ChainStateReader;
use starcoin_statedb::ChainStateDB;
use starcoin_transaction_builder::build_empty_script;
use starcoin_transaction_builder::encode_create_account_script_function;
use starcoin_types::account_address::AccountAddress;
use starcoin_types::account_config::{association_address, genesis_address, stc_type_tag};
Expand Down Expand Up @@ -439,10 +438,6 @@ pub fn execute_script_on_chain_config(
)
}

pub fn empty_txn_payload() -> TransactionPayload {
TransactionPayload::EntryFunction(build_empty_script())
}

pub fn dao_vote_test(
alice: &Account,
chain_state: &ChainStateDB,
Expand Down
8 changes: 5 additions & 3 deletions test-helper/src/txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,16 @@ pub fn create_account_txn_sent_as_association(
) -> SignedUserTransaction {
let args = vec![
bcs_ext::to_bytes(new_account.address()).unwrap(),
bcs_ext::to_bytes(&new_account.auth_key().to_vec()).unwrap(),
bcs_ext::to_bytes(&initial_amount).unwrap(),
];

create_signed_txn_with_association_account(
TransactionPayload::EntryFunction(EntryFunction::new(
ModuleId::new(core_code_address(), Identifier::new("Account").unwrap()),
Identifier::new("create_account_with_initial_amount").unwrap(),
ModuleId::new(
core_code_address(),
Identifier::new("transfer_scripts").unwrap(),
),
Identifier::new("peer_to_peer_v2").unwrap(),
vec![stc_type_tag()],
args,
)),
Expand Down
6 changes: 2 additions & 4 deletions txpool/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use starcoin_txpool_api::{TxPoolSyncService, TxnStatusFullEvent};
use starcoin_types::{
account_address::{self, AccountAddress},
account_config,
transaction::{SignedUserTransaction, Transaction, TransactionPayload},
transaction::{SignedUserTransaction, Transaction},
U256,
};
use std::time::Duration;
Expand Down Expand Up @@ -299,9 +299,7 @@ fn generate_txn(config: Arc<NodeConfig>, seq: u64) -> SignedUserTransaction {
let (_private_key, public_key) = KeyGen::from_os_rng().generate_keypair();
let account_address = account_address::from_public_key(&public_key);
let txn = starcoin_transaction_builder::create_signed_txn_with_association_account(
TransactionPayload::EntryFunction(
starcoin_transaction_builder::encode_transfer_script_function(account_address, 10000),
),
starcoin_transaction_builder::encode_transfer_script_function(account_address, 10000),
seq,
starcoin_transaction_builder::DEFAULT_MAX_GAS_AMOUNT,
1,
Expand Down
Loading

0 comments on commit 9451893

Please sign in to comment.