Skip to content

Commit

Permalink
add additional check to voting messages to for existing ballots
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Dec 11, 2024
1 parent 1d3ff29 commit 7c14f50
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
8 changes: 4 additions & 4 deletions simulation/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func TestFullAppSimulation(t *testing.T) {
// 1. It runs a full simulation and exports the state
// 2. It creates a new app, and db
// 3. It imports the exported state into the new app
// 4. It compares the two apps
// 4. It compares the key value pairs for the two apps.The comparison function takes a list of keys to skip as an input as well
// a. First app which ran the simulation
// b. Second app which imported the state

Expand Down Expand Up @@ -520,16 +520,15 @@ func TestAppSimulationAfterImport(t *testing.T) {
exported, err := simApp.ExportAppStateAndValidators(true, []string{}, []string{})
require.NoError(t, err)

// Setup a new app with new database and directory
newDB, newDir, _, _, err := cosmossimutils.SetupSimulation(
config,
SimDBBackend+"_new",
SimDBName+"_new",
zetasimulation.FlagVerboseValue,
zetasimulation.FlagEnabledValue,
)

require.NoError(t, err, "simulation setup failed")

t.Cleanup(func() {
if err := newDB.Close(); err != nil {
require.NoError(t, err, "Error closing new database")
Expand All @@ -538,7 +537,6 @@ func TestAppSimulationAfterImport(t *testing.T) {
require.NoError(t, err, "Error removing directory")
}
})

newSimApp, err := zetasimulation.NewSimApp(
logger,
newDB,
Expand All @@ -548,12 +546,14 @@ func TestAppSimulationAfterImport(t *testing.T) {
)
require.NoError(t, err)

// Initialize the new app with the exported genesis state of the first run
t.Log("Importing genesis into the new app")
newSimApp.InitChain(abci.RequestInitChain{
ChainId: SimAppChainID,
AppStateBytes: exported.AppState,
})

// Run simulation on the new app
stopEarly, simParams, simErr = simulation.SimulateFromSeed(
t,
os.Stdout,
Expand Down
15 changes: 13 additions & 2 deletions x/crosschain/simulation/operation_vote_inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,21 @@ func SimulateVoteInbound(k keeper.Keeper) simtypes.Operation {
}

msg := sample.InboundVoteSim(from, to, r, asset)
// Return early if inbound is not enabled.

cf, found := k.GetObserverKeeper().GetCrosschainFlags(ctx)
if !found {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "crosschain flags not found"), nil, nil
}

// Return early if inbound is not enabled.
if !cf.IsInboundEnabled {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "inbound is not enabled"), nil, nil
}

// Return early if the inbound has already been finalized.
if k.IsFinalizedInbound(ctx, msg.InboundHash, msg.SenderChainId, msg.EventIndex) {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "inbound already finalized"), nil, nil
}
// Pick a random observer to create the ballot
// If this returns an error, it is likely that the entire observer set has been removed
simAccount, firstVoter, err := GetRandomAccountAndObserver(r, ctx, k, accs)
Expand All @@ -106,11 +112,16 @@ func SimulateVoteInbound(k keeper.Keeper) simtypes.Operation {
firstMsg := msg
firstMsg.Creator = firstVoter

// THe first vote should always create a new ballot
_, found = k.GetObserverKeeper().GetBallot(ctx, firstMsg.Digest())
if found {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "ballot already exists"), nil, nil
}

err = firstMsg.ValidateBasic()
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to validate first inbound vote"), nil, err
}

tx, err := simtestutil.GenSignedMockTx(
r,
txGen,
Expand Down
6 changes: 6 additions & 0 deletions x/crosschain/simulation/operation_vote_outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ func SimulateVoteOutbound(k keeper.Keeper) simtypes.Operation {
firstMsg := msg
firstMsg.Creator = firstVoter

// THe first vote should always create a new ballot
_, found = k.GetObserverKeeper().GetBallot(ctx, firstMsg.Digest())
if found {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "ballot already exists"), nil, nil
}

err = firstMsg.ValidateBasic()
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to validate first inbound vote"), nil, err
Expand Down
2 changes: 1 addition & 1 deletion x/observer/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func GetRandomAccountAndObserver(
}

if !foundObserver {
return simtypes.Account{}, "", observerList, fmt.Errorf("no observer found")
return simtypes.Account{}, "no observer found", nil, nil
}

simAccount, err := GetObserverAccount(randomObserver, accounts)
Expand Down

0 comments on commit 7c14f50

Please sign in to comment.