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

114 lines
3.2 KiB
Go
Raw Normal View History

2024-08-03 17:31:58 +00:00
package keeper
import (
"bytes"
2024-08-03 17:48:22 +00:00
"math/big"
2024-08-03 17:31:58 +00:00
"sort"
"github.com/0glabs/0g-chain/x/dasigners/v1/types"
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/crypto"
)
type Ballot struct {
account string
content []byte
}
func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
epochNumber, err := k.GetEpochNumber(ctx)
if err != nil {
k.Logger(ctx).Error("[BeginBlock] cannot get epoch number")
panic(err)
}
params := k.GetParams(ctx)
expectedEpoch := uint64(ctx.BlockHeight()) / params.EpochBlocks
if expectedEpoch == epochNumber {
return
}
if expectedEpoch > epochNumber+1 || expectedEpoch < epochNumber {
panic("block height is not continuous")
}
// new epoch
registrations := []Ballot{}
k.IterateRegistrations(ctx, expectedEpoch, func(account string, signature []byte) (stop bool) {
registrations = append(registrations, Ballot{
account: account,
content: signature,
})
return false
})
ballots := []Ballot{}
2024-08-03 17:52:49 +00:00
tokensPerVote := sdk.NewIntFromUint64(params.TokensPerVote)
2024-08-03 17:31:58 +00:00
for _, registration := range registrations {
// get validator
2024-08-03 17:48:22 +00:00
accAddr, err := sdk.AccAddressFromHexUnsafe(registration.account)
2024-08-03 17:31:58 +00:00
if err != nil {
k.Logger(ctx).Error("[BeginBlock] invalid account")
continue
}
2024-08-03 17:48:22 +00:00
bonded := k.GetDelegatorBonded(ctx, accAddr)
2024-08-03 17:52:49 +00:00
num := bonded.Quo(BondedConversionRate).Quo(tokensPerVote).Abs().BigInt()
2024-08-03 17:48:22 +00:00
if num.Cmp(big.NewInt(int64(params.MaxVotesPerSigner))) > 0 {
num = big.NewInt(int64(params.MaxVotesPerSigner))
2024-08-03 17:31:58 +00:00
}
content := registration.content
ballotNum := num.Int64()
for j := 0; j < int(ballotNum); j += 1 {
ballots = append(ballots, Ballot{
account: registration.account,
content: content,
})
content = crypto.Keccak256(content)
}
}
sort.Slice(ballots, func(i, j int) bool {
return bytes.Compare(ballots[i].content, ballots[j].content) < 0
})
2024-08-03 17:42:40 +00:00
quorums := types.Quorums{
Quorums: make([]*types.Quorum, 0),
2024-08-03 17:31:58 +00:00
}
2024-08-03 17:42:40 +00:00
if len(ballots) >= int(params.EncodedSlices) {
2024-08-03 17:44:00 +00:00
for i := 0; i+int(params.EncodedSlices) <= len(ballots); i += int(params.EncodedSlices) {
2024-08-03 17:43:46 +00:00
if int(params.MaxQuorums) < len(quorums.Quorums) {
break
}
2024-08-03 17:42:40 +00:00
quorum := types.Quorum{
Signers: make([]string, params.EncodedSlices),
}
for j := 0; j < int(params.EncodedSlices); j += 1 {
quorum.Signers[j] = ballots[i+j].account
}
quorums.Quorums = append(quorums.Quorums, &quorum)
}
2024-08-03 17:44:00 +00:00
if len(ballots)%int(params.EncodedSlices) != 0 && int(params.MaxQuorums) > len(quorums.Quorums) {
2024-08-03 17:43:46 +00:00
quorum := types.Quorum{
Signers: make([]string, 0),
}
for j := len(ballots) - int(params.EncodedSlices); j < len(ballots); j += 1 {
quorum.Signers = append(quorum.Signers, ballots[j].account)
}
quorums.Quorums = append(quorums.Quorums, &quorum)
}
2024-08-03 17:43:25 +00:00
} else if len(ballots) > 0 {
2024-08-03 17:42:40 +00:00
quorum := types.Quorum{
Signers: make([]string, params.EncodedSlices),
2024-08-03 17:31:58 +00:00
}
2024-08-03 17:42:40 +00:00
n := len(ballots)
for i := 0; i < int(params.EncodedSlices); i += 1 {
quorum.Signers[i] = ballots[i%n].account
}
quorums.Quorums = append(quorums.Quorums, &quorum)
2024-08-03 17:43:25 +00:00
} else {
quorums.Quorums = append(quorums.Quorums, &types.Quorum{
Signers: make([]string, 0),
})
2024-08-03 17:31:58 +00:00
}
2024-08-03 17:42:40 +00:00
2024-08-03 17:31:58 +00:00
// save to store
2024-08-03 17:42:40 +00:00
k.SetEpochQuorums(ctx, expectedEpoch, quorums)
2024-08-03 17:31:58 +00:00
k.SetEpochNumber(ctx, expectedEpoch)
}