neutron-org / neutron

Smart Contract platform secured by Cosmos Hub
https://neutron.org
Apache License 2.0
111 stars 94 forks source link

DUAL-10 Fix: store tick prices and lookup instead of doing log calculation #547

Closed jcompagni10 closed 4 months ago

jcompagni10 commented 4 months ago

Previously we used float64 log to calculate a tick from price. There is no strong guarantee that float64 math will perform the same on all issues and poses a small risk of being consensus-breaking. To fix this we switch to using a pre-calculated array of prices which we binary search to find the correct tick for a given price. This is also slightly more efficient and gives us the option to stop doing all price calculations in the future.

Benchmarks: BenchmarkCalcTickIndexFromPriceV2 301816 5249 ns/op BenchmarkCalcTickIndexFromPrice 206276 6662 ns/op

The price data structure is loaded into memory from a .gob file at startup. The full data structure is calculated to use about 8mb of space, so should have a negligible impact on overall performance

jcompagni10 commented 4 months ago

https://github.com/neutron-org/neutron-tests/actions/runs/9333093601

jcompagni10 commented 4 months ago

https://github.com/neutron-org/neutron-tests/actions/runs/9333093601