Skip to content

Commit

Permalink
add test for adding block headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Oct 23, 2023
1 parent 8c7784d commit 22495c7
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 9 deletions.
20 changes: 20 additions & 0 deletions testutil/sample/common.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions x/observer/keeper/msg_server_add_block_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
96 changes: 96 additions & 0 deletions x/observer/keeper/msg_server_add_block_header_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
4 changes: 2 additions & 2 deletions x/observer/types/crosschain_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func DefaultCrosschainFlags() *CrosschainFlags {
IsOutboundEnabled: true,
GasPriceIncreaseFlags: &DefaultGasPriceIncreaseFlags,
BlockHeaderVerificationFlags: &BlockHeaderVerificationFlags{
IsEthTypeChainEnabled: false,
IsBtcTypeChainEnabled: false,
IsEthTypeChainEnabled: true,
IsBtcTypeChainEnabled: true,
},
}
}
11 changes: 6 additions & 5 deletions x/observer/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

0 comments on commit 22495c7

Please sign in to comment.