Skip to content

Commit

Permalink
fix: avoid attaching values to precompiles (#714)
Browse files Browse the repository at this point in the history
* fix: avoid attaching values to precompiles

* fix: use the correct types

* fix: lint errors

* fix: more clippy errors

* fix: more clippy errors
  • Loading branch information
0x3bfc authored and birchmd committed Apr 5, 2023
1 parent 2f54baf commit 7204ddd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
8 changes: 5 additions & 3 deletions engine-precompiles/src/account_ids.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{EvmPrecompileResult, Precompile};
use crate::prelude::types::{Address, EthGas};
use crate::PrecompileOutput;
use crate::{utils, PrecompileOutput};
use aurora_engine_sdk::env::Env;
use aurora_engine_types::account_id::AccountId;
use evm::{Context, ExitError};
Expand Down Expand Up @@ -45,9 +45,10 @@ impl<'a, E: Env> Precompile for PredecessorAccount<'a, E> {
&self,
input: &[u8],
target_gas: Option<EthGas>,
_context: &Context,
context: &Context,
_is_static: bool,
) -> EvmPrecompileResult {
utils::validate_no_value_attached_to_precompile(context.apparent_value)?;
let cost = Self::required_gas(input)?;
if let Some(target_gas) = target_gas {
if cost > target_gas {
Expand Down Expand Up @@ -90,9 +91,10 @@ impl Precompile for CurrentAccount {
&self,
input: &[u8],
target_gas: Option<EthGas>,
_context: &Context,
context: &Context,
_is_static: bool,
) -> EvmPrecompileResult {
utils::validate_no_value_attached_to_precompile(context.apparent_value)?;
let cost = Self::required_gas(input)?;
if let Some(target_gas) = target_gas {
if cost > target_gas {
Expand Down
5 changes: 3 additions & 2 deletions engine-precompiles/src/prepaid_gas.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{EvmPrecompileResult, Precompile};
use crate::prelude::types::{Address, EthGas};
use crate::PrecompileOutput;
use crate::{utils, PrecompileOutput};
use aurora_engine_sdk::env::Env;
use aurora_engine_types::{vec, U256};
use evm::{Context, ExitError};
Expand Down Expand Up @@ -37,9 +37,10 @@ impl<'a, E: Env> Precompile for PrepaidGas<'a, E> {
&self,
input: &[u8],
target_gas: Option<EthGas>,
_context: &Context,
context: &Context,
_is_static: bool,
) -> EvmPrecompileResult {
utils::validate_no_value_attached_to_precompile(context.apparent_value)?;
let cost = Self::required_gas(input)?;
if let Some(target_gas) = target_gas {
if cost > target_gas {
Expand Down
5 changes: 3 additions & 2 deletions engine-precompiles/src/promise_result.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{EvmPrecompileResult, Precompile};
use crate::prelude::types::{Address, EthGas};
use crate::PrecompileOutput;
use crate::{utils, PrecompileOutput};
use aurora_engine_sdk::promise::ReadOnlyPromiseHandler;
use aurora_engine_types::{Cow, Vec};
use borsh::BorshSerialize;
Expand Down Expand Up @@ -42,9 +42,10 @@ impl<H: ReadOnlyPromiseHandler> Precompile for PromiseResult<H> {
&self,
input: &[u8],
target_gas: Option<EthGas>,
_context: &Context,
context: &Context,
_is_static: bool,
) -> EvmPrecompileResult {
utils::validate_no_value_attached_to_precompile(context.apparent_value)?;
let mut cost = Self::required_gas(input)?;
let check_cost = |cost: EthGas| -> Result<(), ExitError> {
if let Some(target_gas) = target_gas {
Expand Down
5 changes: 3 additions & 2 deletions engine-precompiles/src/random.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{EvmPrecompileResult, Precompile};
use crate::prelude::types::{Address, EthGas};
use crate::prelude::H256;
use crate::PrecompileOutput;
use crate::{utils, PrecompileOutput};
use evm::{Context, ExitError};

mod costs {
Expand Down Expand Up @@ -40,9 +40,10 @@ impl Precompile for RandomSeed {
&self,
input: &[u8],
target_gas: Option<EthGas>,
_context: &Context,
context: &Context,
_is_static: bool,
) -> EvmPrecompileResult {
utils::validate_no_value_attached_to_precompile(context.apparent_value)?;
let cost = Self::required_gas(input)?;
if let Some(target_gas) = target_gas {
if cost > target_gas {
Expand Down
12 changes: 11 additions & 1 deletion engine-precompiles/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::prelude::Borrowed;
use aurora_engine_types::U256;
use core::num::TryFromIntError;

#[cfg(test)]
use evm::Context;
use evm::ExitError;

#[cfg(test)]
pub fn new_context() -> Context {
use aurora_engine_types::{H160, U256};
use aurora_engine_types::H160;

Context {
address: H160::default(),
Expand All @@ -18,3 +20,11 @@ pub fn new_context() -> Context {
pub const fn err_usize_conv(_e: TryFromIntError) -> ExitError {
ExitError::Other(Borrowed("ERR_USIZE_CONVERSION"))
}

pub fn validate_no_value_attached_to_precompile(value: U256) -> Result<(), ExitError> {
if value > U256::zero() {
// don't attach native token value to that precompile
return Err(ExitError::Other(Borrowed("ATTACHED_VALUE_ERROR")));
}
Ok(())
}
3 changes: 2 additions & 1 deletion engine-precompiles/src/xcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Allow Aurora users interacting with NEAR smart contracts using cross contract call primitives.
//! TODO: How they work (low level explanation with examples)
use crate::{HandleBasedPrecompile, PrecompileOutput};
use crate::{utils, HandleBasedPrecompile, PrecompileOutput};
use aurora_engine_sdk::io::IO;
use aurora_engine_types::{
account_id::AccountId,
Expand Down Expand Up @@ -100,6 +100,7 @@ impl<I: IO> HandleBasedPrecompile for CrossContractCall<I> {
let input = handle.input();
let target_gas = handle.gas_limit().map(EthGas::new);
let context = handle.context();
utils::validate_no_value_attached_to_precompile(context.apparent_value)?;
let is_static = handle.is_static();

// This only includes the cost we can easily derive without parsing the input.
Expand Down

0 comments on commit 7204ddd

Please sign in to comment.