Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for bsc headers #1334

Merged
merged 6 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions common/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ func IsEVMChain(chainID int64) bool {
chainID == 137 // polygon mainnet
}

func IsHeaderSupportedEvmChain(chainID int64) bool {
return chainID == 5 || // Goerli
chainID == 97 || // BSC testnet
chainID == 1337 || // eth privnet
chainID == 1 || // eth mainnet
chainID == 56 // bsc mainnet
}

func (chain Chain) IsKlaytnChain() bool {
return chain.ChainId == 1001
}
Expand Down
7 changes: 5 additions & 2 deletions testutil/network/genesis_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ func AddObserverData(t *testing.T, genesisState map[string]json.RawMessage, code
state.Params.BallotMaturityBlocks = 3
state.Keygen = &observerTypes.Keygen{BlockNumber: 10, GranteePubkeys: []string{}}
crosschainFlags := &observerTypes.CrosschainFlags{
IsInboundEnabled: true,
IsOutboundEnabled: true,
IsInboundEnabled: true,
IsOutboundEnabled: true,
GasPriceIncreaseFlags: &observerTypes.DefaultGasPriceIncreaseFlags,
BlockHeaderVerificationFlags: &observerTypes.DefaultBlockHeaderVerificationFlags,
}

nullify.Fill(&crosschainFlags)
state.CrosschainFlags = crosschainFlags

Expand Down
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
}
12 changes: 11 additions & 1 deletion x/observer/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
k.SetParams(ctx, params)

// Set if defined

crosschainFlags := types.DefaultCrosschainFlags()
if genState.CrosschainFlags != nil {
k.SetCrosschainFlags(ctx, *genState.CrosschainFlags)
crosschainFlags.IsOutboundEnabled = genState.CrosschainFlags.IsOutboundEnabled
crosschainFlags.IsInboundEnabled = genState.CrosschainFlags.IsInboundEnabled
if genState.CrosschainFlags.BlockHeaderVerificationFlags != nil {
crosschainFlags.BlockHeaderVerificationFlags = genState.CrosschainFlags.BlockHeaderVerificationFlags
}
if genState.CrosschainFlags.GasPriceIncreaseFlags != nil {
crosschainFlags.GasPriceIncreaseFlags = genState.CrosschainFlags.GasPriceIncreaseFlags
}
k.SetCrosschainFlags(ctx, *crosschainFlags)
} else {
k.SetCrosschainFlags(ctx, *types.DefaultCrosschainFlags())
}
Expand Down
2 changes: 1 addition & 1 deletion x/observer/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestGenesis(t *testing.T) {
sample.NodeAccount(),
sample.NodeAccount(),
},
CrosschainFlags: sample.CrosschainFlags(),
CrosschainFlags: types.DefaultCrosschainFlags(),
Keygen: sample.Keygen(t),
LastObserverCount: sample.LastObserverCount(1000),
CoreParamsList: sample.CoreParamsList(),
Expand Down
17 changes: 16 additions & 1 deletion x/observer/keeper/msg_server_add_block_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"fmt"

cosmoserrors "cosmossdk.io/errors"

Expand All @@ -20,6 +21,20 @@ func (k msgServer) AddBlockHeader(goCtx context.Context, msg *types.MsgAddBlockH
return nil, types.ErrNotAuthorizedPolicy
}

crosschainFlags, found := k.GetCrosschainFlags(ctx)
if !found {
return nil, fmt.Errorf("crosschain flags not found")
}
if crosschainFlags.BlockHeaderVerificationFlags == nil {
return nil, fmt.Errorf("block header verification flags not found")
}
if common.IsBitcoinChain(msg.ChainId) && !crosschainFlags.BlockHeaderVerificationFlags.IsBtcTypeChainEnabled {
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, cosmoserrors.Wrapf(types.ErrBlockHeaderVerficationDisabled, "proof verification not enabled for evm ,chain id: %d", msg.ChainId)
}

// add vote to ballot
ballot, _, err := k.FindBallot(ctx, msg.Digest(), chain, types.ObservationType_InBoundTx)
if err != nil {
Expand All @@ -37,7 +52,7 @@ func (k msgServer) AddBlockHeader(goCtx context.Context, msg *types.MsgAddBlockH
/**
* Vote finalized, add block header to store
*/
_, found := k.GetBlockHeader(ctx, msg.BlockHash)
_, found = k.GetBlockHeader(ctx, msg.BlockHash)
if found {
hashString, err := common.HashToString(msg.ChainId, msg.BlockHash)
if err != nil {
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/migrations/v4/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ func TestMigrateStore(t *testing.T) {
assert.NoError(t, err)
flags, found := k.GetCrosschainFlags(ctx)
assert.True(t, found)
assert.False(t, flags.BlockHeaderVerificationFlags.IsBtcTypeChainEnabled)
assert.False(t, flags.BlockHeaderVerificationFlags.IsEthTypeChainEnabled)
assert.True(t, flags.BlockHeaderVerificationFlags.IsBtcTypeChainEnabled)
assert.True(t, flags.BlockHeaderVerificationFlags.IsEthTypeChainEnabled)
}
16 changes: 9 additions & 7 deletions x/observer/types/crosschain_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ var DefaultGasPriceIncreaseFlags = GasPriceIncreaseFlags{
GasPriceIncreaseMax: 500,
}

var DefaultBlockHeaderVerificationFlags = BlockHeaderVerificationFlags{
IsEthTypeChainEnabled: true,
IsBtcTypeChainEnabled: true,
}

// DefaultCrosschainFlags returns the default crosschain flags used when not defined
func DefaultCrosschainFlags() *CrosschainFlags {
return &CrosschainFlags{
IsInboundEnabled: true,
IsOutboundEnabled: true,
GasPriceIncreaseFlags: &DefaultGasPriceIncreaseFlags,
BlockHeaderVerificationFlags: &BlockHeaderVerificationFlags{
IsEthTypeChainEnabled: false,
IsBtcTypeChainEnabled: false,
},
IsInboundEnabled: true,
IsOutboundEnabled: true,
GasPriceIncreaseFlags: &DefaultGasPriceIncreaseFlags,
BlockHeaderVerificationFlags: &DefaultBlockHeaderVerificationFlags,
}
}
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")
)
2 changes: 1 addition & 1 deletion x/observer/types/messages_add_block_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (msg *MsgAddBlockHeader) ValidateBasic() error {
return cosmoserrors.Wrapf(sdkerrors.ErrInvalidAddress, err.Error())
}

if common.IsEthereumChain(msg.ChainId) || common.IsBitcoinChain(msg.ChainId) {
if common.IsHeaderSupportedEvmChain(msg.ChainId) || common.IsBitcoinChain(msg.ChainId) {
if len(msg.BlockHash) != 32 {
return cosmoserrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid block hash length (%d)", len(msg.BlockHash))
}
Expand Down
Loading