-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PluginProcessors [CCIP-3147] #98
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package chainfee | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
mapset "github.com/deckarep/golang-set/v2" | ||
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3" | ||
|
||
"github.com/smartcontractkit/chainlink-ccip/shared" | ||
) | ||
|
||
type Processor struct { | ||
} | ||
|
||
func NewProcessor() *Processor { | ||
return &Processor{} | ||
} | ||
|
||
func (w *Processor) Query(ctx context.Context, prevOutcome Outcome) (Query, error) { | ||
return Query{}, nil | ||
} | ||
|
||
func (w *Processor) Observation( | ||
ctx context.Context, | ||
prevOutcome Outcome, | ||
query Query, | ||
) (Observation, error) { | ||
return Observation{}, nil | ||
} | ||
|
||
func (w *Processor) Outcome( | ||
prevOutcome Outcome, | ||
query Query, | ||
aos []shared.AttributedObservation[Observation], | ||
) (Outcome, error) { | ||
return Outcome{}, nil | ||
} | ||
|
||
func (w *Processor) ValidateObservation( | ||
prevOutcome Outcome, | ||
query Query, | ||
ao shared.AttributedObservation[Observation], | ||
) error { | ||
//TODO: Validate token prices | ||
return nil | ||
} | ||
|
||
func validateObservedGasPrices(gasPrices []cciptypes.GasPriceChain) error { | ||
// Duplicate gas prices must not appear for the same chain and must not be empty. | ||
gasPriceChains := mapset.NewSet[cciptypes.ChainSelector]() | ||
for _, g := range gasPrices { | ||
if gasPriceChains.Contains(g.ChainSel) { | ||
return fmt.Errorf("duplicate gas price for chain %d", g.ChainSel) | ||
} | ||
gasPriceChains.Add(g.ChainSel) | ||
if g.GasPrice.IsEmpty() { | ||
return fmt.Errorf("gas price must not be empty") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var _ shared.PluginProcessor[Query, Observation, Outcome] = &Processor{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package chainfee | ||
|
||
import ( | ||
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3" | ||
) | ||
|
||
type Query struct { | ||
} | ||
|
||
type Outcome struct { | ||
GasPrices []cciptypes.GasPriceChain `json:"gasPrices"` | ||
} | ||
|
||
type Observation struct { | ||
GasPrices []cciptypes.GasPriceChain `json:"gasPrices"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package chainfee | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3" | ||
) | ||
|
||
func Test_validateObservedGasPrices(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
gasPrices []cciptypes.GasPriceChain | ||
expErr bool | ||
}{ | ||
{ | ||
name: "empty is valid", | ||
gasPrices: []cciptypes.GasPriceChain{}, | ||
expErr: false, | ||
}, | ||
{ | ||
name: "all valid", | ||
gasPrices: []cciptypes.GasPriceChain{ | ||
cciptypes.NewGasPriceChain(big.NewInt(10), 1), | ||
cciptypes.NewGasPriceChain(big.NewInt(20), 2), | ||
cciptypes.NewGasPriceChain(big.NewInt(1312), 3), | ||
}, | ||
expErr: false, | ||
}, | ||
{ | ||
name: "duplicate gas price", | ||
gasPrices: []cciptypes.GasPriceChain{ | ||
cciptypes.NewGasPriceChain(big.NewInt(10), 1), | ||
cciptypes.NewGasPriceChain(big.NewInt(20), 2), | ||
cciptypes.NewGasPriceChain(big.NewInt(1312), 1), // notice we already have a gas price for chain 1 | ||
}, | ||
expErr: true, | ||
}, | ||
{ | ||
name: "empty gas price", | ||
gasPrices: []cciptypes.GasPriceChain{ | ||
cciptypes.NewGasPriceChain(big.NewInt(10), 1), | ||
cciptypes.NewGasPriceChain(big.NewInt(20), 2), | ||
cciptypes.NewGasPriceChain(nil, 3), // nil | ||
}, | ||
expErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := validateObservedGasPrices(tc.gasPrices) | ||
if tc.expErr { | ||
assert.Error(t, err) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package commit | ||
package merkleroot | ||
|
||
import ( | ||
"context" | ||
|
@@ -8,61 +8,68 @@ import ( | |
"sync" | ||
"time" | ||
|
||
"github.com/smartcontractkit/libocr/commontypes" | ||
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types" | ||
"github.com/smartcontractkit/libocr/offchainreporting2plus/types" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/hashutil" | ||
"github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
"github.com/smartcontractkit/chainlink-common/pkg/merklemulti" | ||
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3" | ||
"github.com/smartcontractkit/libocr/commontypes" | ||
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types" | ||
"github.com/smartcontractkit/libocr/offchainreporting2plus/types" | ||
|
||
"github.com/smartcontractkit/chainlink-ccip/internal/reader" | ||
"github.com/smartcontractkit/chainlink-ccip/plugintypes" | ||
"github.com/smartcontractkit/chainlink-ccip/shared" | ||
|
||
"github.com/smartcontractkit/chainlink-ccip/internal/reader" | ||
) | ||
|
||
func (p *Plugin) ObservationQuorum(_ ocr3types.OutcomeContext, _ types.Query) (ocr3types.Quorum, error) { | ||
func (w *Processor) ObservationQuorum(_ ocr3types.OutcomeContext, _ types.Query) (ocr3types.Quorum, error) { | ||
// Across all chains we require at least 2F+1 observations. | ||
return ocr3types.QuorumTwoFPlusOne, nil | ||
} | ||
|
||
func (p *Plugin) Observation( | ||
ctx context.Context, outCtx ocr3types.OutcomeContext, _ types.Query, | ||
) (types.Observation, error) { | ||
func (w *Processor) Query(ctx context.Context, prevOutcome Outcome) (Query, error) { | ||
return Query{}, nil | ||
} | ||
|
||
func (w *Processor) Observation( | ||
ctx context.Context, | ||
prevOutcome Outcome, | ||
_ Query, | ||
) (Observation, error) { | ||
tStart := time.Now() | ||
observation, nextState := p.getObservation(ctx, outCtx) | ||
p.lggr.Infow("Sending Observation", | ||
observation, nextState := w.getObservation(ctx, prevOutcome) | ||
w.lggr.Infow("Sending MerkleRootObs", | ||
"observation", observation, "nextState", nextState, "observationDuration", time.Since(tStart)) | ||
return observation.Encode() | ||
return observation, nil | ||
} | ||
|
||
func (p *Plugin) getObservation(ctx context.Context, outCtx ocr3types.OutcomeContext) (Observation, State) { | ||
previousOutcome, nextState := p.decodeOutcome(outCtx.PreviousOutcome) | ||
func (w *Processor) getObservation(ctx context.Context, previousOutcome Outcome) (Observation, State) { | ||
nextState := previousOutcome.NextState() | ||
switch nextState { | ||
case SelectingRangesForReport: | ||
offRampNextSeqNums := p.observer.ObserveOffRampNextSeqNums(ctx) | ||
offRampNextSeqNums := w.observer.ObserveOffRampNextSeqNums(ctx) | ||
return Observation{ | ||
// TODO: observe OnRamp max seq nums. The use of offRampNextSeqNums here effectively disables batching, | ||
// e.g. the ranges selected for each chain will be [x, x] (e.g. [46, 46]), which means reports will only | ||
// contain one message per chain. Querying the OnRamp contract requires changes to reader.CCIP, which will | ||
// need to be done in a future change. | ||
OnRampMaxSeqNums: offRampNextSeqNums, | ||
OffRampNextSeqNums: offRampNextSeqNums, | ||
FChain: p.observer.ObserveFChain(), | ||
FChain: w.observer.ObserveFChain(), | ||
}, nextState | ||
case BuildingReport: | ||
return Observation{ | ||
MerkleRoots: p.observer.ObserveMerkleRoots(ctx, previousOutcome.RangesSelectedForReport), | ||
GasPrices: p.observer.ObserveGasPrices(ctx), | ||
TokenPrices: p.observer.ObserveTokenPrices(ctx), | ||
FChain: p.observer.ObserveFChain(), | ||
MerkleRoots: w.observer.ObserveMerkleRoots(ctx, previousOutcome.RangesSelectedForReport), | ||
FChain: w.observer.ObserveFChain(), | ||
}, nextState | ||
case WaitingForReportTransmission: | ||
return Observation{ | ||
OffRampNextSeqNums: p.observer.ObserveOffRampNextSeqNums(ctx), | ||
FChain: p.observer.ObserveFChain(), | ||
OffRampNextSeqNums: w.observer.ObserveOffRampNextSeqNums(ctx), | ||
FChain: w.observer.ObserveFChain(), | ||
}, nextState | ||
default: | ||
p.lggr.Errorw("Unexpected state", "state", nextState) | ||
w.lggr.Errorw("Unexpected state", "state", nextState) | ||
return Observation{}, nextState | ||
} | ||
} | ||
|
@@ -74,18 +81,14 @@ type Observer interface { | |
// ObserveMerkleRoots computes the merkle roots for the given sequence number ranges | ||
ObserveMerkleRoots(ctx context.Context, ranges []plugintypes.ChainRange) []cciptypes.MerkleRootChain | ||
|
||
ObserveTokenPrices(ctx context.Context) []cciptypes.TokenPrice | ||
|
||
ObserveGasPrices(ctx context.Context) []cciptypes.GasPriceChain | ||
|
||
ObserveFChain() map[cciptypes.ChainSelector]int | ||
} | ||
|
||
type ObserverImpl struct { | ||
lggr logger.Logger | ||
homeChain reader.HomeChain | ||
nodeID commontypes.OracleID | ||
chainSupport ChainSupport | ||
chainSupport shared.ChainSupport | ||
ccipReader reader.CCIP | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
msgHasher cciptypes.MessageHasher | ||
} | ||
|
@@ -215,14 +218,6 @@ func (o ObserverImpl) computeMerkleRoot(ctx context.Context, msgs []cciptypes.Me | |
return root, nil | ||
} | ||
|
||
func (o ObserverImpl) ObserveTokenPrices(ctx context.Context) []cciptypes.TokenPrice { | ||
return []cciptypes.TokenPrice{} | ||
} | ||
|
||
func (o ObserverImpl) ObserveGasPrices(ctx context.Context) []cciptypes.GasPriceChain { | ||
return []cciptypes.GasPriceChain{} | ||
} | ||
|
||
func (o ObserverImpl) ObserveFChain() map[cciptypes.ChainSelector]int { | ||
fChain, err := o.homeChain.GetFChain() | ||
if err != nil { | ||
|
@@ -232,6 +227,3 @@ func (o ObserverImpl) ObserveFChain() map[cciptypes.ChainSelector]int { | |
} | ||
return fChain | ||
} | ||
|
||
// Interface compliance check | ||
var _ Observer = (*ObserverImpl)(nil) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leaving FChain observed here for now. Will iterate on having Fchain sent to the workers later. to unblock working on tokens and gas in commit plugin.