Skip to content

Commit

Permalink
test: add check in e2e test to ensure deletion of stale ballots (#3503)
Browse files Browse the repository at this point in the history
* sort ballots by creation height

* add check in e2e for stale ballots

* generate files

* add unit test for query ballots

* improve verification logic

* add length check for ballots

* add length check for ballots

* stop checking ballots for upgrade test
  • Loading branch information
kingpinXD authored Feb 11, 2025
1 parent c388b4e commit c2fd4bd
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
### Tests

* [3430](https://github.com/zeta-chain/node/pull/3430) - add simulation test for MsgWithDrawEmission
* [3503](https://github.com/zeta-chain/node/pull/3503) - add check in e2e test to ensure deletion of stale ballots

## v27.0.1

Expand Down
5 changes: 5 additions & 0 deletions cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,11 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
// Verify that the balance of restricted address is zero
deployerRunner.EnsureZeroBalanceOnRestrictedAddressZEVM()

if !deployerRunner.IsRunningUpgrade() {
// Verify that there are no stale ballots left over after tests complete
deployerRunner.EnsureNoStaleBallots()
}

// print and validate report
networkReport, err := deployerRunner.GenerateNetworkReport()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions docs/cli/zetacored/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -6826,12 +6826,18 @@ zetacored query observer list-ballots [flags]
### Options

```
--count-total count total number of records in list-ballots to query for
--grpc-addr string the gRPC endpoint to use for this chain
--grpc-insecure allow gRPC over insecure channels, if not the server must use TLS
--height int Use a specific height to query state at (this can error if the node is pruning state)
-h, --help help for list-ballots
--limit uint pagination limit of list-ballots to query for (default 100)
--node string [host]:[port] to CometBFT RPC interface for this chain
--offset uint pagination offset of list-ballots to query for
-o, --output string Output format (text|json)
--page uint pagination page of list-ballots to query for. This sets offset to a multiple of limit (default 1)
--page-key string pagination page-key of list-ballots to query for
--reverse results are sorted in descending order
```

### Options inherited from parent commands
Expand Down
18 changes: 18 additions & 0 deletions e2e/runner/require.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/zeta-chain/node/testutil/sample"
crosschaintypes "github.com/zeta-chain/node/x/crosschain/types"
emissionstypes "github.com/zeta-chain/node/x/emissions/types"
"github.com/zeta-chain/node/x/observer/types"
)

// EnsureNoTrackers ensures that there are no trackers left on zetacore
Expand Down Expand Up @@ -59,3 +61,19 @@ func ensureZRC20ZeroBalance(r *E2ERunner, zrc20 *zrc20.ZRC20, address ethcommon.
fmt.Sprintf("the balance of address %s should be zero on ZRC20: %s", address, zrc20Name),
)
}

// EnsureNoStaleBallots ensures that there are no stale ballots left on the chain.
func (r *E2ERunner) EnsureNoStaleBallots() {
ballotsRes, err := r.ObserverClient.Ballots(r.Ctx, &types.QueryBallotsRequest{})
require.NoError(r, err)
currentBlockHeight, err := r.Clients.Zetacore.GetBlockHeight(r.Ctx)
require.NoError(r, err)
emissionsParams, err := r.EmissionsClient.Params(r.Ctx, &emissionstypes.QueryParamsRequest{})
require.NoError(r, err)
staleBlockStart := currentBlockHeight - emissionsParams.Params.BallotMaturityBlocks
if len(ballotsRes.Ballots) < 1 {
return
}
firstBallotCreationHeight := ballotsRes.Ballots[0].BallotCreationHeight
require.GreaterOrEqual(r, firstBallotCreationHeight, staleBlockStart, "there should be no stale ballots")
}
1 change: 1 addition & 0 deletions x/observer/client/cli/query_ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ func CmdAllBallots() *cobra.Command {
},
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, cmd.Use)
return cmd
}
5 changes: 5 additions & 0 deletions x/observer/keeper/grpc_query_ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"sort"

"cosmossdk.io/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -83,6 +84,10 @@ func (k Keeper) Ballots(goCtx context.Context, req *types.QueryBallotsRequest) (
return nil
})

sort.Slice(ballots, func(i, j int) bool {
return ballots[i].BallotCreationHeight < ballots[j].BallotCreationHeight
})

if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand Down
7 changes: 6 additions & 1 deletion x/observer/keeper/grpc_query_ballot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func TestKeeper_Ballots(t *testing.T) {
VoterList: []string{sample.AccAddress()},
Votes: []types.VoteType{types.VoteType_SuccessObservation},
BallotStatus: types.BallotStatus_BallotInProgress,
BallotCreationHeight: 1,
BallotCreationHeight: 1 + int64(i),
BallotThreshold: sdkmath.LegacyMustNewDecFromStr("0.5"),
}
k.SetBallot(ctx, &ballot)
Expand All @@ -234,5 +234,10 @@ func TestKeeper_Ballots(t *testing.T) {
res, err := k.Ballots(wctx, &types.QueryBallotsRequest{})
require.NoError(t, err)
require.ElementsMatch(t, ballots, res.Ballots)

firstBallotCreationHeight := res.Ballots[0].BallotCreationHeight
for _, ballot := range res.Ballots {
require.GreaterOrEqual(t, ballot.BallotCreationHeight, firstBallotCreationHeight)
}
})
}

0 comments on commit c2fd4bd

Please sign in to comment.