Skip to content

Commit

Permalink
[KS-363] Update Streams codec to new interface (#13725)
Browse files Browse the repository at this point in the history
Separated Unwrap() and Validate().
  • Loading branch information
bolekk authored Jul 1, 2024
1 parent 2ad16bb commit 0b7d2c4
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 51 deletions.
58 changes: 31 additions & 27 deletions core/capabilities/streams/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,12 @@ type codec struct {

var _ datastreams.ReportCodec = &codec{}

func (c *codec) UnwrapValid(wrapped values.Value, allowedSigners [][]byte, minRequiredSignatures int) ([]datastreams.FeedReport, error) {
signersMap := make(map[common.Address]struct{})
for _, signer := range allowedSigners {
signersMap[common.BytesToAddress(signer)] = struct{}{}
}
func (c *codec) Unwrap(wrapped values.Value) ([]datastreams.FeedReport, error) {
dest, err := datastreams.UnwrapFeedReportList(wrapped)
if err != nil {
return nil, fmt.Errorf("failed to unwrap: %v", err)
}
for i, report := range dest {
// signatures (report and context are signed together)
sigData := append(crypto.Keccak256(report.FullReport), report.ReportContext...)
fullHash := crypto.Keccak256(sigData)
validated := map[common.Address]struct{}{}
for _, sig := range report.Signatures {
signerPubkey, err2 := crypto.SigToPub(fullHash, sig)
if err2 != nil {
return nil, fmt.Errorf("malformed signer: %v", err2)
}
signerAddr := crypto.PubkeyToAddress(*signerPubkey)
if _, ok := signersMap[signerAddr]; !ok {
c.lggr.Debugw("invalid signer", "signerAddr", signerAddr)
continue
}
validated[signerAddr] = struct{}{}
if len(validated) >= minRequiredSignatures {
break // early exit
}
}
if len(validated) < minRequiredSignatures {
return nil, fmt.Errorf("not enough valid signatures %d, needed %d", len(validated), minRequiredSignatures)
}
// decoding fields
id, err2 := datastreams.NewFeedID(report.FeedID)
if err2 != nil {
Expand All @@ -70,6 +44,36 @@ func (c *codec) Wrap(reports []datastreams.FeedReport) (values.Value, error) {
return values.Wrap(reports)
}

func (c *codec) Validate(report datastreams.FeedReport, allowedSigners [][]byte, minRequiredSignatures int) error {
signersMap := make(map[common.Address]struct{})
for _, signer := range allowedSigners {
signersMap[common.BytesToAddress(signer)] = struct{}{}
}
// signatures (report and context are signed together)
sigData := append(crypto.Keccak256(report.FullReport), report.ReportContext...)
fullHash := crypto.Keccak256(sigData)
validated := map[common.Address]struct{}{}
for _, sig := range report.Signatures {
signerPubkey, err2 := crypto.SigToPub(fullHash, sig)
if err2 != nil {
return fmt.Errorf("malformed signer: %v", err2)
}
signerAddr := crypto.PubkeyToAddress(*signerPubkey)
if _, ok := signersMap[signerAddr]; !ok {
c.lggr.Debugw("invalid signer", "signerAddr", signerAddr)
continue
}
validated[signerAddr] = struct{}{}
if len(validated) >= minRequiredSignatures {
break // early exit
}
}
if len(validated) < minRequiredSignatures {
return fmt.Errorf("not enough valid signatures %d, needed %d", len(validated), minRequiredSignatures)
}
return nil
}

func NewCodec(lggr logger.Logger) *codec {
return &codec{lggr: lggr}
}
28 changes: 16 additions & 12 deletions core/capabilities/streams/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,25 @@ func TestCodec_WrapUnwrap(t *testing.T) {
require.NoError(t, err)

// wrong type
_, err = codec.UnwrapValid(values.NewBool(true), nil, 0)
_, err = codec.Unwrap(values.NewBool(true))
require.Error(t, err)

// wrong signatures
_, err = codec.UnwrapValid(wrapped, nil, 1)
require.Error(t, err)

// success
reports, err := codec.UnwrapValid(wrapped, allowedSigners, 2)
// correct reports byt wrong signatures
unwrapped, err := codec.Unwrap(wrapped)
require.NoError(t, err)
require.Equal(t, 2, len(reports))
require.Equal(t, price1.Bytes(), reports[0].BenchmarkPrice)
require.Equal(t, price2.Bytes(), reports[1].BenchmarkPrice)
require.Equal(t, timestamp1, reports[0].ObservationTimestamp)
require.Equal(t, timestamp2, reports[1].ObservationTimestamp)
require.Equal(t, 2, len(unwrapped))
require.Equal(t, price1.Bytes(), unwrapped[0].BenchmarkPrice)
require.Equal(t, price2.Bytes(), unwrapped[1].BenchmarkPrice)
require.Equal(t, timestamp1, unwrapped[0].ObservationTimestamp)
require.Equal(t, timestamp2, unwrapped[1].ObservationTimestamp)
for _, report := range unwrapped {
require.Error(t, codec.Validate(report, nil, 1))
}

// valid signatures
for _, report := range unwrapped {
require.NoError(t, codec.Validate(report, allowedSigners, 2))
}
}

func newFeedID(t *testing.T) ([32]byte, string) {
Expand Down
2 changes: 1 addition & 1 deletion core/scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/prometheus/client_golang v1.17.0
github.com/shopspring/decimal v1.3.1
github.com/smartcontractkit/chainlink-automation v1.0.4
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240627145530-c769d7129f16
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240701095149-7c11e2c2ce36
github.com/smartcontractkit/chainlink-vrf v0.0.0-20240222010609-cd67d123c772
github.com/smartcontractkit/chainlink/v2 v2.0.0-00010101000000-000000000000
github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c
Expand Down
4 changes: 2 additions & 2 deletions core/scripts/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1212,8 +1212,8 @@ github.com/smartcontractkit/chain-selectors v1.0.10 h1:t9kJeE6B6G+hKD0GYR4kGJSCq
github.com/smartcontractkit/chain-selectors v1.0.10/go.mod h1:d4Hi+E1zqjy9HqMkjBE5q1vcG9VGgxf5VxiRHfzi2kE=
github.com/smartcontractkit/chainlink-automation v1.0.4 h1:iyW181JjKHLNMnDleI8umfIfVVlwC7+n5izbLSFgjw8=
github.com/smartcontractkit/chainlink-automation v1.0.4/go.mod h1:u4NbPZKJ5XiayfKHD/v3z3iflQWqvtdhj13jVZXj/cM=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240627145530-c769d7129f16 h1:YTE+iBNaRNZplGiiTZFYlnUrVYq1pyQr8Yiz3V3gsN8=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240627145530-c769d7129f16/go.mod h1:EWvSuqIJUYXZLEHewC7WCaPylM2jyjF3Q36BZPS4MoI=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240701095149-7c11e2c2ce36 h1:x9WPqwSWTZXlUPxZUIHIQIjhL1l6dq5KEiB9jIP+KLs=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240701095149-7c11e2c2ce36/go.mod h1:EWvSuqIJUYXZLEHewC7WCaPylM2jyjF3Q36BZPS4MoI=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240621143432-85370a54b141 h1:TMOoYaeSDkkI3jkCH7lKHOZaLkeDuxFTNC+XblD6M0M=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240621143432-85370a54b141/go.mod h1:0UNuO3nDt9MFsZPaHJBEUolxVkN0iC69j1ccDp95e8k=
github.com/smartcontractkit/chainlink-data-streams v0.0.0-20240220203239-09be0ea34540 h1:xFSv8561jsLtF6gYZr/zW2z5qUUAkcFkApin2mnbYTo=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ require (
github.com/shopspring/decimal v1.3.1
github.com/smartcontractkit/chain-selectors v1.0.10
github.com/smartcontractkit/chainlink-automation v1.0.4
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240627145530-c769d7129f16
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240701095149-7c11e2c2ce36
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240621143432-85370a54b141
github.com/smartcontractkit/chainlink-data-streams v0.0.0-20240220203239-09be0ea34540
github.com/smartcontractkit/chainlink-feeds v0.0.0-20240522213638-159fb2d99917
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1169,8 +1169,8 @@ github.com/smartcontractkit/chain-selectors v1.0.10 h1:t9kJeE6B6G+hKD0GYR4kGJSCq
github.com/smartcontractkit/chain-selectors v1.0.10/go.mod h1:d4Hi+E1zqjy9HqMkjBE5q1vcG9VGgxf5VxiRHfzi2kE=
github.com/smartcontractkit/chainlink-automation v1.0.4 h1:iyW181JjKHLNMnDleI8umfIfVVlwC7+n5izbLSFgjw8=
github.com/smartcontractkit/chainlink-automation v1.0.4/go.mod h1:u4NbPZKJ5XiayfKHD/v3z3iflQWqvtdhj13jVZXj/cM=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240627145530-c769d7129f16 h1:YTE+iBNaRNZplGiiTZFYlnUrVYq1pyQr8Yiz3V3gsN8=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240627145530-c769d7129f16/go.mod h1:EWvSuqIJUYXZLEHewC7WCaPylM2jyjF3Q36BZPS4MoI=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240701095149-7c11e2c2ce36 h1:x9WPqwSWTZXlUPxZUIHIQIjhL1l6dq5KEiB9jIP+KLs=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240701095149-7c11e2c2ce36/go.mod h1:EWvSuqIJUYXZLEHewC7WCaPylM2jyjF3Q36BZPS4MoI=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240621143432-85370a54b141 h1:TMOoYaeSDkkI3jkCH7lKHOZaLkeDuxFTNC+XblD6M0M=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240621143432-85370a54b141/go.mod h1:0UNuO3nDt9MFsZPaHJBEUolxVkN0iC69j1ccDp95e8k=
github.com/smartcontractkit/chainlink-data-streams v0.0.0-20240220203239-09be0ea34540 h1:xFSv8561jsLtF6gYZr/zW2z5qUUAkcFkApin2mnbYTo=
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
github.com/shopspring/decimal v1.3.1
github.com/slack-go/slack v0.12.2
github.com/smartcontractkit/chainlink-automation v1.0.4
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240627145530-c769d7129f16
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240701095149-7c11e2c2ce36
github.com/smartcontractkit/chainlink-testing-framework v1.31.7
github.com/smartcontractkit/chainlink-testing-framework/grafana v0.0.0-20240405215812-5a72bc9af239
github.com/smartcontractkit/chainlink-vrf v0.0.0-20231120191722-fef03814f868
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1513,8 +1513,8 @@ github.com/smartcontractkit/chain-selectors v1.0.10 h1:t9kJeE6B6G+hKD0GYR4kGJSCq
github.com/smartcontractkit/chain-selectors v1.0.10/go.mod h1:d4Hi+E1zqjy9HqMkjBE5q1vcG9VGgxf5VxiRHfzi2kE=
github.com/smartcontractkit/chainlink-automation v1.0.4 h1:iyW181JjKHLNMnDleI8umfIfVVlwC7+n5izbLSFgjw8=
github.com/smartcontractkit/chainlink-automation v1.0.4/go.mod h1:u4NbPZKJ5XiayfKHD/v3z3iflQWqvtdhj13jVZXj/cM=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240627145530-c769d7129f16 h1:YTE+iBNaRNZplGiiTZFYlnUrVYq1pyQr8Yiz3V3gsN8=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240627145530-c769d7129f16/go.mod h1:EWvSuqIJUYXZLEHewC7WCaPylM2jyjF3Q36BZPS4MoI=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240701095149-7c11e2c2ce36 h1:x9WPqwSWTZXlUPxZUIHIQIjhL1l6dq5KEiB9jIP+KLs=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240701095149-7c11e2c2ce36/go.mod h1:EWvSuqIJUYXZLEHewC7WCaPylM2jyjF3Q36BZPS4MoI=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240621143432-85370a54b141 h1:TMOoYaeSDkkI3jkCH7lKHOZaLkeDuxFTNC+XblD6M0M=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240621143432-85370a54b141/go.mod h1:0UNuO3nDt9MFsZPaHJBEUolxVkN0iC69j1ccDp95e8k=
github.com/smartcontractkit/chainlink-data-streams v0.0.0-20240220203239-09be0ea34540 h1:xFSv8561jsLtF6gYZr/zW2z5qUUAkcFkApin2mnbYTo=
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/load/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/rs/zerolog v1.31.0
github.com/slack-go/slack v0.12.2
github.com/smartcontractkit/chainlink-automation v1.0.4
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240627145530-c769d7129f16
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240701095149-7c11e2c2ce36
github.com/smartcontractkit/chainlink-testing-framework v1.31.7
github.com/smartcontractkit/chainlink/integration-tests v0.0.0-20240214231432-4ad5eb95178c
github.com/smartcontractkit/chainlink/v2 v2.9.0-beta0.0.20240216210048-da02459ddad8
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/load/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1503,8 +1503,8 @@ github.com/smartcontractkit/chain-selectors v1.0.10 h1:t9kJeE6B6G+hKD0GYR4kGJSCq
github.com/smartcontractkit/chain-selectors v1.0.10/go.mod h1:d4Hi+E1zqjy9HqMkjBE5q1vcG9VGgxf5VxiRHfzi2kE=
github.com/smartcontractkit/chainlink-automation v1.0.4 h1:iyW181JjKHLNMnDleI8umfIfVVlwC7+n5izbLSFgjw8=
github.com/smartcontractkit/chainlink-automation v1.0.4/go.mod h1:u4NbPZKJ5XiayfKHD/v3z3iflQWqvtdhj13jVZXj/cM=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240627145530-c769d7129f16 h1:YTE+iBNaRNZplGiiTZFYlnUrVYq1pyQr8Yiz3V3gsN8=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240627145530-c769d7129f16/go.mod h1:EWvSuqIJUYXZLEHewC7WCaPylM2jyjF3Q36BZPS4MoI=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240701095149-7c11e2c2ce36 h1:x9WPqwSWTZXlUPxZUIHIQIjhL1l6dq5KEiB9jIP+KLs=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240701095149-7c11e2c2ce36/go.mod h1:EWvSuqIJUYXZLEHewC7WCaPylM2jyjF3Q36BZPS4MoI=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240621143432-85370a54b141 h1:TMOoYaeSDkkI3jkCH7lKHOZaLkeDuxFTNC+XblD6M0M=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240621143432-85370a54b141/go.mod h1:0UNuO3nDt9MFsZPaHJBEUolxVkN0iC69j1ccDp95e8k=
github.com/smartcontractkit/chainlink-data-streams v0.0.0-20240220203239-09be0ea34540 h1:xFSv8561jsLtF6gYZr/zW2z5qUUAkcFkApin2mnbYTo=
Expand Down

0 comments on commit 0b7d2c4

Please sign in to comment.