Skip to content

Commit

Permalink
replace switch cases with declarative statements for better maintaina…
Browse files Browse the repository at this point in the history
…bility
  • Loading branch information
ws4charlie committed Sep 24, 2024
1 parent 4a648b2 commit 3be79ad
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 38 deletions.
3 changes: 2 additions & 1 deletion e2e/runner/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
47 changes: 21 additions & 26 deletions pkg/chains/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions pkg/chains/bitcoin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

Expand All @@ -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)
}
})
}
Expand All @@ -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},
}

Expand Down
5 changes: 3 additions & 2 deletions x/observer/keeper/grpc_query_tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3be79ad

Please sign in to comment.