Skip to content

Commit

Permalink
wip: update e2e test for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
boodyvo committed Nov 4, 2024
1 parent f3401b1 commit 1de642c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 39 deletions.
37 changes: 3 additions & 34 deletions app/params/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/gogoproto/proto"
enccodec "github.com/evmos/ethermint/encoding/codec"
Expand Down Expand Up @@ -51,44 +52,12 @@ func MakeEncodingConfig() EncodingConfig {
enccodec.RegisterLegacyAminoCodec(amino)
enccodec.RegisterInterfaces(interfaceRegistry)

legacytx.RegressionTestingAminoCodec = amino

return EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Marshaler: marshaler,
TxConfig: txCfg,
Amino: amino,
}
}

// cdc := amino.NewLegacyAmino()
// signingOptions := signing.Options{
// AddressCodec: address.Bech32Codec{
// Bech32Prefix: sdk.GetConfig().GetBech32AccountAddrPrefix(),
// },
// ValidatorAddressCodec: address.Bech32Codec{
// Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(),
// },
// CustomGetSigners: map[protoreflect.FullName]signing.GetSignersFunc{
// evmtypes.MsgEthereumTxCustomGetSigner.MsgType: evmtypes.MsgEthereumTxCustomGetSigner.Fn,
// erc20types.MsgConvertERC20CustomGetSigner.MsgType: erc20types.MsgConvertERC20CustomGetSigner.Fn,
// },
// }
//
// interfaceRegistry, _ := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{
// ProtoFiles: proto.HybridResolver,
// SigningOptions: signingOptions,
// })
// codec := amino.NewProtoCodec(interfaceRegistry)
// enccodec.RegisterLegacyAminoCodec(cdc)
// enccodec.RegisterInterfaces(interfaceRegistry)
//
// // This is needed for the EIP712 txs because currently is using
// // the deprecated method legacytx.StdSignBytes
// legacytx.RegressionTestingAminoCodec = cdc
// eip712.SetEncodingConfig(cdc, interfaceRegistry)
//
// return sdktestutil.TestEncodingConfig{
// InterfaceRegistry: interfaceRegistry,
// Codec: codec,
// TxConfig: tx.NewTxConfig(codec, tx.DefaultSignModes),
// Amino: cdc,
// }
10 changes: 10 additions & 0 deletions client/grpc/query/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package query

