Skip to content
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

Adding a parameter to limit variation to the median price #1490

Closed
wants to merge 26 commits into from

Conversation

mrsmkl
Copy link
Contributor

@mrsmkl mrsmkl commented Oct 25, 2019

Description

Added a parameter to limit variation to the reported prices. There is now a new variable where the current "median" is stored, and it is changed each time a new report is made.

Alternative approaches I can think of:

  • setting a window for possible median values each day / time period
  • preventing oracles from changing their individual values too much
  • change the value linearly when reading

Tested

Added tests.

Other changes

Related issues

Backwards compatibility

@asaj asaj assigned mrsmkl and unassigned asaj Nov 6, 2019
@codecov
Copy link

codecov bot commented Nov 7, 2019

Codecov Report

Merging #1490 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff            @@
##           master    #1490    +/-   ##
========================================
  Coverage   74.55%   74.55%            
========================================
  Files         278      278            
  Lines        7777     7777            
  Branches      707      991   +284     
========================================
  Hits         5798     5798            
  Misses       1868     1868            
  Partials      111      111
Flag Coverage Δ
#mobile 74.55% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 987c198...25533c9. Read the comment docs.

@mrsmkl mrsmkl assigned asaj and mrsmkl and unassigned mrsmkl and asaj Nov 7, 2019
@mrsmkl mrsmkl assigned asaj and unassigned mrsmkl Nov 8, 2019
packages/protocol/contracts/common/UsingPrecompiles.sol Outdated Show resolved Hide resolved
recomputeRate(token);
}

function fracMulExp(FixidityLib.Fraction memory a, FixidityLib.Fraction memory b, uint256 exp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps include in the fixidity lib?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixidity lib cannot inherit UsingPrecompiles...

packages/protocol/contracts/stability/SortedOracles.sol Outdated Show resolved Hide resolved
packages/protocol/test/stability/sortedoracles.ts Outdated Show resolved Hide resolved
uint256 maxChange = fracMulExp(
FixidityLib.fixed1(),
maxMedianChangeRatePerSecond.add(FixidityLib.fixed1()),
td
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't feel safe to do as the exponents could grow very large...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So need to add something to make sure it won't overflow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some checks, now it shouldn't be able to overflow.

@asaj asaj assigned mrsmkl and unassigned asaj Nov 8, 2019
@mrsmkl mrsmkl assigned asaj and unassigned mrsmkl Nov 11, 2019
@mrsmkl mrsmkl changed the title [wip] Adding a parameter to limit variation to the median price Adding a parameter to limit variation to the median price Dec 19, 2019
Copy link
Contributor

@mcortesi mcortesi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me, that the intent of the PR is to limit the variation of the median price, but in particular for short time periods.

For "long" time periods, it probably doesn't matter, nor we can anticipate those values. So, why limit it?

And if you check this for short time periods, we eliminate the whole overflow/checks issue

What do you think? @asaj @mrsmkl @rcroessmann

@mrsmkl
Copy link
Contributor Author

mrsmkl commented Dec 27, 2019

I think that's a good idea, changed the behavior to that.

@mcortesi
Copy link
Contributor

Still thinking about this...
I'm not convinced that this whole pr is a good a idea...

we are defining an artificial limit to the exchange rate variation; but since that limit doesn't exist outside the system (any centralized exchange); we create the opportunity to game the system and profit from arbitrage in a non productive way for us.

In particular, taking the Max possible rate 0.000005 here are the possible variations:

Period Max Variation
10 seconds 0,00500%
30 seconds 0,02%
1 minute 0,03%
1 hour 1,82%
1 day 54,03%
15 day 65096,04%
30 day 42505134,29%
1 year 3,02E+68

Here we can see that if we use this max

  1. it doesn't make sense the check the limit after 15 days
  2. Stating that in can only change 1.84% in hour is probably unreal, specially in crypto.

@mrsmkl
Copy link
Contributor Author

mrsmkl commented Dec 27, 2019

If we change the max period from year to for example one day, then we get a bit different results. Then, if max is 0.0005, then the max change in hour would be 600%
Still need to think about if there is any advantage over just having some period of time before an oracle can post a new report. Or perhaps a period until the actual median is updated after a report.

@mcortesi
Copy link
Contributor

Yeah, the more i see the SortedOracles design, the more questions/doubts i have...

Basically, when asking for the medianRate (which imo is the most important method); it will get median from all rates, it won't check if they are expired.

Expiring only happens when somebody calls the removeExpired method.

Then i wonder what's the "mechanics" for it...

If we have a long/medium expiration time, for example 1 hour.. we'll be obtaining a rate considering information from last minute and last 59 minute, without making any distinction. Both will be equally interesting to us; when arguably something that was informed an hour ago should not be as relevant as information from a minute ago.

That means that it should be short... for example 2 minutes; and we need to call removeExpired constantly.

Then there's the other problem... we have "hundreds" of oracles reporting in a 2 minute window... which thinking in terms of block tx space not sure if it's ideal. Maybe not hundreds then ;)

But "tens of oracles" reporting... and you don't want the exchange rate to be change everytime a new report arrives... but changes happen due to a median computation, which it we assume a majority honest and up-to-date reports, it should be quite stable...

so, i'm a bit confused about what are we looking here. But it is important, since this controls the usd/gold exchange rate which is used in several parts, like rewards for validators...

@asaj asaj removed the audit label Feb 24, 2020
@asaj asaj removed their assignment Feb 26, 2020
@cmcewen
Copy link
Contributor

cmcewen commented May 1, 2020

closing after checking with sami

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reports SNBAT move the oracle price too quickly
8 participants