Skip to content

Commit

Permalink
rebase develop 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Mar 25, 2024
2 parents 3390b3d + 3e7fd99 commit b868315
Show file tree
Hide file tree
Showing 187 changed files with 1,646 additions and 173 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* [1805](https://github.com/zeta-chain/node/pull/1805) - add admin and performance test and fix upgrade test
* [1879](https://github.com/zeta-chain/node/pull/1879) - full coverage for messages in types packages
* [1899](https://github.com/zeta-chain/node/pull/1899) - add empty test files so packages are included in coverage
* [1903](https://github.com/zeta-chain/node/pull/1903) - common package tests

### Fixes

Expand Down
8 changes: 0 additions & 8 deletions common/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/btcsuite/btcutil"
eth "github.com/ethereum/go-ethereum/common"
"github.com/zeta-chain/zetacore/common/cosmos"
)

type Address string
Expand All @@ -29,10 +28,6 @@ func NewAddress(address string) Address {
return NoAddress
}

func (addr Address) AccAddress() (cosmos.AccAddress, error) {
return cosmos.AccAddressFromBech32(addr.String())
}

func (addr Address) Equals(addr2 Address) bool {
return strings.EqualFold(addr.String(), addr2.String())
}
Expand Down Expand Up @@ -68,9 +63,6 @@ func DecodeBtcAddress(inputAddress string, chainID int64) (address btcutil.Addre
if err != nil {
return nil, err
}
if chainParams == nil {
return nil, fmt.Errorf("chain params not found")
}
address, err = btcutil.DecodeAddress(inputAddress, chainParams)
if err != nil {
return nil, fmt.Errorf("decode address failed: %s , for input address %s", err.Error(), inputAddress)
Expand Down
38 changes: 38 additions & 0 deletions common/address_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"errors"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -13,12 +14,22 @@ func TestPackage(t *testing.T) { TestingT(t) }
func TestAddress(t *testing.T) {
addr := NewAddress("bnb1lejrrtta9cgr49fuh7ktu3sddhe0ff7wenlpn6")
require.EqualValuesf(t, NoAddress, addr, "address string should be empty")
require.True(t, addr.IsEmpty())

addr = NewAddress("bogus")
require.EqualValuesf(t, NoAddress, addr, "address string should be empty")
require.True(t, addr.IsEmpty())

addr = NewAddress("0x90f2b1ae50e6018230e90a33f98c7844a0ab635a")
require.EqualValuesf(t, "0x90f2b1ae50e6018230e90a33f98c7844a0ab635a", addr.String(), "address string should be equal")
require.False(t, addr.IsEmpty())

addr2 := NewAddress("0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5")
require.EqualValuesf(t, "0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5", addr2.String(), "address string should be equal")
require.False(t, addr.IsEmpty())

require.False(t, addr.Equals(addr2))
require.True(t, addr.Equals(addr))
}

func TestDecodeBtcAddress(t *testing.T) {
Expand Down Expand Up @@ -61,3 +72,30 @@ func TestDecodeBtcAddress(t *testing.T) {
require.NoError(t, err)
})
}

func TestConvertRecoverToError(t *testing.T) {
t.Run("recover with string", func(t *testing.T) {
err := ConvertRecoverToError("error occurred")
require.Error(t, err)
require.Equal(t, "error occurred", err.Error())
})

t.Run("recover with error", func(t *testing.T) {
originalErr := errors.New("original error")
err := ConvertRecoverToError(originalErr)
require.Error(t, err)
require.Equal(t, originalErr, err)
})

t.Run("recover with non-string and non-error", func(t *testing.T) {
err := ConvertRecoverToError(12345)
require.Error(t, err)
require.Equal(t, "12345", err.Error())
})

t.Run("recover with nil", func(t *testing.T) {
err := ConvertRecoverToError(nil)
require.Error(t, err)
require.Equal(t, "<nil>", err.Error())
})
}
51 changes: 51 additions & 0 deletions common/authz_tx_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package common

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestTxTypeString(t *testing.T) {
tests := []struct {
name string
txType TxType
expectedString string
}{
{"InboundVoter", InboundVoter, "InboundVoter"},
{"OutboundVoter", OutboundVoter, "OutboundVoter"},
{"NonceVoter", NonceVoter, "NonceVoter"},
{"GasPriceVoter", GasPriceVoter, "GasPriceVoter"},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.expectedString, test.txType.String())
})
}
}

func TestKeyTypeString(t *testing.T) {
tests := []struct {
name string
keyType KeyType
expectedString string
}{
{"TssSignerKey", TssSignerKey, "tss_signer"},
{"ValidatorGranteeKey", ValidatorGranteeKey, "validator_grantee"},
{"ZetaClientGranteeKey", ZetaClientGranteeKey, "zetaclient_grantee"},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.expectedString, test.keyType.String())
})
}
}

func TestGetAllKeyTypes(t *testing.T) {
expectedKeys := []KeyType{ValidatorGranteeKey, ZetaClientGranteeKey, TssSignerKey}
actualKeys := GetAllKeyTypes()

require.ElementsMatch(t, expectedKeys, actualKeys)
}
50 changes: 50 additions & 0 deletions common/bitcoin/bitcoin_spv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package bitcoin

import (
"testing"

"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/stretchr/testify/require"
)

func TestProve(t *testing.T) {
t.Run("returns true if empty block", func(t *testing.T) {
result := Prove(chainhash.Hash{}, chainhash.Hash{}, []byte{}, 0)
require.True(t, result)
})
}

