-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit EVM contract events for EVM calls made from Go contracts (#1401)
Co-authored-by: Vadim Macagon <[email protected]>
- Loading branch information
Showing
29 changed files
with
1,027 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
builtin/plugins/sample_go_contract/evm_sample_go_contract.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// +build evm | ||
|
||
package sample_go_contract | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"math/big" | ||
"path" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/loomnetwork/go-loom" | ||
types "github.com/loomnetwork/go-loom/builtin/types/sample_go_contract" | ||
"github.com/loomnetwork/go-loom/plugin/contractpb" | ||
) | ||
|
||
const ( | ||
innerEmitterAbiFilename = "./testdata/InnerEmitter.abi" | ||
outerEmitterAbiFilename = "./testdata/OuterEmitter.abi" | ||
) | ||
|
||
func (k *SampleGoContract) TestNestedEvmCalls(ctx contractpb.Context, req *types.SampleGoContractNestedEvmRequest) error { | ||
if err := testInnerEmitter(ctx, loom.UnmarshalAddressPB(req.InnerEmitter), req.InnerEmitterValue); err != nil { | ||
return err | ||
} | ||
if err := testOuterEmitter(ctx, loom.UnmarshalAddressPB(req.OuterEmitter), req.OuterEmitterValue); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func testInnerEmitter(ctx contractpb.Context, addr loom.Address, value uint64) error { | ||
_, filename, _, ok := runtime.Caller(1) | ||
if !ok { | ||
return errors.New("cannot find the path of evm_sample_go_contract.go") | ||
} | ||
abiPath := path.Join(path.Dir(filename), innerEmitterAbiFilename) | ||
abiBytes, err := ioutil.ReadFile(abiPath) | ||
if err != nil { | ||
return err | ||
} | ||
abiEventData, err := abi.JSON(strings.NewReader(string(abiBytes))) | ||
if err != nil { | ||
return err | ||
} | ||
input, err := abiEventData.Pack("sendEvent", big.NewInt(int64(value))) | ||
if err != nil { | ||
return err | ||
} | ||
evmOut := []byte{} | ||
err = contractpb.CallEVM(ctx, addr, input, &evmOut) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func testOuterEmitter(ctx contractpb.Context, addr loom.Address, value uint64) error { | ||
_, filename, _, ok := runtime.Caller(1) | ||
if !ok { | ||
return errors.New("cannot find the path of evm_sample_go_contract.go") | ||
} | ||
abiPath := path.Join(path.Dir(filename), outerEmitterAbiFilename) | ||
abiBytes, err := ioutil.ReadFile(abiPath) | ||
if err != nil { | ||
return err | ||
} | ||
abiEventData, err := abi.JSON(strings.NewReader(string(abiBytes))) | ||
if err != nil { | ||
return err | ||
} | ||
input, err := abiEventData.Pack("sendEvent", big.NewInt(int64(value))) | ||
if err != nil { | ||
return err | ||
} | ||
evmOut := []byte{} | ||
|
||
err = contractpb.CallEVM(ctx, addr, input, &evmOut) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
13 changes: 13 additions & 0 deletions
13
builtin/plugins/sample_go_contract/noevm_sample_go_contract.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// +build !evm | ||
|
||
package sample_go_contract | ||
|
||
import ( | ||
types "github.com/loomnetwork/go-loom/builtin/types/sample_go_contract" | ||
"github.com/loomnetwork/go-loom/plugin/contractpb" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func (k *SampleGoContract) TestNestedEvmCalls(_ contractpb.Context, _ *types.SampleGoContractNestedEvmRequest) error { | ||
return errors.New("testing evm in non evm build") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package sample_go_contract | ||
|
||
import ( | ||
types "github.com/loomnetwork/go-loom/builtin/types/sample_go_contract" | ||
"github.com/loomnetwork/go-loom/plugin" | ||
"github.com/loomnetwork/go-loom/plugin/contractpb" | ||
) | ||
|
||
type SampleGoContract struct { | ||
} | ||
|
||
func (k *SampleGoContract) Meta() (plugin.Meta, error) { | ||
return plugin.Meta{ | ||
Name: "sample-go-contract", | ||
Version: "1.0.0", | ||
}, nil | ||
} | ||
|
||
func (k *SampleGoContract) Init(ctx contractpb.Context, req *types.SampleGoContractInitRequest) error { | ||
return nil | ||
} | ||
|
||
var Contract plugin.Contract = contractpb.MakePluginContract(&SampleGoContract{}) |
69 changes: 69 additions & 0 deletions
69
builtin/plugins/sample_go_contract/sample_go_contract_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// +build evm | ||
|
||
package sample_go_contract | ||
|
||
import ( | ||
"encoding/hex" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/loomnetwork/go-loom" | ||
types "github.com/loomnetwork/go-loom/builtin/types/sample_go_contract" | ||
"github.com/loomnetwork/go-loom/plugin/contractpb" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
|
||
"github.com/loomnetwork/loomchain/evm" | ||
"github.com/loomnetwork/loomchain/features" | ||
"github.com/loomnetwork/loomchain/plugin" | ||
) | ||
|
||
var ( | ||
addr1 = loom.MustParseAddress("chain:0xb16a379ec18d4093666f8f38b11a3071c920207d") | ||
caller = loom.MustParseAddress("chain:0x5cecd1f7261e1f4c684e297be3edf03b825e01c4") | ||
) | ||
|
||
func TestSampleGoContract(t *testing.T) { | ||
pctx := plugin.CreateFakeContextWithEVM(caller, addr1) | ||
|
||
sampleGoContract := &SampleGoContract{} | ||
sampleAddr := pctx.CreateContract(Contract) | ||
ctx := contractpb.WrapPluginContext(pctx.WithAddress(sampleAddr)) | ||
sampleInit := types.SampleGoContractInitRequest{} | ||
require.NoError(t, sampleGoContract.Init(ctx, &sampleInit)) | ||
|
||
pctx.State.SetFeature(features.EvmConstantinopleFeature, true) | ||
|
||
testInnerEmitterAddr, err := deployContractToEVM(pctx, "InnerEmitter", caller) | ||
require.NoError(t, err) | ||
|
||
testChainEventAddr, err := deployContractToEVM(pctx, "OuterEmitter", caller) | ||
require.NoError(t, err) | ||
|
||
req := types.SampleGoContractNestedEvmRequest{ | ||
InnerEmitter: testInnerEmitterAddr.MarshalPB(), | ||
OuterEmitter: testChainEventAddr.MarshalPB(), | ||
InnerEmitterValue: 7, | ||
OuterEmitterValue: 77, | ||
} | ||
require.NoError(t, sampleGoContract.TestNestedEvmCalls(ctx, &req)) | ||
} | ||
|
||
func deployContractToEVM(ctx *plugin.FakeContextWithEVM, filename string, caller loom.Address) (loom.Address, error) { | ||
contractAddr := loom.Address{} | ||
hexByteCode, err := ioutil.ReadFile("testdata/" + filename + ".bin") | ||
if err != nil { | ||
return contractAddr, err | ||
} | ||
byteCode := common.FromHex(string(hexByteCode)) | ||
byteCode, err = hex.DecodeString(string(hexByteCode)) | ||
|
||
vm := evm.NewLoomVm(ctx.State, nil, nil, nil, false) | ||
_, contractAddr, err = vm.Create(caller, byteCode, loom.NewBigUIntFromInt(0)) | ||
if err != nil { | ||
return contractAddr, err | ||
} | ||
ctx.RegisterContract("", contractAddr, caller) | ||
return contractAddr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"constant":false,"inputs":[{"name":"i","type":"uint256"}],"name":"sendEvent","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"number","type":"uint256"}],"name":"MyEvent","type":"event"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
6080604052348015600f57600080fd5b5060b48061001e6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063d0a2d2cb14602d575b600080fd5b605660048036036020811015604157600080fd5b81019080803590602001909291905050506058565b005b807f6c2b4666ba8da5a95717621d879a77de725f3d816709b9cbe9f059b8f875e28460405160405180910390a25056fea165627a7a7230582002f672efd8c6b210ffa70c9cceed43480269dfbcb9cf29a770bf48fa78c845750029 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"constant":false,"inputs":[{"name":"i","type":"uint256"}],"name":"sendEvent","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
608060405234801561001057600080fd5b50610204806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063d0a2d2cb14610030575b600080fd5b61005c6004803603602081101561004657600080fd5b810190808035906020019092919050505061005e565b005b600060405161006c906100fa565b604051809103906000f080158015610088573d6000803e3d6000fd5b5090508073ffffffffffffffffffffffffffffffffffffffff1663d0a2d2cb836040518263ffffffff1660e01b815260040180828152602001915050600060405180830381600087803b1580156100de57600080fd5b505af11580156100f2573d6000803e3d6000fd5b505050505050565b60d2806101078339019056fe6080604052348015600f57600080fd5b5060b48061001e6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063d0a2d2cb14602d575b600080fd5b605660048036036020811015604157600080fd5b81019080803590602001909291905050506058565b005b807f6c2b4666ba8da5a95717621d879a77de725f3d816709b9cbe9f059b8f875e28460405160405180910390a25056fea165627a7a723058207fec2eea969df7b0ecdbb68d123dd1b9365b066f11039c266ec1a86e843e53680029a165627a7a723058209feda54308151c758353c3137751f94d38f3daebc2c2b1278480fa5d25e05eb10029 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.