Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zetacore): add chain static information for bitcoin signet testnet #2883

Merged
merged 8 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
* [2883](https://github.com/zeta-chain/node/pull/2883) - add chain static information for btc signet testnet

### Refactor

Expand Down
16 changes: 10 additions & 6 deletions pkg/chains/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,14 @@ func GetChainFromChainID(chainID int64, additionalChains []Chain) (Chain, bool)
// GetBTCChainParams returns the bitcoin chain config params from the chain ID
func GetBTCChainParams(chainID int64) (*chaincfg.Params, error) {
switch chainID {
case 18444:
case BitcoinRegtest.ChainId:
return &chaincfg.RegressionNetParams, nil
case 18332:
case BitcoinTestnet.ChainId:
return &chaincfg.TestNet3Params, nil
case 8332:
case BitcoinMainnet.ChainId:
return &chaincfg.MainNetParams, nil
case BitcoinSignetTestnet.ChainId:
return &chaincfg.SigNetParams, nil
default:
return nil, fmt.Errorf("error chainID %d is not a bitcoin chain", chainID)
}
Expand All @@ -182,11 +184,13 @@ func GetBTCChainParams(chainID int64) (*chaincfg.Params, error) {
func GetBTCChainIDFromChainParams(params *chaincfg.Params) (int64, error) {
switch params.Name {
case chaincfg.RegressionNetParams.Name:
return 18444, nil
return BitcoinRegtest.ChainId, nil
case chaincfg.TestNet3Params.Name:
return 18332, nil
return BitcoinTestnet.ChainId, nil
case chaincfg.MainNetParams.Name:
return 8332, nil
return BitcoinMainnet.ChainId, nil
case chaincfg.SigNetParams.Name:
return BitcoinSignetTestnet.ChainId, nil
default:
return 0, fmt.Errorf("error chain %s is not a bitcoin chain", params.Name)
}
Expand Down
113 changes: 94 additions & 19 deletions pkg/chains/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ func TestChain_IsBitcoinChain(t *testing.T) {
{"Bitcoin Mainnet", chains.BitcoinMainnet, true},
{"Bitcoin Testnet", chains.BitcoinTestnet, true},
{"Bitcoin Regtest", chains.BitcoinRegtest, true},
{"Bitcoin Signet Testnet", chains.BitcoinSignetTestnet, true},
{"Non-Bitcoin", chains.Ethereum, false},
{"Zeta Mainnet", chains.ZetaChainMainnet, false},
}
Expand Down Expand Up @@ -350,6 +351,7 @@ func TestIsBitcoinChain(t *testing.T) {
{"Bitcoin Mainnet", chains.BitcoinMainnet.ChainId, true},
{"Bitcoin Testnet", chains.BitcoinTestnet.ChainId, true},
{"Bitcoin Regtest", chains.BitcoinRegtest.ChainId, true},
{"Bitcoin Signet Testnet", chains.BitcoinSignetTestnet.ChainId, true},
{"Non-Bitcoin", chains.Ethereum.ChainId, false},
{"Zeta Mainnet", chains.ZetaChainMainnet.ChainId, false},
}
Expand Down Expand Up @@ -405,29 +407,102 @@ func TestGetChainFromChainID(t *testing.T) {
}

func TestGetBTCChainParams(t *testing.T) {
params, err := chains.GetBTCChainParams(chains.BitcoinMainnet.ChainId)
require.NoError(t, err)
require.Equal(t, &chaincfg.MainNetParams, params)
tt := []struct {
name string
chainID int64
expectedParams *chaincfg.Params
expectedError require.ErrorAssertionFunc
}{
{
name: "Bitcoin Mainnet",
chainID: chains.BitcoinMainnet.ChainId,
expectedParams: &chaincfg.MainNetParams,
expectedError: require.NoError,
},
{
name: "Bitcoin Testnet",
chainID: chains.BitcoinTestnet.ChainId,
expectedParams: &chaincfg.TestNet3Params,
expectedError: require.NoError,
},
{
name: "Bitcoin Regtest",
chainID: chains.BitcoinRegtest.ChainId,
expectedParams: &chaincfg.RegressionNetParams,
expectedError: require.NoError,
},
{
name: "Bitcoin Signet Testnet",
chainID: chains.BitcoinSignetTestnet.ChainId,
expectedParams: &chaincfg.SigNetParams,
expectedError: require.NoError,
},
{
name: "Unknown Chain",
chainID: 9999,
expectedParams: nil,
expectedError: func(t require.TestingT, err error, i ...interface{}) {
require.ErrorContains(t, err, "error chainID 9999 is not a bitcoin chain")
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
params, err := chains.GetBTCChainParams(tc.chainID)
tc.expectedError(t, err)
require.Equal(t, tc.expectedParams, params)
})
}

_, err = chains.GetBTCChainParams(9999)
require.Error(t, err)
}

func TestGetBTCChainIDFromChainParams(t *testing.T) {
chainID, err := chains.GetBTCChainIDFromChainParams(&chaincfg.MainNetParams)
require.NoError(t, err)
require.Equal(t, int64(8332), chainID)

chainID, err = chains.GetBTCChainIDFromChainParams(&chaincfg.RegressionNetParams)
require.NoError(t, err)
require.Equal(t, int64(18444), chainID)

chainID, err = chains.GetBTCChainIDFromChainParams(&chaincfg.TestNet3Params)
require.NoError(t, err)
require.Equal(t, int64(18332), chainID)

_, err = chains.GetBTCChainIDFromChainParams(&chaincfg.Params{Name: "unknown"})
require.Error(t, err)
tt := []struct {
name string
params *chaincfg.Params
expectedChainID int64
expectedError require.ErrorAssertionFunc
}{
{
name: "Bitcoin Mainnet",
params: &chaincfg.MainNetParams,
expectedChainID: chains.BitcoinMainnet.ChainId,
expectedError: require.NoError,
},
{
name: "Bitcoin Testnet",
params: &chaincfg.TestNet3Params,
expectedChainID: chains.BitcoinTestnet.ChainId,
expectedError: require.NoError,
},
{
name: "Bitcoin Regtest",
params: &chaincfg.RegressionNetParams,
expectedChainID: chains.BitcoinRegtest.ChainId,
expectedError: require.NoError,
},
{
name: "Bitcoin Signet Testnet",
params: &chaincfg.SigNetParams,
expectedChainID: chains.BitcoinSignetTestnet.ChainId,
expectedError: require.NoError,
},
{
name: "Unknown Chain",
params: &chaincfg.Params{Name: "unknown"},
expectedChainID: 0,
expectedError: func(t require.TestingT, err error, i ...interface{}) {
require.ErrorContains(t, err, "error chain unknown is not a bitcoin chain")
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
chainID, err := chains.GetBTCChainIDFromChainParams(tc.params)
tc.expectedError(t, err)
require.Equal(t, tc.expectedChainID, chainID)
})
}
}

func TestChainIDInChainList(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions pkg/chains/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ var (
Name: "btc_testnet",
}

BitcoinSignetTestnet = Chain{
ChainName: ChainName_btc_signet_testnet,
ChainId: 001,
Network: Network_btc,
NetworkType: NetworkType_testnet,
Vm: Vm_no_vm,
Consensus: Consensus_bitcoin,
IsExternal: true,
CctxGateway: CCTXGateway_observers,
Name: "btc_signet_testnet",
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
}

// Amoy is Polygon amoy testnet
Amoy = Chain{
ChainName: ChainName_amoy_testnet,
Expand Down
Loading
Loading