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

Move checkpoint tag to params #356

Merged
merged 5 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 10 additions & 4 deletions app/ante_btc_validation_decorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ package app

import (
bbn "github.com/babylonchain/babylon/types"
btccheckpointkeeper "github.com/babylonchain/babylon/x/btccheckpoint/keeper"
btccheckpointtypes "github.com/babylonchain/babylon/x/btccheckpoint/types"
btclightclient "github.com/babylonchain/babylon/x/btclightclient/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type BtcValidationDecorator struct {
BtcCfg bbn.BtcConfig
BtcCfg bbn.BtcConfig
btccheckpointKeeper *btccheckpointkeeper.Keeper
}

func NewBtcValidationDecorator(cfg bbn.BtcConfig) BtcValidationDecorator {
func NewBtcValidationDecorator(
cfg bbn.BtcConfig,
k *btccheckpointkeeper.Keeper,
) BtcValidationDecorator {
return BtcValidationDecorator{
BtcCfg: cfg,
BtcCfg: cfg,
btccheckpointKeeper: k,
}
}

Expand All @@ -26,7 +32,7 @@ func (bvd BtcValidationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulat
switch msg := m.(type) {
case *btccheckpointtypes.MsgInsertBTCSpvProof:
powLimit := bvd.BtcCfg.PowLimit()
_, err := btccheckpointtypes.ParseSubmission(msg, &powLimit, bvd.BtcCfg.CheckpointTag())
_, err := btccheckpointtypes.ParseSubmission(msg, &powLimit, bvd.btccheckpointKeeper.GetExpectedTag(ctx))

if err != nil {
return ctx, btccheckpointtypes.ErrInvalidCheckpointProof.Wrap(err.Error())
Expand Down
3 changes: 1 addition & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,6 @@ func NewBabylonApp(
&btclightclientKeeper,
app.CheckpointingKeeper,
&powLimit,
btcConfig.CheckpointTag(),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
app.ZoneConciergeKeeper.SetBtcCheckpointKeeper(app.BtcCheckpointKeeper)
Expand Down Expand Up @@ -841,7 +840,7 @@ func NewBabylonApp(
anteHandler := sdk.ChainAnteDecorators(
NewWrappedAnteHandler(authAnteHandler),
epochingkeeper.NewDropValidatorMsgDecorator(app.EpochingKeeper),
NewBtcValidationDecorator(btcConfig),
NewBtcValidationDecorator(btcConfig, &app.BtcCheckpointKeeper),
)
app.SetAnteHandler(anteHandler)

Expand Down
5 changes: 0 additions & 5 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
errorsmod "cosmossdk.io/errors"
"github.com/babylonchain/babylon/app/params"
appparams "github.com/babylonchain/babylon/app/params"
txformat "github.com/babylonchain/babylon/btctxformatter"
bbn "github.com/babylonchain/babylon/types"
dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -606,10 +605,6 @@ func (ao EmptyAppOptions) Get(o string) interface{} {
return string(bbn.BtcSimnet)
}

if o == "btc-config.checkpoint-tag" {
return txformat.DefaultMainTagStr
}

return nil
}

Expand Down
25 changes: 0 additions & 25 deletions btctxformatter/bbnformatter.go

This file was deleted.

3 changes: 2 additions & 1 deletion btctxformatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func FuzzDecodingWontPanic(f *testing.F) {
f.Add(randNBytes(secondPartLength), uint8(rand.Intn(99)))

f.Fuzz(func(t *testing.T, bytes []byte, tagIdx uint8) {
decoded, err := IsBabylonCheckpointData(MainTag(tagIdx), CurrentVersion, bytes)
tag := []byte{0, 1, 2, 3}
decoded, err := IsBabylonCheckpointData(BabylonTag(tag), CurrentVersion, bytes)

if err == nil {
if decoded.Index != 0 && decoded.Index != 1 {
Expand Down
11 changes: 1 addition & 10 deletions cmd/babylond/cmd/custom_babylon_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"

txformat "github.com/babylonchain/babylon/btctxformatter"
bbn "github.com/babylonchain/babylon/types"
)

Expand All @@ -16,14 +15,11 @@ const (

type BtcConfig struct {
Network string `mapstructure:"network"`

CheckpointTag string `mapstructure:"checkpoint-tag"`
}

func defaultBabylonBtcConfig() BtcConfig {
return BtcConfig{
Network: string(bbn.BtcMainnet),
CheckpointTag: txformat.DefaultMainTagStr,
Network: string(bbn.BtcMainnet),
}
}

Expand Down Expand Up @@ -72,11 +68,6 @@ func DefaultBabylonTemplate() string {
# valid values are: [mainnet, testnet, simnet, regtest]
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. Must have exactly 4 bytes.
checkpoint-tag = "{{ .BtcConfig.CheckpointTag }}"

[signer-config]

# Configures which key that the BLS signer uses to sign BLS-sig transactions
Expand Down
6 changes: 6 additions & 0 deletions cmd/babylond/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

babylonApp "github.com/babylonchain/babylon/app"
btcctypes "github.com/babylonchain/babylon/x/btccheckpoint/types"
tmrand "github.com/cometbft/cometbft/libs/rand"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
Expand All @@ -14,6 +15,7 @@ const (
flagBtcConfirmationDepth = "btc-confirmation-depth"
flagEpochInterval = "epoch-interval"
flagBtcFinalizationTimeout = "btc-finalization-timeout"
flagCheckpointTag = "checkpoint-tag"
flagBaseBtcHeaderHex = "btc-base-header"
flagBaseBtcHeaderHeight = "btc-base-header-height"
flagInflationRateChange = "inflation-rate-change"
Expand All @@ -30,6 +32,7 @@ type GenesisCLIArgs struct {
MaxActiveValidators uint32
BtcConfirmationDepth uint64
BtcFinalizationTimeout uint64
CheckpointTag string
EpochInterval uint64
BaseBtcHeaderHex string
BaseBtcHeaderHeight uint64
Expand All @@ -49,6 +52,7 @@ func addGenesisFlags(cmd *cobra.Command) {
// btccheckpoint flags
cmd.Flags().Uint64(flagBtcConfirmationDepth, 6, "Confirmation depth for Bitcoin headers.")
cmd.Flags().Uint64(flagBtcFinalizationTimeout, 20, "Finalization timeout for Bitcoin headers.")
cmd.Flags().String(flagCheckpointTag, btcctypes.DefaultCheckpointTag, "Hex encoded tag for babylon checkpoint on btc")
// epoch args
cmd.Flags().Uint64(flagEpochInterval, 400, "Number of blocks between epochs. Must be more than 0.")
// btclightclient args
Expand All @@ -69,6 +73,7 @@ func parseGenesisFlags(cmd *cobra.Command) *GenesisCLIArgs {
maxActiveValidators, _ := cmd.Flags().GetUint32(flagMaxActiveValidators)
btcConfirmationDepth, _ := cmd.Flags().GetUint64(flagBtcConfirmationDepth)
btcFinalizationTimeout, _ := cmd.Flags().GetUint64(flagBtcFinalizationTimeout)
checkpointTag, _ := cmd.Flags().GetString(flagCheckpointTag)
epochInterval, _ := cmd.Flags().GetUint64(flagEpochInterval)
baseBtcHeaderHex, _ := cmd.Flags().GetString(flagBaseBtcHeaderHex)
baseBtcHeaderHeight, _ := cmd.Flags().GetUint64(flagBaseBtcHeaderHeight)
Expand All @@ -91,6 +96,7 @@ func parseGenesisFlags(cmd *cobra.Command) *GenesisCLIArgs {
MaxActiveValidators: maxActiveValidators,
BtcConfirmationDepth: btcConfirmationDepth,
BtcFinalizationTimeout: btcFinalizationTimeout,
CheckpointTag: checkpointTag,
EpochInterval: epochInterval,
BaseBtcHeaderHeight: baseBtcHeaderHeight,
BaseBtcHeaderHex: baseBtcHeaderHex,
Expand Down
9 changes: 7 additions & 2 deletions cmd/babylond/cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Example:
var genesisParams GenesisParams
if network == "testnet" {
genesisParams = TestnetGenesisParams(genesisCliArgs.MaxActiveValidators,
genesisCliArgs.BtcConfirmationDepth, genesisCliArgs.BtcFinalizationTimeout,
genesisCliArgs.BtcConfirmationDepth, genesisCliArgs.BtcFinalizationTimeout, genesisCliArgs.CheckpointTag,
genesisCliArgs.EpochInterval, genesisCliArgs.BaseBtcHeaderHex,
genesisCliArgs.BaseBtcHeaderHeight, genesisCliArgs.InflationRateChange,
genesisCliArgs.InflationMin, genesisCliArgs.InflationMax, genesisCliArgs.GoalBonded,
Expand Down Expand Up @@ -211,7 +211,7 @@ type GenesisParams struct {
}

func TestnetGenesisParams(maxActiveValidators uint32, btcConfirmationDepth uint64,
btcFinalizationTimeout uint64, epochInterval uint64, baseBtcHeaderHex string,
btcFinalizationTimeout uint64, checkpointTag string, epochInterval uint64, baseBtcHeaderHex string,
baseBtcHeaderHeight uint64, inflationRateChange float64,
inflationMin float64, inflationMax float64, goalBonded float64,
blocksPerYear uint64, genesisTime time.Time, blockGasLimit int64) GenesisParams {
Expand Down Expand Up @@ -270,6 +270,11 @@ func TestnetGenesisParams(maxActiveValidators uint32, btcConfirmationDepth uint6
genParams.BtccheckpointParams = btccheckpointtypes.DefaultParams()
genParams.BtccheckpointParams.BtcConfirmationDepth = btcConfirmationDepth
genParams.BtccheckpointParams.CheckpointFinalizationTimeout = btcFinalizationTimeout
genParams.BtccheckpointParams.CheckpointTag = checkpointTag

if err := genParams.BtccheckpointParams.Validate(); err != nil {
panic(err)
}

// set the base BTC header in the genesis state
baseBtcHeader, err := bbn.NewBTCHeaderBytesFromHex(baseBtcHeaderHex)
Expand Down
10 changes: 2 additions & 8 deletions cmd/babylond/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"path/filepath"

appparams "github.com/babylonchain/babylon/app/params"
txformat "github.com/babylonchain/babylon/btctxformatter"
bbn "github.com/babylonchain/babylon/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"

Expand Down Expand Up @@ -53,7 +52,6 @@ var (
flagNodeDaemonHome = "node-daemon-home"
flagStartingIPAddress = "starting-ip-address"
flagBtcNetwork = "btc-network"
flagBtcCheckpointTag = "btc-checkpoint-tag"
flagAdditionalSenderAccount = "additional-sender-account"
)

Expand Down Expand Up @@ -89,14 +87,13 @@ Example:
numValidators, _ := cmd.Flags().GetInt(flagNumValidators)
algo, _ := cmd.Flags().GetString(flags.FlagKeyAlgorithm)
btcNetwork, _ := cmd.Flags().GetString(flagBtcNetwork)
btcCheckpointTag, _ := cmd.Flags().GetString(flagBtcCheckpointTag)
additionalAccount, _ := cmd.Flags().GetBool(flagAdditionalSenderAccount)
if err != nil {
return errors.New("base Bitcoin header height should be a uint64")
}

genesisParams := TestnetGenesisParams(genesisCliArgs.MaxActiveValidators,
genesisCliArgs.BtcConfirmationDepth, genesisCliArgs.BtcFinalizationTimeout,
genesisCliArgs.BtcConfirmationDepth, genesisCliArgs.BtcFinalizationTimeout, genesisCliArgs.CheckpointTag,
genesisCliArgs.EpochInterval, genesisCliArgs.BaseBtcHeaderHex,
genesisCliArgs.BaseBtcHeaderHeight, genesisCliArgs.InflationRateChange,
genesisCliArgs.InflationMin, genesisCliArgs.InflationMax, genesisCliArgs.GoalBonded,
Expand All @@ -105,7 +102,7 @@ Example:
return InitTestnet(
clientCtx, cmd, config, mbm, genBalIterator, outputDir, genesisCliArgs.ChainID, minGasPrices,
nodeDirPrefix, nodeDaemonHome, startingIPAddress, keyringBackend, algo, numValidators,
btcNetwork, btcCheckpointTag, additionalAccount, genesisParams,
btcNetwork, additionalAccount, genesisParams,
)
},
}
Expand All @@ -119,7 +116,6 @@ Example:
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|test)")
cmd.Flags().String(flags.FlagKeyAlgorithm, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for")
cmd.Flags().String(flagBtcNetwork, string(bbn.BtcSimnet), "Bitcoin network to use. Available networks: simnet, testnet, regtest, mainnet")
cmd.Flags().String(flagBtcCheckpointTag, string(txformat.DefaultTestTagStr), "Tag to use for Bitcoin checkpoints.")
cmd.Flags().Bool(flagAdditionalSenderAccount, false, "If there should be additional pre funded account per validator")
addGenesisFlags(cmd)

Expand All @@ -145,7 +141,6 @@ func InitTestnet(
algoStr string,
numValidators int,
btcNetwork string,
btcCheckpointTag string,
additionalAccount bool,
genesisParams GenesisParams,
) error {
Expand All @@ -162,7 +157,6 @@ func InitTestnet(
babylonConfig.Telemetry.GlobalLabels = [][]string{{"chain_id", chainID}}
// BTC related config. Default values "simnet" and "BBT1"
babylonConfig.BtcConfig.Network = btcNetwork
babylonConfig.BtcConfig.CheckpointTag = btcCheckpointTag
// Explorer related config. Allow CORS connections.
babylonConfig.API.EnableUnsafeCORS = true
babylonConfig.GRPC.Address = "0.0.0.0:9090"
Expand Down
5 changes: 5 additions & 0 deletions proto/babylon/btccheckpoint/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ message Params {
// has dishonest majority and is stalling checkpoints (w in research paper)
uint64 checkpoint_finalization_timeout = 2
[ (gogoproto.moretags) = "yaml:\"checkpoint_finalization_timeout\"" ];

// 4byte tag in hex format, required to be present in the OP_RETURN transaction
// related to babylon
string checkpoint_tag = 3
[ (gogoproto.moretags) = "yaml:\"checkpoint_tag\"" ];
}
3 changes: 2 additions & 1 deletion test/e2e/configurer/chain/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ func (n *NodeConfig) FinalizeSealedEpochs(startingEpoch uint64, lastEpoch uint64

require.NoError(n.t, err)

babylonTagBytes, _ := hex.DecodeString(initialization.BabylonOpReturnTag)
p1, p2, err := txformat.EncodeCheckpointData(
txformat.BabylonTag(initialization.BabylonOpReturnTag),
txformat.BabylonTag(babylonTagBytes),
txformat.CurrentVersion,
btcCheckpoint,
)
Expand Down
10 changes: 6 additions & 4 deletions test/e2e/initialization/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ type NodeConfig struct {

const (
// common
BabylonDenom = "ubbn"
MinGasPrice = "0.000"
ValidatorWalletName = "val"
BabylonOpReturnTag = "bbni"
BabylonDenom = "ubbn"
MinGasPrice = "0.000"
ValidatorWalletName = "val"
BabylonOpReturnTag = "01020304"

BabylonBtcConfirmationPeriod = 2
BabylonBtcFinalizationPeriod = 4
// chainA
Expand Down Expand Up @@ -308,6 +309,7 @@ func updateBtccheckpointGenesis(btccheckpointGenState *btccheckpointtypes.Genesi
btccheckpointGenState.Params = btccheckpointtypes.DefaultParams()
btccheckpointGenState.Params.BtcConfirmationDepth = BabylonBtcConfirmationPeriod
btccheckpointGenState.Params.CheckpointFinalizationTimeout = BabylonBtcFinalizationPeriod
btccheckpointGenState.Params.CheckpointTag = BabylonOpReturnTag
}

func updateGenUtilGenesis(c *internalChain) func(*genutiltypes.GenesisState) {
Expand Down
1 change: 0 additions & 1 deletion test/e2e/initialization/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ func (n *internalNode) createAppConfig(nodeConfig *NodeConfig) {
appConfig.StateSync.SnapshotKeepRecent = nodeConfig.SnapshotKeepRecent
appConfig.SignerConfig.KeyName = ValidatorWalletName
appConfig.BtcConfig.Network = string(bbn.BtcSimnet)
appConfig.BtcConfig.CheckpointTag = BabylonOpReturnTag
appConfig.GRPC.Enable = true
appConfig.GRPC.Address = "0.0.0.0:9090"

Expand Down
6 changes: 5 additions & 1 deletion test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package babylon_integration

import (
"context"
"encoding/hex"
"errors"
"fmt"
"os"
Expand All @@ -15,6 +16,7 @@ import (
txformat "github.com/babylonchain/babylon/btctxformatter"
"github.com/babylonchain/babylon/testutil/datagen"
bbn "github.com/babylonchain/babylon/types"
btcctypes "github.com/babylonchain/babylon/x/btccheckpoint/types"
lightclient "github.com/babylonchain/babylon/x/btclightclient/types"
checkpointingtypes "github.com/babylonchain/babylon/x/checkpointing/types"
epochingtypes "github.com/babylonchain/babylon/x/epoching/types"
Expand Down Expand Up @@ -291,8 +293,10 @@ func TestSubmitCheckpoint(t *testing.T) {
t.Fatalf("Could not create raw btc checkpoint from raw chekpoint")
}

tagAsBytes, _ := hex.DecodeString(btcctypes.DefaultCheckpointTag)

p1, p2 := txformat.MustEncodeCheckpointData(
txformat.BabylonTag(txformat.DefaultTestTagStr),
txformat.BabylonTag(tagAsBytes),
txformat.CurrentVersion,
rawBtcCheckpoint,
)
Expand Down
Loading