Skip to content

Commit

Permalink
refactor abci tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Sep 11, 2024
1 parent 16d21c4 commit 9adb64e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 35 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* [2725](https://github.com/zeta-chain/node/pull/2725) - refactor SetCctxAndNonceToCctxAndInboundHashToCctx to receive tsspubkey as an argument
* [2802](https://github.com/zeta-chain/node/pull/2802) - set default liquidity cap for new ZRC20s
* [2826](https://github.com/zeta-chain/node/pull/2826) - remove unused code from emissions module and add new parameter for fixed block reward amount
* [2863](https://github.com/zeta-chain/node/pull/2863) - delete ballots after they mature

### Tests

Expand Down
29 changes: 29 additions & 0 deletions typescript/zetachain/zetacore/observer/ballot_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,32 @@ export declare class BallotListForHeight extends Message<BallotListForHeight> {
static equals(a: BallotListForHeight | PlainMessage<BallotListForHeight> | undefined, b: BallotListForHeight | PlainMessage<BallotListForHeight> | undefined): boolean;
}

/**
* @generated from message zetachain.zetacore.observer.VoterList
*/
export declare class VoterList extends Message<VoterList> {
/**
* @generated from field: string voter_address = 1;
*/
voterAddress: string;

/**
* @generated from field: zetachain.zetacore.observer.VoteType vote_type = 2;
*/
voteType: VoteType;

constructor(data?: PartialMessage<VoterList>);

static readonly runtime: typeof proto3;
static readonly typeName = "zetachain.zetacore.observer.VoterList";
static readonly fields: FieldList;

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): VoterList;

static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): VoterList;

static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): VoterList;

static equals(a: VoterList | PlainMessage<VoterList> | undefined, b: VoterList | PlainMessage<VoterList> | undefined): boolean;
}

5 changes: 3 additions & 2 deletions typescript/zetachain/zetacore/observer/events_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "@bufbuild/protobuf";
import { Message, proto3 } from "@bufbuild/protobuf";
import type { VoterList } from "./ballot_pb.js";
import type { GasPriceIncreaseFlags } from "./crosschain_flags_pb.js";

/**
Expand Down Expand Up @@ -71,9 +72,9 @@ export declare class EventBallotDeleted extends Message<EventBallotDeleted> {
ballotType: string;

/**
* @generated from field: string votes_collected = 4;
* @generated from field: repeated zetachain.zetacore.observer.VoterList voters = 4;
*/
votesCollected: string;
voters: VoterList[];

constructor(data?: PartialMessage<EventBallotDeleted>);

Expand Down
31 changes: 1 addition & 30 deletions typescript/zetachain/zetacore/observer/query_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { ChainNonces } from "./chain_nonces_pb.js";
import type { PageRequest, PageResponse } from "../../../cosmos/base/query/v1beta1/pagination_pb.js";
import type { PendingNonces } from "./pending_nonces_pb.js";
import type { TSS } from "./tss_pb.js";
import type { BallotStatus, VoteType } from "./ballot_pb.js";
import type { BallotStatus, VoterList } from "./ballot_pb.js";
import type { LastObserverCount, ObservationType } from "./observer_pb.js";
import type { Chain } from "../pkg/chains/chains_pb.js";
import type { ChainParams, ChainParamsList } from "./params_pb.js";
Expand Down Expand Up @@ -596,35 +596,6 @@ export declare class QueryBallotByIdentifierRequest extends Message<QueryBallotB
static equals(a: QueryBallotByIdentifierRequest | PlainMessage<QueryBallotByIdentifierRequest> | undefined, b: QueryBallotByIdentifierRequest | PlainMessage<QueryBallotByIdentifierRequest> | undefined): boolean;
}

/**
* @generated from message zetachain.zetacore.observer.VoterList
*/
export declare class VoterList extends Message<VoterList> {
/**
* @generated from field: string voter_address = 1;
*/
voterAddress: string;

/**
* @generated from field: zetachain.zetacore.observer.VoteType vote_type = 2;
*/
voteType: VoteType;

constructor(data?: PartialMessage<VoterList>);

static readonly runtime: typeof proto3;
static readonly typeName = "zetachain.zetacore.observer.VoterList";
static readonly fields: FieldList;

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): VoterList;

static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): VoterList;

static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): VoterList;

