Merge pull request #9 from sei-protocol/precompile-gas

add precompile interface with dynamically-calculated gas
This commit is contained in:
codchen 2024-01-04 16:04:38 +08:00 committed by GitHub
commit 6ff4c5e1d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -42,6 +42,10 @@ type PrecompiledContract interface {
Run(evm *EVM, sender common.Address, input []byte) ([]byte, error) // Run runs the precompiled contract
}
type DynamicGasPrecompiledContract interface {
RunAndCalculateGas(evm *EVM, sender common.Address, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) // Run runs the precompiled contract and calculate gas dynamically
}
// PrecompiledContractsHomestead contains the default set of pre-compiled Ethereum
// contracts used in the Frontier and Homestead releases.
var PrecompiledContractsHomestead = map[common.Address]PrecompiledContract{
@ -169,6 +173,9 @@ func ActivePrecompiles(rules params.Rules) []common.Address {
// - the _remaining_ gas,
// - any error that occurred
func RunPrecompiledContract(p PrecompiledContract, evm *EVM, sender common.Address, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) {
if dp, ok := p.(DynamicGasPrecompiledContract); ok {
return dp.RunAndCalculateGas(evm, sender, input, suppliedGas)
}
gasCost := p.RequiredGas(input)
if suppliedGas < gasCost {
return nil, 0, ErrOutOfGas