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

Increase NEAR Gas for ft_on_transfer #389

Merged
merged 15 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 8 additions & 1 deletion engine-sdk/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::{OneYoctoAttachError, PrivateCallError};
use crate::prelude::H256;
use crate::prelude::{NearGas, H256};
use aurora_engine_types::account_id::AccountId;

/// Timestamp represented by the number of nanoseconds since the Unix Epoch.
Expand Down Expand Up @@ -43,6 +43,8 @@ pub trait Env {
fn attached_deposit(&self) -> u128;
/// Random seed generated for the current block
fn random_seed(&self) -> H256;
/// Prepaid NEAR Gas
fn prepaid_gas(&self) -> NearGas;

fn assert_private_call(&self) -> Result<(), PrivateCallError> {
if self.predecessor_account_id() == self.current_account_id() {
Expand Down Expand Up @@ -72,6 +74,7 @@ pub struct Fixed {
pub block_timestamp: Timestamp,
pub attached_deposit: u128,
pub random_seed: H256,
pub prepaid_gas: NearGas,
}

impl Env for Fixed {
Expand Down Expand Up @@ -102,4 +105,8 @@ impl Env for Fixed {
fn random_seed(&self) -> H256 {
self.random_seed
}

fn prepaid_gas(&self) -> NearGas {
self.prepaid_gas
}
}
6 changes: 5 additions & 1 deletion engine-sdk/src/near_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ impl crate::env::Env for Runtime {
bytes
}
}

fn prepaid_gas(&self) -> NearGas {
NearGas::new(unsafe { exports::prepaid_gas() })
}
}

impl crate::promise::PromiseHandler for Runtime {
Expand Down Expand Up @@ -388,7 +392,7 @@ pub(crate) mod exports {
fn account_balance(balance_ptr: u64);
pub(crate) fn attached_deposit(balance_ptr: u64);
pub(crate) fn prepaid_gas() -> u64;
fn used_gas() -> u64;
pub(crate) fn used_gas() -> u64;
mrLSD marked this conversation as resolved.
Show resolved Hide resolved
// ############
// # Math API #
// ############
Expand Down
2 changes: 2 additions & 0 deletions engine-standalone-storage/src/relayer_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use aurora_engine::engine;
use aurora_engine::transaction::EthTransactionKind;
use aurora_engine_sdk::env::{self, Env};
use aurora_engine_types::account_id::AccountId;
use aurora_engine_types::types::NearGas;
use aurora_engine_types::H256;
use postgres::fallible_iterator::FallibleIterator;

Expand Down Expand Up @@ -81,6 +82,7 @@ where
block_timestamp: env::Timestamp::new(0),
attached_deposit: 0,
random_seed: H256::zero(),
prepaid_gas: NearGas::new(0),
mrLSD marked this conversation as resolved.
Show resolved Hide resolved
};
let mut handler = crate::promise::Noop;

Expand Down
4 changes: 4 additions & 0 deletions engine-standalone-storage/src/sync/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use aurora_engine::{connector, engine, parameters};
use aurora_engine_sdk::env::{self, Env};
use aurora_engine_types::types::NearGas;
use aurora_engine_types::TryFrom;
use borsh::BorshDeserialize;

Expand Down Expand Up @@ -46,6 +47,8 @@ pub fn consume_message(storage: &mut crate::Storage, message: Message) -> Result
block_timestamp: block_metadata.timestamp,
attached_deposit: transaction_message.attached_near,
random_seed: block_metadata.random_seed,
// TODO: fix it?
mrLSD marked this conversation as resolved.
Show resolved Hide resolved
prepaid_gas: NearGas::new(300_000_000_000_000),
};
let io =
storage.access_engine_storage_at_position(block_height, transaction_position, &[]);
Expand Down Expand Up @@ -143,6 +146,7 @@ pub fn consume_message(storage: &mut crate::Storage, message: Message) -> Result
env.predecessor_account_id(),
env.current_account_id(),
finish_args,
env.prepaid_gas,
)?;

if let Some(promise_args) = maybe_promise_args {
Expand Down
4 changes: 4 additions & 0 deletions engine-tests/src/test_utils/standalone/mocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use aurora_engine::fungible_token::FungibleTokenMetadata;
use aurora_engine::parameters::{FinishDepositCallArgs, InitCallArgs, NewCallArgs};
use aurora_engine_sdk::env::Env;
use aurora_engine_sdk::io::IO;
use aurora_engine_types::types::NearGas;
use aurora_engine_types::{account_id::AccountId, types::Wei, Address, H256, U256};
use engine_standalone_storage::{BlockMetadata, Storage};
use near_sdk_sim::DEFAULT_GAS;

use crate::test_utils;

Expand Down Expand Up @@ -44,6 +46,7 @@ pub fn default_env(block_height: u64) -> aurora_engine_sdk::env::Fixed {
block_timestamp: aurora_engine_sdk::env::Timestamp::new(0),
attached_deposit: 0,
random_seed: H256::zero(),
prepaid_gas: NearGas::new(0),
mrLSD marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -118,6 +121,7 @@ pub fn mint_evm_account<I: IO + Copy, E: Env>(
aurora_account_id.clone(),
aurora_account_id.clone(),
deposit_args,
NearGas::new(DEFAULT_GAS),
)
.map_err(unsafe_to_string)
.unwrap();
Expand Down
2 changes: 2 additions & 0 deletions engine-tests/src/test_utils/standalone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use aurora_engine::engine;
use aurora_engine::parameters::{CallArgs, DeployErc20TokenArgs, SubmitResult, TransactionStatus};
use aurora_engine::transaction::legacy::{LegacyEthSignedTransaction, TransactionLegacy};
use aurora_engine_sdk::env::{self, Env};
use aurora_engine_types::types::NearGas;
use aurora_engine_types::{types::Wei, Address, H256, U256};
use borsh::BorshDeserialize;
use engine_standalone_storage::engine_state;
Expand Down Expand Up @@ -125,6 +126,7 @@ impl StandaloneRunner {
env.predecessor_account_id = ctx.predecessor_account_id.as_ref().parse().unwrap();
env.current_account_id = ctx.current_account_id.as_ref().parse().unwrap();
env.signer_account_id = ctx.signer_account_id.as_ref().parse().unwrap();
env.prepaid_gas = NearGas::new(ctx.prepaid_gas);

let storage = &mut self.storage;
if method_name == test_utils::SUBMIT {
Expand Down
3 changes: 2 additions & 1 deletion engine-tests/src/tests/standalone/sanity.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::test_utils::standalone::mocks::{promise, storage};
use aurora_engine::engine;
use aurora_engine_types::types::Wei;
use aurora_engine_types::types::{NearGas, Wei};
use aurora_engine_types::{account_id::AccountId, Address, H256, U256};
use std::sync::RwLock;

Expand Down Expand Up @@ -30,6 +30,7 @@ fn test_deploy_code() {
block_timestamp: aurora_engine_sdk::env::Timestamp::new(0),
attached_deposit: 0,
random_seed: H256::zero(),
prepaid_gas: NearGas::new(0),
mrLSD marked this conversation as resolved.
Show resolved Hide resolved
};
let mut handler = promise::PromiseTracker::default();
let mut engine = engine::Engine::new_with_state(state, origin, owner_id, io, &env);
Expand Down
8 changes: 8 additions & 0 deletions engine-types/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ pub type WeiU256 = [u8; 32];
/// Near gas type which wraps an underlying u64.
pub struct NearGas(u64);

impl Sub<NearGas> for NearGas {
type Output = NearGas;

fn sub(self, rhs: NearGas) -> Self::Output {
Self(self.0 - rhs.0)
}
}

impl Display for NearGas {
fn fmt(&self, f: &mut Formatter<'_>) -> crate::fmt::Result {
self.0.fmt(f)
Expand Down
8 changes: 6 additions & 2 deletions engine/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use aurora_engine_sdk::io::{StorageIntermediate, IO};
pub const ERR_NOT_ENOUGH_BALANCE_FOR_FEE: &str = "ERR_NOT_ENOUGH_BALANCE_FOR_FEE";
/// Indicate zero attached balance for promise call
pub const ZERO_ATTACHED_BALANCE: Balance = 0;
/// NEAR Gas for calling `fininsh_deposit` promise. Used in the `deposit` logic.
const GAS_FOR_FINISH_DEPOSIT: NearGas = NearGas::new(50_000_000_000_000);
//// NEAR Gas for calling `fininsh_deposit` promise. Used in the `deposit` logic.
mrLSD marked this conversation as resolved.
Show resolved Hide resolved
pub const GAS_FOR_FINISH_DEPOSIT: NearGas = NearGas::new(50_000_000_000_000);
/// NEAR Gas for calling `verify_log_entry` promise. Used in the `deposit` logic.
// Note: Is 40Tgas always enough?
const GAS_FOR_VERIFY_LOG_ENTRY: NearGas = NearGas::new(40_000_000_000_000);
Expand Down Expand Up @@ -250,6 +250,7 @@ impl<I: IO + Copy> EthConnectorContract<I> {
predecessor_account_id: AccountId,
current_account_id: AccountId,
data: FinishDepositCallArgs,
gas: NearGas,
mrLSD marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<Option<PromiseWithCallbackArgs>, error::FinishDepositError> {
sdk::log!(&format!("Finish deposit with the amount: {}", data.amount));

Expand All @@ -266,6 +267,7 @@ impl<I: IO + Copy> EthConnectorContract<I> {
predecessor_account_id,
current_account_id,
transfer_call_args,
gas,
)?;
Ok(Some(promise))
} else {
Expand Down Expand Up @@ -472,6 +474,7 @@ impl<I: IO + Copy> EthConnectorContract<I> {
predecessor_account_id: AccountId,
current_account_id: AccountId,
args: TransferCallCallArgs,
gas: NearGas,
mrLSD marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<PromiseWithCallbackArgs, error::FtTransferCallError> {
sdk::log!(&format!(
"Transfer call to {} amount {}",
Expand Down Expand Up @@ -520,6 +523,7 @@ impl<I: IO + Copy> EthConnectorContract<I> {
&args.memo,
args.msg,
current_account_id,
gas,
)
.map_err(Into::into)
}
Expand Down
8 changes: 6 additions & 2 deletions engine/src/fungible_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ use crate::prelude::{
};
use aurora_engine_sdk::io::{StorageIntermediate, IO};

/// Gas for `resolve_transfer`: 5 TGas
const GAS_FOR_RESOLVE_TRANSFER: NearGas = NearGas::new(5_000_000_000_000);
const GAS_FOR_FT_ON_TRANSFER: NearGas = NearGas::new(10_000_000_000_000);
/// Gas for `ft_on_transfer`
const GAS_FOR_FT_TRANSFER_CALL: NearGas = NearGas::new(25_000_000_000_000);
mrLSD marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Debug, Default, BorshDeserialize, BorshSerialize)]
pub struct FungibleToken {
Expand Down Expand Up @@ -287,6 +289,7 @@ impl<I: IO + Copy> FungibleTokenOps<I> {
self.get_account_eth_balance(account_id).unwrap_or(0)
}

#[allow(clippy::too_many_arguments)]
pub fn ft_transfer_call(
&mut self,
sender_id: AccountId,
Expand All @@ -295,6 +298,7 @@ impl<I: IO + Copy> FungibleTokenOps<I> {
memo: &Option<String>,
msg: String,
current_account_id: AccountId,
gas: NearGas,
mrLSD marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<PromiseWithCallbackArgs, error::TransferError> {
// Special case for Aurora transfer itself - we shouldn't transfer
if sender_id != receiver_id {
Expand All @@ -321,7 +325,7 @@ impl<I: IO + Copy> FungibleTokenOps<I> {
method: "ft_on_transfer".to_string(),
args: data1.into_bytes(),
attached_balance: ZERO_ATTACHED_BALANCE,
attached_gas: GAS_FOR_FT_ON_TRANSFER.into_u64(),
attached_gas: (gas - GAS_FOR_FT_TRANSFER_CALL - GAS_FOR_RESOLVE_TRANSFER).into_u64(),
};
let ft_resolve_transfer_call = PromiseCreateArgs {
target_account_id: current_account_id,
Expand Down
14 changes: 12 additions & 2 deletions engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,12 @@ mod contract {
let current_account_id = io.current_account_id();
let predecessor_account_id = io.predecessor_account_id();
let maybe_promise_args = EthConnectorContract::init_instance(io)
.finish_deposit(predecessor_account_id, current_account_id, data)
.finish_deposit(
predecessor_account_id,
current_account_id,
data,
io.prepaid_gas(),
)
.sdk_unwrap();

if let Some(promise_args) = maybe_promise_args {
Expand Down Expand Up @@ -711,7 +716,12 @@ mod contract {
let current_account_id = io.current_account_id();
let predecessor_account_id = io.predecessor_account_id();
let promise_args = EthConnectorContract::init_instance(io)
.ft_transfer_call(predecessor_account_id, current_account_id, args)
.ft_transfer_call(
predecessor_account_id,
current_account_id,
args,
io.prepaid_gas(),
)
.sdk_unwrap();
let promise_id = io.promise_crate_with_callback(&promise_args);
io.promise_return(promise_id);
Expand Down