Skip to content

Commit

Permalink
Validate invariants in report
Browse files Browse the repository at this point in the history
  • Loading branch information
samsondav committed Jul 1, 2024
1 parent 10f8eba commit 8627bc5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mercury/v3/mercury.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ func (rp *reportingPlugin) buildReportFields(previousReport types.Report, paos [
func (rp *reportingPlugin) validateReport(rf v3.ReportFields) error {
return errors.Join(
mercury.ValidateBetween("median benchmark price", rf.BenchmarkPrice, rp.onchainConfig.Min, rp.onchainConfig.Max),
mercury.ValidateBetween("median bid invariant", rf.Bid, rp.onchainConfig.Min, rf.BenchmarkPrice),
mercury.ValidateBetween("median ask invariant", rf.Ask, rf.BenchmarkPrice, rp.onchainConfig.Max),
mercury.ValidateBetween("median bid", rf.Bid, rp.onchainConfig.Min, rp.onchainConfig.Max),
mercury.ValidateBetween("median ask", rf.Ask, rp.onchainConfig.Min, rp.onchainConfig.Max),
mercury.ValidateFee("median link fee", rf.LinkFee),
Expand Down
14 changes: 13 additions & 1 deletion mercury/v3/mercury_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,23 @@ func Test_Plugin_validateReport(t *testing.T) {
assert.Contains(t, err.Error(), "median benchmark price (Value: 150000) is outside of allowable range (Min: 1, Max: 1000)")
assert.Contains(t, err.Error(), "median bid (Value: 150000) is outside of allowable range (Min: 1, Max: 1000)")
assert.Contains(t, err.Error(), "median ask (Value: 150000) is outside of allowable range (Min: 1, Max: 1000)")
assert.Contains(t, err.Error(), "median link fee (Value: -1) is outside of allowable range (Min: 0, Max: 3138550867693340381917894711603833208051177722232017256447)")
assert.Contains(t, err.Error(), "median native fee (Value: -1) is outside of allowable range (Min: 0, Max: 3138550867693340381917894711603833208051177722232017256447)")
assert.Contains(t, err.Error(), "observationTimestamp (Value: 43) must be >= validFromTimestamp (Value: 44)")
assert.Contains(t, err.Error(), "median link fee (Value: -1) is outside of allowable range (Min: 0, Max: 3138550867693340381917894711603833208051177722232017256447)")
assert.Contains(t, err.Error(), "expiresAt (Value: 42) must be ahead of observation timestamp (Value: 43)")
})
t.Run("bid/ask invariant violation", func(t *testing.T) {
rf := v3.ReportFields{
BenchmarkPrice: big.NewInt(500),
Bid: big.NewInt(501),
Ask: big.NewInt(499),
}
err := rp.validateReport(rf)
require.Error(t, err)

assert.Contains(t, err.Error(), "median bid invariant (Value: 501) is outside of allowable range (Min: 1, Max: 500)")
assert.Contains(t, err.Error(), "median ask invariant (Value: 499) is outside of allowable range (Min: 500, Max: 1000)")
})

t.Run("zero values", func(t *testing.T) {
rf := v3.ReportFields{}
Expand Down

0 comments on commit 8627bc5

Please sign in to comment.