add a FEVM events itest.

This commit is contained in:
Raúl Kripalani 2022-11-15 15:24:29 +00:00
parent 4313917e4c
commit 907c201912
3 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1 @@
63000000678063000000116000396000f360003560e01c80600014601f578060011460365780600214604157600080fd5b67112233445566778860005260086018a060006000f35b60006000a060006000f35b67112233445566778860005263000044446200333361222261111160086018a460006000f3

View File

@ -0,0 +1,79 @@
package itests
import (
"context"
"encoding/hex"
"fmt"
"os"
"testing"
"time"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/itests/kit"
"github.com/stretchr/testify/require"
)
// TestFEVMEvents does a basic events smoke test.
func TestFEVMEvents(t *testing.T) {
require := require.New(t)
kit.QuietMiningLogs()
blockTime := 100 * time.Millisecond
client, _, ens := kit.EnsembleMinimal(t, kit.MockProofs(), kit.ThroughRPC())
ens.InterconnectAll().BeginMining(blockTime)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
// install contract
contractHex, err := os.ReadFile("contracts/events.bin")
require.NoError(err)
contract, err := hex.DecodeString(string(contractHex))
require.NoError(err)
fromAddr, err := client.WalletDefaultAddress(ctx)
require.NoError(err)
result := client.EVM().DeployContract(ctx, fromAddr, contract)
idAddr, err := address.NewIDAddress(result.ActorID)
require.NoError(err)
t.Logf("actor ID address is %s", idAddr)
// var (
// earliest = "earliest"
// latest = "latest"
// )
//
// // Install a filter.
// filter, err := client.EthNewFilter(ctx, &api.EthFilterSpec{
// FromBlock: &earliest,
// ToBlock: &latest,
// })
// require.NoError(err)
//
// // No logs yet.
// res, err := client.EthGetFilterLogs(ctx, filter)
// require.NoError(err)
// require.Empty(res.NewLogs)
// log a zero topic event with data
ret := client.EVM().InvokeSolidity(ctx, fromAddr, idAddr, []byte{0x00, 0x00, 0x00, 0x00}, nil)
require.True(ret.Receipt.ExitCode.IsSuccess(), "contract execution failed")
require.NotNil(ret.Receipt.EventsRoot)
fmt.Println(client.EVM().LoadEvents(ctx, *ret.Receipt.EventsRoot))
// log a zero topic event with no data
ret = client.EVM().InvokeSolidity(ctx, fromAddr, idAddr, []byte{0x00, 0x00, 0x00, 0x01}, nil)
require.True(ret.Receipt.ExitCode.IsSuccess(), "contract execution failed")
fmt.Println(ret)
fmt.Println(client.EVM().LoadEvents(ctx, *ret.Receipt.EventsRoot))
// log a four topic event with data
ret = client.EVM().InvokeSolidity(ctx, fromAddr, idAddr, []byte{0x00, 0x00, 0x00, 0x02}, nil)
require.True(ret.Receipt.ExitCode.IsSuccess(), "contract execution failed")
fmt.Println(ret)
fmt.Println(client.EVM().LoadEvents(ctx, *ret.Receipt.EventsRoot))
}

View File

@ -4,7 +4,10 @@ import (
"bytes"
"context"
"encoding/binary"
"fmt"
amt4 "github.com/filecoin-project/go-amt-ipld/v4"
"github.com/ipfs/go-cid"
"github.com/stretchr/testify/require"
cbg "github.com/whyrusleeping/cbor-gen"
@ -97,3 +100,55 @@ func (e *EVM) InvokeSolidity(ctx context.Context, sender address.Address, target
return wait
}
// LoadEvents loads all events in an event AMT.
func (e *EVM) LoadEvents(ctx context.Context, eventsRoot cid.Cid) []types.Event {
require := require.New(e.t)
s := &apiIpldStore{ctx, e}
amt, err := amt4.LoadAMT(ctx, s, eventsRoot, amt4.UseTreeBitWidth(5))
require.NoError(err)
ret := make([]types.Event, 0, amt.Len())
err = amt.ForEach(ctx, func(u uint64, deferred *cbg.Deferred) error {
var evt types.Event
if err := evt.UnmarshalCBOR(bytes.NewReader(deferred.Raw)); err != nil {
return err
}
ret = append(ret, evt)
return nil
})
require.NoError(err)
return ret
}
// TODO: cleanup and put somewhere reusable.
type apiIpldStore struct {
ctx context.Context
api api.FullNode
}
func (ht *apiIpldStore) Context() context.Context {
return ht.ctx
}
func (ht *apiIpldStore) Get(ctx context.Context, c cid.Cid, out interface{}) error {
raw, err := ht.api.ChainReadObj(ctx, c)
if err != nil {
return err
}
cu, ok := out.(cbg.CBORUnmarshaler)
if ok {
if err := cu.UnmarshalCBOR(bytes.NewReader(raw)); err != nil {
return err
}
return nil
}
return fmt.Errorf("object does not implement CBORUnmarshaler")
}
func (ht *apiIpldStore) Put(ctx context.Context, v interface{}) (cid.Cid, error) {
panic("No mutations allowed")
}