Skip to content

Commit

Permalink
feat(x/accounts): Add schema caching feature and corresponding test c…
Browse files Browse the repository at this point in the history
…ase (#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>
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
31 changes: 20 additions & 11 deletions x/accounts/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@ import (
v1 "cosmossdk.io/x/accounts/v1"
)

var _ v1.QueryServer = queryServer{}
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 {
return &queryServer{k}
// 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
k Keeper
schemas map[string]*v1.SchemaResponse // Stores precalculated schemas.
}

func (q queryServer) AccountQuery(ctx context.Context, request *v1.AccountQueryRequest) (*v1.AccountQueryResponse, error) {
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 {
Expand Down Expand Up @@ -47,18 +55,19 @@ func (q queryServer) AccountQuery(ctx context.Context, request *v1.AccountQueryR
}, nil
}

func (q queryServer) Schema(_ context.Context, request *v1.SchemaRequest) (*v1.SchemaResponse, error) {
// TODO: maybe we should cache this, considering accounts types are not
// added on the fly as the chain is running.
schemas := v1.MakeAccountsSchemas(q.k.accounts)
schema, ok := schemas[request.AccountType]
// 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) {
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
Expand All @@ -72,7 +81,7 @@ func (q queryServer) AccountType(ctx context.Context, request *v1.AccountTypeReq
}, nil
}

func (q queryServer) AccountNumber(ctx context.Context, request *v1.AccountNumberRequest) (*v1.AccountNumberResponse, error) {
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
Expand Down
16 changes: 16 additions & 0 deletions x/accounts/query_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,20 @@ func TestQueryServer(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "test", typ.AccountType)
})

t.Run("schema caching", func(t *testing.T) {
// Request schema once
schemaReq := &v1.SchemaRequest{AccountType: "test"}
schemaResp1, err := qs.Schema(ctx, schemaReq)
require.NoError(t, err)
require.NotNil(t, schemaResp1)

// Request schema again
schemaResp2, err := qs.Schema(ctx, schemaReq)
require.NoError(t, err)
require.NotNil(t, schemaResp2)

// Check if both responses are the same (cached)
require.Equal(t, schemaResp1, schemaResp2)
})
}

0 comments on commit 06c9c54

Please sign in to comment.