Skip to content

Commit

Permalink
Add babylon app config
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradStaniec committed Aug 28, 2022
1 parent c18ea46 commit d516e71
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 59 deletions.
11 changes: 8 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"path/filepath"

"github.com/btcsuite/btcd/chaincfg"
bbn "github.com/babylonchain/babylon/types"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/spf13/cast"
Expand Down Expand Up @@ -217,6 +217,11 @@ func NewBabylonApp(
homePath string, invCheckPeriod uint, encodingConfig appparams.EncodingConfig,
appOpts servertypes.AppOptions, baseAppOptions ...func(*baseapp.BaseApp),
) *BabylonApp {
btcConfig := bbn.ParseBtcOptionsFromConfig(appOpts)
powLimit := btcConfig.PowLimit()
// WARNING: We are initiating global babylon btc config as first so other modules
// can use it from start
bbn.InitGlobalBtcConfig(btcConfig)

appCodec := encodingConfig.Marshaler
legacyAmino := encodingConfig.Amino
Expand Down Expand Up @@ -359,8 +364,8 @@ func NewBabylonApp(
// from some global config
6,
10,
// TODO take from config
chaincfg.MainNetParams.PowLimit,
&powLimit,
btcConfig.CheckpointTag(),
)

app.BTCLightClientKeeper = *btclightclientKeeper.SetHooks(
Expand Down
28 changes: 19 additions & 9 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"

txformat "github.com/babylonchain/babylon/btctxformatter"
bbn "github.com/babylonchain/babylon/types"
bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdksimapp "github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -31,6 +25,12 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"
)

// DefaultConsensusParams defines the default Tendermint consensus params used in
Expand All @@ -55,7 +55,7 @@ var DefaultConsensusParams = &abci.ConsensusParams{
func setup(withGenesis bool, invCheckPeriod uint) (*BabylonApp, GenesisState) {
db := dbm.NewMemDB()
encCdc := MakeTestEncodingConfig()
app := NewBabylonApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, invCheckPeriod, encCdc, sdksimapp.EmptyAppOptions{})
app := NewBabylonApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, invCheckPeriod, encCdc, EmptyAppOptions{})
if withGenesis {
return app, NewDefaultGenesisState(encCdc.Marshaler)
}
Expand Down Expand Up @@ -432,6 +432,16 @@ type EmptyAppOptions struct{}

// Get implements AppOptions
func (ao EmptyAppOptions) Get(o string) interface{} {
// some defaults required for app.toml config

if o == "btc-config.network" {
return string(bbn.BtcSimnet)
}

if o == "btc-config.checkpoint-tag" {
return string(txformat.MainTag)
}

return nil
}

Expand Down
55 changes: 55 additions & 0 deletions cmd/babylond/cmd/custom_babylon_config.go
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 }}"
`
}
16 changes: 4 additions & 12 deletions cmd/babylond/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path/filepath"

serverconfig "github.com/cosmos/cosmos-sdk/server/config"
"github.com/spf13/cast"
"github.com/spf13/cobra"
tmcli "github.com/tendermint/tendermint/libs/cli"
Expand Down Expand Up @@ -86,13 +85,10 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
func initAppConfig() (string, interface{}) {
// The following code snippet is just for reference.

type CustomAppConfig struct {
serverconfig.Config
}

// Optionally allow the chain developer to overwrite the SDK's default
// server config.
srvCfg := serverconfig.DefaultConfig()
babylonConfig := DefaultBabylonConfig()
babylonTemplate := DefaultBabylonTemplate()
// The SDK's default minimum gas price is set to "" (empty value) inside
// app.toml. If left empty by validators, the node will halt on startup.
// However, the chain developer can set a default app.toml value for their
Expand All @@ -105,13 +101,9 @@ func initAppConfig() (string, interface{}) {
// own app.toml to override, or use this default value.
//
// In app, we set the min gas prices to 0.
srvCfg.MinGasPrices = "0stake"

customAppConfig := CustomAppConfig{
Config: *srvCfg,
}
babylonConfig.MinGasPrices = "0stake"

return serverconfig.DefaultConfigTemplate, customAppConfig
return babylonTemplate, babylonConfig
}

func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
Expand Down
14 changes: 10 additions & 4 deletions cmd/babylond/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (
"bufio"
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"

"github.com/babylonchain/babylon/privval"
"github.com/babylonchain/babylon/testutil/datagen"
bbn "github.com/babylonchain/babylon/types"
checkpointingtypes "github.com/babylonchain/babylon/x/checkpointing/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"net"
"os"
"path/filepath"

"github.com/spf13/cobra"
tmconfig "github.com/tendermint/tendermint/config"
Expand Down Expand Up @@ -126,13 +128,15 @@ func InitTestnet(
nodeIDs := make([]string, numValidators)
valKeys := make([]*privval.ValidatorKeys, numValidators)

babylonConfig := srvconfig.DefaultConfig()
babylonConfig := DefaultBabylonConfig()
babylonConfig.MinGasPrices = minGasPrices
babylonConfig.API.Enable = true
babylonConfig.Telemetry.Enabled = true
babylonConfig.Telemetry.PrometheusRetentionTime = 60
babylonConfig.Telemetry.EnableHostnameLabel = false
babylonConfig.Telemetry.GlobalLabels = [][]string{{"chain_id", chainID}}
// it makes sense in integration tests to use simnet due to lower difficulty
babylonConfig.BtcConfig.Network = string(bbn.BtcSimnet)

var (
genAccounts []authtypes.GenesisAccount
Expand Down Expand Up @@ -266,6 +270,8 @@ func InitTestnet(
return err
}

customTemplate := DefaultBabylonTemplate()
srvconfig.SetConfigTemplate(customTemplate)
srvconfig.WriteConfigFile(filepath.Join(nodeDir, "config/app.toml"), babylonConfig)
}

Expand Down
3 changes: 3 additions & 0 deletions testutil/keeper/btccheckpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"math/big"

txformat "github.com/babylonchain/babylon/btctxformatter"
"github.com/babylonchain/babylon/x/btccheckpoint/keeper"
btcctypes "github.com/babylonchain/babylon/x/btccheckpoint/types"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -55,6 +56,8 @@ func NewBTCCheckpointKeeper(
kDeep,
wDeep,
powLimit,
// use MainTag tests
txformat.MainTag,
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
114 changes: 114 additions & 0 deletions types/btc_config.go
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
}
Loading

0 comments on commit d516e71

Please sign in to comment.