Skip to content

Commit

Permalink
feat precompiled contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
SherLzp committed Mar 18, 2023
1 parent 62c873e commit 91b6a04
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func (k *Keeper) ApplyMessageWithConfig(
ret, _, leftoverGas, vmErr = nEVM.Create(sender, msg.Data(), leftoverGas, msg.Value())
stateDB.SetNonce(sender.Address(), msg.Nonce()+1)
} else {
ret, leftoverGas, vmErr = nEVM.Call(sender, *msg.To(), msg.Data(), leftoverGas, msg.Value())
ret, leftoverGas, vmErr = nEVM.Call(sender, *msg.To(), msg.Data(), leftoverGas, msg.Value(), commit)
}

refundQuotient := params.RefundQuotient
Expand Down
26 changes: 24 additions & 2 deletions x/evm/vm/geth/geth.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,29 @@ func (e *EVM) RunStatefulPrecompiledContract(
return output, suppliedGas, err
}

// PreRunStatefulPrecompiledContract runs a stateful precompiled contract and ignores the address and
// value arguments. It uses the RunPrecompiledContract function from the geth vm package
func (e *EVM) PreRunStatefulPrecompiledContract(
p evm.StatefulPrecompiledContract,
caller common.Address, // address arg is unused
input []byte,
suppliedGas uint64,
value *big.Int,
) (ret []byte, remainingGas uint64, err error) {
gasCost := p.RequiredGas(input)
if suppliedGas < gasCost {
return nil, 0, vm.ErrOutOfGas
}
suppliedGas -= gasCost
//output, err := p.RunStateful(e, caller, input, value)
return nil, suppliedGas, err
}

// Call executes the contract associated with the addr with the given input as
// parameters. It also handles any necessary value transfer required and takes
// the necessary steps to create accounts and reverses the state in case of an
// execution error or failed value transfer.
func (evm *EVM) Call(caller vm.ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error) {
func (evm *EVM) Call(caller vm.ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int, commit bool) (ret []byte, leftOverGas uint64, err error) {
// Fail if we're trying to execute above the call depth limit
oEVM := reflect.ValueOf(evm.EVM).Elem()
depthValue := oEVM.FieldByName("depth")
Expand Down Expand Up @@ -173,7 +191,11 @@ func (evm *EVM) Call(caller vm.ContractRef, addr common.Address, input []byte, g

if isPrecompile {
if customPrecompiledContracts[addr] != nil {
ret, gas, err = evm.RunStatefulPrecompiledContract(customPrecompiledContracts[addr], caller.Address(), input, gas, value)
if !commit {
ret, gas, err = evm.PreRunStatefulPrecompiledContract(customPrecompiledContracts[addr], caller.Address(), input, gas, value)
} else {
ret, gas, err = evm.RunStatefulPrecompiledContract(customPrecompiledContracts[addr], caller.Address(), input, gas, value)
}
} else {
ret, gas, err = vm.RunPrecompiledContract(p, input, gas)
}
Expand Down
2 changes: 1 addition & 1 deletion x/evm/vm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type EVM interface {
Cancel()
Cancelled() bool //nolint
Interpreter() *vm.EVMInterpreter
Call(caller vm.ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error)
Call(caller vm.ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int, commit bool) (ret []byte, leftOverGas uint64, err error)
CallCode(caller vm.ContractRef, addr common.Address, input []byte, gas uint64, value *big.Int) (ret []byte, leftOverGas uint64, err error)
DelegateCall(caller vm.ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error)
StaticCall(caller vm.ContractRef, addr common.Address, input []byte, gas uint64) (ret []byte, leftOverGas uint64, err error)
Expand Down

0 comments on commit 91b6a04

Please sign in to comment.