Skip to content

Commit

Permalink
fix some executor tests
Browse files Browse the repository at this point in the history
* fix executor tests: vm_version, flexidagconfig
* refactor helper functions with auto-generated sdk
* create a account by p2p transfer
* fix building errors
  • Loading branch information
simonjiao committed Nov 27, 2024
1 parent 123945b commit 9c9737a
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 117 deletions.
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
7 changes: 4 additions & 3 deletions executor/tests/executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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 +104,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 +160,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 Down
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
1 change: 0 additions & 1 deletion vm/framework/starcoin-framework/doc/dao.md
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,6 @@ extract proposal action to execute.
proposer_address: <b>address</b>,
proposal_id: u64,
): ActionT <b>acquires</b> <a href="dao.md#0x1_dao_Proposal">Proposal</a> {
<a href="../../starcoin-stdlib/doc/debug.md#0x1_debug_print">debug::print</a>(&std::string::utf8(b"<a href="dao.md#0x1_dao_extract_proposal_action">dao::extract_proposal_action</a> | Entered"));
// Only executable proposal's action can be extracted.
<b>assert</b>!(
<a href="dao.md#0x1_dao_proposal_state">proposal_state</a>&lt;TokenT, ActionT&gt;(proposer_address, proposal_id) == <a href="dao.md#0x1_dao_EXECUTABLE">EXECUTABLE</a>,
Expand Down
114 changes: 33 additions & 81 deletions vm/transaction-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use starcoin_cached_packages::starcoin_framework_sdk_builder::empty_scripts_empty_script;
use starcoin_cached_packages::starcoin_stdlib::{
on_chain_config_scripts_propose_update_vm_config, transfer_scripts_batch_peer_to_peer_v2,
transfer_scripts_peer_to_peer, transfer_scripts_peer_to_peer_v2,
};
use starcoin_config::{genesis_config::G_TOTAL_STC_AMOUNT, ChainNetwork};
use starcoin_crypto::hash::PlainCryptoHash;
use starcoin_crypto::HashValue;
Expand Down Expand Up @@ -71,32 +76,19 @@ pub fn build_transfer_txn(
)
}

pub fn build_batch_script_function(
pub fn build_batch_payload(
receivers: Vec<AccountAddress>,
amounts: Vec<u128>,
) -> EntryFunction {
let addresses = MoveValue::vector_address(receivers);
let amounts = MoveValue::Vector(amounts.into_iter().map(MoveValue::U128).collect());
EntryFunction::new(
ModuleId::new(
core_code_address(),
Identifier::new("transfer_scripts").unwrap(),
),
Identifier::new("batch_peer_to_peer_v2").unwrap(),
vec![stc_type_tag()],
vec![
bcs_ext::to_bytes(&addresses).unwrap(),
bcs_ext::to_bytes(&amounts).unwrap(),
],
)
) -> TransactionPayload {
transfer_scripts_batch_peer_to_peer_v2(stc_type_tag(), receivers, amounts)
}

pub fn build_batch_script_function_same_amount(
pub fn build_batch_payload_same_amount(
receivers: Vec<AccountAddress>,
amount: u128,
) -> EntryFunction {
) -> TransactionPayload {
let len = receivers.len();
build_batch_script_function(receivers, (0..len).map(|_| amount).collect())
build_batch_payload(receivers, (0..len).map(|_| amount).collect())
}

pub fn build_batch_transfer_txn(
Expand All @@ -109,9 +101,7 @@ pub fn build_batch_transfer_txn(
expiration_timestamp_secs: u64,
chain_id: ChainId,
) -> RawUserTransaction {
let payload = TransactionPayload::EntryFunction(build_batch_script_function_same_amount(
receivers, amount,
));
let payload = build_batch_payload_same_amount(receivers, amount);

RawUserTransaction::new_with_default_gas_token(
sender,
Expand Down Expand Up @@ -179,14 +169,11 @@ pub fn raw_peer_to_peer_txn(
expiration_timestamp_secs: u64,
chain_id: ChainId,
) -> RawUserTransaction {
let token_type_tag = TypeTag::Struct(Box::new(token_code.try_into().unwrap()));
RawUserTransaction::new_with_default_gas_token(
sender,
seq_num,
TransactionPayload::EntryFunction(encode_transfer_script_by_token_code(
receiver,
transfer_amount,
token_code,
)),
transfer_scripts_peer_to_peer(token_type_tag, receiver, vec![], transfer_amount),
max_gas,
gas_price,
expiration_timestamp_secs,
Expand Down Expand Up @@ -240,27 +227,20 @@ pub fn encode_create_account_script_function(
)
}

pub fn encode_transfer_script_function(recipient: AccountAddress, amount: u128) -> EntryFunction {
pub fn encode_transfer_script_function(
recipient: AccountAddress,
amount: u128,
) -> TransactionPayload {
encode_transfer_script_by_token_code(recipient, amount, G_STC_TOKEN_CODE.clone())
}

pub fn encode_transfer_script_by_token_code(
recipient: AccountAddress,
amount: u128,
token_code: TokenCode,
) -> EntryFunction {
EntryFunction::new(
ModuleId::new(
core_code_address(),
Identifier::new("transfer_scripts").unwrap(),
),
Identifier::new("peer_to_peer_v2").unwrap(),
vec![TypeTag::Struct(Box::new(token_code.try_into().unwrap()))],
vec![
bcs_ext::to_bytes(&recipient).unwrap(),
bcs_ext::to_bytes(&amount).unwrap(),
],
)
) -> TransactionPayload {
let token_type_tag = TypeTag::Struct(Box::new(token_code.try_into().unwrap()));
transfer_scripts_peer_to_peer_v2(token_type_tag, recipient, amount)
}

pub fn encode_nft_transfer_script(uuid: NFTUUID, recipient: AccountAddress) -> EntryFunction {
Expand All @@ -285,8 +265,8 @@ pub fn peer_to_peer_txn_sent_as_association(
expiration_timestamp_secs: u64,
net: &ChainNetwork,
) -> SignedUserTransaction {
crate::create_signed_txn_with_association_account(
TransactionPayload::EntryFunction(encode_transfer_script_function(recipient, amount)),
create_signed_txn_with_association_account(
transfer_scripts_peer_to_peer(stc_type_tag(), recipient, vec![], amount),
seq_num,
DEFAULT_MAX_GAS_AMOUNT,
1,
Expand All @@ -307,18 +287,7 @@ pub fn peer_to_peer_v2(
.sign_txn(RawUserTransaction::new_with_default_gas_token(
*sender.address(),
seq_num,
TransactionPayload::EntryFunction(EntryFunction::new(
ModuleId::new(
core_code_address(),
Identifier::new("transfer_scripts").unwrap(),
),
Identifier::new("peer_to_peer_v2").unwrap(),
vec![stc_type_tag()],
vec![
bcs_ext::to_bytes(&recipient).unwrap(),
bcs_ext::to_bytes(&amount).unwrap(),
],
)),
transfer_scripts_batch_peer_to_peer_v2(stc_type_tag(), vec![*recipient], vec![amount]),
10000000,
1,
1000 + 60 * 60,
Expand Down Expand Up @@ -582,35 +551,18 @@ pub fn build_module_upgrade_queue(
)
}

pub fn build_vm_config_upgrade_proposal(vm_config: VMConfig, exec_delay: u64) -> EntryFunction {
EntryFunction::new(
ModuleId::new(
core_code_address(),
Identifier::new("OnChainConfigScripts").unwrap(),
),
Identifier::new("propose_update_vm_config").unwrap(),
vec![],
vec![
bcs_ext::to_bytes(&bcs_ext::to_bytes(&vm_config.gas_schedule).unwrap()).unwrap(),
bcs_ext::to_bytes(&exec_delay).unwrap(),
],
)
}

pub fn build_empty_script() -> EntryFunction {
EntryFunction::new(
ModuleId::new(
core_code_address(),
Identifier::new("empty_scripts").unwrap(),
),
Identifier::new("empty_script").unwrap(),
vec![],
vec![],
pub fn build_vm_config_upgrade_proposal(
vm_config: VMConfig,
exec_delay: u64,
) -> TransactionPayload {
on_chain_config_scripts_propose_update_vm_config(
bcs_ext::to_bytes(&vm_config.gas_schedule).unwrap(),
exec_delay,
)
}

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

pub fn build_signed_empty_txn(
Expand Down
Loading

0 comments on commit 9c9737a

Please sign in to comment.