-
Notifications
You must be signed in to change notification settings - Fork 170
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
1 parent
c18ea46
commit d516e71
Showing
11 changed files
with
261 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cmd | ||
|
||
import ( | ||
txformat "github.com/babylonchain/babylon/btctxformatter" | ||
bbn "github.com/babylonchain/babylon/types" | ||
serverconfig "github.com/cosmos/cosmos-sdk/server/config" | ||
) | ||
|
||
type BtcConfig struct { | ||
Network string `mapstructure:"network"` | ||
|
||
CheckpointTag string `mapstructure:"checkpoint-tag"` | ||
} | ||
|
||
func defaultBabylonBtcConfig() BtcConfig { | ||
return BtcConfig{ | ||
Network: string(bbn.BtcMainnet), | ||
CheckpointTag: string(txformat.MainTag), | ||
} | ||
} | ||
|
||
type BabylonAppConfig struct { | ||
serverconfig.Config `mapstructure:",squash"` | ||
|
||
BtcConfig BtcConfig `mapstructure:"btc-config"` | ||
} | ||
|
||
func DefaultBabylonConfig() *BabylonAppConfig { | ||
return &BabylonAppConfig{ | ||
Config: *serverconfig.DefaultConfig(), | ||
BtcConfig: defaultBabylonBtcConfig(), | ||
} | ||
} | ||
|
||
func DefaultBabylonTemplate() string { | ||
return serverconfig.DefaultConfigTemplate + ` | ||
############################################################################### | ||
### Babylon Bitcoin configuration ### | ||
############################################################################### | ||
[btc-config] | ||
# Configures which bitcoin network should be used for checkpointing | ||
# valid values are: [mainnet, testnet, simnet] | ||
network = "{{ .BtcConfig.Network }}" | ||
# Configures what tag should be prepended to op_return data in btc transaction | ||
# for it to be considered as valid babylon checkpoint | ||
# valid values are: | ||
# "BBT" for testing | ||
# "BBN" for production usage | ||
checkpoint-tag = "{{ .BtcConfig.CheckpointTag }}" | ||
` | ||
} |
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,114 @@ | ||
package types | ||
|
||
import ( | ||
"math/big" | ||
"sync" | ||
|
||
txformat "github.com/babylonchain/babylon/btctxformatter" | ||
"github.com/btcsuite/btcd/chaincfg" | ||
servertypes "github.com/cosmos/cosmos-sdk/server/types" | ||
"github.com/spf13/cast" | ||
) | ||
|
||
// Global bitcoin configuration | ||
// It is global, as we are validating few things in stateless checkTx, which | ||
// are dependent on the btc network we are using | ||
var ( | ||
btcConfig *BtcConfig | ||
initConfig sync.Once | ||
) | ||
|
||
type SupportedBtcNetwork string | ||
|
||
type BtcConfig struct { | ||
powLimit *big.Int | ||
checkPointTag txformat.BabylonTag | ||
} | ||
|
||
const ( | ||
BtcMainnet SupportedBtcNetwork = "mainnet" | ||
BtcTestnet SupportedBtcNetwork = "testnet" | ||
BtcSimnet SupportedBtcNetwork = "simnet" | ||
) | ||
|
||
func parsePowLimit(opts servertypes.AppOptions) *big.Int { | ||
valueInterface := opts.Get("btc-config.network") | ||
|
||
if valueInterface == nil { | ||
panic("Bitcoin network should be provided in options") | ||
} | ||
|
||
network, err := cast.ToStringE(valueInterface) | ||
|
||
if err != nil { | ||
panic("Btcoin netowrk config should be valid string") | ||
} | ||
|
||
if network == string(BtcMainnet) { | ||
return chaincfg.MainNetParams.PowLimit | ||
} else if network == string(BtcTestnet) { | ||
return chaincfg.TestNet3Params.PowLimit | ||
} else if network == string(BtcSimnet) { | ||
return chaincfg.SimNetParams.PowLimit | ||
} else { | ||
panic("Bicoin network should be one of [mainet, testnet, simnet]") | ||
} | ||
} | ||
|
||
func parseCheckpointTag(opts servertypes.AppOptions) txformat.BabylonTag { | ||
valueInterface := opts.Get("btc-config.checkpoint-tag") | ||
|
||
if valueInterface == nil { | ||
panic("Bitcoin network should be provided in options") | ||
} | ||
|
||
tag, err := cast.ToStringE(valueInterface) | ||
|
||
if err != nil { | ||
panic("checkpoint-tag should be valid string") | ||
} | ||
|
||
if tag == string(txformat.MainTag) { | ||
return txformat.MainTag | ||
} else if tag == string(txformat.TestTag) { | ||
return txformat.TestTag | ||
} else { | ||
panic("tag should be one of [BBN, BBT]") | ||
} | ||
|
||
} | ||
|
||
func ParseBtcOptionsFromConfig(opts servertypes.AppOptions) BtcConfig { | ||
powLimit := parsePowLimit(opts) | ||
tag := parseCheckpointTag(opts) | ||
return BtcConfig{ | ||
powLimit: powLimit, | ||
checkPointTag: tag, | ||
} | ||
} | ||
|
||
func InitGlobalBtcConfig(c BtcConfig) { | ||
initConfig.Do(func() { | ||
btcConfig = &c | ||
}) | ||
} | ||
|
||
func (c *BtcConfig) PowLimit() big.Int { | ||
return *c.powLimit | ||
} | ||
|
||
func (c *BtcConfig) CheckpointTag() txformat.BabylonTag { | ||
return c.checkPointTag | ||
} | ||
|
||
func GetGlobalPowLimit() big.Int { | ||
// We are making copy of pow limit to avoid anyone changing globally configured | ||
// powlimit. If it start slowing things down, due to multiple copies needed to | ||
// be garbage collected, we will need to think of other way of protecting global | ||
// state | ||
return btcConfig.PowLimit() | ||
} | ||
|
||
func GetGlobalCheckPointTag() txformat.BabylonTag { | ||
return btcConfig.checkPointTag | ||
} |
Oops, something went wrong.