func TestVerifyHash256Merkle(t *testing.T) {
tests := []struct {
name string
proof []byte
index uint
want bool
}{
{
name: "valid length but invalid index and content",
proof: make([]byte, 32),
index: 0,
want: true,
},
{
name: "invalid length not multiple of 32",
proof: make([]byte, 34),
index: 0,
want: false,
},
{
name: "invalid length equal to 64",
proof: make([]byte, 64),
index: 0,
want: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := VerifyHash256Merkle(tc.proof, tc.index)
require.Equal(t, tc.want, result)
})
}
}
101 changes: 100 additions & 1 deletion common/bitcoin/proof_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,100 @@
package bitcoin_test
package bitcoin

import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
"testing"

"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/common/testdata"
)

func TestBitcoinMerkleProof(t *testing.T) {
blocks := testdata.LoadTestBlocks(t)

t.Run("it should verify merkle proof", func(t *testing.T) {
for _, b := range blocks.Blocks {
// Deserialize the header bytes from base64
headerBytes, err := base64.StdEncoding.DecodeString(b.HeaderBase64)
require.NoError(t, err)
header := unmarshalHeader(t, headerBytes)

// Deserialize the block bytes from base64
blockBytes, err := base64.StdEncoding.DecodeString(b.BlockBase64)
require.NoError(t, err)
blockVerbose := &btcjson.GetBlockVerboseTxResult{}
err = json.Unmarshal(blockBytes, blockVerbose)
require.NoError(t, err)

txns := getBlockTxs(t, blockVerbose)

// Build a Merkle tree from the transaction hashes and verify each transaction
mk := NewMerkle(txns)
for i, tx := range txns {
path, index, err := mk.BuildMerkleProof(i)
require.NoError(t, err)

// True proof should verify
pass := Prove(*tx.Hash(), header.MerkleRoot, path, index)
require.True(t, pass)

// Fake proof should not verify
fakeIndex := index ^ 0xffffffff // flip all bits
pass = Prove(*tx.Hash(), header.MerkleRoot, path, fakeIndex)
require.False(t, pass)
}
}
})

t.Run("it should fail if tree is empty", func(t *testing.T) {
mt := Merkle{
tree: []*chainhash.Hash{},
}

_, _, err := mt.BuildMerkleProof(0)
require.Error(t, err)
})

t.Run("it should fail if tree len + 1 is not power of 2", func(t *testing.T) {
mt := Merkle{
tree: []*chainhash.Hash{{}, {}},
}

_, _, err := mt.BuildMerkleProof(0)
require.Error(t, err)
})

t.Run("it should fail if txIndex out of range", func(t *testing.T) {
mt := Merkle{
tree: []*chainhash.Hash{{}},
}

_, _, err := mt.BuildMerkleProof(2)
require.Error(t, err)
})
}

func unmarshalHeader(t *testing.T, headerBytes []byte) *wire.BlockHeader {
var header wire.BlockHeader
err := header.Deserialize(bytes.NewReader(headerBytes))
require.NoError(t, err)
return &header
}

func getBlockTxs(t *testing.T, blockVerbose *btcjson.GetBlockVerboseTxResult) []*btcutil.Tx {
txns := []*btcutil.Tx{}
for _, res := range blockVerbose.Tx {
txBytes, err := hex.DecodeString(res.Hex)
require.NoError(t, err)
tx, err := btcutil.NewTxFromBytes(txBytes)
require.NoError(t, err)
txns = append(txns, tx)
}
return txns
}
46 changes: 46 additions & 0 deletions common/bitcoin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package common

import (
"testing"

"github.com/btcsuite/btcd/chaincfg"
"github.com/stretchr/testify/require"
)

func TestBitcoinNetParamsFromChainID(t *testing.T) {
tests := []struct {
name string
chainID int64
expected *chaincfg.Params
wantErr bool
}{
{"Regnet", BtcRegtestChain().ChainId, BitcoinRegnetParams, false},
{"Mainnet", BtcMainnetChain().ChainId, BitcoinMainnetParams, false},
{"Testnet", BtcTestNetChain().ChainId, BitcoinTestnetParams, false},
{"Unknown", -1, nil, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
params, err := BitcoinNetParamsFromChainID(tt.chainID)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tt.expected, params)
}
})
}
}

func TestIsBitcoinRegnet(t *testing.T) {
require.True(t, IsBitcoinRegnet(BtcRegtestChain().ChainId))
require.False(t, IsBitcoinRegnet(BtcMainnetChain().ChainId))
require.False(t, IsBitcoinRegnet(BtcTestNetChain().ChainId))
}

func TestIsBitcoinMainnet(t *testing.T) {
require.True(t, IsBitcoinMainnet(BtcMainnetChain().ChainId))
require.False(t, IsBitcoinMainnet(BtcRegtestChain().ChainId))
require.False(t, IsBitcoinMainnet(BtcTestNetChain().ChainId))
}
7 changes: 0 additions & 7 deletions common/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ import (
ethcommon "github.com/ethereum/go-ethereum/common"
)

// ParseChainName returns the ChainName from a string
// if no such name exists, returns the empty chain name: ChainName_empty
func ParseChainName(chain string) ChainName {
c := ChainName_value[chain]
return ChainName(c)
}

type SigninAlgo string

// Chains represent a slice of Chain
Expand Down
5 changes: 5 additions & 0 deletions common/chain_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func TestCosmosToEthChainID(t *testing.T) {
chainID: "athens-701_2",
isErr: true,
},
{
name: "invalid number format",
chainID: "cosmoshub_abc-1",
isErr: true,
},
}

for _, tc := range testCases {
Expand Down
Loading

0 comments on commit b868315

Please sign in to comment.