fix: decimals

This commit is contained in:
MiniFrenchBread 2024-06-11 15:20:30 +08:00
parent 7feec1d03b
commit ff01dec980
5 changed files with 57 additions and 72 deletions

View File

@ -10,7 +10,7 @@ import "zgc/dasigners/v1/dasigners.proto";
option go_package = "github.com/0glabs/0g-chain/x/dasigners/v1/types"; option go_package = "github.com/0glabs/0g-chain/x/dasigners/v1/types";
message Params { message Params {
string tokens_per_vote = 1; uint64 tokens_per_vote = 1;
uint64 max_votes_per_signer = 2; uint64 max_votes_per_signer = 2;
uint64 max_quorums = 3; uint64 max_quorums = 3;
uint64 epoch_blocks = 4; uint64 epoch_blocks = 4;

View File

@ -2,6 +2,7 @@ package keeper
import ( import (
"bytes" "bytes"
"fmt"
"math/big" "math/big"
"sort" "sort"
@ -40,10 +41,7 @@ func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
return false return false
}) })
ballots := []Ballot{} ballots := []Ballot{}
tokensPerVote, ok := sdk.NewIntFromString(params.TokensPerVote) tokensPerVote := sdk.NewIntFromUint64(params.TokensPerVote)
if !ok {
panic("failed to load params tokens_per_vote")
}
for _, registration := range registrations { for _, registration := range registrations {
// get validator // get validator
accAddr, err := sdk.AccAddressFromHexUnsafe(registration.account) accAddr, err := sdk.AccAddressFromHexUnsafe(registration.account)
@ -52,10 +50,12 @@ func (k Keeper) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
continue continue
} }
bonded := k.GetDelegatorBonded(ctx, accAddr) bonded := k.GetDelegatorBonded(ctx, accAddr)
num := bonded.Quo(sdk.NewInt(1_000_000_000_000_000_000)).Quo(tokensPerVote).Abs().BigInt() num := bonded.Quo(BondedConversionRate).Quo(tokensPerVote).Abs().BigInt()
fmt.Printf("ballots num: %v\n", num)
if num.Cmp(big.NewInt(int64(params.MaxVotesPerSigner))) > 0 { if num.Cmp(big.NewInt(int64(params.MaxVotesPerSigner))) > 0 {
num = big.NewInt(int64(params.MaxVotesPerSigner)) num = big.NewInt(int64(params.MaxVotesPerSigner))
} }
fmt.Printf("ballots num limited: %v\n", num)
content := registration.content content := registration.content
ballotNum := num.Int64() ballotNum := num.Int64()
for j := 0; j < int(ballotNum); j += 1 { for j := 0; j < int(ballotNum); j += 1 {

View File

@ -13,9 +13,12 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/libs/log"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/x/dasigners/v1/types" "github.com/0glabs/0g-chain/x/dasigners/v1/types"
) )
var BondedConversionRate = math.NewIntFromBigInt(big.NewInt(0).Exp(big.NewInt(10), big.NewInt(chaincfg.GasDenomUnit), nil))
type Keeper struct { type Keeper struct {
storeKey storetypes.StoreKey storeKey storetypes.StoreKey
cdc codec.BinaryCodec cdc codec.BinaryCodec
@ -228,13 +231,10 @@ func (k Keeper) CheckDelegations(ctx sdk.Context, account string) error {
return err return err
} }
bonded := k.GetDelegatorBonded(ctx, accAddr) bonded := k.GetDelegatorBonded(ctx, accAddr)
fmt.Printf("delegation: %v\n", bonded)
params := k.GetParams(ctx) params := k.GetParams(ctx)
tokensPerVote, ok := sdk.NewIntFromString(params.TokensPerVote) tokensPerVote := sdk.NewIntFromUint64(params.TokensPerVote)
if !ok { fmt.Printf("account: %v, bonded: %v, conversion rate: %v, ticket: %v\n", account, bonded, BondedConversionRate, bonded.Quo(BondedConversionRate).Quo(tokensPerVote))
panic("failed to load params tokens_per_vote") if bonded.Quo(BondedConversionRate).Quo(tokensPerVote).Abs().BigInt().Cmp(big.NewInt(0)) <= 0 {
}
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 types.ErrInsufficientBonded
} }
return nil return nil

View File

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

View File

