Skip to content

Commit

Permalink
Commit plugin - Fix non-deterministic outcome on BuildingReport state (
Browse files Browse the repository at this point in the history
…#86)

Fix the non-deterministic outcome while on the BuildingReport state
  • Loading branch information
dimkouv authored Aug 28, 2024
1 parent 33f5819 commit e7f42e1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions commit/outcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ func buildReport(
outcomeType = ReportEmpty
}

sort.Slice(roots, func(i, j int) bool { return roots[i].ChainSel < roots[j].ChainSel })

outcome := Outcome{
OutcomeType: outcomeType,
RootsToReport: roots,
Expand Down
44 changes: 44 additions & 0 deletions commit/outcome_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package commit

import (
"testing"

cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/stretchr/testify/require"
)

func Test_buildReport(t *testing.T) {
t.Run("determinism check", func(t *testing.T) {
const rounds = 50

obs := ConsensusObservation{
MerkleRoots: map[cciptypes.ChainSelector]cciptypes.MerkleRootChain{
cciptypes.ChainSelector(1): {
ChainSel: 1,
SeqNumsRange: cciptypes.NewSeqNumRange(10, 20),
MerkleRoot: cciptypes.Bytes32{1},
},
cciptypes.ChainSelector(2): {
ChainSel: 2,
SeqNumsRange: cciptypes.NewSeqNumRange(20, 30),
MerkleRoot: cciptypes.Bytes32{2},
},
},
GasPrices: map[cciptypes.ChainSelector]cciptypes.BigInt{
cciptypes.ChainSelector(1): cciptypes.NewBigIntFromInt64(1000),
cciptypes.ChainSelector(2): cciptypes.NewBigIntFromInt64(2000),
},
TokenPrices: map[types.Account]cciptypes.BigInt{
types.Account("1"): cciptypes.NewBigIntFromInt64(1000),
types.Account("2"): cciptypes.NewBigIntFromInt64(2000),
},
}

for i := 0; i < rounds; i++ {
report1 := buildReport(Query{}, obs)
report2 := buildReport(Query{}, obs)
require.Equal(t, report1, report2)
}
})
}
2 changes: 2 additions & 0 deletions commit/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func (co ConsensusObservation) GasPricesArray() []cciptypes.GasPriceChain {
for chain, gasPrice := range co.GasPrices {
gasPrices = append(gasPrices, cciptypes.NewGasPriceChain(gasPrice.Int, chain))
}
sort.Slice(gasPrices, func(i, j int) bool { return gasPrices[i].ChainSel < gasPrices[j].ChainSel })

return gasPrices
}
Expand All @@ -173,6 +174,7 @@ func (co ConsensusObservation) TokenPricesArray() []cciptypes.TokenPrice {
for tokenID, tokenPrice := range co.TokenPrices {
tokenPrices = append(tokenPrices, cciptypes.NewTokenPrice(tokenID, tokenPrice.Int))
}
sort.Slice(tokenPrices, func(i, j int) bool { return tokenPrices[i].TokenID < tokenPrices[j].TokenID })

return tokenPrices
}
Expand Down

0 comments on commit e7f42e1

Please sign in to comment.