Skip to content

Commit

Permalink
merge main to branch since BCF-2612-ChainReader was merged. (#275)
Browse files Browse the repository at this point in the history
* Logger extraction (#253)

Co-authored-by: Jordan Krage <[email protected]>

* pkg/services/servicetest: add Run and RunHealthy helpers (#251)

* pkg/services/servicetest: remove debug fmt.Println (#259)

* Bump MaxAllowedBlocks from 5 to 10 (#260)

* pkg/services: add ver & sha params to HealthChecker (#265)

* Utils extraction from chainlink repo (#256)

* Adjust price scaling factor from 1e8 => 1e18 (#266)

* Adjust price scaling factor from 1e8 => 1e18

* Adjust price scaling from 1e8 => 1e18

- Reference: MERC-1771

* Add mathutil (#267)

Co-authored-by: Jordan Krage <[email protected]>
Co-authored-by: Dmytro Haidashenko <[email protected]>

* pkg/reportingplugins: remove; move mercury to chainlink-data-streams (#240)

* Add minimal ChainReader interface & types (#196)

Co-authored-by: Jordan Krage <[email protected]>
Co-authored-by: Ryan Tinianov <[email protected]>
Co-authored-by: ilija42 <[email protected]>

* Bump action to get relay renamed to common changes (#272)

* .

---------

Co-authored-by: Dimitris Grigoriou <[email protected]>
Co-authored-by: Jordan Krage <[email protected]>
Co-authored-by: Sam <[email protected]>
Co-authored-by: Dmytro Haidashenko <[email protected]>
Co-authored-by: Domino Valdano <[email protected]>
Co-authored-by: ilija42 <[email protected]>
Co-authored-by: Tate <[email protected]>
  • Loading branch information
8 people authored Dec 7, 2023
1 parent edbb7de commit 1a40c33
Show file tree
Hide file tree
Showing 92 changed files with 1,407 additions and 6,320 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/build_external.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ jobs:
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Build the image
uses: smartcontractkit/chainlink-github-actions/chainlink-testing-framework/build-image@v2.0.21
uses: smartcontractkit/chainlink-github-actions/chainlink-testing-framework/build-image@912bed7e07a1df4d06ea53a031e9773bb65dc7bd # v2.3.0
env:
GH_TOKEN: ${{ github.token }}
with:
push_tag: ""
cl_repo: smartcontractkit/chainlink
cl_ref: develop
dep_relay_sha: ${{ github.event.pull_request.head.sha }}
dep_common_sha: ${{ github.event.pull_request.head.sha }}
should_checkout: true
QA_AWS_REGION: ""
QA_AWS_ROLE_TO_ASSUME: ""
solana-build-relay:
environment: integration
permissions:
Expand Down
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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
Expand Down Expand Up @@ -58,7 +58,7 @@ 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
Expand Down
131 changes: 48 additions & 83 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/codec/modifier_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/types"
)

func NewModifierCodec(codec types.CodecTypeProvider, modifier Modifier, hooks ...mapstructure.DecodeHookFunc) (types.CodecTypeProvider, error) {
func NewModifierCodec(codec types.RemoteCodec, modifier Modifier, hooks ...mapstructure.DecodeHookFunc) (types.RemoteCodec, error) {
if codec == nil || modifier == nil {
return nil, errors.New("inputs must not be nil")
}
Expand All @@ -26,7 +26,7 @@ func NewModifierCodec(codec types.CodecTypeProvider, modifier Modifier, hooks ..
var _ types.TypeProvider = &modifierCodec{}

type modifierCodec struct {
codec types.CodecTypeProvider
codec types.RemoteCodec
modifier Modifier
hook mapstructure.DecodeHookFunc
}
Expand Down
37 changes: 21 additions & 16 deletions pkg/codec/modifier_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,52 +32,53 @@ func TestModifierCodec(t *testing.T) {
require.NoError(t, err)

t.Run("Nil codec returns error", func(t *testing.T) {
_, err := codec.NewModifierCodec(nil, testModifier{})
_, err = codec.NewModifierCodec(nil, testModifier{})
assert.Error(t, err)
})

t.Run("Nil modifier returns error", func(t *testing.T) {
_, err := codec.NewModifierCodec(&testCodec{}, nil)
_, err = codec.NewModifierCodec(&testCodec{}, nil)
assert.Error(t, err)
})

var encoded []byte
t.Run("Encode calls modifiers then encodes", func(t *testing.T) {
encoded, err := mod.Encode(ctx, &modifierCodecOffChainType{Z: anyValue}, anyItemType)
encoded, err = mod.Encode(ctx, &modifierCodecOffChainType{Z: anyValue}, anyItemType)

require.NoError(t, err)
assert.Equal(t, anyTestBytes, encoded)
})

t.Run("Encode works on compatible types", func(t *testing.T) {
encoded, err := mod.Encode(ctx, ModifierCodecOffChainCompatibleType{Z: anyValue}, anyItemType)
encoded, err = mod.Encode(ctx, ModifierCodecOffChainCompatibleType{Z: anyValue}, anyItemType)

require.NoError(t, err)
assert.Equal(t, anyTestBytes, encoded)
})

t.Run("Encode works on compatible squashed types", func(t *testing.T) {
encoded, err := mod.Encode(ctx, modifierCodecOffChainSquashCompatibleType{ModifierCodecOffChainCompatibleType{Z: anyValue}}, anyItemType)
encoded, err = mod.Encode(ctx, modifierCodecOffChainSquashCompatibleType{ModifierCodecOffChainCompatibleType{Z: anyValue}}, anyItemType)

require.NoError(t, err)
assert.Equal(t, anyTestBytes, encoded)
})

t.Run("Encode works on slices", func(t *testing.T) {
encoded, err := mod.Encode(ctx, &[]modifierCodecOffChainType{{Z: anyValue}, {Z: anyValue + 1}}, anySliceItemType)
encoded, err = mod.Encode(ctx, &[]modifierCodecOffChainType{{Z: anyValue}, {Z: anyValue + 1}}, anySliceItemType)

require.NoError(t, err)
assert.Equal(t, anyTestBytes, encoded)
})

t.Run("Encode works on slices without a pointer", func(t *testing.T) {
encoded, err := mod.Encode(ctx, []modifierCodecOffChainType{{Z: anyValue}, {Z: anyValue + 1}}, anyNonPointerSliceItemType)
encoded, err = mod.Encode(ctx, []modifierCodecOffChainType{{Z: anyValue}, {Z: anyValue + 1}}, anyNonPointerSliceItemType)

require.NoError(t, err)
assert.Equal(t, anyTestBytes, encoded)
})

t.Run("Encode works on compatible slices", func(t *testing.T) {
encoded, err := mod.Encode(ctx, &[]ModifierCodecOffChainCompatibleType{{Z: anyValue}, {Z: anyValue + 1}}, anySliceItemType)
encoded, err = mod.Encode(ctx, &[]ModifierCodecOffChainCompatibleType{{Z: anyValue}, {Z: anyValue + 1}}, anySliceItemType)

require.NoError(t, err)
assert.Equal(t, anyTestBytes, encoded)
Expand Down Expand Up @@ -138,8 +139,9 @@ func TestModifierCodec(t *testing.T) {
assert.True(t, errors.Is(err, types.ErrInvalidType))
})

var actual any
t.Run("CreateType returns modified type", func(t *testing.T) {
actual, err := mod.(types.TypeProvider).CreateType(anyItemType, anyForEncoding)
actual, err = mod.(types.TypeProvider).CreateType(anyItemType, anyForEncoding)
require.NoError(t, err)
assert.Equal(t, reflect.TypeOf(&modifierCodecOffChainType{}), reflect.TypeOf(actual))
})
Expand All @@ -149,14 +151,15 @@ func TestModifierCodec(t *testing.T) {
assert.Equal(t, types.ErrInvalidType, err)
})

var size int
t.Run("GetMaxEncodingSize delegates", func(t *testing.T) {
size, err := mod.GetMaxEncodingSize(ctx, anyValue, anyItemType)
size, err = mod.GetMaxEncodingSize(ctx, anyValue, anyItemType)
require.NoError(t, err)
assert.Equal(t, anyMaxEncodingSize, size)
})

t.Run("GetMaxDecodingSize delegates", func(t *testing.T) {
size, err := mod.GetMaxDecodingSize(ctx, anyValue, anyItemType)
size, err = mod.GetMaxDecodingSize(ctx, anyValue, anyItemType)
require.NoError(t, err)
assert.Equal(t, anyMaxDecodingSize, size)
})
Expand All @@ -168,15 +171,17 @@ func TestModifierCodec(t *testing.T) {
}
return from.Interface(), nil
}
mod, err := codec.NewModifierCodec(&testCodec{}, testModifier{}, hook)

var hookMod types.RemoteCodec
hookMod, err = codec.NewModifierCodec(&testCodec{}, testModifier{}, hook)
require.NoError(t, err)
decoded := &modifierCodecDiffType{}
require.NoError(t, mod.Decode(ctx, anyTestBytes, decoded, anyItemType))
require.NoError(t, hookMod.Decode(ctx, anyTestBytes, decoded, anyItemType))
assert.Equal(t, "5", decoded.Z)
})

t.Run("encode works wil nil input", func(t *testing.T) {
actual, err := mod.Encode(ctx, nil, anyItemType)
actual, err = mod.Encode(ctx, nil, anyItemType)
require.NoError(t, err)
assert.Equal(t, anyNilBytes, actual)
})
Expand Down Expand Up @@ -235,7 +240,7 @@ func (t *testCodec) GetMaxEncodingSize(_ context.Context, n int, itemType string
}

if n != anyValue {
return 0, types.ErrUnknown
return 0, types.ErrInvalidEncoding
}

return anyMaxEncodingSize, nil
Expand Down Expand Up @@ -274,7 +279,7 @@ func (t *testCodec) GetMaxDecodingSize(_ context.Context, n int, itemType string
}

if n != anyValue {
return 0, types.ErrUnknown
return 0, types.ErrInvalidEncoding
}

return anyMaxDecodingSize, nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func Test(tb testing.TB) Logger {
return &logger{zaptest.NewLogger(tb).Sugar()}
}

// TestSugared returns a new test SugaredLogger.
func TestSugared(tb testing.TB) SugaredLogger {
return Sugared(&logger{zaptest.NewLogger(tb).Sugar()})
}

// TestObserved returns a new test Logger for tb and ObservedLogs at the given Level.
func TestObserved(tb testing.TB, lvl zapcore.Level) (Logger, *observer.ObservedLogs) {
sl, logs := testObserved(tb, lvl)
Expand Down
12 changes: 12 additions & 0 deletions pkg/logger/sugared.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package logger
type SugaredLogger interface {
Logger
// AssumptionViolation variants log at error level with the message prefix "AssumptionViolation: ".
AssumptionViolation(args ...interface{})
AssumptionViolationf(format string, vals ...interface{})
AssumptionViolationw(format string, vals ...interface{})
// ErrorIf logs the error if present.
ErrorIf(err error, msg string)
// ErrorIfFn calls fn() and logs any returned error along with msg.
Expand Down Expand Up @@ -37,7 +39,17 @@ func (s *sugared) ErrorIfFn(fn func() error, msg string) {
}
}

// AssumptionViolation wraps Error logs with assumption violation tag.
func (s *sugared) AssumptionViolation(args ...interface{}) {
s.h.Error(append([]interface{}{"AssumptionViolation:"}, args...))
}

// AssumptionViolationf wraps Errorf logs with assumption violation tag.
func (s *sugared) AssumptionViolationf(format string, vals ...interface{}) {
s.h.Errorf("AssumptionViolation: "+format, vals...)
}

// AssumptionViolationw wraps Errorw logs with assumption violation tag.
func (s *sugared) AssumptionViolationw(msg string, keyvals ...interface{}) {
s.h.Errorw("AssumptionViolation: "+msg, keyvals...)
}
53 changes: 53 additions & 0 deletions pkg/logger/trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//go:build trace

package logger

const tracePrefix = "[trace] "

func Trace(l Logger, args ...interface{}) {
switch t := l.(type) {
case *logger:
t.DPanic(args...)
return
}
c, ok := l.(interface {
Trace(args ...interface{})
})
if ok {
c.Trace(args...)
return
}
l.Error(append([]any{tracePrefix}, args...)...)
}

func Tracef(l Logger, format string, values ...interface{}) {
switch t := l.(type) {
case *logger:
t.DPanicf(format, values...)
return
}
c, ok := l.(interface {
Tracef(format string, values ...interface{})
})
if ok {
c.Tracef(format, values...)
return
}
l.Errorf(tracePrefix+format, values...)
}

func Tracew(l Logger, msg string, keysAndValues ...interface{}) {
switch t := l.(type) {
case *logger:
t.DPanicw(msg, keysAndValues...)
return
}
c, ok := l.(interface {
Tracew(msg string, keysAndValues ...interface{})
})
if ok {
c.Tracew(msg, keysAndValues...)
return
}
l.Errorf(tracePrefix+msg, keysAndValues)
}
9 changes: 9 additions & 0 deletions pkg/logger/trace_noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !trace

package logger

func Trace(l Logger, args ...interface{}) {}

func Tracef(l Logger, format string, values ...interface{}) {}

func Tracew(l Logger, msg string, keysAndValues ...interface{}) {}
36 changes: 1 addition & 35 deletions pkg/loop/internal/chain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package internal
import (
"context"
jsonv1 "encoding/json"
"errors"
"fmt"
"strings"

jsonv2 "github.com/go-json-experiment/json"

"github.com/fxamacker/cbor/v2"
"google.golang.org/grpc/status"

"github.com/smartcontractkit/chainlink-common/pkg/loop/internal/pb"
"github.com/smartcontractkit/chainlink-common/pkg/types"
Expand Down Expand Up @@ -96,7 +93,7 @@ func (c *chainReaderClient) GetLatestValue(ctx context.Context, bc types.BoundCo

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

return decodeVersionedBytes(retVal, reply.RetVal)
Expand Down Expand Up @@ -148,34 +145,3 @@ func getEncodedType(itemType string, possibleTypeProvider any, forEncoding bool)

return &map[string]any{}, nil
}

func unwrapClientError(err error) error {
if err == nil {
return nil
}
errTypes := []error{
types.ErrInvalidType,
types.ErrFieldNotFound,
types.ErrInvalidEncoding,
types.ErrWrongNumberOfElements,
types.ErrNotASlice,
types.ErrUnknown,
}

s, ok := status.FromError(err)
if !ok {
return fmt.Errorf("%w: %w", types.ErrUnknown, err)
}

msg := s.Message()
for _, etype := range errTypes {
if msg == etype.Error() {
return etype
} else if strings.HasPrefix(msg, etype.Error()+":") {
rest := strings.SplitN(msg, ":", 2)[1]
return fmt.Errorf("%w: %w", etype, errors.New(rest))
}
}

return fmt.Errorf("%w: %w", types.ErrUnknown, err)
}
2 changes: 1 addition & 1 deletion pkg/loop/internal/chain_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestChainReaderClient(t *testing.T) {

t.Run("GetLatestValue unwraps errors from server "+errorType.Error(), func(t *testing.T) {
err := client.GetLatestValue(ctx, types.BoundContract{}, "method", "anything", "anything")
assert.IsType(t, errorType, err)
assert.True(t, errors.Is(err, errorType))
})
}

Expand Down
Loading

0 comments on commit 1a40c33

Please sign in to comment.