static equals(a: VoterList | PlainMessage<VoterList> | undefined, b: VoterList | PlainMessage<VoterList> | undefined): boolean;
}

/**
* @generated from message zetachain.zetacore.observer.QueryBallotByIdentifierResponse
*/
Expand Down
2 changes: 1 addition & 1 deletion x/emissions/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
observertypes "github.com/zeta-chain/node/x/observer/types"

"github.com/zeta-chain/node/cmd/zetacored/config"
"github.com/zeta-chain/node/x/emissions/keeper"
"github.com/zeta-chain/node/x/emissions/types"
observertypes "github.com/zeta-chain/node/x/observer/types"
)

func BeginBlocker(ctx sdk.Context, keeper keeper.Keeper) {
Expand Down
22 changes: 21 additions & 1 deletion x/emissions/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ func TestBeginBlocker(t *testing.T) {
})

t.Run("successfully distribute rewards", func(t *testing.T) {
numberOfTestBlocks := 100
//Arrange
numberOfTestBlocks := 10
k, ctx, sk, zk := keepertest.EmissionsKeeper(t)
observerSet := sample.ObserverSet(10)
zk.ObserverKeeper.SetObserverSet(ctx, observerSet)
Expand Down Expand Up @@ -238,6 +239,9 @@ func TestBeginBlocker(t *testing.T) {

params, found := k.GetParams(ctx)
require.True(t, found)
// Set the ballot maturity blocks to numberOfTestBlocks so that the ballot mature at the end of the for loop which produces blocks
params.BallotMaturityBlocks = int64(numberOfTestBlocks)
err = k.SetParams(ctx, params)

// Get the rewards distribution, this is a fixed amount based on total block rewards and distribution percentages
validatorRewardsForABlock, observerRewardsForABlock, tssSignerRewardsForABlock := emissionstypes.GetRewardsDistributions(
Expand All @@ -247,6 +251,11 @@ func TestBeginBlocker(t *testing.T) {
distributedRewards := observerRewardsForABlock.Add(validatorRewardsForABlock).Add(tssSignerRewardsForABlock)
require.True(t, blockRewards.TruncateInt().GT(distributedRewards))

require.Len(t, zk.ObserverKeeper.GetAllBallots(ctx), len(ballotList))
_, found = zk.ObserverKeeper.GetBallotList(ctx, 0)
require.True(t, found)

// Act
for i := 0; i < numberOfTestBlocks; i++ {
emissionPoolBeforeBlockDistribution := sk.BankKeeper.GetBalance(ctx, emissionPool, config.BaseDenom).Amount
// produce a block
Expand Down Expand Up @@ -277,12 +286,23 @@ func TestBeginBlocker(t *testing.T) {
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
}

// Assert

// 1. Assert Observer rewards, these are distributed at the block in which the ballots mature.
// numberOfTestBlocks is the same maturity blocks for the ballots

// We can simplify the calculation as the rewards are distributed equally among all the observers
rewardPerUnit := observerRewardsForABlock.Quo(
sdk.NewInt(int64(len(ballotList) * len(observerSet.ObserverList))),
)
emissionAmount := rewardPerUnit.Mul(sdk.NewInt(int64(len(ballotList))))

// 2 . Assert ballots and ballot list are deleted on maturity
require.Len(t, zk.ObserverKeeper.GetAllBallots(ctx), 0)
_, found = zk.ObserverKeeper.GetBallotList(ctx, 0)
require.False(t, found)

//3. Assert amounts in undistributed pools
// Check if the rewards are distributed equally among all the observers
for _, observer := range observerSet.ObserverList {
observerEmission, found := k.GetWithdrawableEmission(ctx, observer)
Expand Down
6 changes: 5 additions & 1 deletion x/observer/keeper/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ func TestEmitEventBallotDeleted(t *testing.T) {
if attr.Key == "voters" {
expectedString := ""
for _, voter := range ballot.GenerateVoterList() {
st := fmt.Sprintf("{\"voter_address\":\"%s\",\"vote_type\":\"%s\"}", voter.VoterAddress, voter.VoteType)
st := fmt.Sprintf(
"{\"voter_address\":\"%s\",\"vote_type\":\"%s\"}",
voter.VoterAddress,
voter.VoteType,
)
expectedString += st
expectedString += ","
}
Expand Down

0 comments on commit 9adb64e

Please sign in to comment.