-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add fchain validation for fee token observation (#308)
* add fchain validation for fee token observation * update error message efor consistency * goimport * check that fchain addresses exist in observed chain * boda comments
- Loading branch information
1 parent
dd6fae4
commit 810e556
Showing
5 changed files
with
114 additions
and
31 deletions.
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
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,64 @@ | ||
package tokenprice | ||
|
||
import ( | ||
"fmt" | ||
|
||
mapset "github.com/deckarep/golang-set/v2" | ||
|
||
"github.com/smartcontractkit/chainlink-ccip/internal/plugincommon" | ||
cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3" | ||
) | ||
|
||
func (p *processor) ValidateObservation( | ||
prevOutcome Outcome, | ||
query Query, | ||
ao plugincommon.AttributedObservation[Observation], | ||
) error { | ||
obs := ao.Observation | ||
|
||
if err := validateFChain(obs.FChain); err != nil { | ||
return fmt.Errorf("failed to validate FChain: %w", err) | ||
} | ||
|
||
observerSupportedChains, err := p.chainSupport.SupportedChains(ao.OracleID) | ||
if err != nil { | ||
return fmt.Errorf("failed to get supported chains: %w", err) | ||
} | ||
|
||
for chain := range obs.FChain { | ||
if !observerSupportedChains.Contains(chain) { | ||
return fmt.Errorf("chain %d is not supported by observer", chain) | ||
} | ||
} | ||
|
||
if err := validateObservedTokenPrices(obs.FeedTokenPrices); err != nil { | ||
return fmt.Errorf("failed to validate observed token prices: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateFChain(fChain map[cciptypes.ChainSelector]int) error { | ||
for chainSelector, f := range fChain { | ||
if f <= 0 { | ||
return fmt.Errorf("fChain for chain %d is not positive: %d", chainSelector, f) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateObservedTokenPrices(tokenPrices []cciptypes.TokenPrice) error { | ||
tokensWithPrice := mapset.NewSet[cciptypes.UnknownEncodedAddress]() | ||
for _, t := range tokenPrices { | ||
if tokensWithPrice.Contains(t.TokenID) { | ||
return fmt.Errorf("duplicate token price for token: %s", t.TokenID) | ||
} | ||
tokensWithPrice.Add(t.TokenID) | ||
|
||
if t.Price.IsEmpty() { | ||
return fmt.Errorf("token price of token %v must not be empty", t.TokenID) | ||
} | ||
} | ||
return nil | ||
} |
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