only CALL can use cmBridge.

change cmBridge address
This commit is contained in:
BananaLF 2023-05-11 15:07:13 +08:00
parent b55781be3f
commit fd03ac30ea
4 changed files with 19 additions and 15 deletions

View file

@ -1,14 +1,15 @@
package vm
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
)
var (
//cmBridgeContractAddress is 0xDb327e55CA2C68b23f83a0fbe29b592702e1d4d7
cmBridgeContractAddress = common.BytesToAddress(crypto.Keccak256([]byte("cm_bridge"))[12:])
//cmBridgeContractAddress is 0x0000000000000000000000000000000000000100
cmBridgeContractAddress = common.BytesToAddress([]byte{0x01, 0x00})
)
type CallToWasmByPrecompile func(ctx OKContext, caller, to common.Address, value *big.Int, input []byte, remainGas uint64) ([]byte, uint64, error)
@ -40,7 +41,10 @@ func (c *cmBridge) Run(in []byte) ([]byte, error) {
panic("cmBridge not support <Run> of implement")
}
func (c *cmBridge) CustomRun(in []byte, remainGas uint64) ([]byte, uint64, error) {
func (c *cmBridge) CustomRun(in []byte, remainGas uint64, callType string) ([]byte, uint64, error) {
if callType != CALL.String() {
return nil, 0, fmt.Errorf("cmBridge not support the type of call:%s, only support CALL", callType)
}
// cmBridge can not got coin, when can cmBridgeContract may be send coin to cmBridgeContractAddress, so we must send coin back to caller.
if c.value.Sign() != 0 && !c.EvmContext.CanTransfer(c.context.GetEVMStateDB(), cmBridgeContractAddress, c.value) {
return nil, 0, ErrCMBirdgeInsufficientBalance

View file

@ -147,9 +147,9 @@ func ActivePrecompiles(rules params.Rules) []common.Address {
// - the returned bytes,
// - the _remaining_ gas,
// - any error that occurred
func RunPrecompiledContract(p PrecompiledContract, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) {
func RunPrecompiledContract(p PrecompiledContract, input []byte, suppliedGas uint64, callType string) (ret []byte, remainingGas uint64, err error) {
if cmBirdgeContract, ok := p.(*cmBridge); ok {
return cmBirdgeContract.CustomRun(input, suppliedGas)
return cmBirdgeContract.CustomRun(input, suppliedGas, callType)
}
gasCost := p.RequiredGas(input)
if suppliedGas < gasCost {

View file

@ -96,7 +96,7 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
in := common.Hex2Bytes(test.Input)
gas := p.RequiredGas(in)
t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
if res, _, err := RunPrecompiledContract(p, in, gas); err != nil {
if res, _, err := RunPrecompiledContract(p, in, gas, "TEST"); err != nil {
t.Error(err)
} else if common.Bytes2Hex(res) != test.Expected {
t.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res))
@ -118,7 +118,7 @@ func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) {
gas := p.RequiredGas(in) - 1
t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
_, _, err := RunPrecompiledContract(p, in, gas)
_, _, err := RunPrecompiledContract(p, in, gas, "TEST")
if err.Error() != "out of gas" {
t.Errorf("Expected error [out of gas], got [%v]", err)
}
@ -135,7 +135,7 @@ func testPrecompiledFailure(addr string, test precompiledFailureTest, t *testing
in := common.Hex2Bytes(test.Input)
gas := p.RequiredGas(in)
t.Run(test.Name, func(t *testing.T) {
_, _, err := RunPrecompiledContract(p, in, gas)
_, _, err := RunPrecompiledContract(p, in, gas, "TEST")
if err.Error() != test.ExpectedError {
t.Errorf("Expected error [%v], got [%v]", test.ExpectedError, err)
}
@ -167,7 +167,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
copy(data, in)
res, _, err = RunPrecompiledContract(p, data, reqGas)
res, _, err = RunPrecompiledContract(p, data, reqGas, "TEST")
}
bench.StopTimer()
elapsed := uint64(time.Since(start))

View file

@ -214,7 +214,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
}
if isPrecompile {
ret, gas, err = RunPrecompiledContract(p, input, gas)
ret, gas, err = RunPrecompiledContract(p, input, gas, CALL.String())
} else {
// Initialise a new contract and set the code that is to be used by the EVM.
// The contract is a scoped environment for this execution context only.
@ -280,7 +280,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
// It is allowed to call precompiles, even via delegatecall
if p, isPrecompile := evm.precompile(caller.Address(), addr, value); isPrecompile {
ret, gas, err = RunPrecompiledContract(p, input, gas)
ret, gas, err = RunPrecompiledContract(p, input, gas, CALLCODE.String())
} else {
addrCopy := addr
// Initialise a new contract and set the code that is to be used by the EVM.
@ -324,7 +324,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
// It is allowed to call precompiles, even via delegatecall
if p, isPrecompile := evm.precompile(caller.Address(), addr, big0); isPrecompile {
ret, gas, err = RunPrecompiledContract(p, input, gas)
ret, gas, err = RunPrecompiledContract(p, input, gas, DELEGATECALL.String())
} else {
addrCopy := addr
// Initialise a new contract and make initialise the delegate values
@ -376,7 +376,7 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
evm.StateDB.AddBalance(addr, big0)
if p, isPrecompile := evm.precompile(caller.Address(), addr, big0); isPrecompile {
ret, gas, err = RunPrecompiledContract(p, input, gas)
ret, gas, err = RunPrecompiledContract(p, input, gas, STATICCALL.String())
} else {
// At this point, we use a copy of address. If we don't, the go compiler will
// leak the 'contract' to the outer scope, and make allocation for 'contract'