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

feat: gasless sync for tx pool & execution #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
166 changes: 160 additions & 6 deletions core/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@ use std::collections::BTreeMap;
use std::iter::FromIterator;

use arc_swap::ArcSwap;
use ethers::abi::{AbiDecode, AbiEncode};
use evm::backend::Apply;
use evm::executor::stack::{MemoryStackState, PrecompileFn, StackExecutor, StackSubstateMetadata};
use evm::CreateScheme;
use evm::{CreateScheme, ExitReason, ExitSucceed};

use common_merkle::TrieMerkle;
use protocol::codec::ProtocolCodec;
use protocol::traits::{ApplyBackend, Backend, Executor, ExecutorAdapter as Adapter};
use protocol::types::{
data_gas_cost, Account, Config, ExecResp, Hasher, SignedTransaction, TransactionAction, TxResp,
ValidatorExtend, GAS_CALL_TRANSACTION, GAS_CREATE_TRANSACTION, H160, NIL_DATA, RLP_NULL, U256,
data_gas_cost, Account, Config, ExecResp, Hasher, Log, SignedTransaction, TransactionAction,
TxResp, ValidatorExtend, GAS_CALL_TRANSACTION, GAS_CREATE_TRANSACTION, H160, H256, NIL_DATA,
RLP_NULL, U256,
};
use system_contract::gasless::abi::sponsor_whitelist_control_abi::{
GetSponsorForGasCall, IsWhitelistedCall, SubstractSponsorBalanceCall,
};
use system_contract::gasless::abi::GASLESS_ADDRESS;

