-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Schutz's algorithm for calculating bid/ask spreads #62
Closed
samsondav
wants to merge
4
commits into
master
from
MERC-5381-mercury-should-not-be-able-to-violate-bid-mid-ask-2
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package v3 | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
|
||
"github.com/shopspring/decimal" | ||
) | ||
|
||
func GetConsensusPrices(paos []PAO, f int) (Prices, error) { | ||
var validPrices []Prices | ||
for _, pao := range paos { | ||
prices, valid := pao.GetPrices() | ||
if valid { | ||
validPrices = append(validPrices, prices) | ||
} | ||
} | ||
// FIXME: This should actually check for < 2*f+1, but we can't do that in | ||
// this mercury plugin because it doesn't support ValidateObservation | ||
if len(validPrices) < f+1 { | ||
return Prices{}, fmt.Errorf("fewer than f+1 observations have a valid price (got: %d/%d)", len(validPrices), len(paos)) | ||
} | ||
|
||
bidSpreads := make([]decimal.Decimal, len(validPrices)) | ||
askSpreads := make([]decimal.Decimal, len(validPrices)) | ||
for i, p := range validPrices { | ||
bid := decimal.NewFromBigInt(p.Bid, 0) | ||
ask := decimal.NewFromBigInt(p.Ask, 0) | ||
benchmark := decimal.NewFromBigInt(p.Benchmark, 0) | ||
|
||
bidSpreads[i] = bid.Div(benchmark) | ||
askSpreads[i] = ask.Div(benchmark) | ||
} | ||
|
||
prices := Prices{} | ||
|
||
sort.Slice(validPrices, func(i, j int) bool { | ||
return validPrices[i].Benchmark.Cmp(validPrices[j].Benchmark) < 0 | ||
}) | ||
prices.Benchmark = validPrices[len(validPrices)/2].Benchmark | ||
benchmarkDecimal := decimal.NewFromBigInt(prices.Benchmark, 0) | ||
|
||
sort.Slice(bidSpreads, func(i, j int) bool { | ||
return bidSpreads[i].Cmp(bidSpreads[j]) < 0 | ||
}) | ||
// We started with at least 2f+1 observations. There are at most f | ||
// dishonest participants. Suppose with threw out m observations for | ||
// disordered prices. Then we are left with 2f+1-m observations, f-m of | ||
// which could still have come from dishonest participants. But | ||
// 2f+1-m=2(f-m)+(m+1), so the median must come from an honest participant. | ||
medianBidSpread := bidSpreads[len(bidSpreads)/2] | ||
|
||
prices.Bid = benchmarkDecimal.Mul(medianBidSpread).BigInt() | ||
samsondav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if prices.Bid.Cmp(prices.Benchmark) > 0 { | ||
// Cannot happen unless > f nodes are inverted which is by assumption undefined behavior | ||
return Prices{}, fmt.Errorf("invariant violation: bid price is greater than benchmark price (bid: %s, benchmark: %s)", prices.Bid.String(), benchmarkDecimal.String()) | ||
} | ||
|
||
sort.Slice(askSpreads, func(i, j int) bool { | ||
return askSpreads[i].Cmp(askSpreads[j]) < 0 | ||
}) | ||
medianAskSpread := askSpreads[len(askSpreads)/2] | ||
prices.Ask = benchmarkDecimal.Mul(medianAskSpread).BigInt() | ||
samsondav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if prices.Ask.Cmp(prices.Benchmark) < 0 { | ||
// Cannot happen unless > f nodes are inverted which is by assumption undefined behavior | ||
return Prices{}, fmt.Errorf("invariant violation: ask price is less than benchmark price (ask: %s, benchmark: %s)", prices.Ask.String(), benchmarkDecimal.String()) | ||
} | ||
|
||
return prices, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
package v3 | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/smartcontractkit/libocr/commontypes" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var _ PAO = testPAO{} | ||
|
||
type testPAO struct { | ||
Bid *big.Int | ||
Benchmark *big.Int | ||
Ask *big.Int | ||
PricesValid bool | ||
} | ||
|
||
func (t testPAO) GetPrices() (prices Prices, valid bool) { | ||
return Prices{ | ||
Benchmark: t.Benchmark, | ||
Bid: t.Bid, | ||
Ask: t.Ask, | ||
}, t.PricesValid | ||
} | ||
|
||
func (t testPAO) GetObserver() commontypes.OracleID { return 0 } | ||
func (t testPAO) GetTimestamp() uint32 { return 0 } | ||
func (t testPAO) GetBenchmarkPrice() (*big.Int, bool) { return nil, false } | ||
func (t testPAO) GetMaxFinalizedTimestamp() (int64, bool) { return 0, false } | ||
func (t testPAO) GetLinkFee() (*big.Int, bool) { return nil, false } | ||
func (t testPAO) GetNativeFee() (*big.Int, bool) { return nil, false } | ||
|
||
func Test_GetConsensusPrices(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
paos []PAO | ||
f int | ||
want Prices | ||
err string | ||
}{ | ||
{ | ||
name: "not enough valid observations", | ||
paos: []PAO{ | ||
testPAO{ | ||
PricesValid: false, | ||
}, | ||
testPAO{ | ||
PricesValid: false, | ||
}, | ||
testPAO{ | ||
Bid: big.NewInt(20), | ||
Benchmark: big.NewInt(21), | ||
Ask: big.NewInt(23), | ||
PricesValid: true, | ||
}, | ||
}, | ||
f: 1, | ||
want: Prices{}, | ||
err: "fewer than f+1 observations have a valid price (got: 1/3)", | ||
}, | ||
{ | ||
name: "handles simple case", | ||
paos: []PAO{ | ||
testPAO{ | ||
Bid: big.NewInt(1), | ||
Benchmark: big.NewInt(2), | ||
Ask: big.NewInt(3), | ||
PricesValid: true, | ||
}, | ||
testPAO{ | ||
Bid: big.NewInt(9), | ||
Benchmark: big.NewInt(9), | ||
Ask: big.NewInt(9), | ||
PricesValid: true, | ||
}, | ||
testPAO{ | ||
Bid: big.NewInt(20), | ||
Benchmark: big.NewInt(21), | ||
Ask: big.NewInt(23), | ||
PricesValid: true, | ||
}, | ||
}, | ||
f: 1, | ||
want: Prices{ | ||
Bid: big.NewInt(8), | ||
Benchmark: big.NewInt(9), | ||
Ask: big.NewInt(9), | ||
}, | ||
err: "", | ||
}, | ||
{ | ||
name: "handles simple inverted case", | ||
paos: []PAO{ | ||
testPAO{ | ||
Bid: big.NewInt(1), | ||
Benchmark: big.NewInt(2), | ||
Ask: big.NewInt(3), | ||
PricesValid: true, | ||
}, | ||
testPAO{ | ||
Bid: big.NewInt(10), | ||
Benchmark: big.NewInt(9), | ||
Ask: big.NewInt(8), | ||
PricesValid: true, | ||
}, | ||
testPAO{ | ||
Bid: big.NewInt(20), | ||
Benchmark: big.NewInt(21), | ||
Ask: big.NewInt(23), | ||
PricesValid: true, | ||
}, | ||
}, | ||
f: 1, | ||
want: Prices{ | ||
Bid: big.NewInt(8), | ||
Benchmark: big.NewInt(9), | ||
Ask: big.NewInt(9), | ||
}, | ||
err: "", | ||
}, | ||
{ | ||
name: "handles complex inverted case (real world example)", | ||
paos: []PAO{ | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1314633333), big.NewInt(1315131262), big.NewInt(1315629191), true}, | ||
testPAO{big.NewInt(1315243916), big.NewInt(1316190800), big.NewInt(1316078096), true}, // inverted, ask > benchmark | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1314633333), big.NewInt(1315131262), big.NewInt(1315629191), true}, | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1314633333), big.NewInt(1315131262), big.NewInt(1315629191), true}, | ||
testPAO{big.NewInt(1315243916), big.NewInt(1315661031), big.NewInt(1316078096), true}, | ||
testPAO{big.NewInt(1314633333), big.NewInt(1315131262), big.NewInt(1315629191), true}, | ||
}, | ||
f: 5, | ||
want: Prices{Bid: big.NewInt(1315773517), Benchmark: big.NewInt(1316190800), Ask: big.NewInt(1316526999)}, | ||
err: "", | ||
}, | ||
{ | ||
name: "handles complex inverted case (f failures of various types)", | ||
paos: []PAO{ | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1315131262), big.NewInt(1314633333), big.NewInt(1315629191), true}, // inverted, bid > benchmark | ||
testPAO{big.NewInt(1315243916), big.NewInt(1316190800), big.NewInt(1316078096), true}, // inverted, ask < benchmark | ||
testPAO{big.NewInt(2000000000), big.NewInt(1316190800), big.NewInt(1316527000), true}, // inverted, bid >>>> ask | ||
testPAO{big.NewInt(2000000000), big.NewInt(1315131262), big.NewInt(2001000000), true}, // inverted, bid >>>> benchmark | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1000000000), true}, // inverted, ask <<<< benchmark | ||
testPAO{big.NewInt(1314633333), big.NewInt(1315131262), big.NewInt(1315629191), true}, | ||
testPAO{big.NewInt(1315243916), big.NewInt(1315661031), big.NewInt(1316078096), true}, | ||
testPAO{big.NewInt(1314633333), big.NewInt(1315131262), big.NewInt(1315629191), true}, | ||
}, | ||
f: 5, | ||
want: Prices{Bid: big.NewInt(1315854499), Benchmark: big.NewInt(1316190800), Ask: big.NewInt(1316526999)}, | ||
err: "", | ||
}, | ||
{ | ||
name: "handles complex inverted case (f failures skewed the same way)", | ||
paos: []PAO{ | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1314633333), big.NewInt(1315131262), big.NewInt(1315629191), true}, | ||
testPAO{big.NewInt(1315243916), big.NewInt(1316190800), big.NewInt(1316078096), true}, // inverted, ask < benchmark | ||
testPAO{big.NewInt(1315243916), big.NewInt(1316190800), big.NewInt(1316078096), true}, // inverted, ask < benchmark | ||
testPAO{big.NewInt(1315243916), big.NewInt(1316190800), big.NewInt(1316078096), true}, // inverted, ask < benchmark | ||
testPAO{big.NewInt(1315243916), big.NewInt(1316190800), big.NewInt(1316078096), true}, // inverted, ask < benchmark | ||
testPAO{big.NewInt(1315243916), big.NewInt(1316190800), big.NewInt(1316078096), true}, // inverted, ask < benchmark | ||
testPAO{big.NewInt(1315243916), big.NewInt(1315661031), big.NewInt(1316078096), true}, | ||
testPAO{big.NewInt(1314633333), big.NewInt(1315131262), big.NewInt(1315629191), true}, | ||
}, | ||
f: 5, | ||
want: Prices{Bid: big.NewInt(1315692469), Benchmark: big.NewInt(1316190800), Ask: big.NewInt(1316526999)}, | ||
err: "", | ||
}, | ||
{ | ||
name: "errors output in complex inverted case with f+1 failures such that ask > benchmark", | ||
paos: []PAO{ | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1314633333), big.NewInt(1315131262), big.NewInt(1315629191), true}, | ||
testPAO{big.NewInt(1315243916), big.NewInt(1316190800), big.NewInt(1316078096), true}, // inverted, ask < benchmark | ||
testPAO{big.NewInt(1315243916), big.NewInt(1316190800), big.NewInt(1316078096), true}, // inverted, ask < benchmark | ||
testPAO{big.NewInt(1315243916), big.NewInt(1316190800), big.NewInt(1316078096), true}, // inverted, ask < benchmark | ||
testPAO{big.NewInt(1315243916), big.NewInt(1316190800), big.NewInt(1316078096), true}, // inverted, ask < benchmark | ||
testPAO{big.NewInt(1315243916), big.NewInt(1316190800), big.NewInt(1316078096), true}, // inverted, ask < benchmark | ||
testPAO{big.NewInt(1315243916), big.NewInt(1316190800), big.NewInt(1316078096), true}, // inverted, ask < benchmark | ||
testPAO{big.NewInt(1314633333), big.NewInt(1315131262), big.NewInt(1315629191), true}, | ||
}, | ||
f: 5, | ||
want: Prices{}, | ||
err: "invariant violation: ask price is less than benchmark price (ask: 1316078096, benchmark: 1316190800)", | ||
}, | ||
{ | ||
name: "errors output in complex inverted case with f+1 failures such that bid < benchmark", | ||
paos: []PAO{ | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1315854500), big.NewInt(1316190800), big.NewInt(1316527000), true}, | ||
testPAO{big.NewInt(1314633333), big.NewInt(1315131262), big.NewInt(1315629191), true}, | ||
testPAO{big.NewInt(1314633333), big.NewInt(1315131262), big.NewInt(1315629191), true}, | ||
testPAO{big.NewInt(1315131263), big.NewInt(1315131262), big.NewInt(1315629191), true}, // inverted, bid > benchmark | ||
testPAO{big.NewInt(1315131263), big.NewInt(1315131262), big.NewInt(1315629191), true}, // inverted, bid > benchmark | ||
testPAO{big.NewInt(1315131263), big.NewInt(1315131262), big.NewInt(1315629191), true}, // inverted, bid > benchmark | ||
testPAO{big.NewInt(1315131263), big.NewInt(1315131262), big.NewInt(1315629191), true}, // inverted, bid > benchmark | ||
testPAO{big.NewInt(1315131263), big.NewInt(1315131262), big.NewInt(1315629191), true}, // inverted, bid > benchmark | ||
testPAO{big.NewInt(1315131263), big.NewInt(1315131262), big.NewInt(1315629191), true}, // inverted, bid > benchmark | ||
}, | ||
f: 5, | ||
want: Prices{}, | ||
err: "invariant violation: bid price is greater than benchmark price (bid: 1315131263, benchmark: 1315131262)", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
prices, err := GetConsensusPrices(tt.paos, tt.f) | ||
if tt.err != "" { | ||
require.Error(t, err) | ||
assert.Equal(t, tt.err, err.Error()) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
assert.Equal(t, tt.want, prices) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend adding a comment here, justifying the use of the median. Something along the lines of