Skip to content

Commit

Permalink
commit/merkleroot: unit tests, bump coverage (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
makramkd authored Nov 5, 2024
1 parent c298311 commit 733f5d2
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 27 deletions.
14 changes: 7 additions & 7 deletions commit/merkleroot/observation.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ type Observer interface {
ObserveFChain() map[cciptypes.ChainSelector]int
}

type ObserverImpl struct {
type observerImpl struct {
lggr logger.Logger
homeChain reader.HomeChain
nodeID commontypes.OracleID
Expand All @@ -265,7 +265,7 @@ type ObserverImpl struct {
}

// ObserveOffRampNextSeqNums observes the next sequence numbers for each source chain from the OffRamp
func (o ObserverImpl) ObserveOffRampNextSeqNums(ctx context.Context) []plugintypes.SeqNumChain {
func (o observerImpl) ObserveOffRampNextSeqNums(ctx context.Context) []plugintypes.SeqNumChain {
supportsDestChain, err := o.chainSupport.SupportsDestChain(o.nodeID)
if err != nil {
o.lggr.Warnw("call to SupportsDestChain failed", "err", err)
Expand Down Expand Up @@ -304,7 +304,7 @@ func (o ObserverImpl) ObserveOffRampNextSeqNums(ctx context.Context) []plugintyp
}

// ObserveLatestOnRampSeqNums observes the latest onRamp sequence numbers for each configured source chain.
func (o ObserverImpl) ObserveLatestOnRampSeqNums(
func (o observerImpl) ObserveLatestOnRampSeqNums(
ctx context.Context, destChain cciptypes.ChainSelector) []plugintypes.SeqNumChain {

allSourceChains, err := o.chainSupport.KnownSourceChainsSlice()
Expand Down Expand Up @@ -353,7 +353,7 @@ func (o ObserverImpl) ObserveLatestOnRampSeqNums(
}

// ObserveMerkleRoots computes the merkle roots for the given sequence number ranges
func (o ObserverImpl) ObserveMerkleRoots(
func (o observerImpl) ObserveMerkleRoots(
ctx context.Context,
ranges []plugintypes.ChainRange,
) []cciptypes.MerkleRootChain {
Expand Down Expand Up @@ -414,7 +414,7 @@ func (o ObserverImpl) ObserveMerkleRoots(
}

// computeMerkleRoot computes the merkle root of a list of messages
func (o ObserverImpl) computeMerkleRoot(ctx context.Context, msgs []cciptypes.Message) (cciptypes.Bytes32, error) {
func (o observerImpl) computeMerkleRoot(ctx context.Context, msgs []cciptypes.Message) (cciptypes.Bytes32, error) {
var hashes [][32]byte
sort.Slice(msgs, func(i, j int) bool { return msgs[i].Header.SequenceNumber < msgs[j].Header.SequenceNumber })

Expand Down Expand Up @@ -452,7 +452,7 @@ func (o ObserverImpl) computeMerkleRoot(ctx context.Context, msgs []cciptypes.Me
return root, nil
}

func (o ObserverImpl) ObserveRMNRemoteCfg(
func (o observerImpl) ObserveRMNRemoteCfg(
ctx context.Context,
dstChain cciptypes.ChainSelector) rmntypes.RemoteConfig {
rmnRemoteCfg, err := o.ccipReader.GetRMNRemoteConfig(ctx, dstChain)
Expand All @@ -468,7 +468,7 @@ func (o ObserverImpl) ObserveRMNRemoteCfg(
return rmnRemoteCfg
}

func (o ObserverImpl) ObserveFChain() map[cciptypes.ChainSelector]int {
func (o observerImpl) ObserveFChain() map[cciptypes.ChainSelector]int {
fChain, err := o.homeChain.GetFChain()
if err != nil {
// TODO: metrics
Expand Down
6 changes: 3 additions & 3 deletions commit/merkleroot/observation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func Test_ObserveOffRampNextSeqNums(t *testing.T) {
defer chainSupport.AssertExpectations(t)
defer ccipReader.AssertExpectations(t)

o := ObserverImpl{
o := observerImpl{
nodeID: nodeID,
lggr: logger.Test(t),
msgHasher: mocks.NewMessageHasher(),
Expand Down Expand Up @@ -468,7 +468,7 @@ func Test_ObserveMerkleRoots(t *testing.T) {
chainSupport.On("SupportedChains", nodeID).Return(tc.supportedChains, nil)
}

o := ObserverImpl{
o := observerImpl{
nodeID: nodeID,
lggr: logger.Test(t),
msgHasher: mocks.NewMessageHasher(),
Expand Down Expand Up @@ -571,7 +571,7 @@ func Test_computeMerkleRoot(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
p := ObserverImpl{
p := observerImpl{
lggr: logger.Test(t),
msgHasher: tc.messageHasher,
}
Expand Down
12 changes: 6 additions & 6 deletions commit/merkleroot/outcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (w *Processor) getOutcome(
func reportRangesOutcome(
_ Query,
lggr logger.Logger,
consensusObservation ConsensusObservation,
consensusObservation consensusObservation,
maxMerkleTreeSize uint64,
dstChain cciptypes.ChainSelector,
) Outcome {
Expand Down Expand Up @@ -143,7 +143,7 @@ func reportRangesOutcome(
func buildReport(
q Query,
lggr logger.Logger,
consensusObservation ConsensusObservation,
consensusObservation consensusObservation,
prevOutcome Outcome,
) Outcome {
roots := maps.Values(consensusObservation.MerkleRoots)
Expand Down Expand Up @@ -232,7 +232,7 @@ func checkForReportTransmission(
lggr logger.Logger,
maxReportTransmissionCheckAttempts uint,
previousOutcome Outcome,
consensusObservation ConsensusObservation,
consensusObservation consensusObservation,
) Outcome {

offRampUpdated := false
Expand Down Expand Up @@ -271,7 +271,7 @@ func getConsensusObservation(
fRoleDON int,
destChain cciptypes.ChainSelector,
aos []plugincommon.AttributedObservation[Observation],
) (ConsensusObservation, error) {
) (consensusObservation, error) {
aggObs := aggregateObservations(aos)

// consensus on the fChain map uses the role DON F value
Expand All @@ -281,7 +281,7 @@ func getConsensusObservation(

_, exists := fChains[destChain]
if !exists {
return ConsensusObservation{},
return consensusObservation{},
fmt.Errorf("no consensus value for fDestChain, destChain: %d, fChainObs: %+v, fChainsConsensus: %+v",
destChain, aggObs.FChain, fChains,
)
Expand All @@ -292,7 +292,7 @@ func getConsensusObservation(

// Get consensus using strict 2fChain+1 threshold.
twoFChainPlus1 := consensus.MakeMultiThreshold(fChains, consensus.TwoFPlus1)
consensusObs := ConsensusObservation{
consensusObs := consensusObservation{
MerkleRoots: consensus.GetConsensusMap(lggr, "Merkle Root", aggObs.MerkleRoots, twoFChainPlus1),
OnRampMaxSeqNums: consensus.GetConsensusMap(lggr, "OnRamp Max Seq Nums", aggObs.OnRampMaxSeqNums, twoFChainPlus1),
OffRampNextSeqNums: consensus.GetConsensusMap(
Expand Down
8 changes: 4 additions & 4 deletions commit/merkleroot/outcome_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Test_buildReport(t *testing.T) {
t.Run("determinism check", func(t *testing.T) {
const rounds = 50

obs := ConsensusObservation{
obs := consensusObservation{
MerkleRoots: map[cciptypes.ChainSelector]cciptypes.MerkleRootChain{
cciptypes.ChainSelector(1): {
ChainSel: 1,
Expand Down Expand Up @@ -54,7 +54,7 @@ func Test_reportRangesOutcome(t *testing.T) {

testCases := []struct {
name string
consensusObservation ConsensusObservation
consensusObservation consensusObservation
merkleTreeSizeLimit uint64
expectedOutcome Outcome
}{
Expand All @@ -69,7 +69,7 @@ func Test_reportRangesOutcome(t *testing.T) {
},
{
name: "simple scenario with one chain",
consensusObservation: ConsensusObservation{
consensusObservation: consensusObservation{
OnRampMaxSeqNums: map[cciptypes.ChainSelector]cciptypes.SeqNum{
1: 20,
},
Expand All @@ -94,7 +94,7 @@ func Test_reportRangesOutcome(t *testing.T) {
},
{
name: "simple scenario with one chain",
consensusObservation: ConsensusObservation{
consensusObservation: consensusObservation{
OnRampMaxSeqNums: map[cciptypes.ChainSelector]cciptypes.SeqNum{
1: 20,
2: 1000,
Expand Down
2 changes: 1 addition & 1 deletion commit/merkleroot/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewProcessor(
rmnCrypto cciptypes.RMNCrypto,
rmnHomeReader readerpkg.RMNHome,
) *Processor {
observer := ObserverImpl{
observer := observerImpl{
lggr,
homeChain,
oracleID,
Expand Down
12 changes: 6 additions & 6 deletions commit/merkleroot/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func (o Observation) IsEmpty() bool {
len(o.FChain) == 0
}

// MerkleAggregatedObservation is the aggregation of a list of observations
type MerkleAggregatedObservation struct {
// aggregatedObservation is the aggregation of a list of merkle root processor observations
type aggregatedObservation struct {
// A map from chain selectors to the list of merkle roots observed for each chain
MerkleRoots map[cciptypes.ChainSelector][]cciptypes.MerkleRootChain

Expand All @@ -50,8 +50,8 @@ type MerkleAggregatedObservation struct {
}

// aggregateObservations takes a list of observations and produces an MerkleAggregatedObservation
func aggregateObservations(aos []plugincommon.AttributedObservation[Observation]) MerkleAggregatedObservation {
aggObs := MerkleAggregatedObservation{
func aggregateObservations(aos []plugincommon.AttributedObservation[Observation]) aggregatedObservation {
aggObs := aggregatedObservation{
MerkleRoots: make(map[cciptypes.ChainSelector][]cciptypes.MerkleRootChain),
OnRampMaxSeqNums: make(map[cciptypes.ChainSelector][]cciptypes.SeqNum),
OffRampNextSeqNums: make(map[cciptypes.ChainSelector][]cciptypes.SeqNum),
Expand Down Expand Up @@ -94,8 +94,8 @@ func aggregateObservations(aos []plugincommon.AttributedObservation[Observation]
return aggObs
}

// ConsensusObservation holds the consensus values for all chains across all observations in a round
type ConsensusObservation struct {
// consensusObservation holds the consensus values for all chains across all observations in a round
type consensusObservation struct {
// A map from chain selectors to each chain's consensus merkle root
MerkleRoots map[cciptypes.ChainSelector]cciptypes.MerkleRootChain

Expand Down
Loading

0 comments on commit 733f5d2

Please sign in to comment.