import (
"context"
"cosmossdk.io/log"
"crypto/tls"
"fmt"
dbm "github.com/cosmos/cosmos-db"
"net/url"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -34,6 +36,14 @@ func newGrpcConnection(ctx context.Context, endpoint string) (*grpc.ClientConn,
// otherwise it will produce panics like:
// invalid Go type math.Int for field ...
encodingConfig := app.MakeEncodingConfig()
_ = app.NewApp(
log.NewNopLogger(),
dbm.NewMemDB(),
app.DefaultNodeHome,
nil,
encodingConfig,
app.DefaultOptions,
)
protoCodec := codec.NewProtoCodec(encodingConfig.InterfaceRegistry)
grpcCodec := protoCodec.GRPCCodec()

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ replace (
// Tracking kava-labs/etheremint master branch
// TODO: Tag before release
//github.com/evmos/ethermint => github.com/kava-labs/ethermint v0.21.1-0.20240802224012-586960857184
github.com/evmos/ethermint => github.com/kava-labs/ethermint v0.0.0-20241104150149-8a2ef3e12c4e
//github.com/evmos/ethermint => ../ethermint
//github.com/evmos/ethermint => github.com/kava-labs/ethermint v0.0.0-20241104181845-bb02c25baa11
github.com/evmos/ethermint => ../ethermint
// See https://github.com/cosmos/cosmos-sdk/pull/10401, https://github.com/cosmos/cosmos-sdk/commit/0592ba6158cd0bf49d894be1cef4faeec59e8320
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.0
// Downgraded to avoid bugs in following commits which causes "version does not exist" errors
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -834,8 +834,6 @@ github.com/kava-labs/cometbft-db v0.0.0-20241007145430-b2740b2e4bed h1:3FNJ3fcD9
github.com/kava-labs/cometbft-db v0.0.0-20241007145430-b2740b2e4bed/go.mod h1:buPRZKyVp+u5fmwN7tDtOk1zc5xA2z9BJJTy61tNnws=
github.com/kava-labs/cosmos-sdk v0.0.0-20241104154228-9f413992bbe2 h1:rZ3KCm7PZG/IsX2r1L8ZyMhxRL/8FCZ4pGGjPd7obZ0=
github.com/kava-labs/cosmos-sdk v0.0.0-20241104154228-9f413992bbe2/go.mod h1:6Eesrx3ZE7vxBZWpK++30H+Uc7Q4ahQWCL7JKU/LEdU=
github.com/kava-labs/ethermint v0.0.0-20241104150149-8a2ef3e12c4e h1:184b0xnTXkULy16FCgFuUxPIdbISnEpiEIEFY5TuLt0=
github.com/kava-labs/ethermint v0.0.0-20241104150149-8a2ef3e12c4e/go.mod h1:dFh15Ndobz1zWqWcJm9ML+0BnMt5m8gS+MhVUcAGLpg=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/e2e_evm_contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ func (suite *IntegrationTestSuite) TestEip712BasicMessageAuthorization() {
suite.NoError(err)
suite.Equal(sdkerrors.SuccessABCICode, res.TxResponse.Code)

_, err = util.WaitForSdkTxCommit(suite.Kava.Grpc.Query.Tx, res.TxResponse.TxHash, 6*time.Second)
//_, err = util.WaitForSdkTxCommit(suite.Kava.Grpc.Query.Tx, res.TxResponse.TxHash, 6*time.Second)
_, err = util.WaitForSdkTxCommit(suite.Kava.Grpc.Query.Tx, res.TxResponse.TxHash, 20*time.Second)
suite.NoError(err)

// check that the message was processed & the kava is transferred.
Expand Down
90 changes: 90 additions & 0 deletions tests/e2e/testutil/eip712.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package testutil

import (
"bytes"
"context"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"strings"

"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand All @@ -17,6 +21,87 @@ import (
evmtypes "github.com/evmos/ethermint/x/evm/types"
)

func dataMismatchError(encType string, encValue interface{}) error {
return fmt.Errorf("provided data '%v' doesn't match type '%s'", encValue, encType)
}

func EncodeData(typedData apitypes.TypedData, primaryType string, data map[string]interface{}, depth int) (hexutil.Bytes, error) {
//if err := typedData.validate(); err != nil {
// return nil, err
//}

buffer := bytes.Buffer{}

// Verify extra data
if exp, got := len(typedData.Types[primaryType]), len(data); exp < got {
return nil, fmt.Errorf("there is extra data provided in the message (%d < %d)", exp, got)
}

// Add typehash
buffer.Write(typedData.TypeHash(primaryType))

// Add field contents. Structs and arrays have special handlers.
for _, field := range typedData.Types[primaryType] {
fmt.Println("field", field)
encType := field.Type
encValue := data[field.Name]
if encType[len(encType)-1:] == "]" {
arrayValue, ok := encValue.([]interface{})
fmt.Println("arrayValue 1", arrayValue, ok)
if !ok {
return nil, dataMismatchError(encType, encValue)
}

arrayBuffer := bytes.Buffer{}
parsedType := strings.Split(encType, "[")[0]
for _, item := range arrayValue {
if typedData.Types[parsedType] != nil {
mapValue, ok := item.(map[string]interface{})
fmt.Println("mapValue 1", mapValue, ok)
if !ok {
return nil, dataMismatchError(parsedType, item)
}
encodedData, err := typedData.EncodeData(parsedType, mapValue, depth+1)
fmt.Println("encodedData 1", encodedData, err)
if err != nil {
return nil, err
}
arrayBuffer.Write(crypto.Keccak256(encodedData))
} else {
bytesValue, err := typedData.EncodePrimitiveValue(parsedType, item, depth)
fmt.Println("bytesValue 1", bytesValue, err)
if err != nil {
return nil, err
}
arrayBuffer.Write(bytesValue)
}
}

buffer.Write(crypto.Keccak256(arrayBuffer.Bytes()))
} else if typedData.Types[field.Type] != nil {
mapValue, ok := encValue.(map[string]interface{})
fmt.Println("mapValue 2", mapValue, ok)
if !ok {
return nil, dataMismatchError(encType, encValue)
}
encodedData, err := typedData.EncodeData(field.Type, mapValue, depth+1)
fmt.Println("encodedData 2", encodedData, err)
if err != nil {
return nil, err
}
buffer.Write(crypto.Keccak256(encodedData))
} else {
byteValue, err := typedData.EncodePrimitiveValue(encType, encValue, depth)
fmt.Println("byteValue 2", byteValue, err)
if err != nil {
return nil, err
}
buffer.Write(byteValue)
}
}
return buffer.Bytes(), nil
}

// NewEip712TxBuilder is a helper method for creating an EIP712 signed tx
// A tx like this is what a user signing cosmos messages with Metamask would broadcast.
func (suite *E2eTestSuite) NewEip712TxBuilder(
Expand Down Expand Up @@ -64,6 +149,11 @@ func (suite *E2eTestSuite) NewEip712TxBuilder(
}, evmParams.Params)
suite.NoError(err)

fmt.Println("got typedData", typedData)

encodedData, err := EncodeData(typedData, typedData.PrimaryType, typedData.Message, 1)
fmt.Println("encodedData", encodedData, err)

// -- raw data hash!
data, err := eip712.ComputeTypedDataHash(typedData)
suite.NoError(err)
Expand Down

0 comments on commit 1de642c

Please sign in to comment.