diff --git a/e2e/runner/bitcoin.go b/e2e/runner/bitcoin.go index 60c6ae3025..6fbd6b40d7 100644 --- a/e2e/runner/bitcoin.go +++ b/e2e/runner/bitcoin.go @@ -8,6 +8,7 @@ import ( "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" @@ -296,7 +297,7 @@ func (r *E2ERunner) GetBitcoinChainID() int64 { // IsLocalBitcoin returns true if the runner is running on a local bitcoin network func (r *E2ERunner) IsLocalBitcoin() bool { - return r.BitcoinParams.Name == chains.BitcoinRegnetParams.Name + return r.BitcoinParams.Name == chaincfg.RegressionNetParams.Name } // GenerateToAddressIfLocalBitcoin generates blocks to an address if the runner is interacting diff --git a/pkg/chains/bitcoin.go b/pkg/chains/bitcoin.go index 532ac0dc73..211722cf9c 100644 --- a/pkg/chains/bitcoin.go +++ b/pkg/chains/bitcoin.go @@ -7,42 +7,37 @@ import ( ) var ( - BitcoinMainnetParams = &chaincfg.MainNetParams - BitcoinRegnetParams = &chaincfg.RegressionNetParams - BitcoinTestnetParams = &chaincfg.TestNet3Params - BitcoinSignetParams = &chaincfg.SigNetParams + // chainIDToNetworkParams maps the Bitcoin chain ID to the network parameters + chainIDToNetworkParams = map[int64]*chaincfg.Params{ + BitcoinRegtest.ChainId: &chaincfg.RegressionNetParams, + BitcoinMainnet.ChainId: &chaincfg.MainNetParams, + BitcoinTestnet.ChainId: &chaincfg.TestNet3Params, + BitcoinSignetTestnet.ChainId: &chaincfg.SigNetParams, + } + + // networkNameToChainID maps the Bitcoin network name to the chain ID + networkNameToChainID = map[string]int64{ + chaincfg.RegressionNetParams.Name: BitcoinRegtest.ChainId, + chaincfg.MainNetParams.Name: BitcoinMainnet.ChainId, + chaincfg.TestNet3Params.Name: BitcoinTestnet.ChainId, + chaincfg.SigNetParams.Name: BitcoinSignetTestnet.ChainId, + } ) // BitcoinNetParamsFromChainID returns the bitcoin net params to be used from the chain id func BitcoinNetParamsFromChainID(chainID int64) (*chaincfg.Params, error) { - switch chainID { - case BitcoinRegtest.ChainId: - return BitcoinRegnetParams, nil - case BitcoinMainnet.ChainId: - return BitcoinMainnetParams, nil - case BitcoinTestnet.ChainId: - return BitcoinTestnetParams, nil - case BitcoinSignetTestnet.ChainId: - return BitcoinSignetParams, nil - default: - return nil, fmt.Errorf("no Bitcoin net params for chain ID: %d", chainID) + if params, found := chainIDToNetworkParams[chainID]; found { + return params, nil } + return nil, fmt.Errorf("no Bitcoin network params for chain ID: %d", chainID) } // BitcoinChainIDFromNetworkName returns the chain id for the given bitcoin network name func BitcoinChainIDFromNetworkName(name string) (int64, error) { - switch name { - case BitcoinRegnetParams.Name: - return BitcoinRegtest.ChainId, nil - case BitcoinMainnetParams.Name: - return BitcoinMainnet.ChainId, nil - case BitcoinTestnetParams.Name: - return BitcoinTestnet.ChainId, nil - case BitcoinSignetParams.Name: - return BitcoinSignetTestnet.ChainId, nil - default: - return 0, fmt.Errorf("invalid Bitcoin network name: %s", name) + if chainID, found := networkNameToChainID[name]; found { + return chainID, nil } + return 0, fmt.Errorf("invalid Bitcoin network name: %s", name) } // IsBitcoinRegnet returns true if the chain id is for the regnet diff --git a/pkg/chains/bitcoin_test.go b/pkg/chains/bitcoin_test.go index 841d3de748..ab344c7f95 100644 --- a/pkg/chains/bitcoin_test.go +++ b/pkg/chains/bitcoin_test.go @@ -14,10 +14,10 @@ func TestBitcoinNetParamsFromChainID(t *testing.T) { expected *chaincfg.Params wantErr bool }{ - {"Regnet", BitcoinRegtest.ChainId, BitcoinRegnetParams, false}, - {"Mainnet", BitcoinMainnet.ChainId, BitcoinMainnetParams, false}, - {"Testnet", BitcoinTestnet.ChainId, BitcoinTestnetParams, false}, - {"Signet", BitcoinSignetTestnet.ChainId, BitcoinSignetParams, false}, + {"Regnet", BitcoinRegtest.ChainId, &chaincfg.RegressionNetParams, false}, + {"Mainnet", BitcoinMainnet.ChainId, &chaincfg.MainNetParams, false}, + {"Testnet", BitcoinTestnet.ChainId, &chaincfg.TestNet3Params, false}, + {"Signet", BitcoinSignetTestnet.ChainId, &chaincfg.SigNetParams, false}, {"Unknown", -1, nil, true}, } @@ -26,9 +26,10 @@ func TestBitcoinNetParamsFromChainID(t *testing.T) { params, err := BitcoinNetParamsFromChainID(tt.chainID) if tt.wantErr { require.Error(t, err) + require.Nil(t, params) } else { require.NoError(t, err) - require.Equal(t, tt.expected, params) + require.EqualValues(t, tt.expected, params) } }) } @@ -41,10 +42,10 @@ func TestBitcoinChainIDFromNetParams(t *testing.T) { expectedChainID int64 wantErr bool }{ - {"Regnet", BitcoinRegnetParams.Name, BitcoinRegtest.ChainId, false}, - {"Mainnet", BitcoinMainnetParams.Name, BitcoinMainnet.ChainId, false}, - {"Testnet", BitcoinTestnetParams.Name, BitcoinTestnet.ChainId, false}, - {"Signet", BitcoinSignetParams.Name, BitcoinSignetTestnet.ChainId, false}, + {"Regnet", chaincfg.RegressionNetParams.Name, BitcoinRegtest.ChainId, false}, + {"Mainnet", chaincfg.MainNetParams.Name, BitcoinMainnet.ChainId, false}, + {"Testnet", chaincfg.TestNet3Params.Name, BitcoinTestnet.ChainId, false}, + {"Signet", chaincfg.SigNetParams.Name, BitcoinSignetTestnet.ChainId, false}, {"Unknown", "Unknown", 0, true}, } diff --git a/x/observer/keeper/grpc_query_tss.go b/x/observer/keeper/grpc_query_tss.go index 5ad108d06e..6874c6bade 100644 --- a/x/observer/keeper/grpc_query_tss.go +++ b/x/observer/keeper/grpc_query_tss.go @@ -4,6 +4,7 @@ import ( "context" "sort" + "github.com/btcsuite/btcd/chaincfg" sdk "github.com/cosmos/cosmos-sdk/types" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -54,7 +55,7 @@ func (k Keeper) GetTssAddress( if err != nil { return nil, status.Error(codes.Internal, err.Error()) } - bitcoinParams := chains.BitcoinRegnetParams + bitcoinParams := &chaincfg.RegressionNetParams if req.BitcoinChainId != 0 { bitcoinParams, err = chains.BitcoinNetParamsFromChainID(req.BitcoinChainId) if err != nil { @@ -88,7 +89,7 @@ func (k Keeper) GetTssAddressByFinalizedHeight( if err != nil { return nil, status.Error(codes.Internal, err.Error()) } - bitcoinParams := chains.BitcoinRegnetParams + bitcoinParams := &chaincfg.RegressionNetParams if req.BitcoinChainId != 0 { bitcoinParams, err = chains.BitcoinNetParamsFromChainID(req.BitcoinChainId) if err != nil {