Skip to content

Commit

Permalink
Add discovery processor to commit plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
winder committed Sep 19, 2024
1 parent 88df72b commit 27d995b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
52 changes: 52 additions & 0 deletions commit/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/smartcontractkit/chainlink-ccip/commit/merkleroot/rmn"
"github.com/smartcontractkit/chainlink-ccip/commit/tokenprice"
"github.com/smartcontractkit/chainlink-ccip/internal/plugincommon"
"github.com/smartcontractkit/chainlink-ccip/internal/plugincommon/discovery"
dt "github.com/smartcontractkit/chainlink-ccip/internal/plugincommon/discovery/discoverytypes"
"github.com/smartcontractkit/chainlink-ccip/internal/reader"
readerpkg "github.com/smartcontractkit/chainlink-ccip/pkg/reader"
"github.com/smartcontractkit/chainlink-ccip/pluginconfig"
Expand All @@ -42,7 +44,11 @@ type Plugin struct {
merkleRootProcessor plugincommon.PluginProcessor[merkleroot.Query, merkleroot.Observation, merkleroot.Outcome]
tokenPriceProcessor plugincommon.PluginProcessor[tokenprice.Query, tokenprice.Observation, tokenprice.Outcome]
chainFeeProcessor plugincommon.PluginProcessor[chainfee.Query, chainfee.Observation, chainfee.Outcome]
discoveryProcessor *discovery.ContractDiscoveryProcessor
rmnConfig rmn.Config

// contractsInitialized is used to track if the contracts have been initialized.
contractsInitialized bool
}

func NewPlugin(
Expand Down Expand Up @@ -100,6 +106,14 @@ func NewPlugin(
reportingCfg.F,
)

discoveryProcessor := discovery.NewContractDiscoveryProcessor(
lggr,
&ccipReader,
homeChain,
cfg.DestChain,
reportingCfg.F,
)

return &Plugin{
nodeID: nodeID,
oracleIDToP2pID: oracleIDToP2pID,
Expand All @@ -114,6 +128,7 @@ func NewPlugin(
chainSupport: chainSupport,
merkleRootProcessor: merkleRootProcessor,
tokenPriceProcessor: tokenPriceProcessor,
discoveryProcessor: discoveryProcessor,
chainFeeProcessor: chainfee.NewProcessor(),
rmnConfig: rmnConfig,
}
Expand Down Expand Up @@ -159,6 +174,23 @@ func (p *Plugin) Observation(
return nil, fmt.Errorf("decode query: %w", err)
}

var discoveryObs dt.Observation
// discovery processor disabled by setting it to nil.
if p.discoveryProcessor != nil {
discoveryObs, err = p.discoveryProcessor.Observation(ctx, dt.Outcome{}, dt.Query{})
if err != nil {
p.lggr.Errorw("failed to get gas prices", "err", err)
}
}

if !p.contractsInitialized {
p.lggr.Infow("Contracts not initialized, skipping commit observations.")
return Observation{
FChain: fChain,
DiscoveryObs: discoveryObs,
}.Encode()
}

merkleRootObs, err := p.merkleRootProcessor.Observation(ctx, prevOutcome.MerkleRootOutcome, decodedQ.MerkleRootQuery)
if err != nil {
p.lggr.Errorw("failed to get merkle observation", "err", err)
Expand All @@ -176,6 +208,7 @@ func (p *Plugin) Observation(
MerkleRootObs: merkleRootObs,
TokenPriceObs: tokenPriceObs,
ChainFeeObs: chainFeeObs,
DiscoveryObs: discoveryObs,
FChain: fChain,
}
return obs.Encode()
Expand Down Expand Up @@ -208,6 +241,7 @@ func (p *Plugin) Outcome(
var merkleObservations []MerkleRootObservation
var tokensObservations []TokenPricesObservation
var feeObservations []ChainFeeObservation
var discoveryObservations []plugincommon.AttributedObservation[dt.Observation]

for _, ao := range aos {
obs, err := DecodeCommitPluginObservation(ao.Observation)
Expand Down Expand Up @@ -235,6 +269,12 @@ func (p *Plugin) Outcome(
Observation: obs.ChainFeeObs,
},
)

discoveryObservations = append(discoveryObservations,
plugincommon.AttributedObservation[dt.Observation]{
OracleID: ao.Observer,
Observation: obs.DiscoveryObs,
})
}

merkleRootOutcome, err := p.merkleRootProcessor.Outcome(
Expand Down Expand Up @@ -264,6 +304,18 @@ func (p *Plugin) Outcome(
p.lggr.Warnw("failed to get gas prices outcome", "err", err)
}

// discovery processor disabled by setting it to nil.
if p.discoveryProcessor != nil {
_, err = p.discoveryProcessor.Outcome(
dt.Outcome{},
dt.Query{},
discoveryObservations,
)
if err != nil {
p.lggr.Warnw("failed to handle contract discovery outcome", "err", err)
}
}

return Outcome{
MerkleRootOutcome: merkleRootOutcome,
TokenPriceOutcome: tokenPriceOutcome,
Expand Down
2 changes: 2 additions & 0 deletions commit/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/smartcontractkit/chainlink-ccip/commit/chainfee"
"github.com/smartcontractkit/chainlink-ccip/commit/merkleroot"
"github.com/smartcontractkit/chainlink-ccip/commit/tokenprice"
dt "github.com/smartcontractkit/chainlink-ccip/internal/plugincommon/discovery/discoverytypes"
)

type Query struct {
Expand All @@ -31,6 +32,7 @@ type Observation struct {
MerkleRootObs merkleroot.Observation `json:"merkleObs"`
TokenPriceObs tokenprice.Observation `json:"tokenObs"`
ChainFeeObs chainfee.Observation `json:"chainFeeObs"`
DiscoveryObs dt.Observation `json:"discoveryObs"`
FChain map[cciptypes.ChainSelector]int `json:"fChain"`
}

Expand Down

0 comments on commit 27d995b

Please sign in to comment.