@ -27,7 +27,7 @@ var _ = math.Inf
const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
type Params struct { type Params struct {
TokensPerVote string `protobuf:"bytes,1,opt,name=tokens_per_vote,json=tokensPerVote,proto3" json:"tokens_per_vote,omitempty"` TokensPerVote uint64 `protobuf:"varint,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"` 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"` 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"` EpochBlocks uint64 `protobuf:"varint,4,opt,name=epoch_blocks,json=epochBlocks,proto3" json:"epoch_blocks,omitempty"`
@ -67,11 +67,11 @@ func (m *Params) XXX_DiscardUnknown() {
var xxx_messageInfo_Params proto.InternalMessageInfo var xxx_messageInfo_Params proto.InternalMessageInfo
func (m *Params) GetTokensPerVote() string { func (m *Params) GetTokensPerVote() uint64 {
if m != nil { if m != nil {
return m.TokensPerVote return m.TokensPerVote
} }
return "" return 0
} }
func (m *Params) GetMaxVotesPerSigner() uint64 { func (m *Params) GetMaxVotesPerSigner() uint64 {
@ -183,35 +183,35 @@ func init() {
func init() { proto.RegisterFile("zgc/dasigners/v1/genesis.proto", fileDescriptor_896efa766aaca3be) } func init() { proto.RegisterFile("zgc/dasigners/v1/genesis.proto", fileDescriptor_896efa766aaca3be) }
var fileDescriptor_896efa766aaca3be = []byte{ var fileDescriptor_896efa766aaca3be = []byte{
// 436 bytes of a gzipped FileDescriptorProto // 433 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x92, 0xc1, 0x6e, 0x13, 0x31, 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, 0x10, 0x86, 0xb3, 0x24, 0x04, 0xc9, 0x29, 0xa5, 0x58, 0x3d, 0x6c, 0x7a, 0xd8, 0x84, 0x4a, 0x20,
0x2f, 0xac, 0xdb, 0x22, 0xf1, 0x01, 0x41, 0x08, 0x71, 0x41, 0x65, 0x23, 0x71, 0xe0, 0xb2, 0xf2, 0x2e, 0xac, 0xdb, 0x22, 0xf1, 0x00, 0x41, 0x08, 0x71, 0x41, 0x25, 0x91, 0x38, 0x70, 0x59, 0x79,
0x3a, 0xc6, 0x59, 0x35, 0xde, 0x59, 0xd6, 0xde, 0x28, 0xe9, 0x57, 0xf0, 0x59, 0x3d, 0x70, 0x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xff, 0xff, 0x79, 0x90, 0xf6, 0x00, 0xfa, 0x05, 0xaf, 0xff, 0x04, 0x00, 0x00, 0xff, 0xff, 0xff, 0xe5, 0x90, 0xe2, 0xc8, 0x02, 0x00,
0xc8, 0x02, 0x00, 0x00, 0x00,
} }
func (m *Params) Marshal() (dAtA []byte, err error) { func (m *Params) Marshal() (dAtA []byte, err error) {
@ -254,12 +254,10 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i-- i--
dAtA[i] = 0x10 dAtA[i] = 0x10
} }
if len(m.TokensPerVote) > 0 { if m.TokensPerVote != 0 {
i -= len(m.TokensPerVote) i = encodeVarintGenesis(dAtA, i, uint64(m.TokensPerVote))
copy(dAtA[i:], m.TokensPerVote)
i = encodeVarintGenesis(dAtA, i, uint64(len(m.TokensPerVote)))
i-- i--
dAtA[i] = 0xa dAtA[i] = 0x8
} }
return len(dAtA) - i, nil return len(dAtA) - i, nil
} }
@ -347,9 +345,8 @@ func (m *Params) Size() (n int) {
} }
var l int var l int
_ = l _ = l
l = len(m.TokensPerVote) if m.TokensPerVote != 0 {
if l > 0 { n += 1 + sovGenesis(uint64(m.TokensPerVote))
n += 1 + l + sovGenesis(uint64(l))
} }
if m.MaxVotesPerSigner != 0 { if m.MaxVotesPerSigner != 0 {
n += 1 + sovGenesis(uint64(m.MaxVotesPerSigner)) n += 1 + sovGenesis(uint64(m.MaxVotesPerSigner))
@ -428,10 +425,10 @@ func (m *Params) Unmarshal(dAtA []byte) error {
} }
switch fieldNum { switch fieldNum {
case 1: case 1:
if wireType != 2 { if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field TokensPerVote", wireType) return fmt.Errorf("proto: wrong wireType = %d for field TokensPerVote", wireType)
} }
var stringLen uint64 m.TokensPerVote = 0
for shift := uint(0); ; shift += 7 { for shift := uint(0); ; shift += 7 {
if shift >= 64 { if shift >= 64 {
return ErrIntOverflowGenesis return ErrIntOverflowGenesis
@ -441,24 +438,11 @@ func (m *Params) Unmarshal(dAtA []byte) error {
} }
b := dAtA[iNdEx] b := dAtA[iNdEx]
iNdEx++ iNdEx++
stringLen |= uint64(b&0x7F) << shift m.TokensPerVote |= uint64(b&0x7F) << shift
if b < 0x80 { if b < 0x80 {
break break
} }
} }
intStringLen := int(stringLen)
if intStringLen < 0 {
return ErrInvalidLengthGenesis
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthGenesis
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.TokensPerVote = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 2: case 2:
if wireType != 0 { if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field MaxVotesPerSigner", wireType) return fmt.Errorf("proto: wrong wireType = %d for field MaxVotesPerSigner", wireType)