Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimal ChainReader interface & types #196

Merged
merged 78 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 75 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
f6d01e6
Add chain_reader.go with ChainReader type defs, add ChainReader() to …
reductionista Oct 12, 2023
98c33ec
Add some tests
reductionista Oct 20, 2023
bac1b55
update workflow with external build tags
reductionista Oct 25, 2023
1287ac2
Add UnmarshalJSON() method to libocr.ConfigDigest
reductionista Oct 26, 2023
3ce2b77
Convert params & retVal protobuf fields into versioned data
reductionista Oct 26, 2023
282d82e
Comment out unused BlockID type & QueryEvent struct for linter
reductionista Oct 27, 2023
0b4828c
Add common chainreader errors definitions
reductionista Oct 27, 2023
2bca874
Remove retVal from request + other PR suggestions
reductionista Oct 28, 2023
d29aef8
Add -v to build-test workflow, so test failures show up in logs
reductionista Oct 31, 2023
55da345
Update .tool-versions to golang 1.21.3
reductionista Oct 31, 2023
fc68ba2
Replace = with assert.ObjectEqual, add map test
reductionista Oct 31, 2023
a73c58c
Remove extra replace directives
reductionista Oct 31, 2023
fd892d3
Add location to latestTransmissionDetails time for GetLatestValue test
ilija42 Oct 31, 2023
274a89c
Add err handling tests for encode/decode versioned bytes functions
ilija42 Nov 1, 2023
1222740
Creates a codec interface for encoding and decoding data to a chain. …
nolag Nov 2, 2023
a16abbe
Add map decoder to be used by EVM and other implementations. (#211)
nolag Nov 3, 2023
1472a9b
Add merge and split fields utilities to be used by encoders (#212)
nolag Nov 3, 2023
7ad7b3c
Remove -v from go test in pkg workflow
reductionista Nov 3, 2023
bb9ab3d
Remove commented EventQuery methods from ChainReader
reductionista Nov 3, 2023
706e6b2
Make static struct test simpler, and dynamic test more complex
reductionista Nov 4, 2023
80abf9e
Add get max size to chain reader. (#218)
nolag Nov 6, 2023
3d2d4ab
Fix a typo in the codec interface test. The dummy used to test the r…
nolag Nov 7, 2023
7ea35f6
Reset the test structure in codec interfaces before a new write and d…
nolag Nov 7, 2023
cde910c
Remove the map from inner test struct for the chain reader interface …
nolag Nov 7, 2023
103488f
Wrap err with InvalidTypeError
reductionista Nov 8, 2023
ff2b076
Handle whitespace in json array detection
reductionista Nov 8, 2023
64220de
Fix InvalidTypeError tests
reductionista Nov 8, 2023
977ee5c
Rename JSONEncodingVersion & CBOREncodingVersion constants
reductionista Nov 8, 2023
11032c0
Add test for json whitespace handling in isArray()
reductionista Nov 8, 2023
5b4fda3
Move codec utils into separate codec package
reductionista Nov 9, 2023
723f346
Remove unused utility functions (moved to separate PR)
reductionista Nov 9, 2023
0d0c2fb
Remove references to QueryEvents, RegisterFilter, UnregisterFilter
reductionista Nov 9, 2023
0e4e63b
Handle InvalidType errors for JSON & CBOR separately
reductionista Nov 14, 2023
9138d19
Separate error messages for clarity
reductionista Nov 14, 2023
dabbdce
Add support for json v2
reductionista Nov 14, 2023
695eb9a
Clean up chain reader, removed map decoder, as it will no longer be u…
nolag Nov 16, 2023
8a65ef3
Pull out my changes in one commit so you can merge to dev branch (#245)
nolag Nov 16, 2023
290e135
Update .github/workflows/build_external.yml cl_ref
ilija42 Nov 16, 2023
3cb62b6
Remove temporary refs in build workflows
ilija42 Nov 16, 2023
6f65d73
Change chain Reader Event field name ChainId -> ChainID
ilija42 Nov 16, 2023
26e6394
Simplify client error unwrapping in chain reader
ilija42 Nov 16, 2023
43dbb67
Update Encoder return type to []bytes as it won't encode just reports
ilija42 Nov 16, 2023
d97588f
Unexport errChainReader by co-locating UnwrapClientError to same pckg
ilija42 Nov 16, 2023
a9aa384
Rename ChainReader used for latestHeads to MercuryChainReader (to be …
ilija42 Nov 16, 2023
55f3b90
Change ChainReaderInterfaceTester Setup to return teardown func
ilija42 Nov 16, 2023
e099082
Update pkg/loop/internal/chain_reader_test.go
nolag Nov 17, 2023
06a400e
Change ErrorChainReaderInvalidConfig to generic ErrInvalidConfig
ilija42 Nov 17, 2023
d4f72f1
Cleanup chain reader errors
ilija42 Nov 17, 2023
0a88d60
rename module to chainlink-common (#243)
jmank88 Nov 17, 2023
3606dac
Expose InvalidConfig & Unsupported errors to client, fix Setup signature
reductionista Nov 18, 2023
67c20cf
Rename chainlink-relay -> chainlink-common
reductionista Nov 18, 2023
3b544ae
Change error types to end in Error per google convention
reductionista Nov 18, 2023
6405f22
Fix names of errors (hopefully right this time)
reductionista Nov 20, 2023
bd0849e
Synchronize server shutdown
reductionista Nov 20, 2023
80e22a8
TestChainReaderClient: wait for server thread to exit before ending test
reductionista Nov 20, 2023
3c5cef4
Fix docstring
reductionista Nov 20, 2023
6aa16aa
Because gRPC allows us to set both a status code and an error string,…
reductionista Nov 20, 2023
0bc869c
Remove FieldNotFound from errors tested, add errors.ErrUnsupported
reductionista Nov 21, 2023
5754ac4
Add wrapping & unwrapping of errors returned over gRPC by NewMedianPr…
reductionista Nov 21, 2023
39f1f24
Expand docstring for GetLatestValue to match CLIP
reductionista Nov 22, 2023
f40e580
Remove conversion of errors.ErrUnsupported to codes.Unimplemented
reductionista Nov 22, 2023
800bd28
Update pkg/types/chain_reader.go
reductionista Nov 23, 2023
262f630
Update pkg/loop/internal/chain_reader_test.go
reductionista Nov 23, 2023
7a6860b
Fix new lint issue
reductionista Nov 23, 2023
6bed32f
Marshaller -> Marshaler
reductionista Nov 23, 2023
8e429d6
Move chain reader protobuf definitions to separate file
ilija42 Nov 27, 2023
693c315
Change chain reader encoding so that params and return values use the…
ilija42 Nov 27, 2023
0cb44dc
Add missing chain reader pb gen and run pb gen
ilija42 Nov 27, 2023
3556388
Bump protoc version to 25.1 to match what's in most of the generated …
reductionista Nov 28, 2023
c200967
Make use of InvalidArgumentType gRPC code for ErrInvalidConfig & ErrI…
reductionista Nov 28, 2023
b35e758
Make better use of Is for the custom error types and remove unwrappin…
nolag Nov 29, 2023
02eb3df
revert deletion of GRPCStatus methods
nolag Nov 29, 2023
3ee7465
Upgrade kafka to v2, for arm support
reductionista Dec 1, 2023
72b4b59
Remove call to UnwrapClientError
reductionista Dec 1, 2023
c672a55
Get error tests passing
reductionista Dec 1, 2023
57a6e90
Use slices api
reductionista Dec 5, 2023
2e62afe
Fix slices.ContainsFunc
reductionista Dec 5, 2023
0984572
Use tests.Context(t) instead of context.Background()
reductionista Dec 5, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pkg.yml
jmank88 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: go-test-results
path: ./pkg_coverage.out
path: ./pkg_coverage.out
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
golang 1.21.1
golang 1.21.4
golangci-lint 1.55.2
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ godoc:

PHONY: install-protoc
install-protoc:
script/install-protoc.sh 24.2 /
script/install-protoc.sh 25.1 /
go install google.golang.org/protobuf/cmd/[email protected]; go install google.golang.org/grpc/cmd/[email protected]

.PHONY: mockery
Expand All @@ -26,4 +26,4 @@ generate: mockery install-protoc
.PHONY: golangci-lint
golangci-lint: ## Run golangci-lint for all issues.
[ -d "./golangci-lint" ] || mkdir ./golangci-lint && \
docker run --rm -v $(shell pwd):/app -w /app golangci/golangci-lint:v1.55.2 golangci-lint run --max-issues-per-linter 0 --max-same-issues 0 > ./golangci-lint/$(shell date +%Y-%m-%d_%H:%M:%S).txt
docker run --rm -v $(shell pwd):/app -w /app golangci/golangci-lint:v1.55.2 golangci-lint run --max-issues-per-linter 0 --max-same-issues 0 > ./golangci-lint/$(shell date +%Y-%m-%d_%H:%M:%S).txt
18 changes: 13 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ module github.com/smartcontractkit/chainlink-common
go 1.21

require (
github.com/confluentinc/confluent-kafka-go v1.9.2
github.com/confluentinc/confluent-kafka-go/v2 v2.3.0
github.com/fxamacker/cbor/v2 v2.5.0
github.com/go-json-experiment/json v0.0.0-20231102232822-2e55bd4e08b0
github.com/google/uuid v1.3.1
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.0
github.com/hashicorp/go-hclog v1.5.0
github.com/hashicorp/go-plugin v1.5.2
github.com/jmoiron/sqlx v1.3.5
github.com/jpillora/backoff v1.0.0
github.com/linkedin/goavro/v2 v2.12.0
github.com/mitchellh/mapstructure v1.5.0
github.com/mwitkow/grpc-proxy v0.0.0-20230212185441-f345521cb9c9
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.17.0
Expand Down Expand Up @@ -41,6 +44,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
Expand All @@ -54,16 +58,17 @@ require (
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/santhosh-tekuri/jsonschema/v5 v5.1.1 // indirect
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.18.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
Expand All @@ -74,6 +79,9 @@ replace (
// until merged upstream: https://github.com/hashicorp/go-plugin/pull/257
github.com/hashicorp/go-plugin => github.com/smartcontractkit/go-plugin v0.0.0-20231003134350-e49dad63b306

// until merged upstream: https://github.com/mitchellh/mapstructure/pull/343
github.com/mitchellh/mapstructure v1.5.0 => github.com/nolag/mapstructure v1.5.1

// until merged upstream: https://github.com/mwitkow/grpc-proxy/pull/69
github.com/mwitkow/grpc-proxy => github.com/smartcontractkit/grpc-proxy v0.0.0-20230731113816-f1be6620749f
)
155 changes: 64 additions & 91 deletions go.sum

Large diffs are not rendered by default.

132 changes: 132 additions & 0 deletions pkg/loop/internal/chain_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package internal

import (
"context"
jsonv1 "encoding/json"
"fmt"

"github.com/fxamacker/cbor/v2"
jsonv2 "github.com/go-json-experiment/json"

"github.com/smartcontractkit/chainlink-common/pkg/loop/internal/pb"
"github.com/smartcontractkit/chainlink-common/pkg/types"
)

var _ types.ChainReader = (*chainReaderClient)(nil)

type chainReaderClient struct {
*brokerExt
grpc pb.ChainReaderClient
}

// enum of all known encoding formats for versioned data
const (
JSONEncodingVersion1 = iota
JSONEncodingVersion2
CBOREncodingVersion
)

// Version to be used for encoding (version used for decoding is determined by data received)
const CurrentEncodingVersion = CBOREncodingVersion

func encodeVersionedBytes(data any, version int32) (*pb.VersionedBytes, error) {
var bytes []byte
var err error

switch version {
case JSONEncodingVersion1:
bytes, err = jsonv1.Marshal(data)
if err != nil {
return nil, fmt.Errorf("%w: %w", types.ErrInvalidType, err)
}
case JSONEncodingVersion2:
bytes, err = jsonv2.Marshal(data)
if err != nil {
return nil, fmt.Errorf("%w: %w", types.ErrInvalidType, err)
}
case CBOREncodingVersion:
ilija42 marked this conversation as resolved.
Show resolved Hide resolved
enco := cbor.CoreDetEncOptions()
enco.Time = cbor.TimeRFC3339Nano
var enc cbor.EncMode
enc, err = enco.EncMode()
if err != nil {
return nil, err
}
bytes, err = enc.Marshal(data)
if err != nil {
return nil, fmt.Errorf("%w: %w", types.ErrInvalidType, err)
}
default:
return nil, fmt.Errorf("unsupported encoding version %d for data %v", version, data)
}

return &pb.VersionedBytes{Version: uint32(version), Data: bytes}, nil
}

func decodeVersionedBytes(res any, vData *pb.VersionedBytes) error {
var err error
switch vData.Version {
case JSONEncodingVersion1:
err = jsonv1.Unmarshal(vData.Data, res)
case JSONEncodingVersion2:
err = jsonv2.Unmarshal(vData.Data, res)
case CBOREncodingVersion:
err = cbor.Unmarshal(vData.Data, res)
default:
return fmt.Errorf("unsupported encoding version %d for versionedData %v", vData.Version, vData.Data)
}

if err != nil {
return fmt.Errorf("%w: %w", types.ErrInvalidType, err)
}
return nil
}

func (c *chainReaderClient) GetLatestValue(ctx context.Context, bc types.BoundContract, method string, params, retVal any) error {
versionedParams, err := encodeVersionedBytes(params, CurrentEncodingVersion)
if err != nil {
return err
}

boundContract := pb.BoundContract{Name: bc.Name, Address: bc.Address, Pending: bc.Pending}

reply, err := c.grpc.GetLatestValue(ctx, &pb.GetLatestValueRequest{Bc: &boundContract, Method: method, Params: versionedParams})
if err != nil {
return err
}

return decodeVersionedBytes(retVal, reply.RetVal)
}

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

type chainReaderServer struct {
pb.UnimplementedChainReaderServer
impl types.ChainReader
}

func (c *chainReaderServer) GetLatestValue(ctx context.Context, request *pb.GetLatestValueRequest) (*pb.GetLatestValueReply, error) {
var bc types.BoundContract
bc.Name = request.Bc.Name[:]
bc.Address = request.Bc.Address[:]
bc.Pending = request.Bc.Pending

params := &map[string]any{}

if err := decodeVersionedBytes(params, request.Params); err != nil {
return nil, err
}

retVal := &map[string]any{}
err := c.impl.GetLatestValue(ctx, bc, request.Method, params, retVal)
if err != nil {
return nil, err
}

encodedRetVal, err := encodeVersionedBytes(retVal, CurrentEncodingVersion)
if err != nil {
return nil, err
}

return &pb.GetLatestValueReply{RetVal: encodedRetVal}, nil
}
Loading
Loading