Skip to content

Commit

Permalink
[BCF-2727] Add telemetry adapter service (#11125)
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-cordenier authored Oct 31, 2023
1 parent 7cadec0 commit 8fba9c8
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 9 deletions.
2 changes: 1 addition & 1 deletion core/scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ require (
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704 // indirect
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20231030134738-81a5a89699a0 // indirect
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231030111206-48c9bf5d5436 // indirect
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111 // indirect
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20231023133638-72f4e799ab05 // indirect
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.1-beta-test.0.20231024133459-1ef3a11319eb // indirect
github.com/smartcontractkit/tdh2/go/ocr2/decryptionplugin v0.0.0-20230906073235-9e478e5e19f1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions core/scripts/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1458,8 +1458,8 @@ github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704 h1:T3lFWumv
github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704/go.mod h1:2QuJdEouTWjh5BDy5o/vgGXQtR4Gz8yH1IYB5eT7u4M=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20231030134738-81a5a89699a0 h1:YrJ3moRDu2kgdv4o3Hym/FWVF4MS5cIZ7o7wk+43pvk=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20231030134738-81a5a89699a0/go.mod h1:fxtwgVZzTgoU1CpdSxNvFXecIY2r8DhH2JCzPO4e9G0=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231030111206-48c9bf5d5436 h1:XJuWThPInOZ9Bz0zM8xmACO+Ly/cY9+0JOILkHlN/2o=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231030111206-48c9bf5d5436/go.mod h1:M9U1JV7IQi8Sfj4JR1qSi1tIh6omgW78W/8SHN/8BUQ=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111 h1:CElKhWq0WIa9Rmg5Ssajs5Hp3m3u/nYIQdXtpj2gbcc=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111/go.mod h1:M9U1JV7IQi8Sfj4JR1qSi1tIh6omgW78W/8SHN/8BUQ=
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20231023133638-72f4e799ab05 h1:DaPSVnxe7oz1QJ+AVIhQWs1W3ubQvwvGo9NbHpMs1OQ=
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20231023133638-72f4e799ab05/go.mod h1:o0Pn1pbaUluboaK6/yhf8xf7TiFCkyFl6WUOdwqamuU=
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.1-beta-test.0.20231024133459-1ef3a11319eb h1:HiluOfEVGOQTM6BTDImOqYdMZZ7qq7fkZ3TJdmItNr8=
Expand Down
56 changes: 56 additions & 0 deletions core/services/ocr2/plugins/generic/telemetry_adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package generic

import (
"context"
"errors"

"github.com/smartcontractkit/libocr/commontypes"

"github.com/smartcontractkit/chainlink-relay/pkg/types"
)

var _ types.TelemetryService = (*TelemetryAdapter)(nil)

type TelemetryAdapter struct {
endpointGenerator types.MonitoringEndpointGenerator
endpoints map[[4]string]commontypes.MonitoringEndpoint
}

func NewTelemetryAdapter(endpointGen types.MonitoringEndpointGenerator) *TelemetryAdapter {
return &TelemetryAdapter{
endpoints: make(map[[4]string]commontypes.MonitoringEndpoint),
endpointGenerator: endpointGen,
}
}

func (t *TelemetryAdapter) Send(ctx context.Context, network string, chainID string, contractID string, telemetryType string, payload []byte) error {
e, err := t.getOrCreateEndpoint(contractID, telemetryType, network, chainID)
if err != nil {
return err
}
e.SendLog(payload)
return nil
}

func (t *TelemetryAdapter) getOrCreateEndpoint(contractID string, telemetryType string, network string, chainID string) (commontypes.MonitoringEndpoint, error) {
if contractID == "" {
return nil, errors.New("contractID cannot be empty")
}
if telemetryType == "" {
return nil, errors.New("telemetryType cannot be empty")
}
if network == "" {
return nil, errors.New("network cannot be empty")
}
if chainID == "" {
return nil, errors.New("chainID cannot be empty")
}

key := [4]string{network, chainID, contractID, telemetryType}
e, ok := t.endpoints[key]
if !ok {
e = t.endpointGenerator.GenMonitoringEndpoint(network, chainID, contractID, telemetryType)
t.endpoints[key] = e
}
return e, nil
}
108 changes: 108 additions & 0 deletions core/services/ocr2/plugins/generic/telemetry_adapter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package generic

import (
"context"
"fmt"
"testing"

"github.com/smartcontractkit/libocr/commontypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type mockEndpoint struct {
network string
chainID string
contractID string
telemetryType string
payload []byte
}

func (m *mockEndpoint) SendLog(payload []byte) { m.payload = payload }

type mockGenerator struct{}

func (m *mockGenerator) GenMonitoringEndpoint(network, chainID, contractID, telemetryType string) commontypes.MonitoringEndpoint {
return &mockEndpoint{
network: network,
chainID: chainID,
contractID: contractID,
telemetryType: telemetryType,
}
}

func TestTelemetryAdapter(t *testing.T) {
ta := NewTelemetryAdapter(&mockGenerator{})

tests := []struct {
name string
contractID string
telemetryType string
networkID string
chainID string
payload []byte
errorMsg string
}{
{
name: "valid request",
contractID: "contract",
telemetryType: "mercury",
networkID: "solana",
chainID: "1337",
payload: []byte("uh oh"),
},
{
name: "no valid contractID",
telemetryType: "mercury",
networkID: "solana",
chainID: "1337",
payload: []byte("uh oh"),
errorMsg: "contractID cannot be empty",
},
{
name: "no valid chainID",
contractID: "contract",
telemetryType: "mercury",
networkID: "solana",
payload: []byte("uh oh"),
errorMsg: "chainID cannot be empty",
},
{
name: "no valid telemetryType",
contractID: "contract",
networkID: "solana",
chainID: "1337",
payload: []byte("uh oh"),
errorMsg: "telemetryType cannot be empty",
},
{
name: "no valid network",
contractID: "contract",
telemetryType: "mercury",
chainID: "1337",
payload: []byte("uh oh"),
errorMsg: "network cannot be empty",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := ta.Send(context.Background(), test.networkID, test.chainID, test.contractID, test.telemetryType, test.payload)
if test.errorMsg != "" {
assert.ErrorContains(t, err, test.errorMsg)
} else {
require.NoError(t, err)
key := [4]string{test.networkID, test.chainID, test.contractID, test.telemetryType}
fmt.Printf("%+v", ta.endpoints)
endpoint, ok := ta.endpoints[key]
require.True(t, ok)

me := endpoint.(*mockEndpoint)
assert.Equal(t, test.networkID, me.network)
assert.Equal(t, test.chainID, me.chainID)
assert.Equal(t, test.contractID, me.contractID)
assert.Equal(t, test.telemetryType, me.telemetryType)
assert.Equal(t, test.payload, me.payload)
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ require (
github.com/shopspring/decimal v1.3.1
github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20231030134738-81a5a89699a0
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231030111206-48c9bf5d5436
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20231023133638-72f4e799ab05
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.1-beta-test.0.20231024133459-1ef3a11319eb
github.com/smartcontractkit/libocr v0.0.0-20231020123319-d255366a6545
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1459,8 +1459,8 @@ github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704 h1:T3lFWumv
github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704/go.mod h1:2QuJdEouTWjh5BDy5o/vgGXQtR4Gz8yH1IYB5eT7u4M=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20231030134738-81a5a89699a0 h1:YrJ3moRDu2kgdv4o3Hym/FWVF4MS5cIZ7o7wk+43pvk=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20231030134738-81a5a89699a0/go.mod h1:fxtwgVZzTgoU1CpdSxNvFXecIY2r8DhH2JCzPO4e9G0=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231030111206-48c9bf5d5436 h1:XJuWThPInOZ9Bz0zM8xmACO+Ly/cY9+0JOILkHlN/2o=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231030111206-48c9bf5d5436/go.mod h1:M9U1JV7IQi8Sfj4JR1qSi1tIh6omgW78W/8SHN/8BUQ=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111 h1:CElKhWq0WIa9Rmg5Ssajs5Hp3m3u/nYIQdXtpj2gbcc=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111/go.mod h1:M9U1JV7IQi8Sfj4JR1qSi1tIh6omgW78W/8SHN/8BUQ=
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20231023133638-72f4e799ab05 h1:DaPSVnxe7oz1QJ+AVIhQWs1W3ubQvwvGo9NbHpMs1OQ=
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20231023133638-72f4e799ab05/go.mod h1:o0Pn1pbaUluboaK6/yhf8xf7TiFCkyFl6WUOdwqamuU=
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.1-beta-test.0.20231024133459-1ef3a11319eb h1:HiluOfEVGOQTM6BTDImOqYdMZZ7qq7fkZ3TJdmItNr8=
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ require (
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704 // indirect
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20231030134738-81a5a89699a0 // indirect
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231030111206-48c9bf5d5436 // indirect
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111 // indirect
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20231023133638-72f4e799ab05 // indirect
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.1-beta-test.0.20231024133459-1ef3a11319eb // indirect
github.com/smartcontractkit/sqlx v1.3.5-0.20210805004948-4be295aacbeb // indirect
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2362,8 +2362,8 @@ github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704 h1:T3lFWumv
github.com/smartcontractkit/caigo v0.0.0-20230621050857-b29a4ca8c704/go.mod h1:2QuJdEouTWjh5BDy5o/vgGXQtR4Gz8yH1IYB5eT7u4M=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20231030134738-81a5a89699a0 h1:YrJ3moRDu2kgdv4o3Hym/FWVF4MS5cIZ7o7wk+43pvk=
github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20231030134738-81a5a89699a0/go.mod h1:fxtwgVZzTgoU1CpdSxNvFXecIY2r8DhH2JCzPO4e9G0=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231030111206-48c9bf5d5436 h1:XJuWThPInOZ9Bz0zM8xmACO+Ly/cY9+0JOILkHlN/2o=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231030111206-48c9bf5d5436/go.mod h1:M9U1JV7IQi8Sfj4JR1qSi1tIh6omgW78W/8SHN/8BUQ=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111 h1:CElKhWq0WIa9Rmg5Ssajs5Hp3m3u/nYIQdXtpj2gbcc=
github.com/smartcontractkit/chainlink-relay v0.1.7-0.20231031114820-e9826d481111/go.mod h1:M9U1JV7IQi8Sfj4JR1qSi1tIh6omgW78W/8SHN/8BUQ=
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20231023133638-72f4e799ab05 h1:DaPSVnxe7oz1QJ+AVIhQWs1W3ubQvwvGo9NbHpMs1OQ=
github.com/smartcontractkit/chainlink-solana v1.0.3-0.20231023133638-72f4e799ab05/go.mod h1:o0Pn1pbaUluboaK6/yhf8xf7TiFCkyFl6WUOdwqamuU=
github.com/smartcontractkit/chainlink-starknet/relayer v0.0.1-beta-test.0.20231024133459-1ef3a11319eb h1:HiluOfEVGOQTM6BTDImOqYdMZZ7qq7fkZ3TJdmItNr8=
Expand Down

0 comments on commit 8fba9c8

Please sign in to comment.