Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarnadas committed Jul 24, 2023
1 parent 2d2161c commit ce851d3
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 45 deletions.
11 changes: 10 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ members = [
"engine-transactions",
"engine-types",
"engine-workspace",
"etc/xcc-router",
]

exclude = [
Expand All @@ -89,7 +90,6 @@ exclude = [
"etc/tests/self-contained-5bEgfRQ",
"etc/tests/fibonacci",
"etc/tests/modexp-bench",
"etc/xcc-router",
]

[profile.release]
Expand Down
18 changes: 14 additions & 4 deletions engine-precompiles/src/xcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use aurora_engine_types::{
account_id::AccountId,
borsh::{BorshDeserialize, BorshSerialize},
format,
parameters::{CrossContractCallArgs, PromiseCreateArgs},
parameters::{CrossContractCallArgs, PromiseArgsWithSender, PromiseCreateArgs},
types::{balance::ZERO_YOCTO, Address, EthGas, NearGas},
vec, Cow, Vec, H160, H256, U256,
};
Expand Down Expand Up @@ -138,12 +138,18 @@ impl<I: IO> HandleBasedPrecompile for CrossContractCall<I> {
let call_gas = call.total_gas();
let attached_near = call.total_near();
let callback_count = call.promise_count() - 1;
let refund_costs = call.refund_costs();
let router_exec_cost = costs::ROUTER_EXEC_BASE
+ NearGas::new(callback_count * costs::ROUTER_EXEC_PER_CALLBACK.as_u64());
+ NearGas::new(callback_count * costs::ROUTER_EXEC_PER_CALLBACK.as_u64())
+ NearGas::new(refund_costs);
let args = PromiseArgsWithSender {
sender: *sender.as_fixed_bytes(),
args: call,
};
let promise = PromiseCreateArgs {
target_account_id,
method: consts::ROUTER_EXEC_NAME.into(),
args: call
args: args
.try_to_vec()
.map_err(|_| ExitError::Other(Cow::from(consts::ERR_SERIALIZE)))?,
attached_balance: ZERO_YOCTO,
Expand All @@ -153,10 +159,14 @@ impl<I: IO> HandleBasedPrecompile for CrossContractCall<I> {
}
CrossContractCallArgs::Delayed(call) => {
let attached_near = call.total_near();
let args = PromiseArgsWithSender {
sender: *sender.as_fixed_bytes(),
args: call,
};
let promise = PromiseCreateArgs {
target_account_id,
method: consts::ROUTER_SCHEDULE_NAME.into(),
args: call
args: args
.try_to_vec()
.map_err(|_| ExitError::Other(Cow::from(consts::ERR_SERIALIZE)))?,
attached_balance: ZERO_YOCTO,
Expand Down
6 changes: 5 additions & 1 deletion engine-tests/src/tests/standalone/call_tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::utils::solidity::erc20::{ERC20Constructor, ERC20};
use crate::utils::{self, standalone, Signer};
use aurora_engine_modexp::AuroraModExp;
use aurora_engine_types::borsh::BorshSerialize;
use aurora_engine_types::parameters::PromiseArgsWithSender;
use aurora_engine_types::{
parameters::{CrossContractCallArgs, PromiseArgs, PromiseCreateArgs},
storage,
Expand Down Expand Up @@ -341,7 +342,10 @@ fn test_trace_precompiles_with_subcalls() {
attached_balance: Yocto::new(1),
attached_gas: NearGas::new(100_000_000_000_000),
};
let xcc_args = CrossContractCallArgs::Delayed(PromiseArgs::Create(promise));
let xcc_args = CrossContractCallArgs::Delayed(PromiseArgsWithSender {
sender: signer_address,
args: PromiseArgs::Create(promise),
});
let tx = aurora_engine_transactions::legacy::TransactionLegacy {
nonce: signer.use_nonce().into(),
gas_price: U256::zero(),
Expand Down
55 changes: 55 additions & 0 deletions engine-types/src/parameters/promise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ use borsh::{maybestd::io, BorshDeserialize, BorshSerialize};
#[cfg(feature = "borsh-compat")]
use borsh_compat::{self as borsh, maybestd::io, BorshDeserialize, BorshSerialize};

#[must_use]
#[derive(Debug, BorshSerialize, BorshDeserialize)]
pub struct PromiseArgsWithSender {
pub sender: [u8; 20],
pub args: PromiseArgs,
}

#[must_use]
#[derive(Debug, BorshSerialize, BorshDeserialize)]
pub enum PromiseArgs {
Expand All @@ -16,6 +23,14 @@ pub enum PromiseArgs {
Recursive(NearPromise),
}

fn refund_costs(method: &str) -> u64 {
match method {
"ft_transfer" => 14_000_000_000_000,
"ft_transfer_call" => 39_000_000_000_000,
_ => 0,
}
}

impl PromiseArgs {
/// Counts the total number of promises this call creates (including callbacks).
#[must_use]
Expand All @@ -27,6 +42,18 @@ impl PromiseArgs {
}
}

/// Counts the total gas cost of potentail refunds this call creates.
#[must_use]
pub fn refund_costs(&self) -> u64 {
match self {
Self::Create(call) => refund_costs(call.method.as_str()),
Self::Callback(cb) => {
refund_costs(cb.base.method.as_str()) + refund_costs(cb.callback.method.as_str())
}
Self::Recursive(p) => p.refund_count(),
}
}

#[must_use]
pub fn total_gas(&self) -> NearGas {
match self {
Expand All @@ -53,6 +80,26 @@ pub enum SimpleNearPromise {
}

impl SimpleNearPromise {
#[must_use]
pub fn refund_count(&self) -> u64 {
match self {
Self::Create(call) => refund_costs(call.method.as_str()),
Self::Batch(batch) => {
let total: u64 = batch
.actions
.iter()
.filter_map(|a| {
if let PromiseAction::FunctionCall { name, .. } = a {
Some(refund_costs(name.as_str()))
} else {
None
}
})
.sum();
total
}
}
}
#[must_use]
pub fn total_gas(&self) -> NearGas {
match self {
Expand Down Expand Up @@ -117,6 +164,14 @@ impl NearPromise {
Self::And(ps) => ps.iter().map(Self::promise_count).sum(),
}
}
#[must_use]
pub fn refund_count(&self) -> u64 {
match self {
Self::Simple(x) => x.refund_count(),
Self::Then { base, callback } => base.refund_count() + callback.refund_count(),
Self::And(ps) => ps.iter().map(Self::refund_count).sum(),
}
}

#[must_use]
pub fn total_gas(&self) -> NearGas {
Expand Down
3 changes: 3 additions & 0 deletions etc/xcc-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ panic = "abort"
aurora-engine-types = { path = "../../engine-types", default-features = false, features = ["borsh-compat"] }
near-sdk = "4.1"

[dev-dependencies]
aurora-engine-sdk = { workspace = true, features = ["std"] }

[features]
default = []
all-promise-actions = []
Loading

0 comments on commit ce851d3

Please sign in to comment.