cosmos-sdk/x/accounts/query_server.go
Hwangjae Lee 06c9c543ab
feat(x/accounts): Add schema caching feature and corresponding test case (#20055)
Signed-off-by: Hwangjae Lee <meetrick@gmail.com>
Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: guoguangwu <guoguangwug@gmail.com>
Signed-off-by: cuithon <dscs@outlook.com>
Signed-off-by: ipangpang <arronipangpang@gmail.com>
Co-authored-by: Marko <marko@baricevic.me>
Co-authored-by: Reece Williams <31943163+Reecepbcups@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: guangwu <guoguangwu@magic-shield.com>
Co-authored-by: Tien Nguyen <htiennv@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
Co-authored-by: son trinh <trinhleson2000@gmail.com>
Co-authored-by: Erlangshen219 <104747507+Erlangshen219@users.noreply.github.com>
Co-authored-by: cuithon <65674308+cuithon@users.noreply.github.com>
Co-authored-by: Andi <36215014+ChengenH@users.noreply.github.com>
Co-authored-by: ipangpang <167595720+ipangpang@users.noreply.github.com>
Co-authored-by: goofylfg <165781272+goofylfg@users.noreply.github.com>
Co-authored-by: Alexander Peters <alpe@users.noreply.github.com>
Co-authored-by: Leon <156270887+leonz789@users.noreply.github.com>
Co-authored-by: qinglin89 <316032931@qq.com>
Co-authored-by: Facundo <facundomedica@gmail.com>
Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
Co-authored-by: Matt Kocubinski <mkocubinski@gmail.com>
Co-authored-by: Hieu Vu <72878483+hieuvubk@users.noreply.github.com>
Co-authored-by: Cosmos SDK <113218068+github-prbot@users.noreply.github.com>
Co-authored-by: github-merge-queue <118344674+github-merge-queue@users.noreply.github.com>
Co-authored-by: marbar3778 <marbar3778@yahoo.com>
Co-authored-by: Kien Trinh <51135161+kien6034@users.noreply.github.com>
Co-authored-by: Tuan Tran <tuantran@notional.ventures>
Co-authored-by: Khanh Hoa <khanhoait.bka@gmail.com>
Co-authored-by: Emil Georgiev <emil.georgiev@mail.schwarz>
Co-authored-by: Khanh Hoa <49144992+hoanguyenkh@users.noreply.github.com>
Co-authored-by: Hoang Do <hoangdv2429@gmail.com>
Co-authored-by: Duong Minh Ngoc <153509244+minhngoc274@users.noreply.github.com>
Co-authored-by: Khanh Hoa <hoa@notional.ventures>
Co-authored-by: kienn6034 <kien@notional.ventures>
Co-authored-by: cool-developer <51834436+cool-develope@users.noreply.github.com>
Co-authored-by: Qt <golang.chen@gmail.com>
Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
2024-05-17 05:11:45 +00:00

102 lines
2.8 KiB
Go

package accounts
import (
"context"
"fmt"
"cosmossdk.io/x/accounts/internal/implementation"
v1 "cosmossdk.io/x/accounts/v1"
)
var _ v1.QueryServer = &queryServer{}
// NewQueryServer initializes a new instance of QueryServer.
// It precalculates and stores schemas for efficient schema retrieval.
func NewQueryServer(k Keeper) v1.QueryServer {
// Pre-calculate schemas for efficient retrieval.
schemas := v1.MakeAccountsSchemas(k.accounts)
return &queryServer{
k: k,
schemas: schemas, // Store precalculated schemas.
}
}
type queryServer struct {
k Keeper
schemas map[string]*v1.SchemaResponse // Stores precalculated schemas.
}
func (q *queryServer) AccountQuery(ctx context.Context, request *v1.AccountQueryRequest) (*v1.AccountQueryResponse, error) {
// get target addr
targetAddr, err := q.k.addressCodec.StringToBytes(request.Target)
if err != nil {
return nil, err
}
// decode req into boxed concrete type
queryReq, err := implementation.UnpackAnyRaw(request.Request)
if err != nil {
return nil, err
}
// run query
resp, err := q.k.Query(ctx, targetAddr, queryReq)
if err != nil {
return nil, err
}
// encode response
respAny, err := implementation.PackAny(resp)
if err != nil {
return nil, err
}
return &v1.AccountQueryResponse{
Response: respAny,
}, nil
}
// Schema retrieves the schema for a given account type.
// It checks the precalculated schemas and returns an error if the schema is not found.
func (q *queryServer) Schema(_ context.Context, request *v1.SchemaRequest) (*v1.SchemaResponse, error) {
// Fetch schema from precalculated schemas.
schema, ok := q.schemas[request.AccountType]
if !ok {
return nil, fmt.Errorf("%w: %s", errAccountTypeNotFound, request.AccountType)
}
return schema, nil
}
func (q *queryServer) AccountType(ctx context.Context, request *v1.AccountTypeRequest) (*v1.AccountTypeResponse, error) {
addr, err := q.k.addressCodec.StringToBytes(request.Address)
if err != nil {
return nil, err
}
accType, err := q.k.AccountsByType.Get(ctx, addr)
if err != nil {
return nil, err
}
return &v1.AccountTypeResponse{
AccountType: accType,
}, nil
}
func (q *queryServer) AccountNumber(ctx context.Context, request *v1.AccountNumberRequest) (*v1.AccountNumberResponse, error) {
addr, err := q.k.addressCodec.StringToBytes(request.Address)
if err != nil {
return nil, err
}
number, err := q.k.AccountByNumber.Get(ctx, addr)
if err != nil {
return nil, err
}
return &v1.AccountNumberResponse{Number: number}, nil
}
const (
// TODO(tip): evaluate if the following numbers should be parametrised over state, or over the node.
SimulateAuthenticateGasLimit = 1_000_000
SimulateBundlerPaymentGasLimit = SimulateAuthenticateGasLimit
ExecuteGasLimit = SimulateAuthenticateGasLimit
)