Skip to content

Commit

Permalink
Add commit randomness (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry authored Jul 19, 2023
1 parent cbd47c2 commit c7d9a28
Show file tree
Hide file tree
Showing 16 changed files with 1,002 additions and 264 deletions.
15 changes: 14 additions & 1 deletion bbnclient/bbncontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/babylonchain/babylon/types"
btcstakingtypes "github.com/babylonchain/babylon/x/btcstaking/types"
finalitytypes "github.com/babylonchain/babylon/x/finality/types"
"github.com/babylonchain/rpc-client/client"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -61,7 +62,19 @@ func (bc *BabylonController) RegisterValidator(bbnPubKey *secp256k1.PubKey, btcP
// CommitPubRandList commits a list of Schnorr public randomness via a MsgCommitPubRand to Babylon
// it returns tx hash and error
func (bc *BabylonController) CommitPubRandList(btcPubKey *types.BIP340PubKey, startHeight uint64, pubRandList []types.SchnorrPubRand, sig *types.BIP340Signature) ([]byte, error) {
panic("implement me")
msg := &finalitytypes.MsgCommitPubRandList{
ValBtcPk: btcPubKey,
StartHeight: startHeight,
PubRandList: pubRandList,
Sig: sig,
}

res, err := bc.rpcClient.SendMsg(context.Background(), msg, "")
if err != nil {
return nil, err
}

return []byte(res.TxHash), nil
}

// SubmitJurySig submits the Jury signature via a MsgAddJurySig to Babylon if the daemon runs in Jury mode
Expand Down
84 changes: 63 additions & 21 deletions cmd/valcli/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ package main
import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/client"

"github.com/urfave/cli"

"github.com/babylonchain/btc-validator/codec"
"github.com/babylonchain/btc-validator/proto"
"github.com/babylonchain/btc-validator/service"
dc "github.com/babylonchain/btc-validator/service/client"
"github.com/babylonchain/btc-validator/val"
"github.com/babylonchain/btc-validator/valcfg"
)

const (
chainIdFlag = "chain-id"
keyringDirFlag = "keyring-dir"
keyringBackendFlag = "keyring-backend"
keyNameFlag = "key-name"

chainIdFlag = "chain-id"
keyringDirFlag = "keyring-dir"
keyringBackendFlag = "keyring-backend"
keyNameFlag = "key-name"
randNumFlag = "rand-num"
babylonPkFlag = "babylon-pk"
defaultChainID = "test-chain"
defaultKeyringBackend = "test"
defaultRandomNum = 100
)

var validatorsCommands = []cli.Command{
Expand All @@ -30,7 +32,7 @@ var validatorsCommands = []cli.Command{
Usage: "Control Bitcoin validators.",
Category: "Validators",
Subcommands: []cli.Command{
createValidator, listValidators, importValidator, registerValidator,
createValidator, listValidators, importValidator, registerValidator, commitRandomList,
},
},
}
Expand Down Expand Up @@ -64,7 +66,10 @@ var createValidator = cli.Command{
}

func createVal(ctx *cli.Context) error {
sdkCtx, err := createClientCtx(ctx)
sdkCtx, err := service.CreateClientCtx(
ctx.String(keyringDirFlag),
ctx.String(chainIdFlag),
)
if err != nil {
return err
}
Expand Down Expand Up @@ -181,6 +186,55 @@ func registerVal(ctx *cli.Context) error {
return nil
}

// TODO: consider remove this command after PoC
// because leaving this command to users is dangerous
// committing random list should be an automatic process
var commitRandomList = cli.Command{
Name: "commit-random-list",
ShortName: "crl",
Usage: "generate a list of Schnorr random pair and commit the public rand for BTC validator",
Flags: []cli.Flag{
cli.StringFlag{
Name: validatorDaemonAddressFlag,
Usage: "full address of the validator daemon in format tcp://<host>:<port>",
Value: defaultValidatorDaemonAddress,
},
cli.Int64Flag{
Name: randNumFlag,
Usage: "the number of public randomness you want to commit",
Value: int64(defaultRandomNum),
},
cli.StringFlag{
Name: babylonPkFlag,
Usage: "commit random list for a specific BTC validator",
},
},
Action: commitRand,
}

func commitRand(ctx *cli.Context) error {
daemonAddress := ctx.String(validatorDaemonAddressFlag)
rpcClient, cleanUp, err := dc.NewValidatorServiceGRpcClient(daemonAddress)
if err != nil {
return err
}
defer cleanUp()

var bbnPkBytes []byte
if ctx.String(babylonPkFlag) != "" {
bbnPkBytes = []byte(ctx.String(babylonPkFlag))
}
res, err := rpcClient.CommitPubRandList(context.Background(),
bbnPkBytes)
if err != nil {
return err
}

printRespJSON(res)

return nil
}

func getValStoreFromCtx(ctx *cli.Context) (*val.ValidatorStore, error) {
dbcfg, err := valcfg.NewDatabaseConfig(
ctx.GlobalString(dbTypeFlag),
Expand All @@ -198,15 +252,3 @@ func getValStoreFromCtx(ctx *cli.Context) (*val.ValidatorStore, error) {

return valStore, nil
}

func createClientCtx(ctx *cli.Context) (client.Context, error) {
dir := ctx.String(keyringDirFlag)
if dir == "" {
dir = valcfg.DefaultValidatordDir
}

return client.Context{}.
WithChainID(ctx.String(chainIdFlag)).
WithCodec(codec.MakeCodec()).
WithKeyringDir(dir), nil
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/btcsuite/btcd/btcutil v1.1.3
github.com/cosmos/cosmos-sdk v0.47.2
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/gogoproto v1.4.10
github.com/gogo/protobuf v1.3.3
github.com/golang/mock v1.6.0
github.com/jessevdk/go-flags v1.5.0
github.com/lightningnetwork/lnd v0.16.4-beta.rc1
Expand Down Expand Up @@ -77,6 +77,7 @@ require (
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/gogoproto v1.4.10 // indirect
github.com/cosmos/iavl v0.20.0 // indirect
github.com/cosmos/ibc-go/v7 v7.0.1 // indirect
github.com/cosmos/ics23/go v0.9.1-0.20221207100636-b1abd8678aab // indirect
Expand Down Expand Up @@ -110,7 +111,6 @@ require (
github.com/go-stack/stack v1.8.1 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.3 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down
Loading

0 comments on commit c7d9a28

Please sign in to comment.