0g-chain/x/council/v1/keeper/abci.go

73 lines
1.5 KiB
Go
Raw Normal View History

2024-04-24 07:10:22 +00:00
package keeper
import (
"sort"
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type Ballot struct {
voter sdk.ValAddress
content string
}
func (k *Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
2024-04-25 06:13:27 +00:00
councilID, err := k.GetCurrentCouncilID(ctx)
2024-04-24 07:10:22 +00:00
if err != nil {
2024-04-25 06:13:27 +00:00
// TODO: handle the case where councilID is not available
2024-04-24 07:10:22 +00:00
return
}
2024-04-25 06:13:27 +00:00
council, bz := k.GetCouncil(ctx, councilID)
2024-04-24 07:10:22 +00:00
if !bz {
return
}
2024-04-25 06:13:27 +00:00
if ctx.BlockHeight() >= int64(council.StartHeight) {
// We are ready to accept votes for the next council
if err := k.StoreNewCouncil(ctx, council.StartHeight); err != nil {
2024-04-24 07:10:22 +00:00
return
}
}
2024-04-25 06:13:27 +00:00
if ctx.BlockHeight() < int64(council.EndHeight) {
2024-04-24 07:10:22 +00:00
return
}
2024-04-25 06:13:27 +00:00
k.IncrementCurrentCouncilID(ctx)
council, bz = k.GetCouncil(ctx, councilID+1)
2024-04-24 07:10:22 +00:00
if !bz {
return
}
ballots := []Ballot{}
seen := make(map[string]struct{})
2024-04-25 06:13:27 +00:00
for _, vote := range council.Votes {
2024-04-24 07:10:22 +00:00
for _, ballot := range vote.Ballots {
ballot := Ballot{
voter: vote.Voter,
content: string(ballot.Content),
}
if _, ok := seen[ballot.content]; ok {
continue
}
ballots = append(ballots, ballot)
seen[ballot.content] = struct{}{}
}
}
sort.Slice(ballots, func(i, j int) bool {
return ballots[i].content < ballots[j].content
})
2024-04-25 06:13:27 +00:00
councilSize := k.GetParams(ctx).CouncilSize
council.Members = make([]sdk.ValAddress, councilSize)
for i := 0; i < int(councilSize); i = i + 1 {
council.Members[i] = ballots[i].voter
2024-04-24 07:10:22 +00:00
}
2024-04-25 06:13:27 +00:00
k.SetCouncil(ctx, council)
2024-04-24 07:10:22 +00:00
}
func (k *Keeper) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) {
}