Skip to content

Commit

Permalink
fix: unecessary coupling of signer PrivateSigner inside helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Dec 9, 2024
1 parent 513b73d commit 813a886
Show file tree
Hide file tree
Showing 17 changed files with 179 additions and 93 deletions.
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import (
"github.com/babylonlabs-io/babylon/app/ante"
appkeepers "github.com/babylonlabs-io/babylon/app/keepers"
appparams "github.com/babylonlabs-io/babylon/app/params"
"github.com/babylonlabs-io/babylon/app/signer"
"github.com/babylonlabs-io/babylon/app/upgrades"
"github.com/babylonlabs-io/babylon/client/docs"
bbn "github.com/babylonlabs-io/babylon/types"
Expand Down Expand Up @@ -206,7 +207,7 @@ func NewBabylonApp(
loadLatest bool,
skipUpgradeHeights map[int64]bool,
invCheckPeriod uint,
privSigner *appkeepers.PrivSigner,
privSigner *signer.PrivSigner,
appOpts servertypes.AppOptions,
wasmOpts []wasmkeeper.Option,
baseAppOptions ...func(*baseapp.BaseApp),
Expand Down
5 changes: 3 additions & 2 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"cosmossdk.io/log"
"github.com/babylonlabs-io/babylon/testutil/signer"
abci "github.com/cometbft/cometbft/abci/types"
dbm "github.com/cosmos/cosmos-db"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -14,7 +15,7 @@ import (

func TestBabylonBlockedAddrs(t *testing.T) {
db := dbm.NewMemDB()
signer, _ := SetupTestPrivSigner()
signer, _ := signer.SetupTestPrivSigner()
logger := log.NewTestLogger(t)

app := NewBabylonAppWithCustomOptions(t, false, signer, SetupOptions{
Expand Down Expand Up @@ -71,7 +72,7 @@ func TestGetMaccPerms(t *testing.T) {

func TestUpgradeStateOnGenesis(t *testing.T) {
db := dbm.NewMemDB()
privSigner, err := SetupTestPrivSigner()
privSigner, err := signer.SetupTestPrivSigner()
require.NoError(t, err)
logger := log.NewTestLogger(t)

Expand Down
3 changes: 2 additions & 1 deletion app/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
simsutils "github.com/cosmos/cosmos-sdk/testutil/sims"

appparams "github.com/babylonlabs-io/babylon/app/params"
"github.com/babylonlabs-io/babylon/testutil/signer"
bbn "github.com/babylonlabs-io/babylon/types"
)

Expand All @@ -27,7 +28,7 @@ func TmpAppOptions() simsutils.AppOptionsMap {
}

func NewTmpBabylonApp() *BabylonApp {
signer, _ := SetupTestPrivSigner()
signer, _ := signer.SetupTestPrivSigner()
return NewBabylonApp(
log.NewNopLogger(),
dbm.NewMemDB(),
Expand Down
3 changes: 2 additions & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"

appparams "github.com/babylonlabs-io/babylon/app/params"
"github.com/babylonlabs-io/babylon/app/signer"
bbn "github.com/babylonlabs-io/babylon/types"
owasm "github.com/babylonlabs-io/babylon/wasmbinding"
btccheckpointkeeper "github.com/babylonlabs-io/babylon/x/btccheckpoint/keeper"
Expand Down Expand Up @@ -162,7 +163,7 @@ func (ak *AppKeepers) InitKeepers(
homePath string,
invCheckPeriod uint,
skipUpgradeHeights map[int64]bool,
privSigner *PrivSigner,
privSigner *signer.PrivSigner,
appOpts servertypes.AppOptions,
wasmConfig wasmtypes.WasmConfig,
wasmOpts []wasmkeeper.Option,
Expand Down
27 changes: 0 additions & 27 deletions app/keepers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import (
"path/filepath"
"text/template"

cmtconfig "github.com/cometbft/cometbft/config"
cmtos "github.com/cometbft/cometbft/libs/os"
"github.com/cosmos/cosmos-sdk/client/config"

"github.com/babylonlabs-io/babylon/privval"
)

const defaultConfigTemplate = `# This is a TOML config file.
Expand All @@ -33,29 +29,6 @@ node = "{{ .Node }}"
broadcast-mode = "{{ .BroadcastMode }}"
`

type PrivSigner struct {
WrappedPV *privval.WrappedFilePV
}

func InitPrivSigner(nodeDir string) (*PrivSigner, error) {
nodeCfg := cmtconfig.DefaultConfig()
pvKeyFile := filepath.Join(nodeDir, nodeCfg.PrivValidatorKeyFile())
err := cmtos.EnsureDir(filepath.Dir(pvKeyFile), 0777)
if err != nil {
return nil, err
}
pvStateFile := filepath.Join(nodeDir, nodeCfg.PrivValidatorStateFile())
err = cmtos.EnsureDir(filepath.Dir(pvStateFile), 0777)
if err != nil {
return nil, err
}
wrappedPV := privval.LoadOrGenWrappedFilePV(pvKeyFile, pvStateFile)

return &PrivSigner{
WrappedPV: wrappedPV,
}, nil
}

func CreateClientConfig(chainID string, backend string, homePath string) (*config.ClientConfig, error) {
cliConf := &config.ClientConfig{
ChainID: chainID,
Expand Down
33 changes: 33 additions & 0 deletions app/signer/private.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package signer

import (
"path/filepath"

cmtconfig "github.com/cometbft/cometbft/config"
cmtos "github.com/cometbft/cometbft/libs/os"

"github.com/babylonlabs-io/babylon/privval"
)

type PrivSigner struct {
WrappedPV *privval.WrappedFilePV
}

func InitPrivSigner(nodeDir string) (*PrivSigner, error) {
nodeCfg := cmtconfig.DefaultConfig()
pvKeyFile := filepath.Join(nodeDir, nodeCfg.PrivValidatorKeyFile())
err := cmtos.EnsureDir(filepath.Dir(pvKeyFile), 0777)
if err != nil {
return nil, err
}
pvStateFile := filepath.Join(nodeDir, nodeCfg.PrivValidatorStateFile())
err = cmtos.EnsureDir(filepath.Dir(pvStateFile), 0777)
if err != nil {
return nil, err
}
wrappedPV := privval.LoadOrGenWrappedFilePV(pvKeyFile, pvStateFile)

return &PrivSigner{
WrappedPV: wrappedPV,
}, nil
}
45 changes: 7 additions & 38 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package app
import (
"bytes"
"encoding/json"
"os"
"testing"
"time"

"cosmossdk.io/log"
"cosmossdk.io/math"
pruningtypes "cosmossdk.io/store/pruning/types"
testsigner "github.com/babylonlabs-io/babylon/testutil/signer"
minttypes "github.com/babylonlabs-io/babylon/x/mint/types"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto/ed25519"
Expand All @@ -31,8 +31,8 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require"

appkeepers "github.com/babylonlabs-io/babylon/app/keepers"
appparams "github.com/babylonlabs-io/babylon/app/params"
"github.com/babylonlabs-io/babylon/app/signer"
"github.com/babylonlabs-io/babylon/crypto/bls12381"
"github.com/babylonlabs-io/babylon/privval"
bbn "github.com/babylonlabs-io/babylon/types"
Expand All @@ -49,7 +49,7 @@ type SetupOptions struct {
AppOpts types.AppOptions
}

func setup(t *testing.T, ps *appkeepers.PrivSigner, withGenesis bool, invCheckPeriod uint, btcConf bbn.SupportedBtcNetwork) (*BabylonApp, GenesisState) {
func setup(t *testing.T, ps *signer.PrivSigner, withGenesis bool, invCheckPeriod uint, btcConf bbn.SupportedBtcNetwork) (*BabylonApp, GenesisState) {
db := dbm.NewMemDB()
nodeHome := t.TempDir()

Expand Down Expand Up @@ -83,7 +83,7 @@ func setup(t *testing.T, ps *appkeepers.PrivSigner, withGenesis bool, invCheckPe
// Created Babylon application will have one validator with hardcoed amount of tokens.
// This is necessary as from cosmos-sdk 0.46 it is required that there is at least
// one validator in validator set during InitGenesis abci call - https://github.com/cosmos/cosmos-sdk/pull/9697
func NewBabylonAppWithCustomOptions(t *testing.T, isCheckTx bool, privSigner *appkeepers.PrivSigner, options SetupOptions) *BabylonApp {
func NewBabylonAppWithCustomOptions(t *testing.T, isCheckTx bool, privSigner *signer.PrivSigner, options SetupOptions) *BabylonApp {
t.Helper()
// create validator set with single validator
valKeys, err := privval.NewValidatorKeys(ed25519.GenPrivKey(), bls12381.GenPrivKey())
Expand Down Expand Up @@ -237,7 +237,7 @@ func Setup(t *testing.T, isCheckTx bool) *BabylonApp {
func SetupWithBitcoinConf(t *testing.T, isCheckTx bool, btcConf bbn.SupportedBtcNetwork) *BabylonApp {
t.Helper()

ps, err := SetupTestPrivSigner()
ps, err := testsigner.SetupTestPrivSigner()
require.NoError(t, err)
valPubKey := ps.WrappedPV.Key.PubKey
// generate genesis account
Expand All @@ -248,7 +248,7 @@ func SetupWithBitcoinConf(t *testing.T, isCheckTx bool, btcConf bbn.SupportedBtc
}
ps.WrappedPV.Key.DelegatorAddress = acc.GetAddress().String()
// create validator set with single validator
genesisKey, err := GenesisKeyFromPrivSigner(ps)
genesisKey, err := testsigner.GenesisKeyFromPrivSigner(ps)
require.NoError(t, err)
genesisValSet := []*checkpointingtypes.GenesisKey{genesisKey}

Expand All @@ -257,26 +257,12 @@ func SetupWithBitcoinConf(t *testing.T, isCheckTx bool, btcConf bbn.SupportedBtc
return app
}

// SetupTestPrivSigner sets up a PrivSigner for testing
func SetupTestPrivSigner() (*appkeepers.PrivSigner, error) {
// Create a temporary node directory
nodeDir, err := os.MkdirTemp("", "tmp-signer")
if err != nil {
return nil, err
}
defer func() {
_ = os.RemoveAll(nodeDir)
}()
privSigner, _ := appkeepers.InitPrivSigner(nodeDir)
return privSigner, nil
}

// SetupWithGenesisValSet initializes a new BabylonApp with a validator set and genesis accounts
// that also act as delegators. For simplicity, each validator is bonded with a delegation
// of one consensus engine unit (10^6) in the default token of the babylon app from first genesis
// account. A Nop logger is set in BabylonApp.
// Note that the privSigner should be the 0th item of valSet
func SetupWithGenesisValSet(t *testing.T, btcConf bbn.SupportedBtcNetwork, valSet []*checkpointingtypes.GenesisKey, privSigner *appkeepers.PrivSigner, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *BabylonApp {
func SetupWithGenesisValSet(t *testing.T, btcConf bbn.SupportedBtcNetwork, valSet []*checkpointingtypes.GenesisKey, privSigner *signer.PrivSigner, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *BabylonApp {
t.Helper()
app, genesisState := setup(t, privSigner, true, 5, btcConf)
genesisState = genesisStateWithValSet(t, app, genesisState, valSet, genAccs, balances...)
Expand Down Expand Up @@ -309,23 +295,6 @@ func SetupWithGenesisValSet(t *testing.T, btcConf bbn.SupportedBtcNetwork, valSe
return app
}

func GenesisKeyFromPrivSigner(ps *appkeepers.PrivSigner) (*checkpointingtypes.GenesisKey, error) {
valKeys, err := privval.NewValidatorKeys(ps.WrappedPV.GetValPrivKey(), ps.WrappedPV.GetBlsPrivKey())
if err != nil {
return nil, err
}
valPubkey, err := cryptocodec.FromCmtPubKeyInterface(valKeys.ValPubkey)
if err != nil {
return nil, err
}
return checkpointingtypes.NewGenesisKey(
ps.WrappedPV.GetAddress(),
&valKeys.BlsPubkey,
valKeys.PoP,
&cosmosed.PubKey{Key: valPubkey.Bytes()},
)
}

// createRandomAccounts is a strategy used by addTestAddrs() in order to generated addresses in random order.
func createRandomAccounts(accNum int) []sdk.AccAddress {
testAddrs := make([]sdk.AccAddress, accNum)
Expand Down
5 changes: 3 additions & 2 deletions cmd/babylond/cmd/genhelpers/bls_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/babylonlabs-io/babylon/privval"
"github.com/babylonlabs-io/babylon/testutil/cli"
"github.com/babylonlabs-io/babylon/testutil/datagen"
"github.com/babylonlabs-io/babylon/testutil/signer"
"github.com/babylonlabs-io/babylon/x/checkpointing/types"
)

Expand Down Expand Up @@ -75,7 +76,7 @@ func Test_CmdCreateAddWithoutGentx(t *testing.T) {
require.NoError(t, err)

db := dbm.NewMemDB()
signer, err := app.SetupTestPrivSigner()
signer, err := signer.SetupTestPrivSigner()
require.NoError(t, err)
bbn := app.NewBabylonAppWithCustomOptions(t, false, signer, app.SetupOptions{
Logger: logger,
Expand Down Expand Up @@ -117,7 +118,7 @@ func Test_CmdCreateAddWithoutGentx(t *testing.T) {
// error is expected if adding duplicate
func Test_CmdAddBlsWithGentx(t *testing.T) {
db := dbm.NewMemDB()
signer, err := app.SetupTestPrivSigner()
signer, err := signer.SetupTestPrivSigner()
require.NoError(t, err)
bbn := app.NewBabylonAppWithCustomOptions(t, false, signer, app.SetupOptions{
Logger: log.NewNopLogger(),
Expand Down
3 changes: 2 additions & 1 deletion cmd/babylond/cmd/genhelpers/bls_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/babylonlabs-io/babylon/app"
"github.com/babylonlabs-io/babylon/cmd/babylond/cmd/genhelpers"
"github.com/babylonlabs-io/babylon/privval"
"github.com/babylonlabs-io/babylon/testutil/helper"
"github.com/babylonlabs-io/babylon/x/checkpointing/types"
)

Expand All @@ -34,7 +35,7 @@ func Test_CmdCreateBls(t *testing.T) {
cfg, err := genutiltest.CreateDefaultCometConfig(home)
require.NoError(t, err)

signer, err := app.SetupTestPrivSigner()
signer, err := helper.SetupTestPrivSigner()
require.NoError(t, err)
bbn := app.NewBabylonAppWithCustomOptions(t, false, signer, app.SetupOptions{
Logger: logger,
Expand Down
3 changes: 2 additions & 1 deletion cmd/babylond/cmd/testnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/babylonlabs-io/babylon/app"
"github.com/babylonlabs-io/babylon/testutil/signer"
)

func Test_TestnetCmd(t *testing.T) {
Expand All @@ -26,7 +27,7 @@ func Test_TestnetCmd(t *testing.T) {
cfg, err := genutiltest.CreateDefaultCometConfig(home)
require.NoError(t, err)

signer, err := app.SetupTestPrivSigner()
signer, err := signer.SetupTestPrivSigner()
require.NoError(t, err)
bbn := app.NewBabylonAppWithCustomOptions(t, false, signer, app.SetupOptions{
Logger: logger,
Expand Down
3 changes: 2 additions & 1 deletion cmd/babylond/cmd/validate_genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

dbm "github.com/cosmos/cosmos-db"

"github.com/babylonlabs-io/babylon/testutil/signer"
checkpointingtypes "github.com/babylonlabs-io/babylon/x/checkpointing/types"

"github.com/cosmos/cosmos-sdk/x/genutil"
Expand Down Expand Up @@ -89,7 +90,7 @@ func generateTestGenesisState(t *testing.T, home string, n int) (*app.BabylonApp
logger := log.NewNopLogger()
cfg, _ := genutiltest.CreateDefaultCometConfig(home)

signer, err := app.SetupTestPrivSigner()
signer, err := signer.SetupTestPrivSigner()
require.NoError(t, err)
bbn := app.NewBabylonAppWithCustomOptions(t, false, signer, app.SetupOptions{
Logger: logger,
Expand Down
1 change: 1 addition & 0 deletions test/e2e/btc_staking_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ func (s *BTCStakingTestSuite) Test3CommitPublicRandomnessAndSubmitFinalitySignat
}, true)

// ensure finality provider has received rewards after the block is finalised
// unexpected status code: 500, body: {"code":13,"message":"negative coin amount: -20910810810810","details":[]}
fpRewardGauges, err := nonValidatorNode.QueryRewardGauge(fpBabylonAddr)
s.NoError(err)
fpRewardGauge, ok := fpRewardGauges[itypes.FinalityProviderType.String()]
Expand Down
10 changes: 5 additions & 5 deletions testutil/datagen/genesiskey.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package datagen

import (
"github.com/babylonlabs-io/babylon/app"
appkeepers "github.com/babylonlabs-io/babylon/app/keepers"
"github.com/babylonlabs-io/babylon/app/signer"
"github.com/babylonlabs-io/babylon/crypto/bls12381"
"github.com/babylonlabs-io/babylon/privval"
testsigner "github.com/babylonlabs-io/babylon/testutil/signer"
checkpointingtypes "github.com/babylonlabs-io/babylon/x/checkpointing/types"
cmtcrypto "github.com/cometbft/cometbft/crypto"
cmted25519 "github.com/cometbft/cometbft/crypto/ed25519"
Expand Down Expand Up @@ -88,12 +88,12 @@ func GenesisValidatorSet(numVals int) (*GenesisValidators, error) {

// GenesisValidatorSetWithPrivSigner generates a set with `numVals` genesis validators
// along with the privSigner, which will be in the 0th position of the return validator set
func GenesisValidatorSetWithPrivSigner(numVals int) (*GenesisValidators, *appkeepers.PrivSigner, error) {
ps, err := app.SetupTestPrivSigner()
func GenesisValidatorSetWithPrivSigner(numVals int) (*GenesisValidators, *signer.PrivSigner, error) {
ps, err := testsigner.SetupTestPrivSigner()
if err != nil {
return nil, nil, err
}
signerGenesisKey, err := app.GenesisKeyFromPrivSigner(ps)
signerGenesisKey, err := testsigner.GenesisKeyFromPrivSigner(ps)
if err != nil {
return nil, nil, err
}
Expand Down
Loading

0 comments on commit 813a886

Please sign in to comment.