Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Jan 22, 2025
1 parent c385444 commit c813deb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 45 deletions.
17 changes: 0 additions & 17 deletions scripts/build/simulations.mk
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
# TODO: This should be ported to work with SimApp v2.

#? test-sim-nondeterminism: Run non-determinism test for simapp
test-sim-nondeterminism:
@echo "Running non-determinism test..."
@cd ${CURRENT_DIR}/simapp/v2 && go test -failfast -mod=readonly -timeout=30m -tags='sims' -run TestAppStateDeterminism \
-NumBlocks=100 -BlockSize=200


# Requires an exported plugin. See store/streaming/README.md for documentation.
#
# example:
# export COSMOS_SDK_ABCI_V1=<path-to-plugin-binary>
# make test-sim-nondeterminism-streaming
#
# Using the built-in examples:
# export COSMOS_SDK_ABCI_V1=<path-to-sdk>/store/streaming/abci/examples/file/file
# make test-sim-nondeterminism-streaming
test-sim-nondeterminism-streaming:
# @echo "Running non-determinism-streaming test..."
# @cd ${CURRENT_DIR}/simapp && go test -failfast -mod=readonly -timeout=30m -tags='sims' -run TestAppStateDeterminism \
# -NumBlocks=100 -BlockSize=200 -EnableStreaming=true

test-sim-import-export:
@echo "Running application import/export simulation. This may take several minutes..."
@cd ${CURRENT_DIR}/simapp/v2 && go test -failfast -mod=readonly -timeout 20m -tags='sims' -run TestAppImportExport \
Expand All @@ -44,7 +28,6 @@ test-sim-multi-seed-short:

.PHONY: \
test-sim-nondeterminism \
test-sim-nondeterminism-streaming \
test-sim-import-export \
test-sim-after-import \
test-sim-multi-seed-short \
Expand Down
9 changes: 0 additions & 9 deletions server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/comet"
corecontext "cosmossdk.io/core/context"
"cosmossdk.io/core/event"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
Expand Down Expand Up @@ -510,14 +509,6 @@ func (c *consensus[T]) FinalizeBlock(
return nil, fmt.Errorf("unable to commit the changeset: %w", err)
}

var events []event.Event
events = append(events, resp.PreBlockEvents...)
events = append(events, resp.BeginBlockEvents...)
for _, tx := range resp.TxResults {
events = append(events, tx.Events...)
}
events = append(events, resp.EndBlockEvents...)

// listen to state streaming changes in accordance with the block
err = c.streamDeliverBlockChanges(ctx, req.Height, req.Txs, decodedTxs, *resp, stateChanges)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion server/v2/cometbft/streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package cometbft

import (
"context"
"cosmossdk.io/core/transaction"
"encoding/json"
"fmt"

"cosmossdk.io/core/event"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
errorsmod "cosmossdk.io/errors/v2"
"cosmossdk.io/schema/appdata"
"cosmossdk.io/server/v2/streaming"
Expand Down
2 changes: 1 addition & 1 deletion server/v2/store/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *Server[T]) ExportSnapshotCmd() *cobra.Command {
if err != nil {
return err
}
height = int64(lastCommitId.Version)
height = lastCommitId.Version
}

cmd.Printf("Exporting snapshot for height %d\n", height)
Expand Down
38 changes: 21 additions & 17 deletions simapp/v2/sim_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package simapp

