refactor: delegator

This commit is contained in:
MiniFrenchBread 2024-06-07 15:47:56 +08:00 committed by 0g-wh
parent b62eea0798
commit 22077a98ca
8 changed files with 138 additions and 54 deletions

View File

@ -11,9 +11,10 @@ option go_package = "github.com/0glabs/0g-chain/x/dasigners/v1/types";
message Params {
string tokens_per_vote = 1;
uint64 max_quorums = 2;
uint64 epoch_blocks = 3;
uint64 encoded_slices = 4;
uint64 max_votes_per_signer = 2;
uint64 max_quorums = 3;
uint64 epoch_blocks = 4;
uint64 encoded_slices = 5;
}
// GenesisState defines the dasigners module's genesis state.

View File

@ -2,6 +2,7 @@ package keeper
import (
"bytes"
"math/big"
"sort"
"github.com/0glabs/0g-chain/x/dasigners/v1/types"
@ -45,16 +46,16 @@ func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
}
for _, registration := range registrations {
// get validator
valAddr, err := sdk.ValAddressFromHex(registration.account)
accAddr, err := sdk.AccAddressFromHexUnsafe(registration.account)
if err != nil {
k.Logger(ctx).Error("[BeginBlock] invalid account")
continue
}
validator, found := k.stakingKeeper.GetValidator(ctx, valAddr)
if !found {
continue
bonded := k.GetDelegatorBonded(ctx, accAddr)
num := bonded.Quo(sdk.NewInt(1_000_000_000_000_000_000)).Quo(tokensPerVote).Abs().BigInt()
if num.Cmp(big.NewInt(int64(params.MaxVotesPerSigner))) > 0 {
num = big.NewInt(int64(params.MaxVotesPerSigner))
}
num := validator.Tokens.Quo(sdk.NewInt(1_000_000_000_000_000_000)).Quo(tokensPerVote).Abs().BigInt()
content := registration.content
ballotNum := num.Int64()
for j := 0; j < int(ballotNum); j += 1 {

View File

@ -2,11 +2,15 @@ package keeper
import (
"encoding/hex"
"fmt"
"math/big"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/0glabs/0g-chain/x/dasigners/v1/types"
@ -196,3 +200,42 @@ func (k Keeper) SetRegistration(ctx sdk.Context, epoch uint64, account string, s
store.Set(key, signature)
return nil
}
func (k Keeper) GetDelegatorBonded(ctx sdk.Context, delegator sdk.AccAddress) math.Int {
bonded := sdk.ZeroDec()
cnt := 0
k.stakingKeeper.IterateDelegatorDelegations(ctx, delegator, func(delegation stakingtypes.Delegation) bool {
validatorAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
panic(err) // shouldn't happen
}
validator, found := k.stakingKeeper.GetValidator(ctx, validatorAddr)
if found {
shares := delegation.Shares
tokens := validator.TokensFromSharesTruncated(shares)
bonded = bonded.Add(tokens)
}
cnt += 1
return cnt > 10
})
return bonded.RoundInt()
}
func (k Keeper) CheckDelegations(ctx sdk.Context, account string) error {
accAddr, err := sdk.AccAddressFromHexUnsafe(account)
if err != nil {
return err
}
bonded := k.GetDelegatorBonded(ctx, accAddr)
fmt.Printf("delegation: %v\n", bonded)
params := k.GetParams(ctx)
tokensPerVote, ok := sdk.NewIntFromString(params.TokensPerVote)
if !ok {
panic("failed to load params tokens_per_vote")
}
if bonded.Quo(sdk.NewInt(1_000_000_000_000_000_000)).Quo(tokensPerVote).Abs().BigInt().Cmp(big.NewInt(0)) <= 0 {
return types.ErrInsufficientBonded
}
return nil
}

View File

@ -6,7 +6,6 @@ import (
"github.com/0glabs/0g-chain/crypto/bn254util"
"github.com/0glabs/0g-chain/x/dasigners/v1/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/ethereum/go-ethereum/common"
etherminttypes "github.com/evmos/ethermint/types"
)
@ -16,15 +15,11 @@ var _ types.MsgServer = &Keeper{}
func (k Keeper) RegisterSigner(goCtx context.Context, msg *types.MsgRegisterSigner) (*types.MsgRegisterSignerResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// validate sender
valAddr, err := sdk.ValAddressFromHex(msg.Signer.Account)
err := k.CheckDelegations(ctx, msg.Signer.Account)
if err != nil {
return nil, err
}
_, found := k.stakingKeeper.GetValidator(ctx, valAddr)
if !found {
return nil, stakingtypes.ErrNoValidatorFound
}
_, found, err = k.GetSigner(ctx, msg.Signer.Account)
_, found, err := k.GetSigner(ctx, msg.Signer.Account)
if err != nil {
return nil, err
}
@ -66,6 +61,10 @@ func (k Keeper) UpdateSocket(goCtx context.Context, msg *types.MsgUpdateSocket)
func (k Keeper) RegisterNextEpoch(goCtx context.Context, msg *types.MsgRegisterNextEpoch) (*types.MsgRegisterNextEpochResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// get signer
err := k.CheckDelegations(ctx, msg.Account)
if err != nil {
return nil, err
}
signer, found, err := k.GetSigner(ctx, msg.Account)
if err != nil {
return nil, err

View File

@ -10,4 +10,5 @@ var (
ErrQuorumNotFound = errorsmod.Register(ModuleName, 5, "quorum for epoch not found")
ErrQuorumIdOutOfBound = errorsmod.Register(ModuleName, 6, "quorum id out of bound")
ErrQuorumBitmapLengthMismatch = errorsmod.Register(ModuleName, 7, "quorum bitmap length mismatch")
ErrInsufficientBonded = errorsmod.Register(ModuleName, 8, "insufficient bonded amount")
)

View File

@ -15,10 +15,11 @@ func NewGenesisState(params Params, epoch uint64, signers []*Signer, quorumsByEp
// DefaultGenesisState returns the default genesis state for the module.
func DefaultGenesisState() *GenesisState {
return NewGenesisState(Params{
TokensPerVote: "100",
MaxQuorums: 100,
EpochBlocks: 1000,
EncodedSlices: 3072,
TokensPerVote: "100",
MaxVotesPerSigner: 100,
MaxQuorums: 100,
EpochBlocks: 1000,
EncodedSlices: 3072,
}, 0, make([]*Signer, 0), []*Quorums{{
Quorums: make([]*Quorum, 0),
}})

View File

@ -27,10 +27,11 @@ var _ = math.Inf
const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
type Params struct {
TokensPerVote string `protobuf:"bytes,1,opt,name=tokens_per_vote,json=tokensPerVote,proto3" json:"tokens_per_vote,omitempty"`
MaxQuorums uint64 `protobuf:"varint,2,opt,name=max_quorums,json=maxQuorums,proto3" json:"max_quorums,omitempty"`
EpochBlocks uint64 `protobuf:"varint,3,opt,name=epoch_blocks,json=epochBlocks,proto3" json:"epoch_blocks,omitempty"`
EncodedSlices uint64 `protobuf:"varint,4,opt,name=encoded_slices,json=encodedSlices,proto3" json:"encoded_slices,omitempty"`
TokensPerVote string `protobuf:"bytes,1,opt,name=tokens_per_vote,json=tokensPerVote,proto3" json:"tokens_per_vote,omitempty"`
MaxVotesPerSigner uint64 `protobuf:"varint,2,opt,name=max_votes_per_signer,json=maxVotesPerSigner,proto3" json:"max_votes_per_signer,omitempty"`
MaxQuorums uint64 `protobuf:"varint,3,opt,name=max_quorums,json=maxQuorums,proto3" json:"max_quorums,omitempty"`
EpochBlocks uint64 `protobuf:"varint,4,opt,name=epoch_blocks,json=epochBlocks,proto3" json:"epoch_blocks,omitempty"`
EncodedSlices uint64 `protobuf:"varint,5,opt,name=encoded_slices,json=encodedSlices,proto3" json:"encoded_slices,omitempty"`
}
func (m *Params) Reset() { *m = Params{} }
@ -73,6 +74,13 @@ func (m *Params) GetTokensPerVote() string {
return ""
}
func (m *Params) GetMaxVotesPerSigner() uint64 {
if m != nil {
return m.MaxVotesPerSigner
}
return 0
}
func (m *Params) GetMaxQuorums() uint64 {
if m != nil {
return m.MaxQuorums
@ -175,33 +183,35 @@ func init() {
func init() { proto.RegisterFile("zgc/dasigners/v1/genesis.proto", fileDescriptor_896efa766aaca3be) }
var fileDescriptor_896efa766aaca3be = []byte{
// 416 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x91, 0xc1, 0x6e, 0x13, 0x31,
0x10, 0x86, 0xb3, 0x24, 0x0a, 0xc2, 0x69, 0x4b, 0xb5, 0xe2, 0xb0, 0xe9, 0x61, 0x13, 0x2a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 0x21, 0x7d,
0xea, 0xcb, 0x5f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x28, 0xe1, 0x73, 0x5d, 0x97, 0x02, 0x00, 0x00,
// 436 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x92, 0xc1, 0x6e, 0x13, 0x31,
0x10, 0x40, 0xb3, 0x24, 0x04, 0xe1, 0xb4, 0xa5, 0x58, 0x3d, 0x38, 0x3d, 0x6c, 0x42, 0x25, 0x50,
0x2f, 0xac, 0xdb, 0x22, 0xf1, 0x01, 0x41, 0x08, 0x71, 0x41, 0x65, 0x23, 0x71, 0xe0, 0xb2, 0xf2,
0x3a, 0xc6, 0x59, 0x35, 0xde, 0x59, 0xd6, 0xde, 0x28, 0xe9, 0x57, 0xf0, 0x59, 0x3d, 0x70, 0xe8,
0x91, 0x13, 0x42, 0xc9, 0x8f, 0xa0, 0x1d, 0x9b, 0x56, 0xb4, 0xbd, 0xd9, 0x6f, 0xde, 0x8c, 0x66,
0xc6, 0x26, 0xf1, 0xa5, 0x96, 0x7c, 0x26, 0x6c, 0xa1, 0x4b, 0x55, 0x5b, 0xbe, 0x3c, 0xe5, 0x5a,
0x95, 0xca, 0x16, 0x36, 0xa9, 0x6a, 0x70, 0x40, 0xf7, 0x2f, 0xb5, 0x4c, 0x6e, 0xe2, 0xc9, 0xf2,
0xf4, 0x70, 0x28, 0xc1, 0x1a, 0xb0, 0x19, 0xc6, 0xb9, 0xbf, 0x78, 0xf9, 0xf0, 0x40, 0x83, 0x06,
0xcf, 0xdb, 0x53, 0xa0, 0x43, 0x0d, 0xa0, 0x17, 0x8a, 0xe3, 0x2d, 0x6f, 0xbe, 0x71, 0x51, 0xae,
0x43, 0x68, 0x74, 0x37, 0xe4, 0x0a, 0xa3, 0xac, 0x13, 0xa6, 0x0a, 0xc2, 0xf8, 0x5e, 0x7b, 0xb7,
0xbd, 0xa0, 0x71, 0x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0x5f, 0x67, 0x58, 0x8d, 0xf5,
0x30, 0x79, 0x78, 0x3f, 0x39, 0xac, 0x27, 0xdd, 0x0b, 0x29, 0x93, 0xf5, 0x7b, 0xdc, 0xc8, 0xc7,
0xab, 0x4d, 0x1c, 0x5d, 0x6f, 0xe2, 0xe8, 0xcf, 0x26, 0x8e, 0x7e, 0x6c, 0xe3, 0xce, 0xf5, 0x36,
0xee, 0xfc, 0xda, 0xc6, 0x9d, 0xaf, 0x5c, 0x17, 0x6e, 0xde, 0xe4, 0x89, 0x04, 0xc3, 0x4f, 0xf4,
0x42, 0xe4, 0x96, 0x9f, 0xe8, 0xd7, 0x72, 0x2e, 0x8a, 0x92, 0xaf, 0xfe, 0xff, 0x08, 0x6e, 0x5d,
0x29, 0x9b, 0xf7, 0xf1, 0x17, 0xbc, 0xf9, 0x1b, 0x00, 0x00, 0xff, 0xff, 0x79, 0x90, 0xf6, 0x00,
0xc8, 0x02, 0x00, 0x00,
}
func (m *Params) Marshal() (dAtA []byte, err error) {
@ -227,16 +237,21 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) {
if m.EncodedSlices != 0 {
i = encodeVarintGenesis(dAtA, i, uint64(m.EncodedSlices))
i--
dAtA[i] = 0x20
dAtA[i] = 0x28
}
if m.EpochBlocks != 0 {
i = encodeVarintGenesis(dAtA, i, uint64(m.EpochBlocks))
i--
dAtA[i] = 0x18
dAtA[i] = 0x20
}
if m.MaxQuorums != 0 {
i = encodeVarintGenesis(dAtA, i, uint64(m.MaxQuorums))
i--
dAtA[i] = 0x18
}
if m.MaxVotesPerSigner != 0 {
i = encodeVarintGenesis(dAtA, i, uint64(m.MaxVotesPerSigner))
i--
dAtA[i] = 0x10
}
if len(m.TokensPerVote) > 0 {
@ -336,6 +351,9 @@ func (m *Params) Size() (n int) {
if l > 0 {
n += 1 + l + sovGenesis(uint64(l))
}
if m.MaxVotesPerSigner != 0 {
n += 1 + sovGenesis(uint64(m.MaxVotesPerSigner))
}
if m.MaxQuorums != 0 {
n += 1 + sovGenesis(uint64(m.MaxQuorums))
}
@ -442,6 +460,25 @@ func (m *Params) Unmarshal(dAtA []byte) error {
m.TokensPerVote = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 2:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field MaxVotesPerSigner", wireType)
}
m.MaxVotesPerSigner = 0
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowGenesis
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
m.MaxVotesPerSigner |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
case 3:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field MaxQuorums", wireType)
}
@ -460,7 +497,7 @@ func (m *Params) Unmarshal(dAtA []byte) error {
break
}
}
case 3:
case 4:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field EpochBlocks", wireType)
}
@ -479,7 +516,7 @@ func (m *Params) Unmarshal(dAtA []byte) error {
break
}
}
case 4:
case 5:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field EncodedSlices", wireType)
}

View File

@ -7,4 +7,5 @@ import (
type StakingKeeper interface {
GetValidator(ctx sdk.Context, addr sdk.ValAddress) (validator stakingtypes.Validator, found bool)
IterateDelegatorDelegations(ctx sdk.Context, delegator sdk.AccAddress, cb func(delegation stakingtypes.Delegation) (stop bool))
}