Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

Commit

Permalink
fix estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
rachel-bousfield committed Dec 17, 2021
1 parent 2240d9a commit e02dac9
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 46 deletions.
2 changes: 1 addition & 1 deletion arb_os/arbos-upgrade.mexe

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion arb_os/arbos.mexe

Large diffs are not rendered by default.

16 changes: 2 additions & 14 deletions arb_os/evmCallStack.mini
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,7 @@ public view write func initEvmCallStack(
let (startTxResult, acctStore) = if request.maxGas < const::Charging_MinArbGasForContractTx {
(const::TxResultCode_belowMinimumTxGas, safeGetGlobalAccountStore())
} else {
let (result, astore) = gasAccounting_startTxCharges(
request.maxGas,
request.gasPrice,
request.gasPayer,
safeGetGlobalAccountStore(),
if let Some(_) = request.incomingRequest.gasEstimationParams { true } else { false },
);
let (result, astore) = gasAccounting_startTxCharges(request, safeGetGlobalAccountStore());
safeSetGlobalAccountStore(astore);
(result, astore)
};
Expand Down Expand Up @@ -523,13 +517,7 @@ public view write func initEvmCallStackForConstructor(
let (startTxResult, *acctStore) = if request.maxGas < const::Charging_MinArbGasForContractTx {
(const::TxResultCode_belowMinimumTxGas, acctStore)
} else {
let (result, astore) = gasAccounting_startTxCharges(
request.maxGas,
request.gasPrice,
request.gasPayer,
acctStore,
if let Some(_) = request.incomingRequest.gasEstimationParams { true } else { false },
);
let (result, astore) = gasAccounting_startTxCharges(request, acctStore);
safeSetGlobalAccountStore(astore);
(result, astore)
};
Expand Down
63 changes: 44 additions & 19 deletions arb_os/gasAccounting.mini
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,24 @@ public view func gasAccounting_summaryToPublish() -> (uint, uint, uint, uint, ui
// (It's up to us to figure out whether there actually was an out-of-gas situation, vs some unrelated error.)

public view write func gasAccounting_startTxCharges(
maxGas: uint,
gasPrice: uint,
payer: address,
request: TxRequestData,
acctStore: AccountStore,
gasEstimationMode: bool,
) -> (
uint, // const::TxResultCode_success if success, otherwise an error code
AccountStore, // acctStore, with payments deducted
) {
let maxGas = request.maxGas;
let gasPrice = request.gasPrice;
let payer = request.gasPayer;

let gasEstimationMode = false;
let setAsideWei = true;

if let Some(gep) = request.incomingRequest.gasEstimationParams {
setAsideWei = !(gep.ignoreGasPrice && gep.ignoreMaxGas);
gasEstimationMode = true;
}

// Start charging a new Tx for ArbGas usage
// The caller should already have verified that the payer has enough funds, but we'll return None if not.

Expand All @@ -456,18 +465,23 @@ public view write func gasAccounting_startTxCharges(
};

// escrow funds from the payer, enough to pay for maxGas at gasPrice
let weiToHold = safeMul(maxGas, gasPrice);
let (*acctStore, amount) = accountStore_addToEscrowUpToMax(acctStore, const::EscrowStoreID_txGas, payer, weiToHold);
if amount < weiToHold {
// payer doesn't have enough funds to pay for gas
// give the collected funds to the network fee recipient, then return failure
acctStore = accountStore_payFullyFromEscrow(
acctStore,
const::EscrowStoreID_txGas,
address(chainParams_getOrDie(const::Atom_Param_NetworkFeeRecipient)),
);
return (const::TxResultCode_insufficientGasFunds, acctStore);
}
let weiToHold = if setAsideWei {
let wei = safeMul(maxGas, gasPrice);
let (*acctStore, amount) = accountStore_addToEscrowUpToMax(acctStore, const::EscrowStoreID_txGas, payer, wei);
if amount < wei {
// payer doesn't have enough funds to pay for gas
// give the collected funds to the network fee recipient, then return failure
acctStore = accountStore_payFullyFromEscrow(
acctStore,
const::EscrowStoreID_txGas,
address(chainParams_getOrDie(const::Atom_Param_NetworkFeeRecipient)),
);
return (const::TxResultCode_insufficientGasFunds, acctStore);
}
wei
} else {
0
};

let gasUsedByOS = ((~0) - getGas()) / gasAccountingInfo.pricingParams.arbGasDivisor;

Expand Down Expand Up @@ -778,12 +792,21 @@ public view write func gasAccounting_prepareTx(tx: TxRequestData) -> (
set tx.feeStats.units.tx = 1;
set tx.feeStats = txFeeStats_setL2Prices(tx.feeStats, prices);

let ignoreGasPrice = false;
let ignoreMaxGas = false;

let estimationCompGasLimit = if let Some(gep) = tx.incomingRequest.gasEstimationParams {
if gep.ignoreGasPrice {
set tx.feeStats.noFeeGasEstimationMode = true;
ignoreGasPrice = true;
}
if gep.ignoreMaxGas {
set tx.maxGas = account_getEthBalance(accountStore_get(safeGetGlobalAccountStore(), tx.gasPayer)) / gasAccountingInfo.currentPrices.perArbGasTotal;
ignoreMaxGas = true;
}
if ignoreGasPrice && ignoreMaxGas {
// high enough to never run out, but not so high as to make safeMath fail
set tx.maxGas = 1 << 128;
}
gep.computeGasLimit
} else {
Expand Down Expand Up @@ -843,9 +866,11 @@ public view write func gasAccounting_prepareTx(tx: TxRequestData) -> (
if tx.value > gasPayerFunds {
(const::TxResultCode_insufficientBalance, tx)
} else {
let affordableGas = (gasPayerFunds - tx.value) / gasPrice;
if (tx.maxGas == 0) || (tx.maxGas > affordableGas) {
set tx.maxGas = affordableGas;
if !(ignoreMaxGas && ignoreGasPrice) {
let affordableGas = (gasPayerFunds - tx.value) / gasPrice;
if (tx.maxGas == 0) || (tx.maxGas > affordableGas) {
set tx.maxGas = affordableGas;
}
}
set tx.gasPrice = gasPrice;
(const::TxResultCode_success, tx)
Expand Down
2 changes: 1 addition & 1 deletion arb_os/upgrade.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion replayTests/evm_direct_deploy_add.aoslog

Large diffs are not rendered by default.

Loading

0 comments on commit e02dac9

Please sign in to comment.