import (
"context"
"cosmossdk.io/schema/appdata"
"cosmossdk.io/server/v2/cometbft"
"cosmossdk.io/server/v2/streaming"
"encoding/json"
"fmt"
"iter"
"maps"
"math/rand"
"os"
"slices"
"testing"
"time"
Expand All @@ -29,7 +27,10 @@ import (
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/runtime/v2"
"cosmossdk.io/schema/appdata"
"cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/cometbft"
"cosmossdk.io/server/v2/streaming"
storev2 "cosmossdk.io/store/v2"
consensustypes "cosmossdk.io/x/consensus/types"

Expand All @@ -49,6 +50,7 @@ type (
HasWeightedOperationsX = simsx.HasWeightedOperationsX
HasWeightedOperationsXWithProposals = simsx.HasWeightedOperationsXWithProposals
HasProposalMsgsX = simsx.HasProposalMsgsX
HasLegacyProposalMsgs = simsx.HasLegacyProposalMsgs
)

const SimAppChainID = "simulation-app"
Expand Down Expand Up @@ -287,11 +289,15 @@ func RunWithRandSourceX[T Tx](
defer done()

testInstance, chainState, accounts := setupChainStateFn(rootCtx, r)

emptySimParams := make(map[string]json.RawMessage) // todo read sims params from disk as before
customFactoryParams := make(map[string]json.RawMessage)
if tCfg.ParamsFile != "" {
bz, err := os.ReadFile(tCfg.ParamsFile)
require.NoError(tb, err)
require.NoError(tb, json.Unmarshal(bz, &customFactoryParams))
}

modules := testInstance.ModuleManager.Modules()
msgFactoriesFn := prepareSimsMsgFactories(r, modules, simsx.ParamWeightSource(emptySimParams))
msgFactoriesFn := prepareSimsMsgFactories(tb, r, modules, simsx.ParamWeightSource(customFactoryParams))

if b, ok := tb.(interface{ ResetTimer() }); ok {
b.ResetTimer()
Expand Down Expand Up @@ -325,7 +331,6 @@ func prepareInitialGenesisState[T Tx](
moduleManager ModuleManager,
) ([]simtypes.Account, json.RawMessage, string, time.Time) {
txConfig := app.TxConfig()
// todo: replace legacy testdata functions ?
appStateFn := simtestutil.AppStateFn(
app.AppCodec(),
txConfig.SigningContext().AddressCodec(),
Expand Down Expand Up @@ -448,9 +453,10 @@ func doMainLoop[T Tx](
}

cometInfo := comet.Info{
ValidatorsHash: nil,
Evidence: cs.ValsetHistory.MissBehaviour(r),
ProposerAddress: cs.ActiveValidatorSet[0].Address, // todo: pick random one
ValidatorsHash: nil,
Evidence: cs.ValsetHistory.MissBehaviour(r),
// pick one of top 10
ProposerAddress: cs.ActiveValidatorSet[r.Intn(min(len(cs.ActiveValidatorSet), 10))].Address,
LastCommit: cs.ActiveValidatorSet.NewCommitInfo(r),
}
fOps, pos := futureOpsReg.PopScheduledFor(cs.BlockTime), 0
Expand Down Expand Up @@ -538,21 +544,19 @@ func doMainLoop[T Tx](

// prepareSimsMsgFactories constructs and returns a function to retrieve simulation message factories for all modules.
// It initializes proposal and factory registries, registers proposals and weighted operations, and sorts deterministically.
func prepareSimsMsgFactories(
r *rand.Rand,
modules map[string]appmodulev2.AppModule,
weights simsx.WeightSource,
) func() simsx.SimMsgFactoryX {
func prepareSimsMsgFactories(tb testing.TB, r *rand.Rand, modules map[string]appmodulev2.AppModule, weights simsx.WeightSource) func() simsx.SimMsgFactoryX {
tb.Helper()
moduleNames := slices.Collect(maps.Keys(modules))
slices.Sort(moduleNames) // make deterministic

// get all proposal types
proposalRegistry := simsx.NewUniqueTypeRegistry()
for _, n := range moduleNames {
switch xm := modules[n].(type) { // nolint: gocritic // extended in the future
switch xm := modules[n].(type) {
case HasProposalMsgsX:
xm.ProposalMsgsX(weights, proposalRegistry)
// todo: register legacy and v1 msg proposals
case HasLegacyProposalMsgs:
tb.Logf("Ignoring legacy proposal messages for module: %s", n)
}
}
// register all msg factories
Expand Down

0 comments on commit c813deb

Please sign in to comment.