Skip to content

Commit

Permalink
fix: pass height to abci query
Browse files Browse the repository at this point in the history
  • Loading branch information
hallazzang committed Mar 28, 2024
1 parent fb892e2 commit fbc8c2b
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions gprc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"context"
"fmt"
"net/http"
"strconv"
"time"

"github.com/cosmos/cosmos-sdk/codec"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/encoding"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"github.com/forbole/juno/v4/jsonrpc2"
Expand Down Expand Up @@ -60,7 +63,9 @@ func (c *Connection) Invoke(ctx context.Context, method string, args, reply any,
return err
}

res, err := c.RunABCIQuery(ctx, method, req)
height, _ := BlockHeightFromOutgoingContext(ctx)

res, err := c.RunABCIQuery(ctx, method, req, height)
if err != nil {
return fmt.Errorf("abci query: %w", err)
}
Expand All @@ -78,12 +83,12 @@ func (c *Connection) Invoke(ctx context.Context, method string, args, reply any,
}

// RunABCIQuery runs a new query through the ABCI protocol
func (c *Connection) RunABCIQuery(ctx context.Context, path string, data []byte) (*ABCIQueryResult, error) {
func (c *Connection) RunABCIQuery(ctx context.Context, path string, data []byte, height int64) (*ABCIQueryResult, error) {
var res ABCIQueryResult
err := c.jsonrpcClient.Call(ctx, "abci_query", ABCIQueryRequest{
Path: path,
Data: data,
Height: 0,
Height: height,
Prove: false,
}, &res)

Expand All @@ -98,3 +103,19 @@ func (c *Connection) RunABCIQuery(ctx context.Context, path string, data []byte)
func (c *Connection) NewStream(_ context.Context, _ *grpc.StreamDesc, _ string, _ ...grpc.CallOption) (grpc.ClientStream, error) {
return nil, fmt.Errorf("not implemented")
}

func BlockHeightFromOutgoingContext(ctx context.Context) (int64, bool) {
md, ok := metadata.FromOutgoingContext(ctx)
if !ok {
return 0, false
}
vs := md.Get(grpctypes.GRPCBlockHeightHeader)
if len(vs) == 0 {
return 0, false
}
height, err := strconv.ParseInt(vs[0], 10, 64)
if err != nil {
return 0, false
}
return height, true
}

0 comments on commit fbc8c2b

Please sign in to comment.