refactor: epoch quorum storage

This commit is contained in:
MiniFrenchBread 2024-06-14 18:29:01 +08:00
parent 455d173924
commit f3f9b706ea
4 changed files with 45 additions and 36 deletions

View File

@ -40,11 +40,19 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
})
epochQuorums := make([]*types.Quorums, 0)
for i := 0; i < int(epochNumber); i += 1 {
quorums, found := keeper.GetEpochQuorums(ctx, uint64(i))
if !found {
quorumCnt, err := keeper.GetQuorumCount(ctx, uint64(i))
if err != nil {
panic("historical quorums not found")
}
epochQuorums = append(epochQuorums, &quorums)
quorums := make([]*types.Quorum, quorumCnt)
for quorumId := uint64(0); quorumId < quorumCnt; quorumId += 1 {
quorum, err := keeper.GetEpochQuorum(ctx, uint64(i), quorumId)
if err != nil {
panic("failed to load historical quorum")
}
quorums[quorumId] = &quorum
}
epochQuorums = append(epochQuorums, &types.Quorums{Quorums: quorums})
}
return types.NewGenesisState(params, epochNumber, signers, epochQuorums)
}

View File

@ -57,26 +57,20 @@ func (k Keeper) QuorumCount(
func (k Keeper) EpochQuorum(c context.Context, request *types.QueryEpochQuorumRequest) (*types.QueryEpochQuorumResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
quorums, found := k.GetEpochQuorums(ctx, request.EpochNumber)
if !found {
return nil, types.ErrQuorumNotFound
quorum, err := k.GetEpochQuorum(ctx, request.EpochNumber, request.QuorumId)
if err != nil {
return nil, err
}
if len(quorums.Quorums) <= int(request.QuorumId) {
return nil, types.ErrQuorumIdOutOfBound
}
return &types.QueryEpochQuorumResponse{Quorum: quorums.Quorums[request.QuorumId]}, nil
return &types.QueryEpochQuorumResponse{Quorum: &quorum}, nil
}
func (k Keeper) EpochQuorumRow(c context.Context, request *types.QueryEpochQuorumRowRequest) (*types.QueryEpochQuorumRowResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
quorums, found := k.GetEpochQuorums(ctx, request.EpochNumber)
if !found {
return nil, types.ErrQuorumNotFound
quorum, err := k.GetEpochQuorum(ctx, request.EpochNumber, request.QuorumId)
if err != nil {
return nil, err
}
if len(quorums.Quorums) <= int(request.QuorumId) {
return nil, types.ErrQuorumIdOutOfBound
}
signers := quorums.Quorums[request.QuorumId].Signers
signers := quorum.Signers
if len(signers) <= int(request.RowIndex) {
return nil, types.ErrRowIndexOutOfBound
}
@ -85,14 +79,10 @@ func (k Keeper) EpochQuorumRow(c context.Context, request *types.QueryEpochQuoru
func (k Keeper) AggregatePubkeyG1(c context.Context, request *types.QueryAggregatePubkeyG1Request) (*types.QueryAggregatePubkeyG1Response, error) {
ctx := sdk.UnwrapSDKContext(c)
quorums, found := k.GetEpochQuorums(ctx, request.EpochNumber)
if !found {
return nil, types.ErrQuorumNotFound
quorum, err := k.GetEpochQuorum(ctx, request.EpochNumber, request.QuorumId)
if err != nil {
return nil, err
}
if len(quorums.Quorums) <= int(request.QuorumId) {
return nil, types.ErrQuorumIdOutOfBound
}
quorum := quorums.Quorums[request.QuorumId]
if (len(quorum.Signers)+7)/8 != len(request.QuorumBitmap) {
return nil, types.ErrQuorumBitmapLengthMismatch
}

View File

@ -142,21 +142,30 @@ func (k Keeper) IterateSigners(ctx sdk.Context, fn func(index int64, signer type
}
}
func (k Keeper) GetEpochQuorums(ctx sdk.Context, epoch uint64) (types.Quorums, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.EpochQuorumsKeyPrefix)
bz := store.Get(types.GetEpochQuorumsKeyFromEpoch(epoch))
if bz == nil {
return types.Quorums{}, false
func (k Keeper) GetEpochQuorum(ctx sdk.Context, epoch uint64, quorumId uint64) (types.Quorum, error) {
quorumCount, err := k.GetQuorumCount(ctx, epoch)
if err != nil {
return types.Quorum{}, err
}
var quorums types.Quorums
k.cdc.MustUnmarshal(bz, &quorums)
return quorums, true
if quorumCount <= quorumId {
return types.Quorum{}, types.ErrQuorumIdOutOfBound
}
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.EpochQuorumsKeyPrefix)
bz := store.Get(types.GetEpochQuorumKey(epoch, quorumId))
if bz == nil {
return types.Quorum{}, types.ErrQuorumNotFound
}
var quorum types.Quorum
k.cdc.MustUnmarshal(bz, &quorum)
return quorum, nil
}
func (k Keeper) SetEpochQuorums(ctx sdk.Context, epoch uint64, quorums types.Quorums) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.EpochQuorumsKeyPrefix)
bz := k.cdc.MustMarshal(&quorums)
store.Set(types.GetEpochQuorumsKeyFromEpoch(epoch), bz)
for quorumId, quorum := range quorums.Quorums {
bz := k.cdc.MustMarshal(quorum)
store.Set(types.GetEpochQuorumKey(epoch, uint64(quorumId)), bz)
}
k.SetQuorumCount(ctx, epoch, uint64(len(quorums.Quorums)))
}

View File

@ -33,8 +33,10 @@ func GetSignerKeyFromAccount(account string) ([]byte, error) {
return hex.DecodeString(account)
}
func GetEpochQuorumsKeyFromEpoch(epoch uint64) []byte {
return sdk.Uint64ToBigEndian(epoch)
func GetEpochQuorumKey(epoch uint64, quorumId uint64) []byte {
b := sdk.Uint64ToBigEndian(epoch)
b = append(b, sdk.Uint64ToBigEndian(quorumId)...)
return b
}
func GetQuorumCountKey(epoch uint64) []byte {