Skip to content

Commit

Permalink
add validators into status
Browse files Browse the repository at this point in the history
  • Loading branch information
randygrok committed Aug 8, 2024
1 parent 57c9751 commit a32d778
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
15 changes: 15 additions & 0 deletions server/v2/appmanager/appmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,18 @@ func (a AppManager[T]) QueryWithState(
) (transaction.Msg, error) {
return a.stf.Query(ctx, state, a.config.QueryGasLimit, request)
}

// RunWithCtx is made to support genesis, if genesis was just the execution of messages instead
// of being something custom then we would not need this. PLEASE DO NOT USE.
// TODO: Remove
func (a AppManager[T]) RunWithCtx(
ctx context.Context,
closure func(ctx context.Context) error,
) (corestore.WriterMap, error) {
_, st, err := a.db.StateLatest()
if err != nil {
return nil, fmt.Errorf("unable to get latest state: %w", err)
}

return a.stf.RunWithCtx(ctx, st, closure)
}
34 changes: 34 additions & 0 deletions simapp/v2/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import (
app2 "cosmossdk.io/core/app"
"cosmossdk.io/core/comet"
context2 "cosmossdk.io/core/context"
sdkmath "cosmossdk.io/math"
serverv2 "cosmossdk.io/server/v2"
authtypes "cosmossdk.io/x/auth/types"
banktypes "cosmossdk.io/x/bank/types"
"crypto/sha256"
"encoding/json"
"github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/testutil/mock"
sdk "github.com/cosmos/cosmos-sdk/types"
"testing"
"time"

Expand All @@ -16,6 +23,7 @@ import (
"cosmossdk.io/log"
comettypes "cosmossdk.io/server/v2/cometbft/types"
"cosmossdk.io/store/v2/db"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
Expand All @@ -29,6 +37,32 @@ func NewTestApp(t *testing.T) (*SimApp[transaction.Tx], context.Context) {

app := NewSimApp[transaction.Tx](logger, vp)
genesis := app.ModuleManager().DefaultGenesis()

privVal := mock.NewPV()
pubKey, err := privVal.GetPubKey()
require.NoError(t, err)

// create validator set with single validator
validator := types.NewValidator(pubKey, 1)
valSet := types.NewValidatorSet([]*types.Validator{validator})

// generate genesis account
senderPrivKey := secp256k1.GenPrivKey()
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
balance := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(100000000000000))),
}

genesis, err = simtestutil.GenesisStateWithValSet(
app.AppCodec(),
genesis,
valSet,
[]authtypes.GenesisAccount{acc},
balance,
)
require.NoError(t, err)

genesisBytes, err := json.Marshal(genesis)
require.NoError(t, err)

Expand Down
14 changes: 13 additions & 1 deletion simapp/v2/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"cosmossdk.io/x/staking"

cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
cmttypes "github.com/cometbft/cometbft/types"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
)

Expand All @@ -26,7 +27,18 @@ func (app *SimApp[T]) ExportAppStateAndValidators(forZeroHeight bool, jailAllowe
return servertypes.ExportedApp{}, err
}

validators, err := staking.WriteValidators(ctx, app.StakingKeeper)
var validators []cmttypes.GenesisValidator
_, err = app.RunWithCtx(ctx, func(ctx context.Context) error {
validators, err = staking.WriteValidators(ctx, app.StakingKeeper)
if err != nil {
return err
}
return nil
})
if err != nil {
return servertypes.ExportedApp{}, err
}

return servertypes.ExportedApp{
AppState: genesis,
Validators: validators,
Expand Down

0 comments on commit a32d778

Please sign in to comment.