use crate::precompiles::build_precompile_set;
use crate::system_contract::{system_contract_dispatch, NativeTokenContract, SystemContract};
Expand Down Expand Up @@ -135,7 +142,7 @@ impl Executor for AxonExecutor {
// Execute a transaction, if system contract dispatch return None, means the
// transaction called EVM
let mut r = system_contract_dispatch(backend, tx)
.unwrap_or_else(|| Self::evm_exec(backend, &config, &precompiles, tx));
.unwrap_or_else(|| Self::evm_exec(self, backend, &config, &precompiles, tx));

r.logs = backend.get_logs();
gas += r.gas_used;
Expand Down Expand Up @@ -188,19 +195,141 @@ impl Executor for AxonExecutor {
}

impl AxonExecutor {
fn call2<B: Backend>(
&self,
backend: &B,
gas_limit: u64,
from: Option<H160>,
to: Option<H160>,
value: U256,
data: Vec<u8>,
) -> (
ExitReason,
impl IntoIterator<Item = Apply<impl IntoIterator<Item = (H256, H256)>>>,
impl IntoIterator<Item = Log>,
) {
let config = Config::london();
let metadata = StackSubstateMetadata::new(gas_limit, &config);
let state = MemoryStackState::new(metadata, backend);
let precompiles = build_precompile_set();
let mut executor = StackExecutor::new_with_precompiles(state, &config, &precompiles);

let _base_gas = if to.is_some() {
GAS_CALL_TRANSACTION + data_gas_cost(&data)
} else {
GAS_CREATE_TRANSACTION + GAS_CALL_TRANSACTION + data_gas_cost(&data)
};

let (exit, _res) = if let Some(addr) = &to {
executor.transact_call(
from.unwrap_or_default(),
*addr,
value,
data,
gas_limit,
Vec::new(),
)
} else {
executor.transact_create(from.unwrap_or_default(), value, data, gas_limit, Vec::new())
};

let (values, logs) = executor.into_state().deconstruct();
(exit, values, logs)
}

pub fn is_sponsored<B: Backend + ApplyBackend + Adapter>(
&self,
backend: &mut B,
sender: &H160,
receiver: &Option<H160>,
gas_limit: &U256,
prepay_gas: &U256,
) -> bool {
if let Some(receiver) = receiver {
// call isWhitelisted
let call_data: Vec<u8> = AbiEncode::encode(IsWhitelistedCall {
contract_addr: *receiver,
user: *sender,
});
let rsp = Self::call(
self,
backend,
gas_limit.as_u64(),
Some(*sender),
Some(GASLESS_ADDRESS),
U256::default(),
call_data,
);
let is_sponsored: bool = AbiDecode::decode(rsp.ret).unwrap();
if !is_sponsored {
return false;
}

// call getSponsoredBalanceForGas
let call_data: Vec<u8> = AbiEncode::encode(GetSponsorForGasCall {
contract_addr: *receiver,
});
let rsp = Self::call(
self,
backend,
gas_limit.as_u64(),
Some(*sender),
Some(GASLESS_ADDRESS),
U256::default(),
call_data,
);
let sponsor_balance: U256 = AbiDecode::decode(rsp.ret).unwrap();
if sponsor_balance < *prepay_gas {
return false;
}

// call getSponsoredGasFeeUpperBound
let call_data: Vec<u8> = AbiEncode::encode(GetSponsorForGasCall {
contract_addr: *receiver,
});
let rsp = Self::call(
self,
backend,
gas_limit.as_u64(),
Some(*sender),
Some(GASLESS_ADDRESS),
U256::default(),
call_data,
);
let sponsor_bound: U256 = AbiDecode::decode(rsp.ret).unwrap();
if sponsor_bound < *gas_limit {
return false;
}

true
} else {
false
}
}

pub fn evm_exec<B: Backend + ApplyBackend + Adapter>(
&self,
backend: &mut B,
config: &Config,
precompiles: &BTreeMap<H160, PrecompileFn>,
tx: &SignedTransaction,
) -> TxResp {
// Deduct pre-pay gas
let sender = tx.sender;
let mut sender = tx.sender;
let tx_gas_price = backend.gas_price();
let gas_limit = tx.transaction.unsigned.gas_limit();
let prepay_gas = tx_gas_price * gas_limit;

let mut account = backend.get_account(&sender);

let receiver = tx.transaction.unsigned.to();
let mut is_sponsored = false;
if self.is_sponsored(backend, &sender, &receiver, gas_limit, &prepay_gas) {
sender = GASLESS_ADDRESS;
account = backend.get_account(&GASLESS_ADDRESS);
is_sponsored = true;
}

account.balance = account.balance.saturating_sub(prepay_gas);
backend.save_account(&sender, &account);

Expand Down Expand Up @@ -263,13 +392,38 @@ impl AxonExecutor {
let remain_gas = U256::from(remained_gas)
.checked_mul(tx_gas_price)
.unwrap_or_else(U256::max_value);

if is_sponsored {
sender = GASLESS_ADDRESS;
account = backend.get_account(&GASLESS_ADDRESS);
let actual_used_balance = prepay_gas.saturating_sub(remain_gas);

// call substractSponsorBalance
let call_data: Vec<u8> = AbiEncode::encode(SubstractSponsorBalanceCall {
contract_addr: receiver.unwrap(),
balance: actual_used_balance,
});

let (exit, values, logs) = Self::call2(
self,
backend,
gas_limit.as_u64(),
Some(sender),
Some(GASLESS_ADDRESS),
U256::default(),
call_data,
);
assert_eq!(exit, ExitReason::Succeed(ExitSucceed::Stopped));
backend.apply(values, logs, true);
}

account.balance = account
.balance
.checked_add(remain_gas)
.unwrap_or_else(U256::max_value);
}

backend.save_account(&tx.sender, &account);
backend.save_account(&sender, &account);

TxResp {
exit_reason: exit,
Expand Down
5 changes: 5 additions & 0 deletions core/executor/src/system_contract/gasless/abi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod sponsor_whitelist_control_abi;

use crate::system_contract::system_contract_address;
use protocol::types::H160;
pub const GASLESS_ADDRESS: H160 = system_contract_address(0x1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "SponsorWhitelistControl",
"sourceName": "contracts/SponsorWhitelistControl.sol",
"abi": [
{
"inputs": [
{
"internalType": "address[]",
"name": "",
"type": "address[]"
}
],
"name": "addPrivilege",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "contractAddr",
"type": "address"
}
],
"name": "getSponsorForGas",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "contractAddr",
"type": "address"
}
],
"name": "getSponsoredBalanceForGas",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "contractAddr",
"type": "address"
}
],
"name": "getSponsoredGasFeeUpperBound",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "contractAddr",
"type": "address"
}
],
"name": "isAllWhitelisted",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "contractAddr",
"type": "address"
},
{
"internalType": "address",
"name": "user",
"type": "address"
}
],
"name": "isWhitelisted",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address[]",
"name": "",
"type": "address[]"
}
],
"name": "removePrivilege",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "contractAddr",
"type": "address"
},
{
"internalType": "uint256",
"name": "upperBound",
"type": "uint256"
}
],
"name": "setSponsorForGas",
"outputs": [],
"stateMutability": "payable",
"type": "function"
}
],
"bytecode": "0x608060405234801561001057600080fd5b50610604806100206000396000f3fe60806040526004361061007b5760003560e01c8063b3b28fac1161004e578063b3b28fac1461013f578063b6b352721461017c578063d2932db6146101b9578063d665f9dd146101e25761007b565b806310128d3e1461008057806333a1af31146100a95780633e3e6428146100e657806379b47faa14610102575b600080fd5b34801561008c57600080fd5b506100a760048036038101906100a29190610418565b61021f565b005b3480156100b557600080fd5b506100d060048036038101906100cb9190610461565b610222565b6040516100dd919061049d565b60405180910390f35b61010060048036038101906100fb91906104ee565b610229565b005b34801561010e57600080fd5b5061012960048036038101906101249190610461565b61022d565b6040516101369190610549565b60405180910390f35b34801561014b57600080fd5b5061016660048036038101906101619190610461565b610234565b6040516101739190610573565b60405180910390f35b34801561018857600080fd5b506101a3600480360381019061019e919061058e565b61023b565b6040516101b09190610549565b60405180910390f35b3480156101c557600080fd5b506101e060048036038101906101db9190610418565b610243565b005b3480156101ee57600080fd5b5061020960048036038101906102049190610461565b610246565b6040516102169190610573565b60405180910390f35b50565b6000919050565b5050565b6000919050565b6000919050565b600092915050565b50565b6000919050565b6000604051905090565b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6102af82610266565b810181811067ffffffffffffffff821117156102ce576102cd610277565b5b80604052505050565b60006102e161024d565b90506102ed82826102a6565b919050565b600067ffffffffffffffff82111561030d5761030c610277565b5b602082029050602081019050919050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061034e82610323565b9050919050565b61035e81610343565b811461036957600080fd5b50565b60008135905061037b81610355565b92915050565b600061039461038f846102f2565b6102d7565b905080838252602082019050602084028301858111156103b7576103b661031e565b5b835b818110156103e057806103cc888261036c565b8452602084019350506020810190506103b9565b5050509392505050565b600082601f8301126103ff576103fe610261565b5b813561040f848260208601610381565b91505092915050565b60006020828403121561042e5761042d610257565b5b600082013567ffffffffffffffff81111561044c5761044b61025c565b5b610458848285016103ea565b91505092915050565b60006020828403121561047757610476610257565b5b60006104858482850161036c565b91505092915050565b61049781610343565b82525050565b60006020820190506104b2600083018461048e565b92915050565b6000819050919050565b6104cb816104b8565b81146104d657600080fd5b50565b6000813590506104e8816104c2565b92915050565b6000806040838503121561050557610504610257565b5b60006105138582860161036c565b9250506020610524858286016104d9565b9150509250929050565b60008115159050919050565b6105438161052e565b82525050565b600060208201905061055e600083018461053a565b92915050565b61056d816104b8565b82525050565b60006020820190506105886000830184610564565b92915050565b600080604083850312156105a5576105a4610257565b5b60006105b38582860161036c565b92505060206105c48582860161036c565b915050925092905056fea2646970667358221220ce653dafe48943ee691b3733dd1f18012475589ceb2ba78d1d52dda24972cde164736f6c63430008110033",
"deployedBytecode": "0x60806040526004361061007b5760003560e01c8063b3b28fac1161004e578063b3b28fac1461013f578063b6b352721461017c578063d2932db6146101b9578063d665f9dd146101e25761007b565b806310128d3e1461008057806333a1af31146100a95780633e3e6428146100e657806379b47faa14610102575b600080fd5b34801561008c57600080fd5b506100a760048036038101906100a29190610418565b61021f565b005b3480156100b557600080fd5b506100d060048036038101906100cb9190610461565b610222565b6040516100dd919061049d565b60405180910390f35b61010060048036038101906100fb91906104ee565b610229565b005b34801561010e57600080fd5b5061012960048036038101906101249190610461565b61022d565b6040516101369190610549565b60405180910390f35b34801561014b57600080fd5b5061016660048036038101906101619190610461565b610234565b6040516101739190610573565b60405180910390f35b34801561018857600080fd5b506101a3600480360381019061019e919061058e565b61023b565b6040516101b09190610549565b60405180910390f35b3480156101c557600080fd5b506101e060048036038101906101db9190610418565b610243565b005b3480156101ee57600080fd5b5061020960048036038101906102049190610461565b610246565b6040516102169190610573565b60405180910390f35b50565b6000919050565b5050565b6000919050565b6000919050565b600092915050565b50565b6000919050565b6000604051905090565b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6102af82610266565b810181811067ffffffffffffffff821117156102ce576102cd610277565b5b80604052505050565b60006102e161024d565b90506102ed82826102a6565b919050565b600067ffffffffffffffff82111561030d5761030c610277565b5b602082029050602081019050919050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061034e82610323565b9050919050565b61035e81610343565b811461036957600080fd5b50565b60008135905061037b81610355565b92915050565b600061039461038f846102f2565b6102d7565b905080838252602082019050602084028301858111156103b7576103b661031e565b5b835b818110156103e057806103cc888261036c565b8452602084019350506020810190506103b9565b5050509392505050565b600082601f8301126103ff576103fe610261565b5b813561040f848260208601610381565b91505092915050565b60006020828403121561042e5761042d610257565b5b600082013567ffffffffffffffff81111561044c5761044b61025c565b5b610458848285016103ea565b91505092915050565b60006020828403121561047757610476610257565b5b60006104858482850161036c565b91505092915050565b61049781610343565b82525050565b60006020820190506104b2600083018461048e565b92915050565b6000819050919050565b6104cb816104b8565b81146104d657600080fd5b50565b6000813590506104e8816104c2565b92915050565b6000806040838503121561050557610504610257565b5b60006105138582860161036c565b9250506020610524858286016104d9565b9150509250929050565b60008115159050919050565b6105438161052e565b82525050565b600060208201905061055e600083018461053a565b92915050565b61056d816104b8565b82525050565b60006020820190506105886000830184610564565b92915050565b600080604083850312156105a5576105a4610257565b5b60006105b38582860161036c565b92505060206105c48582860161036c565b915050925092905056fea2646970667358221220ce653dafe48943ee691b3733dd1f18012475589ceb2ba78d1d52dda24972cde164736f6c63430008110033",
"linkReferences": {},
"deployedLinkReferences": {}
}
Loading