refactor: epoch quorum storage

This commit is contained in:
MiniFrenchBread 2024-06-14 18:29:01 +08:00 committed by 0g-wh
parent 1adfdbbc65
commit 22bcf1f42f
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) epochQuorums := make([]*types.Quorums, 0)
for i := 0; i < int(epochNumber); i += 1 { for i := 0; i < int(epochNumber); i += 1 {
quorums, found := keeper.GetEpochQuorums(ctx, uint64(i)) quorumCnt, err := keeper.GetQuorumCount(ctx, uint64(i))
if !found { if err != nil {
panic("historical quorums not found") 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) 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) { func (k Keeper) EpochQuorum(c context.Context, request *types.QueryEpochQuorumRequest) (*types.QueryEpochQuorumResponse, error) {
ctx := sdk.UnwrapSDKContext(c) ctx := sdk.UnwrapSDKContext(c)
quorums, found := k.GetEpochQuorums(ctx, request.EpochNumber) quorum, err := k.GetEpochQuorum(ctx, request.EpochNumber, request.QuorumId)
if !found { if err != nil {
return nil, types.ErrQuorumNotFound return nil, err
} }
if len(quorums.Quorums) <= int(request.QuorumId) { return &types.QueryEpochQuorumResponse{Quorum: &quorum}, nil
return nil, types.ErrQuorumIdOutOfBound
}
return &types.QueryEpochQuorumResponse{Quorum: quorums.Quorums[request.QuorumId]}, nil
} }
func (k Keeper) EpochQuorumRow(c context.Context, request *types.QueryEpochQuorumRowRequest) (*types.QueryEpochQuorumRowResponse, error) { func (k Keeper) EpochQuorumRow(c context.Context, request *types.QueryEpochQuorumRowRequest) (*types.QueryEpochQuorumRowResponse, error) {
ctx := sdk.UnwrapSDKContext(c) ctx := sdk.UnwrapSDKContext(c)
quorums, found := k.GetEpochQuorums(ctx, request.EpochNumber) quorum, err := k.GetEpochQuorum(ctx, request.EpochNumber, request.QuorumId)
if !found { if err != nil {
return nil, types.ErrQuorumNotFound return nil, err
} }
if len(quorums.Quorums) <= int(request.QuorumId) { signers := quorum.Signers
return nil, types.ErrQuorumIdOutOfBound
}
signers := quorums.Quorums[request.QuorumId].Signers
if len(signers) <= int(request.RowIndex) { if len(signers) <= int(request.RowIndex) {
return nil, types.ErrRowIndexOutOfBound 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) { func (k Keeper) AggregatePubkeyG1(c context.Context, request *types.QueryAggregatePubkeyG1Request) (*types.QueryAggregatePubkeyG1Response, error) {
ctx := sdk.UnwrapSDKContext(c) ctx := sdk.UnwrapSDKContext(c)
quorums, found := k.GetEpochQuorums(ctx, request.EpochNumber) quorum, err := k.GetEpochQuorum(ctx, request.EpochNumber, request.QuorumId)
if !found { if err != nil {
return nil, types.ErrQuorumNotFound 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) { if (len(quorum.Signers)+7)/8 != len(request.QuorumBitmap) {
return nil, types.ErrQuorumBitmapLengthMismatch 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) { func (k Keeper) GetEpochQuorum(ctx sdk.Context, epoch uint64, quorumId uint64) (types.Quorum, error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.EpochQuorumsKeyPrefix) quorumCount, err := k.GetQuorumCount(ctx, epoch)
bz := store.Get(types.GetEpochQuorumsKeyFromEpoch(epoch)) if err != nil {
if bz == nil { return types.Quorum{}, err
return types.Quorums{}, false
} }
var quorums types.Quorums if quorumCount <= quorumId {
k.cdc.MustUnmarshal(bz, &quorums) return types.Quorum{}, types.ErrQuorumIdOutOfBound
return quorums, true }
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) { func (k Keeper) SetEpochQuorums(ctx sdk.Context, epoch uint64, quorums types.Quorums) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.EpochQuorumsKeyPrefix) store := prefix.NewStore(ctx.KVStore(k.storeKey), types.EpochQuorumsKeyPrefix)
bz := k.cdc.MustMarshal(&quorums) for quorumId, quorum := range quorums.Quorums {
store.Set(types.GetEpochQuorumsKeyFromEpoch(epoch), bz) bz := k.cdc.MustMarshal(quorum)
store.Set(types.GetEpochQuorumKey(epoch, uint64(quorumId)), bz)
}
k.SetQuorumCount(ctx, epoch, uint64(len(quorums.Quorums))) k.SetQuorumCount(ctx, epoch, uint64(len(quorums.Quorums)))
} }

View File

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