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

set max gas to double of the charged gas for the 'intrinsic' smart contract calls #472

Merged
merged 3 commits into from
Sep 18, 2019
Merged
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
12 changes: 9 additions & 3 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ func (st *StateTransition) canBuyGas(accountOwner common.Address, gasNeeded *big
if gasCurrency == nil {
return st.state.GetBalance(accountOwner).Cmp(gasNeeded) > 0
}
balanceOf, _, err := currency.GetBalanceOf(accountOwner, *gasCurrency, params.MaxGasToReadErc20Balance, st.evm.GetHeader(), st.evm.GetStateDB())
balanceOf, gasUsed, err := currency.GetBalanceOf(accountOwner, *gasCurrency, params.MaxGasToReadErc20Balance, st.evm.GetHeader(), st.evm.GetStateDB())
log.Debug("balanceOf called", "gasCurrency", *gasCurrency, "gasUsed", gasUsed)

if err != nil {
return false
}
Expand All @@ -233,7 +235,9 @@ func (st *StateTransition) debitFrom(address common.Address, amount *big.Int, ga

rootCaller := vm.AccountRef(common.HexToAddress("0x0"))
// The caller was already charged for the cost of this operation via IntrinsicGas.
_, _, err := evm.Call(rootCaller, *gasCurrency, transactionData, params.MaxGasForDebitFromTransactions, big.NewInt(0))
_, leftoverGas, err := evm.Call(rootCaller, *gasCurrency, transactionData, params.MaxGasForDebitFromTransactions, big.NewInt(0))
gasUsed := params.MaxGasForDebitFromTransactions - leftoverGas
log.Debug("debitFrom called", "gasCurrency", *gasCurrency, "gasUsed", gasUsed)
return err
}

Expand All @@ -251,7 +255,9 @@ func (st *StateTransition) creditTo(address common.Address, amount *big.Int, gas
transactionData := common.GetEncodedAbi(functionSelector, [][]byte{common.AddressToAbi(address), common.AmountToAbi(amount)})
rootCaller := vm.AccountRef(common.HexToAddress("0x0"))
// The caller was already charged for the cost of this operation via IntrinsicGas.
_, _, err := evm.Call(rootCaller, *gasCurrency, transactionData, params.MaxGasForCreditToTransactions, big.NewInt(0))
_, leftoverGas, err := evm.Call(rootCaller, *gasCurrency, transactionData, params.MaxGasForCreditToTransactions, big.NewInt(0))
gasUsed := params.MaxGasForCreditToTransactions - leftoverGas
log.Debug("creditTo called", "gasCurrency", *gasCurrency, "gasUsed", gasUsed)
return err
}

Expand Down
19 changes: 14 additions & 5 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,20 @@ const (
// TODO(asa): Make these operations less expensive by charging only what is used.
// The problem is we don't know how much to refund until the refund is complete.
// If these values are changed, "setDefaults" will need updating.
MaxGasForDebitFromTransactions uint64 = 38 * 1000

// The plan is to have these values set within a system smart contract,
// and that they are read during runtime. They could then be changed via
// governance.
ExpectedGasForDebitFromTransactions uint64 = 23 * 1000
MaxGasForCreditToTransactions uint64 = 32 * 1000
MaxGasToReadErc20Balance uint64 = 15 * 1000
MaxGasToReadTobinTax uint64 = 50 * 1000
MaxGasForDebitFromTransactions uint64 = 46 * 1000
kevjue marked this conversation as resolved.
Show resolved Hide resolved

ExpectedGasForCreditToTransactions uint64 = 32 * 1000
MaxGasForCreditToTransactions uint64 = 64 * 1000

ExpectedGasToReadErc20Balance uint64 = 15 * 1000
MaxGasToReadErc20Balance uint64 = 30 * 1000

MaxGasToReadTobinTax uint64 = 50 * 1000
// We charge for reading the balance, 1 debit, and 3 credits (refunding gas, paying the gas fee recipient, sending to the infrastructure fund)
AdditionalGasForNonGoldCurrencies uint64 = 3*MaxGasForCreditToTransactions + ExpectedGasForDebitFromTransactions + MaxGasToReadErc20Balance
AdditionalGasForNonGoldCurrencies uint64 = 3*ExpectedGasForCreditToTransactions + ExpectedGasForDebitFromTransactions + ExpectedGasToReadErc20Balance
)