Refactor to ensure conformance test can run in circleci

This commit is contained in:
Ian Davis 2023-01-27 15:13:38 +00:00
parent 6efe08dd61
commit 67805fd25a
4 changed files with 283 additions and 275 deletions

View File

@ -3,6 +3,7 @@ package itests
import (
"bytes"
"context"
"encoding/binary"
"encoding/hex"
"encoding/json"
"os"
@ -87,7 +88,7 @@ func TestEthOpenRPCConformance(t *testing.T) {
client, _, ens := kit.EnsembleMinimal(t, kit.MockProofs(), kit.ThroughRPC(), kit.WithEthRPC())
ens.InterconnectAll().BeginMining(10 * time.Millisecond)
contractHex, err := os.ReadFile(EventMatrixContract.Filename)
contractHex, err := os.ReadFile("contracts/EventMatrix.hex")
require.NoError(t, err)
// strip any trailing newlines from the file
@ -109,7 +110,7 @@ func TestEthOpenRPCConformance(t *testing.T) {
blockFilterID, err := client.EthNewBlockFilter(ctx)
require.NoError(t, err)
filterAllLogs := newEthFilterBuilder().FromBlockEpoch(0).Filter()
filterAllLogs := kit.NewEthFilterBuilder().FromBlockEpoch(0).Filter()
logFilterID, err := client.EthNewFilter(ctx, filterAllLogs)
require.NoError(t, err)
@ -488,31 +489,31 @@ func createRawSignedEthTx(ctx context.Context, t *testing.T, client *kit.TestFul
}
func waitForMessageWithEvents(ctx context.Context, t *testing.T, client *kit.TestFullNode, sender address.Address, target address.Address) (ethtypes.EthHash, ethtypes.EthHash, ethtypes.EthUint64) {
// send a message that exercises event logs
messages := invokeAndWaitUntilAllOnChain(t, client, []Invocation{
{
Sender: sender,
Target: target,
Selector: EventMatrixContract.Fn["logEventThreeIndexedWithData"],
Data: packUint64Values(44, 27, 19, 12),
},
})
require.NotEmpty(t, messages)
for msgHash, mts := range messages {
ts := mts.ts
blockNumber := ethtypes.EthUint64(ts.Height())
tsCid, err := ts.Key().Cid()
require.NoError(t, err)
blockHash, err := ethtypes.EthHashFromCid(tsCid)
require.NoError(t, err)
return msgHash, blockHash, blockNumber
vals := []uint64{44, 27, 19, 12}
inputData := []byte{}
for _, v := range vals {
buf := make([]byte, 32)
binary.BigEndian.PutUint64(buf[24:], v)
inputData = append(inputData, buf...)
}
// should not get here
t.FailNow()
return ethtypes.EthHash{}, ethtypes.EthHash{}, 0
// send a message that exercises event logs
ret := client.EVM().InvokeSolidity(ctx, sender, target, kit.EventMatrixContract.Fn["logEventThreeIndexedWithData"], inputData)
require.True(t, ret.Receipt.ExitCode.IsSuccess(), "contract execution failed")
msgHash, err := client.EthGetTransactionHashByCid(ctx, ret.Message)
require.NoError(t, err)
require.NotNil(t, msgHash)
ts, err := client.ChainGetTipSet(ctx, ret.TipSet)
require.NoError(t, err)
blockNumber := ethtypes.EthUint64(ts.Height())
tsCid, err := ts.Key().Cid()
require.NoError(t, err)
blockHash, err := ethtypes.EthHashFromCid(tsCid)
require.NoError(t, err)
return *msgHash, blockHash, blockNumber
}

File diff suppressed because it is too large Load Diff

View File

@ -252,6 +252,7 @@ func (e *EVM) InvokeContractByFuncNameExpectExit(ctx context.Context, fromAddr a
require.Equal(e.t, exit, wait.Receipt.ExitCode)
}
// function signatures are the first 4 bytes of the hash of the function name and types
func CalcFuncSignature(funcName string) []byte {
hasher := sha3.NewLegacyKeccak256()
@ -310,3 +311,77 @@ func removeLeadingZeros(data []byte) []byte {
}
return data[firstNonZeroIndex:]
}
func NewEthFilterBuilder() *EthFilterBuilder {
return &EthFilterBuilder{}
}
type EthFilterBuilder struct {
filter ethtypes.EthFilterSpec
}
func (e *EthFilterBuilder) Filter() *ethtypes.EthFilterSpec { return &e.filter }
func (e *EthFilterBuilder) FromBlock(v string) *EthFilterBuilder {
e.filter.FromBlock = &v
return e
}
func (e *EthFilterBuilder) FromBlockEpoch(v abi.ChainEpoch) *EthFilterBuilder {
s := ethtypes.EthUint64(v).Hex()
e.filter.FromBlock = &s
return e
}
func (e *EthFilterBuilder) ToBlock(v string) *EthFilterBuilder {
e.filter.ToBlock = &v
return e
}
func (e *EthFilterBuilder) ToBlockEpoch(v abi.ChainEpoch) *EthFilterBuilder {
s := ethtypes.EthUint64(v).Hex()
e.filter.ToBlock = &s
return e
}
func (e *EthFilterBuilder) BlockHash(h ethtypes.EthHash) *EthFilterBuilder {
e.filter.BlockHash = &h
return e
}
func (e *EthFilterBuilder) AddressOneOf(as ...ethtypes.EthAddress) *EthFilterBuilder {
e.filter.Address = as
return e
}
func (e *EthFilterBuilder) Topic1OneOf(hs ...ethtypes.EthHash) *EthFilterBuilder {
if len(e.filter.Topics) == 0 {
e.filter.Topics = make(ethtypes.EthTopicSpec, 1)
}
e.filter.Topics[0] = hs
return e
}
func (e *EthFilterBuilder) Topic2OneOf(hs ...ethtypes.EthHash) *EthFilterBuilder {
for len(e.filter.Topics) < 2 {
e.filter.Topics = append(e.filter.Topics, nil)
}
e.filter.Topics[1] = hs
return e
}
func (e *EthFilterBuilder) Topic3OneOf(hs ...ethtypes.EthHash) *EthFilterBuilder {
for len(e.filter.Topics) < 3 {
e.filter.Topics = append(e.filter.Topics, nil)
}
e.filter.Topics[2] = hs
return e
}
func (e *EthFilterBuilder) Topic4OneOf(hs ...ethtypes.EthHash) *EthFilterBuilder {
for len(e.filter.Topics) < 4 {
e.filter.Topics = append(e.filter.Topics, nil)
}
e.filter.Topics[3] = hs
return e
}

64
itests/kit/solidity.go Normal file
View File

@ -0,0 +1,64 @@
package kit
import (
"golang.org/x/crypto/sha3"
)
func EthTopicHash(sig string) []byte {
hasher := sha3.NewLegacyKeccak256()
hasher.Write([]byte(sig))
return hasher.Sum(nil)
}
func EthFunctionHash(sig string) []byte {
hasher := sha3.NewLegacyKeccak256()
hasher.Write([]byte(sig))
return hasher.Sum(nil)[:4]
}
// SolidityContractDef holds information about one of the test contracts
type SolidityContractDef struct {
Filename string // filename of the hex of the contract, e.g. contracts/EventMatrix.hex
Fn map[string][]byte // mapping of function names to 32-bit selector
Ev map[string][]byte // mapping of event names to 256-bit signature hashes
}
var EventMatrixContract = SolidityContractDef{
Filename: "contracts/EventMatrix.hex",
Fn: map[string][]byte{
"logEventZeroData": EthFunctionHash("logEventZeroData()"),
"logEventOneData": EthFunctionHash("logEventOneData(uint256)"),
"logEventTwoData": EthFunctionHash("logEventTwoData(uint256,uint256)"),
"logEventThreeData": EthFunctionHash("logEventThreeData(uint256,uint256,uint256)"),
"logEventFourData": EthFunctionHash("logEventFourData(uint256,uint256,uint256,uint256)"),
"logEventOneIndexed": EthFunctionHash("logEventOneIndexed(uint256)"),
"logEventTwoIndexed": EthFunctionHash("logEventTwoIndexed(uint256,uint256)"),
"logEventThreeIndexed": EthFunctionHash("logEventThreeIndexed(uint256,uint256,uint256)"),
"logEventOneIndexedWithData": EthFunctionHash("logEventOneIndexedWithData(uint256,uint256)"),
"logEventTwoIndexedWithData": EthFunctionHash("logEventTwoIndexedWithData(uint256,uint256,uint256)"),
"logEventThreeIndexedWithData": EthFunctionHash("logEventThreeIndexedWithData(uint256,uint256,uint256,uint256)"),
},
Ev: map[string][]byte{
"EventZeroData": EthTopicHash("EventZeroData()"),
"EventOneData": EthTopicHash("EventOneData(uint256)"),
"EventTwoData": EthTopicHash("EventTwoData(uint256,uint256)"),
"EventThreeData": EthTopicHash("EventThreeData(uint256,uint256,uint256)"),
"EventFourData": EthTopicHash("EventFourData(uint256,uint256,uint256,uint256)"),
"EventOneIndexed": EthTopicHash("EventOneIndexed(uint256)"),
"EventTwoIndexed": EthTopicHash("EventTwoIndexed(uint256,uint256)"),
"EventThreeIndexed": EthTopicHash("EventThreeIndexed(uint256,uint256,uint256)"),
"EventOneIndexedWithData": EthTopicHash("EventOneIndexedWithData(uint256,uint256)"),
"EventTwoIndexedWithData": EthTopicHash("EventTwoIndexedWithData(uint256,uint256,uint256)"),
"EventThreeIndexedWithData": EthTopicHash("EventThreeIndexedWithData(uint256,uint256,uint256,uint256)"),
},
}
var EventsContract = SolidityContractDef{
Filename: "contracts/events.bin",
Fn: map[string][]byte{
"log_zero_data": {0x00, 0x00, 0x00, 0x00},
"log_zero_nodata": {0x00, 0x00, 0x00, 0x01},
"log_four_data": {0x00, 0x00, 0x00, 0x02},
},
Ev: map[string][]byte{},
}