Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli] dry run at cli side and print warn log when transaction execute failed #3597

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions cmd/starcoin/src/cli_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ use bcs_ext::BCSCodec;
use starcoin_abi_decoder::{decode_txn_payload, DecodedTransactionPayload};
use starcoin_account_api::{AccountInfo, AccountProvider};
use starcoin_config::{ChainNetworkID, DataDirPath};
use starcoin_dev::playground;
use starcoin_node::NodeHandle;
use starcoin_rpc_api::chain::GetEventOption;
use starcoin_rpc_api::types::{
RawUserTransactionView, SignedUserTransactionView, TransactionStatusView,
DryRunOutputView, RawUserTransactionView, SignedUserTransactionView, TransactionStatusView,
};
use starcoin_rpc_client::{RpcClient, StateRootOption};
use starcoin_state_api::StateReaderExt;
Expand Down Expand Up @@ -258,6 +259,11 @@ impl CliState {
))
}

pub fn dry_run_transaction(&self, txn: DryRunTransaction) -> Result<DryRunOutputView> {
let state_reader = self.client().state_reader(StateRootOption::Latest)?;
playground::dry_run_explain(&state_reader, txn, None)
}

pub fn execute_transaction(
&self,
raw_txn: RawUserTransaction,
Expand All @@ -266,7 +272,7 @@ impl CliState {
) -> Result<ExecuteResultView> {
let sender = self.get_account(raw_txn.sender())?;
let public_key = sender.public_key;
let dry_output = self.client.dry_run_raw(DryRunTransaction {
let dry_output = self.dry_run_transaction(DryRunTransaction {
public_key: public_key.clone(),
raw_txn: raw_txn.clone(),
})?;
Expand All @@ -281,7 +287,7 @@ impl CliState {
TransactionStatusView::Executed
)
{
eprintln!("txn dry run failed");
eprintln!("txn dry run result:");
return Ok(execute_result);
}

Expand Down
28 changes: 15 additions & 13 deletions vm/dev/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
[package]
name = "starcoin-dev"
version = "1.11.11"
authors = ["Starcoin Core Dev <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
name = "starcoin-dev"
publish = false
edition = "2021"
version = "1.11.11"

[dependencies]
anyhow = "1.0.41"
thiserror = "1.0"
starcoin-crypto = { git = "https://github.com/starcoinorg/starcoin-crypto", rev = "d871dfb4216f034ee334a575926c101574d9d6dc"}
starcoin-vm-types = { path = "../types" }
starcoin-vm-runtime = { path = "../vm-runtime"}
starcoin-logger = {path = "../../commons/logger"}
starcoin-state-api = { path = "../../state/api"}
starcoin-statedb = { path = "../../state/statedb"}
starcoin-resource-viewer = {path = "../resource-viewer"}
starcoin-abi-resolver = {path = "../../abi/resolver"}
bcs-ext = {path = "../../commons/bcs_ext"}
starcoin-abi-decoder = {path = "../../abi/decoder"}
starcoin-abi-resolver = {path = "../../abi/resolver"}
starcoin-abi-types = {path = "../../abi/types"}
bcs-ext = {path = "../../commons/bcs_ext" }
starcoin-crypto = {git = "https://github.com/starcoinorg/starcoin-crypto", rev = "d871dfb4216f034ee334a575926c101574d9d6dc"}
starcoin-logger = {path = "../../commons/logger"}
starcoin-resource-viewer = {path = "../resource-viewer"}
starcoin-rpc-api = {path = "../../rpc/api"}
starcoin-state-api = {path = "../../state/api"}
starcoin-statedb = {path = "../../state/statedb"}
starcoin-vm-runtime = {path = "../vm-runtime"}
starcoin-vm-types = {path = "../types"}
thiserror = "1.0"
vm-status-translator = {path = "../vm-status-translator"}
58 changes: 56 additions & 2 deletions vm/dev/src/playground.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use anyhow::{format_err, Result};
use starcoin_abi_decoder::decode_move_value;
use starcoin_abi_resolver::ABIResolver;
use starcoin_abi_types::TypeInstantiation;
use starcoin_crypto::HashValue;
use starcoin_resource_viewer::module_cache::ModuleCache;
use starcoin_resource_viewer::{AnnotatedMoveStruct, AnnotatedMoveValue, MoveValueAnnotator};
use starcoin_rpc_api::types::{DryRunOutputView, TransactionOutputView, WriteOpValueView};
use starcoin_state_api::StateNodeStore;
use starcoin_statedb::ChainStateDB;
use starcoin_vm_runtime::metrics::VMMetrics;
use starcoin_vm_runtime::starcoin_vm::StarcoinVM;
use starcoin_vm_types::file_format::CompiledModule;
use starcoin_vm_types::identifier::{IdentStr, Identifier};
use starcoin_vm_types::language_storage::{ModuleId, StructTag, TypeTag};
use starcoin_vm_types::state_view::StateView;
use starcoin_vm_types::transaction::{DryRunTransaction, TransactionOutput};
use starcoin_vm_types::transaction::{DryRunTransaction, TransactionOutput, TransactionPayload};
use starcoin_vm_types::transaction_argument::convert_txn_args;
use starcoin_vm_types::transaction_argument::TransactionArgument;
use starcoin_vm_types::vm_status::VMStatus;
Expand Down Expand Up @@ -96,6 +100,56 @@ pub fn dry_run<S: StateView>(
vm.dry_run_transaction(state_view, txn)
}

pub fn dry_run_explain<S: StateView>(
state_view: &S,
txn: DryRunTransaction,
metrics: Option<VMMetrics>,
) -> anyhow::Result<DryRunOutputView> {
let (vm_status, output) = dry_run(state_view, txn.clone(), metrics)?;
let vm_status_explain = vm_status_translator::explain_vm_status(state_view, vm_status)?;
let mut txn_output: TransactionOutputView = output.into();

let resolver = {
let module_cache = ModuleCache::new();
// If the txn is package txn, we need to use modules in the package to resolve transaction output.
if let TransactionPayload::Package(p) = txn.raw_txn.into_payload() {
let modules = p
.modules()
.iter()
.map(|m| CompiledModule::deserialize(m.code()))
.collect::<Result<Vec<_>, _>>()?;
for m in modules {
module_cache.insert(m.self_id(), m);
}
}
ABIResolver::new_with_module_cache(state_view, module_cache)
};
for action in txn_output.write_set.iter_mut() {
let access_path = action.access_path.clone();
if let Some(value) = &mut action.value {
match value {
WriteOpValueView::Code(view) => {
view.abi = Some(resolver.resolve_module_code(view.code.0.as_slice())?);
}
WriteOpValueView::Resource(view) => {
let struct_tag = access_path.path.as_struct_tag().ok_or_else(|| {
format_err!("invalid resource access path: {}", access_path)
})?;
let struct_abi = resolver.resolve_struct_tag(struct_tag)?;
view.json = Some(decode_move_value(
&TypeInstantiation::Struct(Box::new(struct_abi)),
view.raw.0.as_slice(),
)?)
}
}
}
}
Ok(DryRunOutputView {
explained_status: vm_status_explain,
txn_output,
})
}

pub fn call_contract<S: StateView>(
state_view: &S,
module_id: ModuleId,
Expand Down
11 changes: 9 additions & 2 deletions vm/vm-runtime/src/starcoin_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,10 @@ impl StarcoinVM {
only_new_module,
},
)
.map_err(|e| e.into_vm_status())?;
.map_err(|e| {
warn!("[VM] execute_package error, status_type: {:?}, status_code:{:?}, message:{:?}, location:{:?}", e.status_type(), e.major_status(), e.message(), e.location());
e.into_vm_status()
})?;

// after publish the modules, we need to clear loader cache, to make init script function and
// epilogue use the new modules.
Expand Down Expand Up @@ -599,7 +602,11 @@ impl StarcoinVM {
return Err(VMStatus::Error(StatusCode::UNREACHABLE));
}
}
.map_err(|e| e.into_vm_status())?;
.map_err(|e|
{
warn!("[VM] execute_script_function error, status_type: {:?}, status_code:{:?}, message:{:?}, location:{:?}", e.status_type(), e.major_status(), e.message(), e.location());
e.into_vm_status()
})?;

charge_global_write_gas_usage(cost_strategy, &session, &txn_data.sender())?;

Expand Down