From 22495c7d0952644261c9748352be2494b846c9c6 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Mon, 23 Oct 2023 16:20:08 -0400 Subject: [PATCH] add test for adding block headers --- testutil/sample/common.go | 20 ++++ .../keeper/msg_server_add_block_header.go | 4 +- .../msg_server_add_block_header_test.go | 96 +++++++++++++++++++ x/observer/types/crosschain_flags.go | 4 +- x/observer/types/errors.go | 11 ++- 5 files changed, 126 insertions(+), 9 deletions(-) create mode 100644 x/observer/keeper/msg_server_add_block_header_test.go diff --git a/testutil/sample/common.go b/testutil/sample/common.go index 77daaf09b0..a2d3c18dd7 100644 --- a/testutil/sample/common.go +++ b/testutil/sample/common.go @@ -1,7 +1,12 @@ package sample import ( + "context" + "math/big" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/ethereum/go-ethereum/rlp" "github.com/tendermint/tendermint/crypto/secp256k1" "github.com/zeta-chain/zetacore/common" ) @@ -22,3 +27,18 @@ func PubKeySet() *common.PubKeySet { } return &pubKeySet } + +func EthHeader() (headerRLP []byte, err error) { + url := "https://rpc.ankr.com/eth_goerli" + client, err := ethclient.Dial(url) + if err != nil { + return + } + bn := int64(9889649) + block, err := client.BlockByNumber(context.Background(), big.NewInt(bn)) + if err != nil { + return + } + headerRLP, _ = rlp.EncodeToBytes(block.Header()) + return +} diff --git a/x/observer/keeper/msg_server_add_block_header.go b/x/observer/keeper/msg_server_add_block_header.go index bfc49e8199..83f8c97dc0 100644 --- a/x/observer/keeper/msg_server_add_block_header.go +++ b/x/observer/keeper/msg_server_add_block_header.go @@ -29,10 +29,10 @@ func (k msgServer) AddBlockHeader(goCtx context.Context, msg *types.MsgAddBlockH return nil, fmt.Errorf("block header verification flags not found") } if common.IsBitcoinChain(msg.ChainId) && !crosschainFlags.BlockHeaderVerificationFlags.IsBtcTypeChainEnabled { - return nil, fmt.Errorf("proof verification not enabled for bitcoin chain") + return nil, cosmoserrors.Wrapf(types.ErrBlockHeaderVerficationDisabled, "proof verification not enabled for bitcoin ,chain id: %d", msg.ChainId) } if common.IsEVMChain(msg.ChainId) && !crosschainFlags.BlockHeaderVerificationFlags.IsEthTypeChainEnabled { - return nil, fmt.Errorf("proof verification not enabled for evm chain") + return nil, cosmoserrors.Wrapf(types.ErrBlockHeaderVerficationDisabled, "proof verification not enabled for evm ,chain id: %d", msg.ChainId) } // add vote to ballot diff --git a/x/observer/keeper/msg_server_add_block_header_test.go b/x/observer/keeper/msg_server_add_block_header_test.go new file mode 100644 index 0000000000..db77cac8ab --- /dev/null +++ b/x/observer/keeper/msg_server_add_block_header_test.go @@ -0,0 +1,96 @@ +//go:build TESTNET +// +build TESTNET + +package keeper_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/zeta-chain/zetacore/common" + keepertest "github.com/zeta-chain/zetacore/testutil/keeper" + "github.com/zeta-chain/zetacore/testutil/sample" + "github.com/zeta-chain/zetacore/x/observer/keeper" + "github.com/zeta-chain/zetacore/x/observer/types" +) + +func TestMsgServer_AddBlockHeader(t *testing.T) { + header, err := sample.EthHeader() + assert.NoError(t, err) + observerChain := common.GoerliChain() + observerAddress := sample.AccAddress() + // Add tests for btc headers : https://github.com/zeta-chain/node/issues/1336 + tt := []struct { + name string + msg *types.MsgAddBlockHeader + IsEthTypeChainEnabled bool + IsBtcTypeChainEnabled bool + wantErr require.ErrorAssertionFunc + }{ + { + name: "success submit eth header", + msg: &types.MsgAddBlockHeader{ + Creator: observerAddress, + ChainId: common.GoerliChain().ChainId, + BlockHash: sample.Bytes(), + Height: 1, + Header: common.NewEthereumHeader(header), + }, + IsEthTypeChainEnabled: true, + IsBtcTypeChainEnabled: true, + wantErr: require.NoError, + }, + { + name: "failure submit eth header eth disabled", + msg: &types.MsgAddBlockHeader{ + Creator: observerAddress, + ChainId: common.GoerliChain().ChainId, + BlockHash: sample.Bytes(), + Height: 1, + Header: common.NewEthereumHeader(header), + }, + IsEthTypeChainEnabled: false, + IsBtcTypeChainEnabled: true, + wantErr: func(t require.TestingT, err error, i ...interface{}) { + assert.ErrorIs(t, err, types.ErrBlockHeaderVerficationDisabled) + }, + }, + { + name: "failure submit eth header eth disabled", + msg: &types.MsgAddBlockHeader{ + Creator: sample.AccAddress(), + ChainId: common.GoerliChain().ChainId, + BlockHash: sample.Bytes(), + Height: 1, + Header: common.NewEthereumHeader(header), + }, + IsEthTypeChainEnabled: false, + IsBtcTypeChainEnabled: true, + wantErr: func(t require.TestingT, err error, i ...interface{}) { + assert.ErrorIs(t, err, types.ErrNotAuthorizedPolicy) + }, + }, + } + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + k, ctx := keepertest.ObserverKeeper(t) + srv := keeper.NewMsgServerImpl(*k) + k.SetObserverMapper(ctx, &types.ObserverMapper{ + ObserverChain: &observerChain, + ObserverList: []string{observerAddress}, + }) + k.SetCrosschainFlags(ctx, types.CrosschainFlags{ + IsInboundEnabled: true, + IsOutboundEnabled: true, + GasPriceIncreaseFlags: nil, + BlockHeaderVerificationFlags: &types.BlockHeaderVerificationFlags{ + IsEthTypeChainEnabled: tc.IsEthTypeChainEnabled, + IsBtcTypeChainEnabled: tc.IsBtcTypeChainEnabled, + }, + }) + _, err := srv.AddBlockHeader(ctx, tc.msg) + tc.wantErr(t, err) + }) + } +} diff --git a/x/observer/types/crosschain_flags.go b/x/observer/types/crosschain_flags.go index 4dccfbc8c4..772bd8f58d 100644 --- a/x/observer/types/crosschain_flags.go +++ b/x/observer/types/crosschain_flags.go @@ -25,8 +25,8 @@ func DefaultCrosschainFlags() *CrosschainFlags { IsOutboundEnabled: true, GasPriceIncreaseFlags: &DefaultGasPriceIncreaseFlags, BlockHeaderVerificationFlags: &BlockHeaderVerificationFlags{ - IsEthTypeChainEnabled: false, - IsBtcTypeChainEnabled: false, + IsEthTypeChainEnabled: true, + IsBtcTypeChainEnabled: true, }, } } diff --git a/x/observer/types/errors.go b/x/observer/types/errors.go index 3767712e34..c4659faa33 100644 --- a/x/observer/types/errors.go +++ b/x/observer/types/errors.go @@ -25,9 +25,10 @@ var ( ErrKeygenCompleted = errorsmod.Register(ModuleName, 1115, "keygen already completed") ErrNotAuthorized = errorsmod.Register(ModuleName, 1116, "not authorized") - ErrBlockHeaderNotFound = errorsmod.Register(ModuleName, 1117, "block header not found") - ErrUnrecognizedBlockHeader = errorsmod.Register(ModuleName, 1118, "unrecognized block header") - ErrBlockAlreadyExist = errorsmod.Register(ModuleName, 1119, "block already exists") - ErrNoParentHash = errorsmod.Register(ModuleName, 1120, "no parent hash") - ErrInvalidTimestamp = errorsmod.Register(ModuleName, 1121, "invalid timestamp") + ErrBlockHeaderNotFound = errorsmod.Register(ModuleName, 1117, "block header not found") + ErrUnrecognizedBlockHeader = errorsmod.Register(ModuleName, 1118, "unrecognized block header") + ErrBlockAlreadyExist = errorsmod.Register(ModuleName, 1119, "block already exists") + ErrNoParentHash = errorsmod.Register(ModuleName, 1120, "no parent hash") + ErrInvalidTimestamp = errorsmod.Register(ModuleName, 1121, "invalid timestamp") + ErrBlockHeaderVerficationDisabled = errorsmod.Register(ModuleName, 1122, "block header verification is disabled") )