Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
rstout committed Aug 9, 2024
1 parent 348226b commit f37494d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 96 deletions.
2 changes: 1 addition & 1 deletion commitrmnocb/observation.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (p *Plugin) Observation(

default:
p.lggr.Warnw("Unexpected state", "state", nextState)
return types.Observation{}, nil
return observation.Encode()
}

p.lggr.Infow("Observation", "observation", observation)
Expand Down
127 changes: 62 additions & 65 deletions commitrmnocb/outcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/smartcontractkit/libocr/offchainreporting2plus/types"

"github.com/smartcontractkit/chainlink-ccip/internal/libs/slicelib"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
)

Expand Down Expand Up @@ -41,7 +40,8 @@ func (p *Plugin) Outcome(
outcome = p.checkForReportTransmission(previousOutcome, consensusObservation)

default:
return nil, fmt.Errorf("outcome unexpected state: %d", nextState)
p.lggr.Warnw("Unexpected state in Outcome", "state", nextState)
return outcome.Encode()
}

p.lggr.Infow("Commit Plugin Outcome", "outcome", outcome, "oid", p.nodeID)
Expand Down Expand Up @@ -96,7 +96,7 @@ func (p *Plugin) ReportRangesOutcome(
// Given a set of observed merkle roots, gas prices and token prices, and roots from RMN, construct a report
// to transmit on-chain
func (p *Plugin) buildReport(
query CommitQuery,
_ CommitQuery,
consensusObservation ConsensusObservation,
) CommitPluginOutcome {
// TODO: Only include chains in the report that have gas prices?
Expand Down Expand Up @@ -179,17 +179,12 @@ func (p *Plugin) getConsensusObservation(aos []types.AttributedObservation) (Con
fmt.Errorf("no consensus value for fDestChain, DestChain: %d", p.cfg.DestChain)
}

//fTokenChain, exists := fChains[cciptypes.ChainSelector(p.cfg.OffchainConfig.TokenPriceChainSelector)]
//if !exists {
// return ConsensusObservation{},
// fmt.Errorf("no consensus value for fTokenChain, TokenPriceChain: %d",
// p.cfg.OffchainConfig.TokenPriceChainSelector)
//}

consensusObs := ConsensusObservation{
MerkleRoots: p.merkleRootConsensus(aggObs.MerkleRoots, fChains),
GasPrices: make(map[cciptypes.ChainSelector]cciptypes.BigInt), // p.gasPriceConsensus(aggObs.GasPrices, fChains),
TokenPrices: make(map[types.Account]cciptypes.BigInt), // p.tokenPriceConsensus(aggObs.TokenPrices, fTokenChain),
MerkleRoots: p.merkleRootConsensus(aggObs.MerkleRoots, fChains),
// TODO: use consensus of observed gas prices
GasPrices: make(map[cciptypes.ChainSelector]cciptypes.BigInt),
// TODO: use consensus of observed token prices
TokenPrices: make(map[types.Account]cciptypes.BigInt),
OnRampMaxSeqNums: p.onRampMaxSeqNumsConsensus(aggObs.OnRampMaxSeqNums, fChains),
OffRampNextSeqNums: p.offRampMaxSeqNumsConsensus(aggObs.OffRampNextSeqNums, fDestChain),
FChain: fChains,
Expand Down Expand Up @@ -229,58 +224,60 @@ func (p *Plugin) merkleRootConsensus(
return consensus
}

// Given a mapping from chains to a list of gas prices, return a mapping from chains to a single consensus gas price.
// The consensus gas price for a given chain is the median gas price if the number of gas price observations is
// greater or equal than 2f+1, where f is the FChain of the corresponding source chain.
func (p *Plugin) gasPriceConsensus(
pricesByChain map[cciptypes.ChainSelector][]cciptypes.BigInt,
fChains map[cciptypes.ChainSelector]int,
) map[cciptypes.ChainSelector]cciptypes.BigInt {
consensus := make(map[cciptypes.ChainSelector]cciptypes.BigInt)

for chain, prices := range pricesByChain {
if f, exists := fChains[chain]; exists {
if len(prices) < 2*f+1 {
// TODO: metrics
p.lggr.Warnf("could not reach consensus on gas prices for chain %d "+
"because we did not receive more than 2f+1 observed prices, 2f+1: %d, len(prices): %d, prices: %v",
chain, 2*f+1, len(prices), prices)
}

consensus[chain] = slicelib.BigIntSortedMiddle(prices)
} else {
// TODO: metrics
p.lggr.Warnf("could not reach consensus on gas prices for chain %d because "+
"there was no consensus f value for this chain", chain)
}
}

return consensus
}

// Given a mapping from token IDs to a list of token prices, return a mapping from token IDs to a single consensus
// token price. The consensus token price for a given token ID is the median token price if the number of token price
// observations is greater or equal than 2f+1, where f is the FChain of the chain that token prices were retrieved
// from.
func (p *Plugin) tokenPriceConsensus(
pricesByToken map[types.Account][]cciptypes.BigInt,
fTokenChain int,
) map[types.Account]cciptypes.BigInt {
consensus := make(map[types.Account]cciptypes.BigInt)

for tokenID, prices := range pricesByToken {
if len(prices) < 2*fTokenChain+1 {
// TODO: metrics
p.lggr.Warnf("could not reach consensus on token prices for token %s because "+
"we did not receive more than 2f+1 observed prices, 2f+1: %d, len(prices): %d, prices: %v",
tokenID, 2*fTokenChain+1, len(prices), prices)
}

consensus[tokenID] = slicelib.BigIntSortedMiddle(prices)
}

return consensus
}
// TODO: uncomment when gas price reporting is enabled
//// Given a mapping from chains to a list of gas prices, return a mapping from chains to a single consensus gas price.
//// The consensus gas price for a given chain is the median gas price if the number of gas price observations is
//// greater or equal than 2f+1, where f is the FChain of the corresponding source chain.
//func (p *Plugin) gasPriceConsensus(
// pricesByChain map[cciptypes.ChainSelector][]cciptypes.BigInt,
// fChains map[cciptypes.ChainSelector]int,
//) map[cciptypes.ChainSelector]cciptypes.BigInt {
// consensus := make(map[cciptypes.ChainSelector]cciptypes.BigInt)
//
// for chain, prices := range pricesByChain {
// if f, exists := fChains[chain]; exists {
// if len(prices) < 2*f+1 {
// // TODO: metrics
// p.lggr.Warnf("could not reach consensus on gas prices for chain %d "+
// "because we did not receive more than 2f+1 observed prices, 2f+1: %d, len(prices): %d, prices: %v",
// chain, 2*f+1, len(prices), prices)
// }
//
// consensus[chain] = slicelib.BigIntSortedMiddle(prices)
// } else {
// // TODO: metrics
// p.lggr.Warnf("could not reach consensus on gas prices for chain %d because "+
// "there was no consensus f value for this chain", chain)
// }
// }
//
// return consensus
//}

// TODO: uncomment when token price reporting is enabled
//// Given a mapping from token IDs to a list of token prices, return a mapping from token IDs to a single consensus
//// token price. The consensus token price for a given token ID is the median token price if the number of token price
//// observations is greater or equal than 2f+1, where f is the FChain of the chain that token prices were retrieved
//// from.
//func (p *Plugin) tokenPriceConsensus(
// pricesByToken map[types.Account][]cciptypes.BigInt,
// fTokenChain int,
//) map[types.Account]cciptypes.BigInt {
// consensus := make(map[types.Account]cciptypes.BigInt)
//
// for tokenID, prices := range pricesByToken {
// if len(prices) < 2*fTokenChain+1 {
// // TODO: metrics
// p.lggr.Warnf("could not reach consensus on token prices for token %s because "+
// "we did not receive more than 2f+1 observed prices, 2f+1: %d, len(prices): %d, prices: %v",
// tokenID, 2*fTokenChain+1, len(prices), prices)
// }
//
// consensus[tokenID] = slicelib.BigIntSortedMiddle(prices)
// }
//
// return consensus
//}

// Given a mapping from chains to a list of max seq nums on their corresponding OnRamp, return a mapping from chains
// to a single max seq num. The consensus max seq num for a given chain is the f'th lowest max seq num if the number
Expand Down
30 changes: 0 additions & 30 deletions commitrmnocb/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import (
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
)

type RmnSig struct {
sig []byte
}

type CommitQuery struct {
RmnOnRampMaxSeqNums []plugintypes.SeqNumChain
MerkleRoots []cciptypes.MerkleRootChain
Expand Down Expand Up @@ -257,29 +253,3 @@ type ChainRange struct {
ChainSel cciptypes.ChainSelector `json:"chain"`
SeqNumRange cciptypes.SeqNumRange `json:"seqNumRange"`
}

// CommitPluginReport is the report that will be transmitted by the Commit Plugin
type CommitPluginReport struct {
MerkleRoots []cciptypes.MerkleRootChain
TokenPrices []cciptypes.TokenPrice `json:"tokenPrices"`
GasPrices []cciptypes.GasPriceChain `json:"gasPrices"`
}

func (r CommitPluginReport) IsEmpty() bool {
return len(r.MerkleRoots) == 0 && len(r.TokenPrices) == 0 && len(r.GasPrices) == 0
}

func (r CommitPluginReport) Encode() ([]byte, error) {
encodedReport, err := json.Marshal(r)
if err != nil {
return nil, fmt.Errorf("failed to encode CommitPluginReport: %w", err)
}

return encodedReport, nil
}

func DecodeCommitPluginReport(b []byte) (CommitPluginReport, error) {
r := CommitPluginReport{}
err := json.Unmarshal(b, &r)
return r, err
}

0 comments on commit f37494d

Please sign in to comment.