Skip to content

Commit

Permalink
Add get max size to chain reader. (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolag authored and reductionista committed Dec 1, 2023
1 parent 0c5edd6 commit 0c278da
Show file tree
Hide file tree
Showing 11 changed files with 592 additions and 738 deletions.
33 changes: 33 additions & 0 deletions pkg/loop/internal/chain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,24 @@ func (c *chainReaderClient) Decode(ctx context.Context, raw []byte, into any, it
return decodeVersionedBytes(into, resp.RetVal)
}

func (c *chainReaderClient) GetMaxEncodingSize(ctx context.Context, n int, itemType string) (int, error) {
res, err := c.grpc.GetMaxSize(ctx, &pb.GetMaxSizeRequest{N: int32(n), ItemType: itemType, ForEncoding: true})
if err != nil {
return 0, unwrapClientError(err)
}

return int(res.SizeInBytes), nil
}

func (c *chainReaderClient) GetMaxDecodingSize(ctx context.Context, n int, itemType string) (int, error) {
res, err := c.grpc.GetMaxSize(ctx, &pb.GetMaxSizeRequest{N: int32(n), ItemType: itemType, ForEncoding: false})
if err != nil {
return 0, unwrapClientError(err)
}

return int(res.SizeInBytes), nil
}

var _ pb.ChainReaderServer = (*chainReaderServer)(nil)

type chainReaderServer struct {
Expand Down Expand Up @@ -216,6 +234,21 @@ func (c *chainReaderServer) GetDecoding(ctx context.Context, req *pb.GetDecoding
return &pb.GetDecodingResponse{RetVal: versionBytes}, err
}

func (c *chainReaderServer) GetMaxSize(ctx context.Context, req *pb.GetMaxSizeRequest) (*pb.GetMaxSizeResponse, error) {
var sizeFn func(context.Context, int, string) (int, error)
if req.ForEncoding {
sizeFn = c.impl.GetMaxEncodingSize
} else {
sizeFn = c.impl.GetMaxDecodingSize
}

maxSize, err := sizeFn(ctx, int(req.N), req.ItemType)
if err != nil {
return nil, err
}
return &pb.GetMaxSizeResponse{SizeInBytes: int32(maxSize)}, nil
}

func (c *chainReaderServer) RegisterEventFilter(ctx context.Context, in *pb.RegisterEventFilterRequest) (*pb.RegisterEventFilterReply, error) {
return nil, nil
}
Expand Down
29 changes: 27 additions & 2 deletions pkg/loop/internal/chain_reader_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"context"
"errors"
"net"
"sync"
Expand All @@ -13,8 +14,6 @@ import (

"github.com/smartcontractkit/chainlink-relay/pkg/loop/internal/pb"

"context"

"github.com/fxamacker/cbor/v2"
"github.com/mitchellh/mapstructure"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
Expand Down Expand Up @@ -104,6 +103,16 @@ func TestChainReaderClient(t *testing.T) {
err := client.GetLatestValue(ctx, types.BoundContract{}, "method", "anything", "anything")
assert.IsType(t, errorType, err)
})

t.Run("GetMaxEncodingSize unwraps errors from server "+errorType.Error(), func(t *testing.T) {
_, err := client.GetMaxEncodingSize(ctx, 1, "anything")
assert.IsType(t, errorType, err)
})

t.Run("GetMaxEncodingSize unwraps errors from server "+errorType.Error(), func(t *testing.T) {
_, err := client.GetMaxDecodingSize(ctx, 1, "anything")
assert.IsType(t, errorType, err)
})
}

// make sure that errors come from client directly
Expand Down Expand Up @@ -204,6 +213,18 @@ type fakeCodecServer struct {
lock *sync.Mutex
}

func (f *fakeCodecServer) GetMaxDecodingSize(ctx context.Context, n int, itemType string) (int, error) {
return f.GetMaxEncodingSize(ctx, n, itemType)
}

func (f *fakeCodecServer) GetMaxEncodingSize(ctx context.Context, n int, itemType string) (int, error) {
switch itemType {
case TestItemType, TestItemSliceType, TestItemArray2Type, TestItemArray1Type:
return 1, nil
}
return 0, types.InvalidTypeError{}
}

func (f *fakeCodecServer) SetLatestValue(ts *TestStruct) {
f.lock.Lock()
defer f.lock.Unlock()
Expand Down Expand Up @@ -275,6 +296,10 @@ func (e *errorServer) GetDecoding(context.Context, *pb.GetDecodingRequest) (*pb.
return nil, e.err
}

func (e *errorServer) GetMaxSize(context.Context, *pb.GetMaxSizeRequest) (*pb.GetMaxSizeResponse, error) {
return nil, e.err
}

func connFromLis(t *testing.T, lis *bufconn.Listener) *grpc.ClientConn {
conn, err := grpc.Dial("bufnet",
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) { return lis.Dial() }),
Expand Down
Loading

0 comments on commit 0c278da

Please sign in to comment.