-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
187 changed files
with
1,646 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.