Skip to content

Commit

Permalink
[aptos-vm] remove currency code
Browse files Browse the repository at this point in the history
  • Loading branch information
davidiw committed Apr 25, 2022
1 parent 1ef2ee6 commit 9be5e33
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 141 deletions.
112 changes: 17 additions & 95 deletions aptos-move/aptos-vm/src/aptos_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use crate::{
validate_signed_transaction, PreprocessedTransaction, VMAdapter,
},
aptos_vm_impl::{
charge_global_write_gas_usage, get_currency_info, get_gas_currency_code,
get_transaction_output, AptosVMImpl, AptosVMInternals,
charge_global_write_gas_usage, get_transaction_output, AptosVMImpl, AptosVMInternals,
},
counters::*,
data_cache::{RemoteStorage, StateViewCache},
Expand Down Expand Up @@ -44,7 +43,6 @@ use move_binary_format::{errors::VMResult, CompiledModule};
use move_core_types::{
account_address::AccountAddress,
gas_schedule::{GasAlgebra, GasUnits},
identifier::IdentStr,
language_storage::ModuleId,
resolver::MoveResolver,
transaction_argument::convert_txn_args,
Expand Down Expand Up @@ -176,15 +174,13 @@ impl AptosVM {
gas_status: &mut GasStatus,
txn_data: &TransactionMetadata,
storage: &S,
account_currency_symbol: &IdentStr,
log_context: &AdapterLogSchema,
) -> TransactionOutput {
self.failed_transaction_cleanup_and_keep_vm_status(
error_code,
gas_status,
txn_data,
storage,
account_currency_symbol,
log_context,
)
.1
Expand All @@ -196,7 +192,6 @@ impl AptosVM {
gas_status: &mut GasStatus,
txn_data: &TransactionMetadata,
storage: &S,
account_currency_symbol: &IdentStr,
log_context: &AdapterLogSchema,
) -> (VMStatus, TransactionOutput) {
gas_status.set_metering(false);
Expand All @@ -209,13 +204,10 @@ impl AptosVM {
// so even if the previous failure occurred while running the epilogue, it
// should not fail now. If it somehow fails here, there is no choice but to
// discard the transaction.
if let Err(e) = self.0.run_failure_epilogue(
&mut session,
gas_status,
txn_data,
account_currency_symbol,
log_context,
) {
if let Err(e) =
self.0
.run_failure_epilogue(&mut session, gas_status, txn_data, log_context)
{
return discard_error_vm_status(e);
}
let txn_output = get_transaction_output(
Expand All @@ -240,17 +232,11 @@ impl AptosVM {
mut session: SessionExt<S>,
gas_status: &mut GasStatus,
txn_data: &TransactionMetadata,
account_currency_symbol: &IdentStr,
log_context: &AdapterLogSchema,
) -> Result<(VMStatus, TransactionOutput), VMStatus> {
gas_status.set_metering(false);
self.0.run_success_epilogue(
&mut session,
gas_status,
txn_data,
account_currency_symbol,
log_context,
)?;
self.0
.run_success_epilogue(&mut session, gas_status, txn_data, log_context)?;

Ok((
VMStatus::Executed,
Expand All @@ -270,7 +256,6 @@ impl AptosVM {
gas_status: &mut GasStatus,
txn_data: &TransactionMetadata,
payload: &TransactionPayload,
account_currency_symbol: &IdentStr,
log_context: &AdapterLogSchema,
) -> Result<(VMStatus, TransactionOutput), VMStatus> {
fail_point!("move_adapter::execute_script_or_script_function", |_| {
Expand Down Expand Up @@ -346,13 +331,7 @@ impl AptosVM {

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

self.success_transaction_cleanup(
session,
gas_status,
txn_data,
account_currency_symbol,
log_context,
)
self.success_transaction_cleanup(session, gas_status, txn_data, log_context)
}
}

Expand All @@ -362,7 +341,6 @@ impl AptosVM {
gas_status: &mut GasStatus,
txn_data: &TransactionMetadata,
modules: &ModuleBundle,
account_currency_symbol: &IdentStr,
log_context: &AdapterLogSchema,
) -> Result<(VMStatus, TransactionOutput), VMStatus> {
fail_point!("move_adapter::execute_module", |_| {
Expand All @@ -388,13 +366,7 @@ impl AptosVM {

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

self.success_transaction_cleanup(
session,
gas_status,
txn_data,
account_currency_symbol,
log_context,
)
self.success_transaction_cleanup(session, gas_status, txn_data, log_context)
}

pub(crate) fn execute_user_transaction<S: MoveResolver>(
Expand All @@ -411,18 +383,6 @@ impl AptosVM {
}
};
}
let account_currency_symbol = match get_gas_currency_code(txn) {
Ok(symbol) => symbol,
Err(err) => {
return discard_error_vm_status(err);
}
};

if self.0.chain_info().currency_code_required {
if let Err(err) = get_currency_info(&account_currency_symbol, storage) {
return discard_error_vm_status(err);
}
}

// Revalidate the transaction.
let mut session = self.0.new_session(storage, SessionId::txn(txn));
Expand All @@ -448,17 +408,11 @@ impl AptosVM {
&mut gas_status,
&txn_data,
payload,
&account_currency_symbol,
log_context,
),
TransactionPayload::ModuleBundle(m) => self.execute_modules(
session,
&mut gas_status,
&txn_data,
m,
&account_currency_symbol,
log_context,
),
TransactionPayload::ModuleBundle(m) => {
self.execute_modules(session, &mut gas_status, &txn_data, m, log_context)
}
TransactionPayload::WriteSet(_) => {
return discard_error_vm_status(VMStatus::Error(StatusCode::UNREACHABLE));
}
Expand All @@ -482,7 +436,6 @@ impl AptosVM {
&mut gas_status,
&txn_data,
storage,
&account_currency_symbol,
log_context,
)
}
Expand Down Expand Up @@ -652,19 +605,6 @@ impl AptosVM {
))
});

let account_currency_symbol = match get_gas_currency_code(txn) {
Ok(symbol) => symbol,
Err(err) => {
return Ok(discard_error_vm_status(err));
}
};

if self.0.chain_info().currency_code_required {
if let Err(err) = get_currency_info(&account_currency_symbol, storage) {
return Ok(discard_error_vm_status(err));
}
}

// Revalidate the transaction.
let mut session = self.0.new_session(storage, SessionId::txn(txn));
if let Err(e) = validate_signature_checked_transaction::<S, Self>(
Expand Down Expand Up @@ -890,23 +830,9 @@ impl VMAdapter for AptosVM {
fn get_gas_price<S: MoveResolver>(
&self,
txn: &SignedTransaction,
remote_cache: &S,
_remote_cache: &S,
) -> Result<u64, VMStatus> {
let gas_price = txn.gas_unit_price();
let currency_code = get_gas_currency_code(txn)?;

let normalized_gas_price = if self.0.chain_info().currency_code_required {
match get_currency_info(&currency_code, remote_cache) {
Ok(info) => info.convert_to_xdx(gas_price),
Err(err) => {
return Err(err);
}
}
} else {
gas_price
};

Ok(normalized_gas_price)
Ok(txn.gas_unit_price())
}

fn run_prologue<S: MoveResolver>(
Expand All @@ -915,25 +841,21 @@ impl VMAdapter for AptosVM {
transaction: &SignatureCheckedTransaction,
log_context: &AdapterLogSchema,
) -> Result<(), VMStatus> {
let currency_code = get_gas_currency_code(transaction)?;
let txn_data = TransactionMetadata::new(transaction);
//let account_blob = session.data_cache.get_resource
match transaction.payload() {
TransactionPayload::Script(_) => {
self.0.check_gas(&txn_data, log_context)?;
self.0
.run_script_prologue(session, &txn_data, &currency_code, log_context)
self.0.run_script_prologue(session, &txn_data, log_context)
}
TransactionPayload::ScriptFunction(_) => {
// NOTE: Script and ScriptFunction shares the same prologue
self.0.check_gas(&txn_data, log_context)?;
self.0
.run_script_prologue(session, &txn_data, &currency_code, log_context)
self.0.run_script_prologue(session, &txn_data, log_context)
}
TransactionPayload::ModuleBundle(_module) => {
self.0.check_gas(&txn_data, log_context)?;
self.0
.run_module_prologue(session, &txn_data, &currency_code, log_context)
self.0.run_module_prologue(session, &txn_data, log_context)
}
TransactionPayload::WriteSet(_cs) => {
self.0
Expand Down
53 changes: 7 additions & 46 deletions aptos-move/aptos-vm/src/aptos_vm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use aptos_logger::prelude::*;
use aptos_state_view::StateView;
use aptos_types::{
account_config,
account_config::{ChainSpecificAccountInfo, CurrencyInfoResource},
account_config::ChainSpecificAccountInfo,
contract_event::ContractEvent,
event::EventKey,
on_chain_config::{
ConfigStorage, OnChainConfig, VMConfig, VMPublishingOption, Version, APTOS_VERSION_3,
},
state_store::state_key::StateKey,
transaction::{SignedTransaction, TransactionOutput, TransactionStatus},
transaction::{TransactionOutput, TransactionStatus},
vm_status::{KeptVMStatus, StatusCode, VMStatus},
write_set::{WriteOp, WriteSet, WriteSetMut},
};
Expand All @@ -35,8 +35,7 @@ use move_core_types::{
account_address::AccountAddress,
effects::{ChangeSet as MoveChangeSet, Event as MoveEvent},
gas_schedule::{CostTable, GasAlgebra, GasCarrier, GasUnits, InternalGasUnits},
identifier::{IdentStr, Identifier},
language_storage::{ModuleId, TypeTag},
language_storage::ModuleId,
move_resource::MoveStructType,
resolver::{MoveResolver, ResourceResolver},
value::{serialize_values, MoveValue},
Expand Down Expand Up @@ -237,27 +236,16 @@ impl AptosVMImpl {
Ok(())
}

fn get_gas_currency_args(&self, account_currency_symbol: &IdentStr) -> Vec<TypeTag> {
if self.chain_info().currency_code_required {
vec![account_config::type_tag_for_currency_code(
account_currency_symbol.to_owned(),
)]
} else {
vec![]
}
}

/// Run the prologue of a transaction by calling into either `SCRIPT_PROLOGUE_NAME` function
/// or `MULTI_AGENT_SCRIPT_PROLOGUE_NAME` function stored in the `ACCOUNT_MODULE` on chain.
pub(crate) fn run_script_prologue<S: MoveResolver>(
&self,
session: &mut SessionExt<S>,
txn_data: &TransactionMetadata,
account_currency_symbol: &IdentStr,
log_context: &AdapterLogSchema,
) -> Result<(), VMStatus> {
let chain_specific_info = self.chain_info();
let gas_currency = self.get_gas_currency_args(account_currency_symbol);
let gas_currency = vec![];
let txn_sequence_number = txn_data.sequence_number();
let txn_public_key = txn_data.authentication_key_preimage().to_vec();
let txn_gas_price = txn_data.gas_unit_price().get();
Expand Down Expand Up @@ -319,11 +307,10 @@ impl AptosVMImpl {
&self,
session: &mut SessionExt<S>,
txn_data: &TransactionMetadata,
account_currency_symbol: &IdentStr,
log_context: &AdapterLogSchema,
) -> Result<(), VMStatus> {
let chain_specific_info = self.chain_info();
let gas_currency = self.get_gas_currency_args(account_currency_symbol);
let gas_currency = vec![];
let txn_sequence_number = txn_data.sequence_number();
let txn_public_key = txn_data.authentication_key_preimage().to_vec();
let txn_gas_price = txn_data.gas_unit_price().get();
Expand Down Expand Up @@ -359,7 +346,6 @@ impl AptosVMImpl {
session: &mut SessionExt<S>,
gas_status: &mut GasStatus,
txn_data: &TransactionMetadata,
account_currency_symbol: &IdentStr,
log_context: &AdapterLogSchema,
) -> Result<(), VMStatus> {
fail_point!("move_adapter::run_success_epilogue", |_| {
Expand All @@ -368,7 +354,7 @@ impl AptosVMImpl {
))
});

let gas_currency = self.get_gas_currency_args(account_currency_symbol);
let gas_currency = vec![];
let chain_specific_info = self.chain_info();
let txn_sequence_number = txn_data.sequence_number();
let txn_gas_price = txn_data.gas_unit_price().get();
Expand Down Expand Up @@ -400,10 +386,9 @@ impl AptosVMImpl {
session: &mut SessionExt<S>,
gas_status: &mut GasStatus,
txn_data: &TransactionMetadata,
account_currency_symbol: &IdentStr,
log_context: &AdapterLogSchema,
) -> Result<(), VMStatus> {
let gas_currency = self.get_gas_currency_args(account_currency_symbol);
let gas_currency = vec![];
let chain_specific_info = self.chain_info();
let txn_sequence_number = txn_data.sequence_number();
let txn_gas_price = txn_data.gas_unit_price().get();
Expand Down Expand Up @@ -650,30 +635,6 @@ pub(crate) fn get_transaction_output<A: AccessPathCache, S: MoveResolver>(
))
}

pub(crate) fn get_gas_currency_code(txn: &SignedTransaction) -> Result<Identifier, VMStatus> {
let currency_code_string = txn.gas_currency_code();
match account_config::from_currency_code_string(currency_code_string) {
Ok(code) => Ok(code),
Err(_) => Err(VMStatus::Error(StatusCode::INVALID_GAS_SPECIFIER)),
}
}

pub(crate) fn get_currency_info<S: MoveResolver>(
currency_code: &IdentStr,
remote_cache: &S,
) -> Result<CurrencyInfoResource, VMStatus> {
if let Ok(Some(blob)) = remote_cache.get_resource(
&account_config::aptos_root_address(),
&CurrencyInfoResource::struct_tag_for(currency_code.to_owned()),
) {
let x = bcs::from_bytes::<CurrencyInfoResource>(&blob)
.map_err(|_| VMStatus::Error(StatusCode::CURRENCY_INFO_DOES_NOT_EXIST))?;
Ok(x)
} else {
Err(VMStatus::Error(StatusCode::CURRENCY_INFO_DOES_NOT_EXIST))
}
}

#[test]
fn vm_thread_safe() {
fn assert_send<T: Send>() {}
Expand Down

0 comments on commit 9be5e33

Please sign in to comment.