Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(zetacore): delete ballots after maturity #2863

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0a39fd8
add function to clear ballots
kingpinXD Sep 10, 2024
16d21c4
add unit tests
kingpinXD Sep 10, 2024
9adb64e
refactor abci tests
kingpinXD Sep 11, 2024
0c0d077
Merge branch 'develop' into clear-ballots
kingpinXD Sep 11, 2024
7e771da
Merge branch 'develop' into clear-ballots
kingpinXD Sep 11, 2024
4779bda
refactor generate voter list method
kingpinXD Sep 11, 2024
1799d09
Merge remote-tracking branch 'origin/clear-ballots' into clear-ballots
kingpinXD Sep 11, 2024
afaa312
refactor names for ballot list functions
kingpinXD Sep 16, 2024
bf3c438
refactor ClearMaturedBallots to ClearMaturedBallotsAndBallotList
kingpinXD Sep 23, 2024
fbf277b
make getMaturedBallotHeight private
kingpinXD Sep 26, 2024
8be7f3f
make getMaturedBallotHeight private
kingpinXD Sep 26, 2024
12f9baa
add migration script to delete ballots and tests with mainnet data
kingpinXD Oct 1, 2024
a2c01e1
rebase develop 1
kingpinXD Dec 19, 2024
68bd73a
add test for migration
kingpinXD Dec 19, 2024
b074dda
add a query for all ballots
kingpinXD Dec 19, 2024
3c38438
generate files
kingpinXD Dec 19, 2024
5171ebd
add changelog
kingpinXD Dec 20, 2024
e533f7e
fixed test naming
kingpinXD Dec 20, 2024
3a807ec
update migration comment
kingpinXD Dec 20, 2024
c56fa11
update module version
kingpinXD Dec 23, 2024
0069905
make changes for comments 2
kingpinXD Jan 7, 2025
259d5f8
remove max limit for Ballots query
kingpinXD Jan 7, 2025
04402c4
remove BallotDeleteEvent
kingpinXD Jan 8, 2025
590467a
remove BallotDeleteEvent
kingpinXD Jan 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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
5 changes: 5 additions & 0 deletions proto/zetachain/zetacore/observer/ballot.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ message BallotListForHeight {
int64 height = 1;
repeated string ballots_index_list = 2;
}

message VoterList {
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
string voter_address = 1;
VoteType vote_type = 2;
}
8 changes: 8 additions & 0 deletions proto/zetachain/zetacore/observer/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package zetachain.zetacore.observer;
import "gogoproto/gogo.proto";
import "zetachain/zetacore/observer/crosschain_flags.proto";
import "zetachain/zetacore/observer/observer.proto";
import "zetachain/zetacore/observer/ballot.proto";

option go_package = "github.com/zeta-chain/node/x/observer/types";

Expand All @@ -15,6 +16,13 @@ message EventBallotCreated {
string ballot_type = 5;
}

message EventBallotDeleted {
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
string msg_type_url = 1;
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
string ballot_identifier = 2;
string ballot_type = 3;
repeated VoterList voters = 4 [ (gogoproto.nullable) = false ];
}

message EventKeygenBlockUpdated {
string msg_type_url = 1;
string keygen_block = 2;
Expand Down
6 changes: 1 addition & 5 deletions proto/zetachain/zetacore/observer/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,10 @@ message QueryHasVotedRequest {
message QueryHasVotedResponse { bool has_voted = 1; }

message QueryBallotByIdentifierRequest { string ballot_identifier = 1; }
message VoterList {
string voter_address = 1;
VoteType vote_type = 2;
}

message QueryBallotByIdentifierResponse {
string ballot_identifier = 1;
repeated VoterList voters = 2;
repeated VoterList voters = 2 [ (gogoproto.nullable) = false ];
ObservationType observation_type = 3;
BallotStatus ballot_status = 4;
}
Expand Down
5 changes: 5 additions & 0 deletions testutil/keeper/mocks/emissions/observer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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;
}

40 changes: 40 additions & 0 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 @@ -51,6 +52,45 @@ export declare class EventBallotCreated extends Message<EventBallotCreated> {
static equals(a: EventBallotCreated | PlainMessage<EventBallotCreated> | undefined, b: EventBallotCreated | PlainMessage<EventBallotCreated> | undefined): boolean;
}

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

/**
* @generated from field: string ballot_identifier = 2;
*/
ballotIdentifier: string;

/**
* @generated from field: string ballot_type = 3;
*/
ballotType: string;

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

constructor(data?: PartialMessage<EventBallotDeleted>);

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

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

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

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

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

/**
* @generated from message zetachain.zetacore.observer.EventKeygenBlockUpdated
*/
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
8 changes: 6 additions & 2 deletions x/emissions/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"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 Expand Up @@ -102,11 +103,13 @@ func DistributeObserverRewards(
if len(ballotIdentifiers) == 0 {
return nil
}
ballots := make([]observertypes.Ballot, 0, len(ballotIdentifiers))
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
for _, ballotIdentifier := range ballotIdentifiers {
ballot, found := keeper.GetObserverKeeper().GetBallot(ctx, ballotIdentifier)
if !found {
continue
}
ballots = append(ballots, ballot)
totalRewardsUnits += ballot.BuildRewardsDistribution(rewardsDistributer)
}
rewardPerUnit := sdkmath.ZeroInt()
Expand Down Expand Up @@ -161,8 +164,9 @@ func DistributeObserverRewards(
}
}
types.EmitObserverEmissions(ctx, finalDistributionList)
// TODO : Delete Ballots after distribution
// https://github.com/zeta-chain/node/issues/942

