Skip to content

Commit

Permalink
keep BitcoinConfig to be backward compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Sep 13, 2024
1 parent 1d5bcf1 commit b1ee2eb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* [2784](https://github.com/zeta-chain/node/pull/2784) - staking precompiled contract
* [2795](https://github.com/zeta-chain/node/pull/2795) - support restricted address in Solana
* [2861](https://github.com/zeta-chain/node/pull/2861) - emit events from staking precompile
* [2870](https://github.com/zeta-chain/node/pull/2870) - support for multiple Bitcoin chains in the zetaclient

### Refactor

Expand Down
3 changes: 3 additions & 0 deletions cmd/zetaclientd/start_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func maskCfg(cfg config.Config) string {
RPCParams: val.RPCParams,
}
}
maskedCfg.BitcoinConfig = config.BTCConfig{
RPCParams: cfg.BitcoinConfig.RPCParams,
}

// Mask Solana endpoint
maskedCfg.SolanaConfig.Endpoint = ""
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/config/config_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func New(setDefaults bool) Config {
// bitcoinConfigRegnet contains Bitcoin config for regnet
func bitcoinConfigRegnet() BTCConfig {
return BTCConfig{
RPCUsername: "smoketest", // smoketest is the previous name for E2E test, we keep this name for compatibility between client versions in upgrade test
RPCUsername: "e2etest",
RPCPassword: "123",
RPCHost: "bitcoin:18443",
RPCParams: "regtest",
Expand Down
12 changes: 10 additions & 2 deletions zetaclient/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ type Config struct {
// chain configs
EVMChainConfigs map[int64]EVMConfig `json:"EVMChainConfigs"`
BTCChainConfigs map[int64]BTCConfig `json:"BTCChainConfigs"`
SolanaConfig SolanaConfig `json:"SolanaConfig"`
// Deprecated: the 'BitcoinConfig' will be removed once the 'BTCChainConfigs' is fully adopted
BitcoinConfig BTCConfig `json:"BitcoinConfig"`
SolanaConfig SolanaConfig `json:"SolanaConfig"`

// compliance config
ComplianceConfig ComplianceConfig `json:"ComplianceConfig"`
Expand Down Expand Up @@ -124,7 +126,13 @@ func (c Config) GetBTCConfig(chainID int64) (BTCConfig, bool) {
c.mu.RLock()
defer c.mu.RUnlock()

btcCfg := c.BTCChainConfigs[chainID]
// we prefer 'BTCChainConfigs' over 'BitcoinConfig' but still fallback to be backward compatible
// this will allow new 'zetaclientd' binary to work with old config file
btcCfg, found := c.BTCChainConfigs[chainID]
if !found || btcCfg.Empty() {
btcCfg = c.BitcoinConfig
}

return btcCfg, !btcCfg.Empty()
}

Expand Down
44 changes: 30 additions & 14 deletions zetaclient/config/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,42 @@ func Test_GetEVMConfig(t *testing.T) {

func Test_GetBTCConfig(t *testing.T) {
tests := []struct {
name string
chainID int64
chainCfg config.BTCConfig
want bool
name string
chainID int64
oldCfg config.BTCConfig
btcCfg *config.BTCConfig
want bool
}{
{
name: "should find non-empty btc config",
chainID: chains.BitcoinRegtest.ChainId,
chainCfg: config.BTCConfig{
RPCUsername: "",
RPCPassword: "",
RPCHost: "localhost",
RPCParams: "",
btcCfg: &config.BTCConfig{
RPCHost: "localhost",
},
want: true,
},
{
name: "should not find btc config if rpc host is empty",
name: "should fallback to old 'BitcoinConfig' if new config is not set",
chainID: chains.BitcoinRegtest.ChainId,
chainCfg: config.BTCConfig{
oldCfg: config.BTCConfig{
RPCHost: "old_host",
},
btcCfg: nil, // new config is not set
want: true,
},
{
name: "should fallback to old config but still can't find btc config as it's empty",
chainID: chains.BitcoinRegtest.ChainId,
oldCfg: config.BTCConfig{
RPCUsername: "user",
RPCPassword: "pass",
RPCHost: "", // empty config
RPCParams: "regtest",
},
btcCfg: &config.BTCConfig{
RPCUsername: "user",
RPCPassword: "pass",
RPCHost: "",
RPCHost: "", // empty config
RPCParams: "regtest",
},
want: false,
Expand All @@ -97,8 +110,11 @@ func Test_GetBTCConfig(t *testing.T) {
// create config with defaults
cfg := config.New(true)

// set chain config
cfg.BTCChainConfigs[tt.chainID] = tt.chainCfg
// set both new and old btc config
cfg.BitcoinConfig = tt.oldCfg
if tt.btcCfg != nil {
cfg.BTCChainConfigs[tt.chainID] = *tt.btcCfg
}

// should return btc config
btcCfg, found := cfg.GetBTCConfig(tt.chainID)
Expand Down

0 comments on commit b1ee2eb

Please sign in to comment.