Skip to content

Commit

Permalink
bump libocr; add context
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Jul 30, 2024
1 parent 3ed288a commit ad0e1c9
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 47 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/smartcontractkit/chainlink-cosmos

go 1.21.7

replace github.com/smartcontractkit/libocr => github.com/jmank88/libocr v0.0.0-20240425103000-0eed0c09969e

require (
cosmossdk.io/errors v1.0.0
github.com/CosmWasm/wasmd v0.40.1
Expand All @@ -15,7 +17,7 @@ require (
github.com/pelletier/go-toml/v2 v2.2.0
github.com/prometheus/client_golang v1.17.0
github.com/shopspring/decimal v1.3.1
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240709155552-d76403db14e5
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240425103110-dbf028ff5724
github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7Bd
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c=
github.com/jhump/protoreflect v1.15.1/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gavG9e0Q693nKo=
github.com/jmank88/libocr v0.0.0-20240425103000-0eed0c09969e h1:gswd1U2Jht2eq230MMRq1E9+XMUiiTvm/30KGaOfuvA=
github.com/jmank88/libocr v0.0.0-20240425103000-0eed0c09969e/go.mod h1:fb1ZDVXACvu4frX3APHZaEBp0xi1DIm34DcA0CwTsZM=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
Expand Down Expand Up @@ -723,14 +725,12 @@ github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240709155552-d76403db14e5 h1:1J1M+rpMJn+5R4EPaU8hbCqR17PFClEBOJyazsvbWck=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240709155552-d76403db14e5/go.mod h1:fh9eBbrReCmv31bfz52ENCAMa7nTKQbdhb2B3+S2VGo=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240425103110-dbf028ff5724 h1:1qpCBUyjYP7nTYi2mdN27ZmOB9r7kA0GFww4M6VYbBA=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240425103110-dbf028ff5724/go.mod h1:2BbaKM4DOcZCH+x1SNAPq1+X/1BCMOJNx12u/OzLlNk=
github.com/smartcontractkit/go-plugin v0.0.0-20231003134350-e49dad63b306 h1:ko88+ZznniNJZbZPWAvHQU8SwKAdHngdDZ+pvVgB5ss=
github.com/smartcontractkit/go-plugin v0.0.0-20231003134350-e49dad63b306/go.mod h1:w1sAEES3g3PuV/RzUrgow20W2uErMly84hhD3um1WL4=
github.com/smartcontractkit/grpc-proxy v0.0.0-20230731113816-f1be6620749f h1:hgJif132UCdjo8u43i7iPN1/MFnu49hv7lFGFftCHKU=
github.com/smartcontractkit/grpc-proxy v0.0.0-20230731113816-f1be6620749f/go.mod h1:MvMXoufZAtqExNexqi4cjrNYE9MefKddKylxjS+//n0=
github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c h1:lIyMbTaF2H0Q71vkwZHX/Ew4KF2BxiKhqEXwF8rn+KI=
github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c/go.mod h1:fb1ZDVXACvu4frX3APHZaEBp0xi1DIm34DcA0CwTsZM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
Expand Down
4 changes: 3 additions & 1 deletion integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ module github.com/smartcontractkit/chainlink-cosmos/integration-tests

go 1.21.7

replace github.com/smartcontractkit/libocr => github.com/jmank88/libocr v0.0.0-20240425103000-0eed0c09969e

require (
github.com/cometbft/cometbft v0.37.2
github.com/cosmos/cosmos-sdk v0.47.4
github.com/google/uuid v1.6.0
github.com/lib/pq v1.10.9
github.com/rs/zerolog v1.31.0
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240709155552-d76403db14e5
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240425103110-dbf028ff5724
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240621143432-85370a54b141
github.com/smartcontractkit/chainlink-cosmos/ops v0.0.0-20231206164210-03f8b219402e
github.com/smartcontractkit/chainlink-testing-framework v1.31.10
Expand Down
7 changes: 4 additions & 3 deletions pkg/cosmos/adapters/cosmwasm/config_digester.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cosmwasm

import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
Expand Down Expand Up @@ -29,7 +30,7 @@ func NewOffchainConfigDigester(chainID string, contract cosmosSDK.AccAddress) Of
}
}

func (cd OffchainConfigDigester) ConfigDigest(cfg types.ContractConfig) (types.ConfigDigest, error) {
func (cd OffchainConfigDigester) ConfigDigest(ctx context.Context, cfg types.ContractConfig) (types.ConfigDigest, error) {
digest := types.ConfigDigest{}
buf := bytes.NewBuffer([]byte{})

Expand Down Expand Up @@ -103,7 +104,7 @@ func (cd OffchainConfigDigester) ConfigDigest(cfg types.ContractConfig) (types.C
return digest, fmt.Errorf("incorrect hash size %d, expected %d", n, len(digest))
}

pre, err := cd.ConfigDigestPrefix()
pre, err := cd.ConfigDigestPrefix(ctx)
if err != nil {
return digest, err
}
Expand All @@ -115,6 +116,6 @@ func (cd OffchainConfigDigester) ConfigDigest(cfg types.ContractConfig) (types.C
}

// This should return the same constant value on every invocation
func (OffchainConfigDigester) ConfigDigestPrefix() (types.ConfigDigestPrefix, error) {
func (OffchainConfigDigester) ConfigDigestPrefix(ctx context.Context) (types.ConfigDigestPrefix, error) {
return ConfigDigestPrefixCosmos, nil
}
6 changes: 4 additions & 2 deletions pkg/cosmos/adapters/cosmwasm/config_digester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/smartcontractkit/libocr/offchainreporting2/types"
"github.com/stretchr/testify/assert"

"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
)

var testConfig = types.ContractConfig{
Expand Down Expand Up @@ -36,7 +38,7 @@ func TestConfigDigester(t *testing.T) {
sdk.MustAccAddressFromBech32("wasm1cd65xyq076dm9cw3xxqtdh4d6ypzug0edd9958"),
)

digest, err := d.ConfigDigest(testConfig)
digest, err := d.ConfigDigest(tests.Context(t), testConfig)
assert.NoError(t, err)
assert.Equal(t, "000289b55121341b1ff99cc8e15659fb8de14fca52a695b2b269a7fb94059b9f", digest.Hex())
}
Expand All @@ -47,6 +49,6 @@ func TestConfigDigester_InvalidChainID(t *testing.T) {
sdk.MustAccAddressFromBech32("wasm1cd65xyq076dm9cw3xxqtdh4d6ypzug0edd9958"),
)

_, err := d.ConfigDigest(testConfig)
_, err := d.ConfigDigest(tests.Context(t), testConfig)
assert.Error(t, err)
}
2 changes: 1 addition & 1 deletion pkg/cosmos/adapters/cosmwasm/contract_transmitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ func (ct *ContractTransmitter) Transmit(
return err
}

func (ct *ContractTransmitter) FromAccount() (types.Account, error) {
func (ct *ContractTransmitter) FromAccount(ctx context.Context) (types.Account, error) {
return types.Account(ct.sender.String()), nil
}
6 changes: 3 additions & 3 deletions pkg/cosmos/adapters/cosmwasm/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type configProvider struct {
contractAddr cosmosSDK.AccAddress
}

func NewConfigProvider(lggr logger.Logger, chain adapters.Chain, args relaytypes.RelayArgs) (*configProvider, error) {
func NewConfigProvider(ctx context.Context, lggr logger.Logger, chain adapters.Chain, args relaytypes.RelayArgs) (*configProvider, error) {
var relayConfig adapters.RelayConfig
err := json.Unmarshal(args.RelayConfig, &relayConfig)
if err != nil {
Expand Down Expand Up @@ -100,8 +100,8 @@ type medianProvider struct {
transmitter types.ContractTransmitter
}

func NewMedianProvider(lggr logger.Logger, chain adapters.Chain, rargs relaytypes.RelayArgs, pargs relaytypes.PluginArgs) (relaytypes.MedianProvider, error) {
configProvider, err := NewConfigProvider(lggr, chain, rargs)
func NewMedianProvider(ctx context.Context, lggr logger.Logger, chain adapters.Chain, rargs relaytypes.RelayArgs, pargs relaytypes.PluginArgs) (relaytypes.MedianProvider, error) {
configProvider, err := NewConfigProvider(ctx, lggr, chain, rargs)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/cosmos/adapters/cosmwasm/report.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cosmwasm

import (
"context"
"encoding/binary"
"fmt"
"math/big"
Expand Down Expand Up @@ -34,7 +35,7 @@ var _ median.ReportCodec = (*ReportCodec)(nil)

type ReportCodec struct{}

func (c ReportCodec) BuildReport(oo []median.ParsedAttributedObservation) (types.Report, error) {
func (c ReportCodec) BuildReport(ctx context.Context, oo []median.ParsedAttributedObservation) (types.Report, error) {
n := len(oo)
if n == 0 {
return nil, fmt.Errorf("cannot build report from empty attributed observations")
Expand Down Expand Up @@ -95,11 +96,11 @@ func (c ReportCodec) BuildReport(oo []median.ParsedAttributedObservation) (types
return report, nil
}

func (c ReportCodec) MaxReportLength(n int) (int, error) {
func (c ReportCodec) MaxReportLength(ctx context.Context, n int) (int, error) {
return prefixSizeBytes + (n * observationSizeBytes) + juelsPerFeeCoinSizeBytes, nil
}

func (c ReportCodec) MedianFromReport(report types.Report) (*big.Int, error) {
func (c ReportCodec) MedianFromReport(ctx context.Context, report types.Report) (*big.Int, error) {
// report should at least be able to contain timestamp, observers, observations length
rLen := len(report)
if rLen < prefixSizeBytes {
Expand Down
9 changes: 6 additions & 3 deletions pkg/cosmos/adapters/cosmwasm/report_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/libocr/offchainreporting2/reportingplugin/median"

"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
)

// go test -tags=go1.18 -fuzz ./...
func FuzzReportCodecMedianFromReport(f *testing.F) {
cdc := ReportCodec{}
report, err := cdc.BuildReport([]median.ParsedAttributedObservation{
report, err := cdc.BuildReport(tests.Context(f), []median.ParsedAttributedObservation{
{Timestamp: uint32(time.Now().Unix()), Value: big.NewInt(10), JuelsPerFeeCoin: big.NewInt(100000)},
{Timestamp: uint32(time.Now().Unix()), Value: big.NewInt(10), JuelsPerFeeCoin: big.NewInt(200000)},
{Timestamp: uint32(time.Now().Unix()), Value: big.NewInt(11), JuelsPerFeeCoin: big.NewInt(300000)}})
Expand All @@ -25,10 +27,11 @@ func FuzzReportCodecMedianFromReport(f *testing.F) {
// Seed with valid report
f.Add([]byte(report))
f.Fuzz(func(t *testing.T, report []byte) {
med, err := cdc.MedianFromReport(report)
ctx := tests.Context(t)
med, err := cdc.MedianFromReport(ctx, report)
if err == nil {
// Should always be able to build a report from the medians extracted
_, err = cdc.BuildReport([]median.ParsedAttributedObservation{{Timestamp: uint32(time.Now().Unix()), Value: med, JuelsPerFeeCoin: med}})
_, err = cdc.BuildReport(ctx, []median.ParsedAttributedObservation{{Timestamp: uint32(time.Now().Unix()), Value: med, JuelsPerFeeCoin: med}})
require.NoError(t, err)
}
})
Expand Down
15 changes: 9 additions & 6 deletions pkg/cosmos/adapters/cosmwasm/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/smartcontractkit/libocr/offchainreporting2/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
)

func TestBuildReport(t *testing.T) {
Expand All @@ -36,7 +38,7 @@ func TestBuildReport(t *testing.T) {
observers[i] = uint8(i)
}

report, err := c.BuildReport(oo)
report, err := c.BuildReport(tests.Context(t), oo)
assert.NoError(t, err)

// validate length
Expand Down Expand Up @@ -74,7 +76,7 @@ func TestMedianFromOnChainReport(t *testing.T) {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 150, 2, 210, // observation 2
0, 0, 0, 0, 0, 0, 0, 0, 13, 224, 182, 179, 167, 100, 0, 0, // juels per atom (1 with 18 decimal places)
}
res, err := c.MedianFromReport(report)
res, err := c.MedianFromReport(tests.Context(t), report)
assert.NoError(t, err)
assert.Equal(t, "1234567890", res.String())
}
Expand All @@ -88,7 +90,7 @@ type medianTest struct {
func TestMedianFromReport(t *testing.T) {
cdc := ReportCodec{}
// Requires at least one obs
_, err := cdc.BuildReport(nil)
_, err := cdc.BuildReport(tests.Context(t), nil)
require.Error(t, err)
var tt = []medianTest{
{
Expand Down Expand Up @@ -142,6 +144,7 @@ func TestMedianFromReport(t *testing.T) {
for _, tc := range tt {
tc := tc
t.Run(tc.name, func(t *testing.T) {
ctx := tests.Context(t)
var pos []median.ParsedAttributedObservation
for i, obs := range tc.obs {
pos = append(pos, median.ParsedAttributedObservation{
Expand All @@ -150,12 +153,12 @@ func TestMedianFromReport(t *testing.T) {
Observer: commontypes.OracleID(uint8(i))},
)
}
report, err := cdc.BuildReport(pos)
report, err := cdc.BuildReport(ctx, pos)
require.NoError(t, err)
max, err := cdc.MaxReportLength(len(tc.obs))
max, err := cdc.MaxReportLength(ctx, len(tc.obs))
require.NoError(t, err)
assert.Equal(t, len(report), max)
med, err := cdc.MedianFromReport(report)
med, err := cdc.MedianFromReport(ctx, report)
require.NoError(t, err)
assert.Equal(t, tc.expectedMedian.String(), med.String())
})
Expand Down
7 changes: 4 additions & 3 deletions pkg/cosmos/adapters/injective/medianreport/report.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package medianreport

import (
"context"
"errors"
"fmt"
"math/big"
Expand All @@ -19,7 +20,7 @@ var _ median.ReportCodec = ReportCodec{}

type ReportCodec struct{}

func (ReportCodec) BuildReport(observations []median.ParsedAttributedObservation) (types.Report, error) {
func (ReportCodec) BuildReport(ctx context.Context, observations []median.ParsedAttributedObservation) (types.Report, error) {
if len(observations) == 0 {
err := errors.New("cannot build report from empty attributed observations")
return nil, err
Expand Down Expand Up @@ -60,13 +61,13 @@ func (ReportCodec) BuildReport(observations []median.ParsedAttributedObservation
return types.Report(reportBytes), err
}

func (ReportCodec) MaxReportLength(n int) (int, error) {
func (ReportCodec) MaxReportLength(ctx context.Context, n int) (int, error) {
// TODO:
return 0, nil
// return prefixSizeBytes + (n * observationSizeBytes) + juelsPerFeeCoinSizeBytes
}

func (ReportCodec) MedianFromReport(report types.Report) (*big.Int, error) {
func (ReportCodec) MedianFromReport(ctx context.Context, report types.Report) (*big.Int, error) {
reportRaw, err := ParseReport([]byte(report))
if err != nil {
return nil, err
Expand Down
6 changes: 4 additions & 2 deletions pkg/cosmos/adapters/injective/offchain_config_digester.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package injective

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/smartcontractkit/libocr/offchainreporting2/types"

Expand All @@ -23,7 +25,7 @@ func NewCosmosOffchainConfigDigester(chainID string, feedID string) *CosmosOffch
}
}

func (d CosmosOffchainConfigDigester) ConfigDigest(cc types.ContractConfig) (types.ConfigDigest, error) {
func (d CosmosOffchainConfigDigester) ConfigDigest(ctx context.Context, cc types.ContractConfig) (types.ConfigDigest, error) {
signers := make([]string, 0, len(cc.Signers))
for _, acc := range cc.Signers {
signers = append(signers, sdk.AccAddress(acc).String())
Expand Down Expand Up @@ -54,7 +56,7 @@ func (d CosmosOffchainConfigDigester) ConfigDigest(cc types.ContractConfig) (typ
return configDigest, nil
}

func (d CosmosOffchainConfigDigester) ConfigDigestPrefix() (types.ConfigDigestPrefix, error) {
func (d CosmosOffchainConfigDigester) ConfigDigestPrefix(ctx context.Context) (types.ConfigDigestPrefix, error) {
return ConfigDigestPrefixCosmos, nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/cosmos/adapters/injective/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type configProvider struct {
feedID string
}

func NewConfigProvider(lggr logger.Logger, chain adapters.Chain, args relaytypes.RelayArgs) (*configProvider, error) {
func NewConfigProvider(ctx context.Context, lggr logger.Logger, chain adapters.Chain, args relaytypes.RelayArgs) (*configProvider, error) {
var relayConfig adapters.RelayConfig
err := json.Unmarshal(args.RelayConfig, &relayConfig)
if err != nil {
Expand Down Expand Up @@ -102,8 +102,8 @@ type medianProvider struct {
transmitter types.ContractTransmitter
}

func NewMedianProvider(lggr logger.Logger, chain adapters.Chain, rargs relaytypes.RelayArgs, pargs relaytypes.PluginArgs) (relaytypes.MedianProvider, error) {
configProvider, err := NewConfigProvider(lggr, chain, rargs)
func NewMedianProvider(ctx context.Context, lggr logger.Logger, chain adapters.Chain, rargs relaytypes.RelayArgs, pargs relaytypes.PluginArgs) (relaytypes.MedianProvider, error) {
configProvider, err := NewConfigProvider(ctx, lggr, chain, rargs)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cosmos/adapters/injective/transmitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewCosmosModuleTransmitter(
}
}

func (c *CosmosModuleTransmitter) FromAccount() (types.Account, error) {
func (c *CosmosModuleTransmitter) FromAccount(ctx context.Context) (types.Account, error) {
return types.Account(c.sender.String()), nil
}

Expand Down
Loading

0 comments on commit ad0e1c9

Please sign in to comment.