2024-05-09 18:54:47 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/0glabs/0g-chain/crypto/bn254util"
|
|
|
|
"github.com/0glabs/0g-chain/x/dasigners/v1/types"
|
|
|
|
"github.com/consensys/gnark-crypto/ecc/bn254"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ types.QueryServer = Keeper{}
|
|
|
|
|
|
|
|
func (k Keeper) Signer(
|
|
|
|
c context.Context,
|
2024-05-16 14:49:29 +00:00
|
|
|
request *types.QuerySignerRequest,
|
2024-05-09 18:54:47 +00:00
|
|
|
) (*types.QuerySignerResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-05-16 14:49:29 +00:00
|
|
|
n := len(request.Accounts)
|
|
|
|
response := types.QuerySignerResponse{Signer: make([]*types.Signer, n)}
|
|
|
|
for i := 0; i < n; i += 1 {
|
|
|
|
signer, found, err := k.GetSigner(ctx, request.Accounts[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !found {
|
2024-08-09 05:34:37 +00:00
|
|
|
return nil, types.ErrSignerNotFound
|
2024-05-16 14:49:29 +00:00
|
|
|
}
|
|
|
|
response.Signer[i] = &signer
|
2024-05-09 18:54:47 +00:00
|
|
|
}
|
2024-05-16 14:49:29 +00:00
|
|
|
return &response, nil
|
2024-05-09 18:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k Keeper) EpochNumber(
|
|
|
|
c context.Context,
|
|
|
|
_ *types.QueryEpochNumberRequest,
|
|
|
|
) (*types.QueryEpochNumberResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
epochNumber, err := k.GetEpochNumber(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.QueryEpochNumberResponse{EpochNumber: epochNumber}, nil
|
|
|
|
}
|
|
|
|
|
2024-05-16 14:49:29 +00:00
|
|
|
func (k Keeper) QuorumCount(
|
|
|
|
c context.Context,
|
|
|
|
request *types.QueryQuorumCountRequest,
|
|
|
|
) (*types.QueryQuorumCountResponse, error) {
|
2024-05-09 18:54:47 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-05-16 14:49:29 +00:00
|
|
|
quorumCount, err := k.GetQuorumCount(ctx, request.EpochNumber)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &types.QueryQuorumCountResponse{QuorumCount: quorumCount}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k Keeper) EpochQuorum(c context.Context, request *types.QueryEpochQuorumRequest) (*types.QueryEpochQuorumResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-06-14 10:29:01 +00:00
|
|
|
quorum, err := k.GetEpochQuorum(ctx, request.EpochNumber, request.QuorumId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-05-09 18:54:47 +00:00
|
|
|
}
|
2024-06-14 10:29:01 +00:00
|
|
|
return &types.QueryEpochQuorumResponse{Quorum: &quorum}, nil
|
2024-05-09 18:54:47 +00:00
|
|
|
}
|
|
|
|
|
2024-06-12 05:27:48 +00:00
|
|
|
func (k Keeper) EpochQuorumRow(c context.Context, request *types.QueryEpochQuorumRowRequest) (*types.QueryEpochQuorumRowResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-06-14 10:29:01 +00:00
|
|
|
quorum, err := k.GetEpochQuorum(ctx, request.EpochNumber, request.QuorumId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-06-12 05:27:48 +00:00
|
|
|
}
|
2024-06-14 10:29:01 +00:00
|
|
|
signers := quorum.Signers
|
2024-06-12 05:27:48 +00:00
|
|
|
if len(signers) <= int(request.RowIndex) {
|
|
|
|
return nil, types.ErrRowIndexOutOfBound
|
|
|
|
}
|
|
|
|
return &types.QueryEpochQuorumRowResponse{Signer: signers[request.RowIndex]}, nil
|
|
|
|
}
|
|
|
|
|
2024-05-09 18:54:47 +00:00
|
|
|
func (k Keeper) AggregatePubkeyG1(c context.Context, request *types.QueryAggregatePubkeyG1Request) (*types.QueryAggregatePubkeyG1Response, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-06-14 10:29:01 +00:00
|
|
|
quorum, err := k.GetEpochQuorum(ctx, request.EpochNumber, request.QuorumId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-05-16 14:49:29 +00:00
|
|
|
}
|
|
|
|
if (len(quorum.Signers)+7)/8 != len(request.QuorumBitmap) {
|
|
|
|
return nil, types.ErrQuorumBitmapLengthMismatch
|
2024-05-09 18:54:47 +00:00
|
|
|
}
|
|
|
|
aggPubkeyG1 := new(bn254.G1Affine)
|
2024-05-11 09:13:54 +00:00
|
|
|
hit := 0
|
2024-05-16 14:49:29 +00:00
|
|
|
added := make(map[string]struct{})
|
|
|
|
for i, signer := range quorum.Signers {
|
2024-05-16 15:25:43 +00:00
|
|
|
if _, ok := added[signer]; ok {
|
|
|
|
hit += 1
|
2024-05-09 18:54:47 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-05-16 15:25:43 +00:00
|
|
|
b := request.QuorumBitmap[i/8] & (1 << (i % 8))
|
|
|
|
if b == 0 {
|
2024-05-16 14:49:29 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-05-16 15:25:43 +00:00
|
|
|
hit += 1
|
2024-05-16 14:49:29 +00:00
|
|
|
added[signer] = struct{}{}
|
|
|
|
signer, found, err := k.GetSigner(ctx, signer)
|
2024-05-09 18:54:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return nil, types.ErrSignerNotFound
|
|
|
|
}
|
|
|
|
aggPubkeyG1.Add(aggPubkeyG1, bn254util.DeserializeG1(signer.PubkeyG1))
|
|
|
|
}
|
|
|
|
return &types.QueryAggregatePubkeyG1Response{
|
|
|
|
AggregatePubkeyG1: bn254util.SerializeG1(aggPubkeyG1),
|
2024-05-16 14:49:29 +00:00
|
|
|
Total: uint64(len(quorum.Signers)),
|
2024-05-11 09:13:54 +00:00
|
|
|
Hit: uint64(hit),
|
2024-05-09 18:54:47 +00:00
|
|
|
}, nil
|
|
|
|
}
|