// Clear the matured ballots
keeper.GetObserverKeeper().ClearMaturedBallots(ctx, ballots, params.BallotMaturityBlocks)
return nil
}

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
1 change: 1 addition & 0 deletions x/emissions/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type AccountKeeper interface {
type ObserverKeeper interface {
GetBallot(ctx sdk.Context, index string) (val observertypes.Ballot, found bool)
GetMaturedBallots(ctx sdk.Context, maturityBlocks int64) (val observertypes.BallotListForHeight, found bool)
ClearMaturedBallots(ctx sdk.Context, ballots []observertypes.Ballot, maturityBlocks int64)
}

// BankKeeper defines the expected interface needed to retrieve account balances.
Expand Down
27 changes: 26 additions & 1 deletion x/observer/keeper/ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ func (k Keeper) SetBallotList(ctx sdk.Context, ballotlist *types.BallotListForHe
store.Set(types.BallotListKeyPrefix(ballotlist.Height), b)
}

func (k Keeper) DeleteBallot(ctx sdk.Context, index string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.VoterKey))
store.Delete([]byte(index))
}

func (k Keeper) DeleteBallotList(ctx sdk.Context, height int64) {
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.BallotListKey))
store.Delete(types.BallotListKeyPrefix(height))
}

func (k Keeper) GetBallot(ctx sdk.Context, index string) (val types.Ballot, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.VoterKey))
b := store.Get(types.KeyPrefix(index))
Expand All @@ -41,7 +51,7 @@ func (k Keeper) GetBallotList(ctx sdk.Context, height int64) (val types.BallotLi
}

func (k Keeper) GetMaturedBallots(ctx sdk.Context, maturityBlocks int64) (val types.BallotListForHeight, found bool) {
return k.GetBallotList(ctx, ctx.BlockHeight()-maturityBlocks)
return k.GetBallotList(ctx, GetMaturedBallotHeight(ctx, maturityBlocks))
}

func (k Keeper) GetAllBallots(ctx sdk.Context) (voters []*types.Ballot) {
Expand All @@ -65,3 +75,18 @@ func (k Keeper) AddBallotToList(ctx sdk.Context, ballot types.Ballot) {
list.BallotsIndexList = append(list.BallotsIndexList, ballot.BallotIdentifier)
k.SetBallotList(ctx, &list)
}

lumtis marked this conversation as resolved.
Show resolved Hide resolved
// ClearMaturedBallots deletes all matured ballots and the list of ballots for a given height.
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
// It also emits an event for each ballot deleted.
func (k Keeper) ClearMaturedBallots(ctx sdk.Context, ballots []types.Ballot, maturityBlocks int64) {
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
for _, ballot := range ballots {
k.DeleteBallot(ctx, ballot.BallotIdentifier)
EmitEventBallotDeleted(ctx, ballot)
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
}
k.DeleteBallotList(ctx, GetMaturedBallotHeight(ctx, maturityBlocks))
return
}

func GetMaturedBallotHeight(ctx sdk.Context, maturityBlocks int64) int64 {
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
return ctx.BlockHeight() - maturityBlocks
}
Loading
Loading