diff --git a/contracts/ABDKMath64x64.sol b/contracts/ABDKMath64x64.sol new file mode 100644 index 0000000..2cbe46a --- /dev/null +++ b/contracts/ABDKMath64x64.sol @@ -0,0 +1,750 @@ +// SPDX-License-Identifier: BSD-4-Clause +/* + * ABDK Math 64.64 Smart Contract Library. Copyright © 2019 by ABDK Consulting. + * Author: Mikhail Vladimirov + */ +pragma solidity ^0.8.0; + +/** + * Smart contract library of mathematical functions operating with signed + * 64.64-bit fixed point numbers. Signed 64.64-bit fixed point number is + * basically a simple fraction whose numerator is signed 128-bit integer and + * denominator is 2^64. As long as denominator is always the same, there is no + * need to store it, thus in Solidity signed 64.64-bit fixed point numbers are + * represented by int128 type holding only the numerator. + */ +library ABDKMath64x64 { + /* + * Minimum value signed 64.64-bit fixed point number may have. + */ + int128 private constant MIN_64x64 = -0x80000000000000000000000000000000; + + /* + * Maximum value signed 64.64-bit fixed point number may have. + */ + int128 private constant MAX_64x64 = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; + + /** + * Convert signed 256-bit integer number into signed 64.64-bit fixed point + * number. Revert on overflow. + * + * @param x signed 256-bit integer number + * @return signed 64.64-bit fixed point number + */ + function fromInt (int256 x) internal pure returns (int128) { + unchecked { + require (x >= -0x8000000000000000 && x <= 0x7FFFFFFFFFFFFFFF); + return int128 (x << 64); + } + } + + /** + * Convert signed 64.64 fixed point number into signed 64-bit integer number + * rounding down. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64-bit integer number + */ + function toInt (int128 x) internal pure returns (int64) { + unchecked { + return int64 (x >> 64); + } + } + + /** + * Convert unsigned 256-bit integer number into signed 64.64-bit fixed point + * number. Revert on overflow. + * + * @param x unsigned 256-bit integer number + * @return signed 64.64-bit fixed point number + */ + function fromUInt (uint256 x) internal pure returns (int128) { + unchecked { + require (x <= 0x7FFFFFFFFFFFFFFF); + return int128 (int256 (x << 64)); + } + } + + /** + * Convert signed 64.64 fixed point number into unsigned 64-bit integer + * number rounding down. Revert on underflow. + * + * @param x signed 64.64-bit fixed point number + * @return unsigned 64-bit integer number + */ + function toUInt (int128 x) internal pure returns (uint64) { + unchecked { + require (x >= 0); + return uint64 (uint128 (x >> 64)); + } + } + + /** + * Convert signed 128.128 fixed point number into signed 64.64-bit fixed point + * number rounding down. Revert on overflow. + * + * @param x signed 128.128-bin fixed point number + * @return signed 64.64-bit fixed point number + */ + function from128x128 (int256 x) internal pure returns (int128) { + unchecked { + int256 result = x >> 64; + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Convert signed 64.64 fixed point number into signed 128.128 fixed point + * number. + * + * @param x signed 64.64-bit fixed point number + * @return signed 128.128 fixed point number + */ + function to128x128 (int128 x) internal pure returns (int256) { + unchecked { + return int256 (x) << 64; + } + } + + /** + * Calculate x + y. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @param y signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function add (int128 x, int128 y) internal pure returns (int128) { + unchecked { + int256 result = int256(x) + y; + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Calculate x - y. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @param y signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function sub (int128 x, int128 y) internal pure returns (int128) { + unchecked { + int256 result = int256(x) - y; + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Calculate x * y rounding down. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @param y signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function mul (int128 x, int128 y) internal pure returns (int128) { + unchecked { + int256 result = int256(x) * y >> 64; + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Calculate x * y rounding towards zero, where x is signed 64.64 fixed point + * number and y is signed 256-bit integer number. Revert on overflow. + * + * @param x signed 64.64 fixed point number + * @param y signed 256-bit integer number + * @return signed 256-bit integer number + */ + function muli (int128 x, int256 y) internal pure returns (int256) { + unchecked { + if (x == MIN_64x64) { + require (y >= -0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF && + y <= 0x1000000000000000000000000000000000000000000000000); + return -y << 63; + } else { + bool negativeResult = false; + if (x < 0) { + x = -x; + negativeResult = true; + } + if (y < 0) { + y = -y; // We rely on overflow behavior here + negativeResult = !negativeResult; + } + uint256 absoluteResult = mulu (x, uint256 (y)); + if (negativeResult) { + require (absoluteResult <= + 0x8000000000000000000000000000000000000000000000000000000000000000); + return -int256 (absoluteResult); // We rely on overflow behavior here + } else { + require (absoluteResult <= + 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + return int256 (absoluteResult); + } + } + } + } + + /** + * Calculate x * y rounding down, where x is signed 64.64 fixed point number + * and y is unsigned 256-bit integer number. Revert on overflow. + * + * @param x signed 64.64 fixed point number + * @param y unsigned 256-bit integer number + * @return unsigned 256-bit integer number + */ + function mulu (int128 x, uint256 y) internal pure returns (uint256) { + unchecked { + if (y == 0) return 0; + + require (x >= 0); + + uint256 lo = (uint256 (int256 (x)) * (y & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)) >> 64; + uint256 hi = uint256 (int256 (x)) * (y >> 128); + + require (hi <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + hi <<= 64; + + require (hi <= + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF - lo); + return hi + lo; + } + } + + /** + * Calculate x / y rounding towards zero. Revert on overflow or when y is + * zero. + * + * @param x signed 64.64-bit fixed point number + * @param y signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function div (int128 x, int128 y) internal pure returns (int128) { + unchecked { + require (y != 0); + int256 result = (int256 (x) << 64) / y; + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Calculate x / y rounding towards zero, where x and y are signed 256-bit + * integer numbers. Revert on overflow or when y is zero. + * + * @param x signed 256-bit integer number + * @param y signed 256-bit integer number + * @return signed 64.64-bit fixed point number + */ + function divi (int256 x, int256 y) internal pure returns (int128) { + unchecked { + require (y != 0); + + bool negativeResult = false; + if (x < 0) { + x = -x; // We rely on overflow behavior here + negativeResult = true; + } + if (y < 0) { + y = -y; // We rely on overflow behavior here + negativeResult = !negativeResult; + } + uint128 absoluteResult = divuu (uint256 (x), uint256 (y)); + if (negativeResult) { + require (absoluteResult <= 0x80000000000000000000000000000000); + return -int128 (absoluteResult); // We rely on overflow behavior here + } else { + require (absoluteResult <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + return int128 (absoluteResult); // We rely on overflow behavior here + } + } + } + + /** + * Calculate x / y rounding towards zero, where x and y are unsigned 256-bit + * integer numbers. Revert on overflow or when y is zero. + * + * @param x unsigned 256-bit integer number + * @param y unsigned 256-bit integer number + * @return signed 64.64-bit fixed point number + */ + function divu (uint256 x, uint256 y) internal pure returns (int128) { + unchecked { + require (y != 0); + uint128 result = divuu (x, y); + require (result <= uint128 (MAX_64x64)); + return int128 (result); + } + } + + /** + * Calculate -x. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function neg (int128 x) internal pure returns (int128) { + unchecked { + require (x != MIN_64x64); + return -x; + } + } + + /** + * Calculate |x|. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function abs (int128 x) internal pure returns (int128) { + unchecked { + require (x != MIN_64x64); + return x < 0 ? -x : x; + } + } + + /** + * Calculate 1 / x rounding towards zero. Revert on overflow or when x is + * zero. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function inv (int128 x) internal pure returns (int128) { + unchecked { + require (x != 0); + int256 result = int256 (0x100000000000000000000000000000000) / x; + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Calculate arithmetics average of x and y, i.e. (x + y) / 2 rounding down. + * + * @param x signed 64.64-bit fixed point number + * @param y signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function avg (int128 x, int128 y) internal pure returns (int128) { + unchecked { + return int128 ((int256 (x) + int256 (y)) >> 1); + } + } + + /** + * Calculate geometric average of x and y, i.e. sqrt (x * y) rounding down. + * Revert on overflow or in case x * y is negative. + * + * @param x signed 64.64-bit fixed point number + * @param y signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function gavg (int128 x, int128 y) internal pure returns (int128) { + unchecked { + int256 m = int256 (x) * int256 (y); + require (m >= 0); + require (m < + 0x4000000000000000000000000000000000000000000000000000000000000000); + return int128 (sqrtu (uint256 (m))); + } + } + + /** + * Calculate x^y assuming 0^0 is 1, where x is signed 64.64 fixed point number + * and y is unsigned 256-bit integer number. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @param y uint256 value + * @return signed 64.64-bit fixed point number + */ + function pow (int128 x, uint256 y) internal pure returns (int128) { + unchecked { + bool negative = x < 0 && y & 1 == 1; + + uint256 absX = uint128 (x < 0 ? -x : x); + uint256 absResult; + absResult = 0x100000000000000000000000000000000; + + if (absX <= 0x10000000000000000) { + absX <<= 63; + while (y != 0) { + if (y & 0x1 != 0) { + absResult = absResult * absX >> 127; + } + absX = absX * absX >> 127; + + if (y & 0x2 != 0) { + absResult = absResult * absX >> 127; + } + absX = absX * absX >> 127; + + if (y & 0x4 != 0) { + absResult = absResult * absX >> 127; + } + absX = absX * absX >> 127; + + if (y & 0x8 != 0) { + absResult = absResult * absX >> 127; + } + absX = absX * absX >> 127; + + y >>= 4; + } + + absResult >>= 64; + } else { + uint256 absXShift = 63; + if (absX < 0x1000000000000000000000000) { absX <<= 32; absXShift -= 32; } + if (absX < 0x10000000000000000000000000000) { absX <<= 16; absXShift -= 16; } + if (absX < 0x1000000000000000000000000000000) { absX <<= 8; absXShift -= 8; } + if (absX < 0x10000000000000000000000000000000) { absX <<= 4; absXShift -= 4; } + if (absX < 0x40000000000000000000000000000000) { absX <<= 2; absXShift -= 2; } + if (absX < 0x80000000000000000000000000000000) { absX <<= 1; absXShift -= 1; } + + uint256 resultShift = 0; + while (y != 0) { + require (absXShift < 64); + + if (y & 0x1 != 0) { + absResult = absResult * absX >> 127; + resultShift += absXShift; + if (absResult > 0x100000000000000000000000000000000) { + absResult >>= 1; + resultShift += 1; + } + } + absX = absX * absX >> 127; + absXShift <<= 1; + if (absX >= 0x100000000000000000000000000000000) { + absX >>= 1; + absXShift += 1; + } + + y >>= 1; + } + + require (resultShift < 64); + absResult >>= 64 - resultShift; + } + int256 result = negative ? -int256 (absResult) : int256 (absResult); + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Calculate sqrt (x) rounding down. Revert if x < 0. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function sqrt (int128 x) internal pure returns (int128) { + unchecked { + require (x >= 0); + return int128 (sqrtu (uint256 (int256 (x)) << 64)); + } + } + + /** + * Calculate binary logarithm of x. Revert if x <= 0. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function log_2 (int128 x) internal pure returns (int128) { + unchecked { + require (x > 0); + + int256 msb = 0; + int256 xc = x; + if (xc >= 0x10000000000000000) { xc >>= 64; msb += 64; } + if (xc >= 0x100000000) { xc >>= 32; msb += 32; } + if (xc >= 0x10000) { xc >>= 16; msb += 16; } + if (xc >= 0x100) { xc >>= 8; msb += 8; } + if (xc >= 0x10) { xc >>= 4; msb += 4; } + if (xc >= 0x4) { xc >>= 2; msb += 2; } + if (xc >= 0x2) msb += 1; // No need to shift xc anymore + + int256 result = msb - 64 << 64; + uint256 ux = uint256 (int256 (x)) << uint256 (127 - msb); + for (int256 bit = 0x8000000000000000; bit > 0; bit >>= 1) { + ux *= ux; + uint256 b = ux >> 255; + ux >>= 127 + b; + result += bit * int256 (b); + } + + return int128 (result); + } + } + + /** + * Calculate natural logarithm of x. Revert if x <= 0. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function ln (int128 x) internal pure returns (int128) { + unchecked { + require (x > 0); + + return int128 (int256 ( + uint256 (int256 (log_2 (x))) * 0xB17217F7D1CF79ABC9E3B39803F2F6AF >> 128)); + } + } + + /** + * Calculate binary exponent of x. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function exp_2 (int128 x) internal pure returns (int128) { + unchecked { + require (x < 0x400000000000000000); // Overflow + + if (x < -0x400000000000000000) return 0; // Underflow + + uint256 result = 0x80000000000000000000000000000000; + + if (x & 0x8000000000000000 > 0) + result = result * 0x16A09E667F3BCC908B2FB1366EA957D3E >> 128; + if (x & 0x4000000000000000 > 0) + result = result * 0x1306FE0A31B7152DE8D5A46305C85EDEC >> 128; + if (x & 0x2000000000000000 > 0) + result = result * 0x1172B83C7D517ADCDF7C8C50EB14A791F >> 128; + if (x & 0x1000000000000000 > 0) + result = result * 0x10B5586CF9890F6298B92B71842A98363 >> 128; + if (x & 0x800000000000000 > 0) + result = result * 0x1059B0D31585743AE7C548EB68CA417FD >> 128; + if (x & 0x400000000000000 > 0) + result = result * 0x102C9A3E778060EE6F7CACA4F7A29BDE8 >> 128; + if (x & 0x200000000000000 > 0) + result = result * 0x10163DA9FB33356D84A66AE336DCDFA3F >> 128; + if (x & 0x100000000000000 > 0) + result = result * 0x100B1AFA5ABCBED6129AB13EC11DC9543 >> 128; + if (x & 0x80000000000000 > 0) + result = result * 0x10058C86DA1C09EA1FF19D294CF2F679B >> 128; + if (x & 0x40000000000000 > 0) + result = result * 0x1002C605E2E8CEC506D21BFC89A23A00F >> 128; + if (x & 0x20000000000000 > 0) + result = result * 0x100162F3904051FA128BCA9C55C31E5DF >> 128; + if (x & 0x10000000000000 > 0) + result = result * 0x1000B175EFFDC76BA38E31671CA939725 >> 128; + if (x & 0x8000000000000 > 0) + result = result * 0x100058BA01FB9F96D6CACD4B180917C3D >> 128; + if (x & 0x4000000000000 > 0) + result = result * 0x10002C5CC37DA9491D0985C348C68E7B3 >> 128; + if (x & 0x2000000000000 > 0) + result = result * 0x1000162E525EE054754457D5995292026 >> 128; + if (x & 0x1000000000000 > 0) + result = result * 0x10000B17255775C040618BF4A4ADE83FC >> 128; + if (x & 0x800000000000 > 0) + result = result * 0x1000058B91B5BC9AE2EED81E9B7D4CFAB >> 128; + if (x & 0x400000000000 > 0) + result = result * 0x100002C5C89D5EC6CA4D7C8ACC017B7C9 >> 128; + if (x & 0x200000000000 > 0) + result = result * 0x10000162E43F4F831060E02D839A9D16D >> 128; + if (x & 0x100000000000 > 0) + result = result * 0x100000B1721BCFC99D9F890EA06911763 >> 128; + if (x & 0x80000000000 > 0) + result = result * 0x10000058B90CF1E6D97F9CA14DBCC1628 >> 128; + if (x & 0x40000000000 > 0) + result = result * 0x1000002C5C863B73F016468F6BAC5CA2B >> 128; + if (x & 0x20000000000 > 0) + result = result * 0x100000162E430E5A18F6119E3C02282A5 >> 128; + if (x & 0x10000000000 > 0) + result = result * 0x1000000B1721835514B86E6D96EFD1BFE >> 128; + if (x & 0x8000000000 > 0) + result = result * 0x100000058B90C0B48C6BE5DF846C5B2EF >> 128; + if (x & 0x4000000000 > 0) + result = result * 0x10000002C5C8601CC6B9E94213C72737A >> 128; + if (x & 0x2000000000 > 0) + result = result * 0x1000000162E42FFF037DF38AA2B219F06 >> 128; + if (x & 0x1000000000 > 0) + result = result * 0x10000000B17217FBA9C739AA5819F44F9 >> 128; + if (x & 0x800000000 > 0) + result = result * 0x1000000058B90BFCDEE5ACD3C1CEDC823 >> 128; + if (x & 0x400000000 > 0) + result = result * 0x100000002C5C85FE31F35A6A30DA1BE50 >> 128; + if (x & 0x200000000 > 0) + result = result * 0x10000000162E42FF0999CE3541B9FFFCF >> 128; + if (x & 0x100000000 > 0) + result = result * 0x100000000B17217F80F4EF5AADDA45554 >> 128; + if (x & 0x80000000 > 0) + result = result * 0x10000000058B90BFBF8479BD5A81B51AD >> 128; + if (x & 0x40000000 > 0) + result = result * 0x1000000002C5C85FDF84BD62AE30A74CC >> 128; + if (x & 0x20000000 > 0) + result = result * 0x100000000162E42FEFB2FED257559BDAA >> 128; + if (x & 0x10000000 > 0) + result = result * 0x1000000000B17217F7D5A7716BBA4A9AE >> 128; + if (x & 0x8000000 > 0) + result = result * 0x100000000058B90BFBE9DDBAC5E109CCE >> 128; + if (x & 0x4000000 > 0) + result = result * 0x10000000002C5C85FDF4B15DE6F17EB0D >> 128; + if (x & 0x2000000 > 0) + result = result * 0x1000000000162E42FEFA494F1478FDE05 >> 128; + if (x & 0x1000000 > 0) + result = result * 0x10000000000B17217F7D20CF927C8E94C >> 128; + if (x & 0x800000 > 0) + result = result * 0x1000000000058B90BFBE8F71CB4E4B33D >> 128; + if (x & 0x400000 > 0) + result = result * 0x100000000002C5C85FDF477B662B26945 >> 128; + if (x & 0x200000 > 0) + result = result * 0x10000000000162E42FEFA3AE53369388C >> 128; + if (x & 0x100000 > 0) + result = result * 0x100000000000B17217F7D1D351A389D40 >> 128; + if (x & 0x80000 > 0) + result = result * 0x10000000000058B90BFBE8E8B2D3D4EDE >> 128; + if (x & 0x40000 > 0) + result = result * 0x1000000000002C5C85FDF4741BEA6E77E >> 128; + if (x & 0x20000 > 0) + result = result * 0x100000000000162E42FEFA39FE95583C2 >> 128; + if (x & 0x10000 > 0) + result = result * 0x1000000000000B17217F7D1CFB72B45E1 >> 128; + if (x & 0x8000 > 0) + result = result * 0x100000000000058B90BFBE8E7CC35C3F0 >> 128; + if (x & 0x4000 > 0) + result = result * 0x10000000000002C5C85FDF473E242EA38 >> 128; + if (x & 0x2000 > 0) + result = result * 0x1000000000000162E42FEFA39F02B772C >> 128; + if (x & 0x1000 > 0) + result = result * 0x10000000000000B17217F7D1CF7D83C1A >> 128; + if (x & 0x800 > 0) + result = result * 0x1000000000000058B90BFBE8E7BDCBE2E >> 128; + if (x & 0x400 > 0) + result = result * 0x100000000000002C5C85FDF473DEA871F >> 128; + if (x & 0x200 > 0) + result = result * 0x10000000000000162E42FEFA39EF44D91 >> 128; + if (x & 0x100 > 0) + result = result * 0x100000000000000B17217F7D1CF79E949 >> 128; + if (x & 0x80 > 0) + result = result * 0x10000000000000058B90BFBE8E7BCE544 >> 128; + if (x & 0x40 > 0) + result = result * 0x1000000000000002C5C85FDF473DE6ECA >> 128; + if (x & 0x20 > 0) + result = result * 0x100000000000000162E42FEFA39EF366F >> 128; + if (x & 0x10 > 0) + result = result * 0x1000000000000000B17217F7D1CF79AFA >> 128; + if (x & 0x8 > 0) + result = result * 0x100000000000000058B90BFBE8E7BCD6D >> 128; + if (x & 0x4 > 0) + result = result * 0x10000000000000002C5C85FDF473DE6B2 >> 128; + if (x & 0x2 > 0) + result = result * 0x1000000000000000162E42FEFA39EF358 >> 128; + if (x & 0x1 > 0) + result = result * 0x10000000000000000B17217F7D1CF79AB >> 128; + + result >>= uint256 (int256 (63 - (x >> 64))); + require (result <= uint256 (int256 (MAX_64x64))); + + return int128 (int256 (result)); + } + } + + /** + * Calculate natural exponent of x. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function exp (int128 x) internal pure returns (int128) { + unchecked { + require (x < 0x400000000000000000); // Overflow + + if (x < -0x400000000000000000) return 0; // Underflow + + return exp_2 ( + int128 (int256 (x) * 0x171547652B82FE1777D0FFDA0D23A7D12 >> 128)); + } + } + + /** + * Calculate x / y rounding towards zero, where x and y are unsigned 256-bit + * integer numbers. Revert on overflow or when y is zero. + * + * @param x unsigned 256-bit integer number + * @param y unsigned 256-bit integer number + * @return unsigned 64.64-bit fixed point number + */ + function divuu (uint256 x, uint256 y) private pure returns (uint128) { + unchecked { + require (y != 0); + + uint256 result; + + if (x <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) + result = (x << 64) / y; + else { + uint256 msb = 192; + uint256 xc = x >> 192; + if (xc >= 0x100000000) { xc >>= 32; msb += 32; } + if (xc >= 0x10000) { xc >>= 16; msb += 16; } + if (xc >= 0x100) { xc >>= 8; msb += 8; } + if (xc >= 0x10) { xc >>= 4; msb += 4; } + if (xc >= 0x4) { xc >>= 2; msb += 2; } + if (xc >= 0x2) msb += 1; // No need to shift xc anymore + + result = (x << 255 - msb) / ((y - 1 >> msb - 191) + 1); + require (result <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + + uint256 hi = result * (y >> 128); + uint256 lo = result * (y & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + + uint256 xh = x >> 192; + uint256 xl = x << 64; + + if (xl < lo) xh -= 1; + xl -= lo; // We rely on overflow behavior here + lo = hi << 128; + if (xl < lo) xh -= 1; + xl -= lo; // We rely on overflow behavior here + + result += xh == hi >> 128 ? xl / y : 1; + } + + require (result <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + return uint128 (result); + } + } + + /** + * Calculate sqrt (x) rounding down, where x is unsigned 256-bit integer + * number. + * + * @param x unsigned 256-bit integer number + * @return unsigned 128-bit integer number + */ + function sqrtu (uint256 x) private pure returns (uint128) { + unchecked { + if (x == 0) return 0; + else { + uint256 xx = x; + uint256 r = 1; + if (xx >= 0x100000000000000000000000000000000) { xx >>= 128; r <<= 64; } + if (xx >= 0x10000000000000000) { xx >>= 64; r <<= 32; } + if (xx >= 0x100000000) { xx >>= 32; r <<= 16; } + if (xx >= 0x10000) { xx >>= 16; r <<= 8; } + if (xx >= 0x100) { xx >>= 8; r <<= 4; } + if (xx >= 0x10) { xx >>= 4; r <<= 2; } + if (xx >= 0x4) { r <<= 1; } + r = (r + x / r) >> 1; + r = (r + x / r) >> 1; + r = (r + x / r) >> 1; + r = (r + x / r) >> 1; + r = (r + x / r) >> 1; + r = (r + x / r) >> 1; + r = (r + x / r) >> 1; // Seven iterations should be enough + uint256 r1 = x / r; + return uint128 (r < r1 ? r : r1); + } + } + } +} diff --git a/contracts/public_data_storage.sol b/contracts/public_data_storage.sol index b95998f..6a7b349 100644 --- a/contracts/public_data_storage.sol +++ b/contracts/public_data_storage.sol @@ -8,15 +8,19 @@ import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; +import "./ABDKMath64x64.sol"; + import "hardhat/console.sol"; using SortedScoreList for SortedScoreList.List; +using ABDKMath64x64 for int128; //Review:这个作为ERC的一部分,要仔细考虑一下 interface IERCPublicDataContract { //return the owner of the data function getDataOwner(bytes32 dataHash) external view returns (address); } + /* interface IERC721VerifyDataHash{ //return token data hash @@ -25,7 +29,6 @@ interface IERC721VerifyDataHash{ */ // Review: 考虑有一些链的出块时间不确定,使用区块间隔要谨慎,可以用区块的时间戳 - // mixedDataHash: 2bit hash algorithm + 62bit data size + 192bit data hash // 2bit hash algorithm: 00: keccak256, 01: sha256 @@ -37,8 +40,15 @@ interface IERC721VerifyDataHash{ * 可能有精度损失? */ -contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable { - enum ShowType { Normal, Immediately } +contract PublicDataStorage is + Initializable, + UUPSUpgradeable, + OwnableUpgradeable +{ + enum ShowType { + Normal, + Immediately + } struct PublicData { address sponsor; @@ -46,11 +56,18 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable uint256 maxDeposit; uint256 dataBalance; uint64 depositRatio; - - mapping(address => uint256) show_records;//miner address - > last show time + mapping(address => uint256) show_records; //miner address - > last show time //uint64 point; } + struct PublicDataForOutput { + address sponsor; + address dataContract; + uint256 maxDeposit; + uint256 dataBalance; + uint64 depositRatio; + } + struct DataProof { uint256 nonceBlockHeight; uint256 proofBlockHeight; @@ -67,7 +84,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable uint256 lastShowBlock; } - GWT public gwtToken;// Gb per Week Token + GWT public gwtToken; // Gb per Week Token address public foundationAddress; mapping(address => SupplierInfo) _supplierInfos; @@ -78,17 +95,16 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable struct CycleDataInfo { address[] lastShowers; - uint64 score;// score = 0表示已经提现过了 + uint64 score; // score = 0表示已经提现过了 uint8 showerIndex; - uint64 showCount;//在这个周期里的总show次数, + uint64 showCount; //在这个周期里的总show次数, } struct CycleInfo { - mapping(bytes32 => CycleDataInfo) dataInfos; - + mapping(bytes32 => CycleDataInfo) dataInfos; SortedScoreList.List scoreList; - uint256 totalAward; // 记录这个cycle的总奖励 - uint256 totalShowPower;//记录这个cycle的总算力 + uint256 totalAward; // 记录这个cycle的总奖励 + uint256 totalShowPower; //记录这个cycle的总算力 } struct CycleOutputInfo { @@ -119,25 +135,50 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable SysConfig public sysConfig; uint256 public totalRewardScore; - event SupplierBalanceChanged(address supplier, uint256 avalibleBalance, uint256 lockedBalance); + event SupplierBalanceChanged( + address supplier, + uint256 avalibleBalance, + uint256 lockedBalance + ); event GWTStacked(address supplier, uint256 amount); event GWTUnstacked(address supplier, uint256 amount); event PublicDataCreated(bytes32 mixedHash); - event DepositData(address depositer, bytes32 mixedHash, uint256 balance, uint256 reward); - event SponsorChanged(bytes32 mixedHash, address oldSponsor, address newSponsor); + event DepositData( + address depositer, + bytes32 mixedHash, + uint256 balance, + uint256 reward + ); + event SponsorChanged( + bytes32 mixedHash, + address oldSponsor, + address newSponsor + ); // event DataScoreUpdated(bytes32 mixedHash, uint256 cycle, uint64 score); event DataPointAdded(bytes32 mixedHash, uint64 point); event SupplierReward(address supplier, bytes32 mixedHash, uint256 amount); event SupplierPunished(address supplier, bytes32 mixedHash, uint256 amount); - event ShowDataProof(address supplier, bytes32 dataMixedHash, uint256 nonce_block); + event ShowDataProof( + address supplier, + bytes32 dataMixedHash, + uint256 nonce_block + ); event WithdrawReward(bytes32 mixedHash, uint256 cycle); event CycleStart(uint256 cycleNumber, uint256 startReward); - function initialize(address _gwtToken, address _Foundation) public initializer { + bool isGWT10Enabled; + + function initialize( + address _gwtToken, + address _Foundation + ) public initializer { __PublicDataStorageUpgradable_init(_gwtToken, _Foundation); } - function __PublicDataStorageUpgradable_init(address _gwtToken, address _Foundation) internal onlyInitializing { + function __PublicDataStorageUpgradable_init( + address _gwtToken, + address _Foundation + ) internal onlyInitializing { __UUPSUpgradeable_init(); __Ownable_init(msg.sender); @@ -148,31 +189,40 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable totalRewardScore = 1600; // 设置初始参数 - sysConfig.minDepositRatio = 64; // create data时最小为64倍 - sysConfig.minPublicDataStorageWeeks = 96; // create data时最小为96周 - sysConfig.minLockWeeks = 24; // show的时候最小为24周,目前固定为最小值 - sysConfig.blocksPerCycle = 17280; // 每个cycle为72小时 - sysConfig.topRewards = 32; // top 32名进榜 - sysConfig.lockAfterShow = 11520; // show成功后48小时内才能解锁 - sysConfig.showTimeout = 5760; // show之后24小时允许挑战 - sysConfig.maxNonceBlockDistance = 2; // 允许的nonce block距离, 要小于256 - sysConfig.minRankingScore = 64; // 最小的排名分数 - sysConfig.minDataSize = 1 << 27; // dataSize换算GWT时,最小值为128M - sysConfig.createDepositRatio = 5; // 因为初期推荐使用Immediate Show,这里会设置成5倍,可以让前十几个show都可以立即成立 + sysConfig.minDepositRatio = 64; // create data时最小为64倍 + sysConfig.minPublicDataStorageWeeks = 96; // create data时最小为96周 + sysConfig.minLockWeeks = 24; // show的时候最小为24周,目前固定为最小值 + sysConfig.blocksPerCycle = 17280; // 每个cycle为72小时 + sysConfig.topRewards = 32; // top 32名进榜 + sysConfig.lockAfterShow = 11520; // show成功后48小时内才能解锁 + sysConfig.showTimeout = 5760; // show之后24小时允许挑战 + sysConfig.maxNonceBlockDistance = 2; // 允许的nonce block距离, 要小于256 + sysConfig.minRankingScore = 64; // 最小的排名分数 + sysConfig.minDataSize = 1 << 27; // dataSize换算GWT时,最小值为128M + sysConfig.createDepositRatio = 5; // 因为初期推荐使用Immediate Show,这里会设置成5倍,可以让前十几个show都可以立即成立 + + isGWT10Enabled = false; } - function _authorizeUpgrade(address newImplementation) internal virtual override onlyOwner { - + function disableGWT10() public onlyOwner { + isGWT10Enabled = false; } - function allowPublicDataContract(address[] calldata contractAddrs) public onlyOwner { + function _authorizeUpgrade( + address newImplementation + ) internal virtual override onlyOwner {} + + function allowPublicDataContract( + address[] calldata contractAddrs + ) public onlyOwner { for (uint i = 0; i < contractAddrs.length; i++) { _allowedPublicDataContract[contractAddrs[i]] = true; } - } - function denyPublicDataContract(address[] calldata contractAddrs) public onlyOwner { + function denyPublicDataContract( + address[] calldata contractAddrs + ) public onlyOwner { for (uint i = 0; i < contractAddrs.length; i++) { _allowedPublicDataContract[contractAddrs[i]] = false; } @@ -183,11 +233,40 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable sysConfig = config; } - function _getRewardScore(uint256 ranking) internal pure returns(uint8) { + function _getRewardScore(uint256 ranking) internal pure returns (uint8) { uint8[32] memory rewardScores = [ - 240, 180, 150, 120, 100, 80, 60, 53, 42, 36, - 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, - 24, 23, 22, 21, 20, 19, 18 ,17, 16, 15, 14 + 240, + 180, + 150, + 120, + 100, + 80, + 60, + 53, + 42, + 36, + 35, + 34, + 33, + 32, + 31, + 30, + 29, + 28, + 27, + 26, + 25, + 24, + 23, + 22, + 21, + 20, + 19, + 18, + 17, + 16, + 15, + 14 ]; if (ranking <= rewardScores.length) { return rewardScores[ranking - 1]; @@ -196,28 +275,59 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable } } - function _getRemainScore(uint256 length) internal pure returns(uint16) { + function _getRemainScore(uint256 length) internal pure returns (uint16) { uint16[33] memory remainScores = [ - 1600, 1360, 1180, 1030, 910, 810, - 730, 670, 617, 575, 539, 504, - 470, 437, 405, 374, 344, 315, - 287, 260, 234, 209, 185, 162, - 140, 119, 99, 80, 62, 45, - 29, 14, 0 + 1600, + 1360, + 1180, + 1030, + 910, + 810, + 730, + 670, + 617, + 575, + 539, + 504, + 470, + 437, + 405, + 374, + 344, + 315, + 287, + 260, + 234, + 209, + 185, + 162, + 140, + 119, + 99, + 80, + 62, + 45, + 29, + 14, + 0 ]; return remainScores[length]; } - function _cycleNumber(uint256 blockNumber, uint256 startBlock) internal view returns(uint256) { - uint cycleNumber = (blockNumber - startBlock) / sysConfig.blocksPerCycle; + function _cycleNumber( + uint256 blockNumber, + uint256 startBlock + ) internal view returns (uint256) { + uint cycleNumber = (blockNumber - startBlock) / + sysConfig.blocksPerCycle; if (cycleNumber * sysConfig.blocksPerCycle + startBlock < blockNumber) { cycleNumber += 1; } return cycleNumber; } - function _curCycleNumber() internal view returns(uint256) { + function _curCycleNumber() internal view returns (uint256) { return _cycleNumber(block.number, _startBlock); } @@ -230,13 +340,15 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable if (cycleInfo.totalAward == 0) { uint256 lastCycleReward = _cycleInfos[_currectCycle].totalAward; // 5%作为基金会收入 - uint256 fundationIncome = lastCycleReward * 5 / 100; + uint256 fundationIncome = (lastCycleReward * 5) / 100; gwtToken.transfer(foundationAddress, fundationIncome); // 如果上一轮的获奖数据不足32个,剩余的奖金也滚动进此轮奖池 - uint16 remainScore = _getRemainScore(_cycleInfos[_currectCycle].scoreList.length()); - uint256 remainReward = lastCycleReward * 4 * remainScore / totalRewardScore / 5; + uint16 remainScore = _getRemainScore( + _cycleInfos[_currectCycle].scoreList.length() + ); + uint256 remainReward = (lastCycleReward * 4 * remainScore) / totalRewardScore / 5; - cycleInfo.totalAward = lastCycleReward - (lastCycleReward * 4 / 5) - fundationIncome + remainReward; + cycleInfo.totalAward = lastCycleReward - ((lastCycleReward * 4) / 5) - fundationIncome + remainReward; _currectCycle = cycleNumber; emit CycleStart(cycleNumber, cycleInfo.totalAward); @@ -245,7 +357,6 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable return cycleInfo; } - function _addCycleReward(uint256 amount) private { CycleInfo storage cycleInfo = _ensureCurrentCycleStart(); cycleInfo.totalAward += amount; @@ -253,7 +364,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable // 计算这些空间对应多少GWT,单位是wei // 不满128MB的按照128MB计算 - function _dataSizeToGWT(uint64 dataSize) internal view returns(uint256) { + function _dataSizeToGWT(uint64 dataSize) internal view returns (uint256) { uint64 fixedDataSize = dataSize; if (fixedDataSize < sysConfig.minDataSize) { fixedDataSize = sysConfig.minDataSize; @@ -264,21 +375,33 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable function createPublicData( bytes32 dataMixedHash, uint64 depositRatio, - uint256 depositAmount, + uint256 depositAmount, address publicDataContract ) public { require(dataMixedHash != bytes32(0), "data hash is empty"); - require(_allowedPublicDataContract[publicDataContract], " data contract not allowed"); - require(IERCPublicDataContract(publicDataContract).getDataOwner(dataMixedHash) != address(0), "not found in data contract"); + require( + _allowedPublicDataContract[publicDataContract], + " data contract not allowed" + ); + require( + IERCPublicDataContract(publicDataContract).getDataOwner(dataMixedHash) != address(0), + "not found in data contract" + ); // 质押率影响用户SHOW数据所需要冻结的质押 - require(depositRatio >= sysConfig.minDepositRatio, "deposit ratio is too small"); + require( + depositRatio >= sysConfig.minDepositRatio, + "deposit ratio is too small" + ); // minAmount = 数据大小*GWT兑换比例*最小时长*质押率 // get data size from data hash uint64 dataSize = PublicDataProof.lengthFromMixedHash(dataMixedHash); - uint256 minAmount = depositRatio * _dataSizeToGWT(dataSize) * sysConfig.minPublicDataStorageWeeks * sysConfig.createDepositRatio; + uint256 minAmount = depositRatio * + _dataSizeToGWT(dataSize) * + sysConfig.minPublicDataStorageWeeks * + sysConfig.createDepositRatio; require(depositAmount >= minAmount, "deposit amount is too small"); - + PublicData storage publicDataInfo = _publicDatas[dataMixedHash]; require(publicDataInfo.maxDeposit == 0, "public data already exists"); @@ -294,38 +417,64 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable _addCycleReward(system_reward); + // TODO: PublicData的奖励在哪里? + emit PublicDataCreated(dataMixedHash); emit SponsorChanged(dataMixedHash, address(0), msg.sender); emit DepositData(msg.sender, dataMixedHash, balance_add, system_reward); } - function getPublicData(bytes32 dataMixedHash) public view returns(PublicData memory) { - return _publicDatas[dataMixedHash]; + function getPublicData( + bytes32 dataMixedHash + ) public view returns (PublicDataForOutput memory) { + PublicData storage info = _publicDatas[dataMixedHash]; + return PublicDataForOutput( + info.sponsor, + info.dataContract, + info.maxDeposit, + info.dataBalance, + info.depositRatio + ); } - function getCurrectLastShowed(bytes32 dataMixedHash) public view returns(address[] memory) { - return _cycleInfos[_curCycleNumber()].dataInfos[dataMixedHash].lastShowers; + function getCurrectLastShowed( + bytes32 dataMixedHash + ) public view returns (address[] memory) { + return + _cycleInfos[_curCycleNumber()].dataInfos[dataMixedHash].lastShowers; } - function getDataInCycle(uint256 cycleNumber, bytes32 dataMixedHash) public view returns(CycleDataInfo memory) { + function getDataInCycle( + uint256 cycleNumber, + bytes32 dataMixedHash + ) public view returns (CycleDataInfo memory) { return _cycleInfos[cycleNumber].dataInfos[dataMixedHash]; } - function getCycleInfo(uint256 cycleNumber) public view returns(CycleOutputInfo memory) { - return CycleOutputInfo(_cycleInfos[cycleNumber].totalAward, _cycleInfos[cycleNumber].scoreList.getSortedList()); + function getCycleInfo( + uint256 cycleNumber + ) public view returns (CycleOutputInfo memory) { + return + CycleOutputInfo( + _cycleInfos[cycleNumber].totalAward, + _cycleInfos[cycleNumber].scoreList.getSortedList() + ); } - function getPledgeInfo(address supplier) public view returns(SupplierInfo memory) { + function getPledgeInfo( + address supplier + ) public view returns (SupplierInfo memory) { return _supplierInfos[supplier]; } - function isDataContractAllowed(address contractAddr) public view returns(bool) { + function isDataContractAllowed( + address contractAddr + ) public view returns (bool) { return _allowedPublicDataContract[contractAddr]; } - function getOwner(bytes32 dataMixedHash) public view returns(address) { - PublicData memory info = _publicDatas[dataMixedHash]; - return _getDataOwner(dataMixedHash, info); + function getOwner(bytes32 dataMixedHash) public view returns (address) { + return _getDataOwner(dataMixedHash, _publicDatas[dataMixedHash]); } function addDeposit(bytes32 dataMixedHash, uint256 depositAmount) public { @@ -344,7 +493,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable if (depositAmount > ((publicDataInfo.maxDeposit * 11) / 10)) { publicDataInfo.maxDeposit = depositAmount; address oldSponsor = publicDataInfo.sponsor; - if(oldSponsor != msg.sender) { + if (oldSponsor != msg.sender) { publicDataInfo.sponsor = msg.sender; emit SponsorChanged(dataMixedHash, oldSponsor, msg.sender); } @@ -353,7 +502,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable emit DepositData(msg.sender, dataMixedHash, balance_add, system_reward); } - function dataBalance(bytes32 dataMixedHash) public view returns(uint256) { + function dataBalance(bytes32 dataMixedHash) public view returns (uint256) { return _publicDatas[dataMixedHash].dataBalance; } @@ -369,7 +518,11 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable gwtToken.transferFrom(msg.sender, address(this), amount); _supplierInfos[msg.sender].avalibleBalance += amount; - emit SupplierBalanceChanged(msg.sender, _supplierInfos[msg.sender].avalibleBalance, _supplierInfos[msg.sender].lockedBalance); + emit SupplierBalanceChanged( + msg.sender, + _supplierInfos[msg.sender].avalibleBalance, + _supplierInfos[msg.sender].lockedBalance + ); } function unstakeGWT(uint256 amount) public { @@ -379,50 +532,97 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable require(amount <= supplierInfo.avalibleBalance, "insufficient balance"); supplierInfo.avalibleBalance -= amount; gwtToken.transfer(msg.sender, amount); - emit SupplierBalanceChanged(msg.sender, supplierInfo.avalibleBalance, supplierInfo.lockedBalance); + emit SupplierBalanceChanged( + msg.sender, + supplierInfo.avalibleBalance, + supplierInfo.lockedBalance + ); } - function _getLockAmountByHash(bytes32 dataMixedHash, ShowType showType) internal view returns(uint256, bool) { + function _getLockAmountByHash( + bytes32 dataMixedHash, + ShowType showType + ) internal view returns (uint256, bool) { uint64 dataSize = PublicDataProof.lengthFromMixedHash(dataMixedHash); - uint256 immediatelyLockAmount = (showType == ShowType.Immediately) ? _publicDatas[dataMixedHash].dataBalance * 2 / 10 : 0; - uint256 normalLockAmount = _dataSizeToGWT(dataSize) * _publicDatas[dataMixedHash].depositRatio * sysConfig.minLockWeeks; + uint256 immediatelyLockAmount = (showType == ShowType.Immediately) + ? (_publicDatas[dataMixedHash].dataBalance * 2) / 10 + : 0; + uint256 normalLockAmount = _dataSizeToGWT(dataSize) * + _publicDatas[dataMixedHash].depositRatio * + sysConfig.minLockWeeks; bool isImmediately = immediatelyLockAmount > normalLockAmount; - return (isImmediately ? immediatelyLockAmount : normalLockAmount, isImmediately); + return ( + isImmediately ? immediatelyLockAmount : normalLockAmount, + isImmediately + ); } - function _LockSupplierPledge(address supplierAddress, bytes32 dataMixedHash, ShowType showType) internal returns(uint256, bool){ + function _LockSupplierPledge( + address supplierAddress, + bytes32 dataMixedHash, + ShowType showType + ) internal returns (uint256, bool) { _adjustSupplierBalance(supplierAddress); - + SupplierInfo storage supplierInfo = _supplierInfos[supplierAddress]; - - (uint256 lockAmount, bool isImmediately) = _getLockAmountByHash(dataMixedHash, showType); - - require(supplierInfo.avalibleBalance >= lockAmount, "insufficient balance"); + + (uint256 lockAmount, bool isImmediately) = _getLockAmountByHash( + dataMixedHash, + showType + ); + + require( + supplierInfo.avalibleBalance >= lockAmount, + "insufficient balance" + ); supplierInfo.avalibleBalance -= lockAmount; supplierInfo.lockedBalance += lockAmount; supplierInfo.unlockBlock = block.number + sysConfig.lockAfterShow; - emit SupplierBalanceChanged(supplierAddress, supplierInfo.avalibleBalance, supplierInfo.lockedBalance); + emit SupplierBalanceChanged( + supplierAddress, + supplierInfo.avalibleBalance, + supplierInfo.lockedBalance + ); return (lockAmount, isImmediately); } - function _verifyDataProof(bytes32 dataMixedHash,uint256 nonce_block, uint32 index, bytes16[] calldata m_path, bytes calldata leafdata) private view returns(bytes32,bytes32) { + function _verifyDataProof( + bytes32 dataMixedHash, + uint256 nonce_block, + uint32 index, + bytes16[] calldata m_path, + bytes calldata leafdata + ) private view returns (bytes32, bytes32) { require(nonce_block < block.number, "invalid nonce_block_high"); require(block.number - nonce_block < 256, "nonce block too old"); bytes32 nonce = blockhash(nonce_block); - return PublicDataProof.calcDataProof(dataMixedHash, nonce, index, m_path, leafdata, bytes32(0)); - } - - function _mergeMixHashAndHeight(uint256 dataMixedHash, uint256 nonce_block) public pure returns (uint256) { - uint256 highBits = dataMixedHash >> 64; - uint256 lowBits = nonce_block & ((1 << 64) - 1); - return (highBits << 64) | lowBits; - } - - function _scoreFromHash(bytes32 dataMixedHash) public pure returns (uint64) { + return + PublicDataProof.calcDataProof( + dataMixedHash, + nonce, + index, + m_path, + leafdata, + bytes32(0) + ); + } + + function _mergeMixHashAndHeight( + uint256 dataMixedHash, + uint256 nonce_block + ) public pure returns (uint256) { + uint256 highBits = dataMixedHash >> 64; + uint256 lowBits = nonce_block & ((1 << 64) - 1); + return (highBits << 64) | lowBits; + } + + function _scoreFromHash( + bytes32 dataMixedHash + ) public pure returns (uint64) { uint256 size = PublicDataProof.lengthFromMixedHash(dataMixedHash) >> 30; if (size == 0) { // 1GB以下算1分 @@ -431,7 +631,11 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable return uint64(size / 4 + 2); } - function _updateLastSupplier(CycleDataInfo storage dataInfo, address oldSupplier, address supplier) private { + function _updateLastSupplier( + CycleDataInfo storage dataInfo, + address oldSupplier, + address supplier + ) private { if (oldSupplier != address(0)) { for (uint8 i = 0; i < dataInfo.lastShowers.length; i++) { if (dataInfo.lastShowers[i] == oldSupplier) { @@ -451,41 +655,54 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable dataInfo.showerIndex = 0; } } - } - function _getGWTDifficultRatio() private view returns(uint256) { + + //由于cyclePower的单位是GB,先扩大1024*1024*1000,再进行计算 + function _getGWTDifficultRatio(uint256 lastCyclePower, uint256 curCyclePower) public pure returns (uint256) { //这个函数本质上,返回的是周利率 //1)根据总算力计算基础难度值,算力每次翻倍,基础难度值都会下降 从<= 1PB 开始, 2PB,4PB,8PB,16PB,32PB,64PB,128PB,256PB,512PB .. 都会调整基础难度值 - // 倍率结果为8x - 1x,当总算力是1PB(GWT)时, 倍率为8x,随后算力每增加一倍,倍率下降10%。 倍率 = 0.9^(log2(总算力/1PB)),每次总算力翻倍,倍率为上一档倍率的90% + // 倍率结果为8x - 1x,当总算力是1PB(GWT)时, 倍率为8x,随后算力每增加一倍,倍率下降10%。 倍率 = 0.9^(log2(总算力/1PB)),每次总算力翻倍,倍率为上一档倍率的90% // 约21次难度调整后会变成1x,此时系统容量已经是 1PB * 2^21 = 2EB //2)根据算力增速x计算基准GWT利率(增发速度),y = f(x),x的值域是从[0,正无穷] y的取值范围最小值是 0.2%, 最大值是 2% - + //根据上述规则,一周的GWT挖矿比例最大,是抵押总数的 16%(周回报16%)。即矿工在公共数据挖矿中质押了100个GWT,在1周后,能挖出16个GWT,接近6.25周回本 //如果早期算力总量低,但没什么人挖,则周回报为 1.6%(周回报1.6%),即矿工在公共数据挖矿中质押了100个GWT,在1周后,能挖出1.6个GWT,接近62.5周回本 - uint256 lastCyclePower = _cycleInfos[_currectCycle - 2].totalShowPower; - uint256 curCyclePower = _cycleInfos[_currectCycle - 1].totalShowPower; - uint256 base_r = 0.002; //基础利率 - if(curCyclePower == 0) { - base_r = 0.01; + //uint256 base_r = 0.002; + uint256 base_r = 2097152; + if (curCyclePower == 0) { + // base_r = 0.01 + base_r = 10485760; } else { //给我一个数学函数,满足:y = f(x),x的含义是增长率的值域是从[0,正无穷] y的取值范围最小值是 0.2%, 最大值是 2%。 我希望在x在200%(2倍前),y能快速的增长到1% - if(curCyclePower > lastCyclePower) { - base_r = 0.002 + 0.008 * (curCyclePower - lastCyclePower) / lastCyclePower; - if(base_r > 0.02) { - base_r = 0.02; + if (curCyclePower > lastCyclePower) { + base_r += (8 * (curCyclePower - lastCyclePower) * 1024 * 1024 * 1000) / lastCyclePower; + //base_r = 0.002 + (0.008 * (curCyclePower - lastCyclePower)) / lastCyclePower; + if (base_r > 20971520) { + base_r = 20971520; } } } // 8 * 0.9^(log2((curCyclePower / 1PB))); - uint256 ratio = 8 * 0.9^(log2((curCyclePower / 1024*1024))); - if(ratio < 1) { + // curCyclePower的单位是GB + int128 exp1 = ABDKMath64x64.fromUInt(curCyclePower).log_2().toInt() - 20; + if (exp1 < 0) { + exp1 = 0; + } + uint256 ratio = ABDKMath64x64.divu(9, 10).pow(uint256(int256(exp1))).toUInt() * 8; + //uint256 ratio = 8 * ((9/10)^(log2(curCyclePower / 1024 / 1024))); + // = 8 * 0.9^(log2(curCyclePower) - 20) + if (ratio < 1) { ratio = 1; } return ratio * base_r; } - function _onProofSuccess(DataProof storage proof,PublicData storage publicDataInfo,bytes32 dataMixedHash) private { + function _onProofSuccess( + DataProof storage proof, + PublicData storage publicDataInfo, + bytes32 dataMixedHash + ) private { uint256 reward = publicDataInfo.dataBalance / 10; emit SupplierReward(proof.prover, dataMixedHash, reward); if (reward > 0) { @@ -496,8 +713,8 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable // 更新本cycle的score CycleInfo storage cycleInfo = _ensureCurrentCycleStart(); CycleDataInfo storage dataInfo = cycleInfo.dataInfos[dataMixedHash]; - - // 按文件大小比例增加 , 0.1G - 1G 1分,1G-4G 2分, 4G-8G 3分,8G-16G 4分 16G-32G 5分 ... + + // 按文件大小比例增加 , 0.1G - 1G 1分,1G-4G 2分, 4G-8G 3分,8G-16G 4分 16G-32G 5分 ... uint64 score = _scoreFromHash(dataMixedHash); dataInfo.score += score; @@ -506,7 +723,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable // 合约里不关注的数据先不记录了,省gas //publicDataInfo.point += score; - + // 更新cycle的last shower _updateLastSupplier(dataInfo, address(0), msg.sender); @@ -519,83 +736,125 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable } //获得gwt 挖矿奖励 - lastRecordShowTime = publicDataInfo.show_records[msg.sender]; - if(lastRecordShowTime ==0) { + uint256 lastRecordShowTime = publicDataInfo.show_records[msg.sender]; + if (lastRecordShowTime == 0) { publicDataInfo.show_records[msg.sender] = block.timestamp; } else { - if(block.timestamp - lastRecordShowTime > 1 weeks) { + if (block.timestamp - lastRecordShowTime > 1 weeks) { //获得有效算力! - // reward = 文件大小* T * 存储质量比率(含质押率) * 公共数据挖矿难度比 + // reward = 文件大小* T * 存储质量比率(含质押率) * 公共数据挖矿难度比 // T = 当前时间 - 上次show时间,T必须大于1周,最长为4周 publicDataInfo.show_records[msg.sender] = block.timestamp; - uint32 storageWeeks = (block.timestamp - lastRecordShowTime) / 1 weeks; - if(storageWeeks > 4) { + uint256 storageWeeks = (block.timestamp - lastRecordShowTime) / 1 weeks; + if (storageWeeks > 4) { storageWeeks = 4; } uint256 size = PublicDataProof.lengthFromMixedHash(dataMixedHash) >> 30; if (size == 0) { // 1GB以下算1G - return 1; + size = 1; } - - uint256 storagePower = size * storageWeeks ; + + uint256 storagePower = size * storageWeeks; //更新当前周期的总算力,公共数据的算力是私有数据的3倍 - cycleInfo.totalShowPower += storagePower* 3; + cycleInfo.totalShowPower += storagePower * 3; //计算挖矿奖励,难度和当前算力总量,上一个周期的算力总量有关 - uint256 gwtReward = storagePower * publicDataInfo.depositRatio * _getGWTDifficultRatio(); + // 如果_currectCycle是0或者1,这个要怎么算? + uint256 lastCyclePower = _cycleInfos[_currectCycle - 2].totalShowPower; + uint256 curCyclePower = _cycleInfos[_currectCycle - 1].totalShowPower; + // 由于_getGWTDifficultRatio的返回扩大了1024*1024*1000倍,这里要除去 + uint256 gwtReward = storagePower *publicDataInfo.depositRatio * _getGWTDifficultRatio(lastCyclePower, curCyclePower) / 1024 / 1024 / 1000; //更新奖励,80%给矿工,20%给当前数据的余额 - gwtToken.mint(msg.sender, gwtReward*0.8); + gwtToken.mint(msg.sender, gwtReward * 8 / 10); //TODO:是否需要留一部分挖矿奖励给基金会?目前思考是不需要的 - gwtToken.mint(this, gwtReward*0.2); - publicDataInfo.dataBalance += gwtReward*0.2; + gwtToken.mint(address(this), gwtReward * 2 / 10); + publicDataInfo.dataBalance += gwtReward * 2 / 10; } } } - function getDataProof(bytes32 dataMixedHash, uint256 nonce_blocks) public view returns(DataProof memory) { - uint256 proofKey = _mergeMixHashAndHeight(uint256(dataMixedHash), nonce_blocks); + function getDataProof( + bytes32 dataMixedHash, + uint256 nonce_blocks + ) public view returns (DataProof memory) { + uint256 proofKey = _mergeMixHashAndHeight( + uint256(dataMixedHash), + nonce_blocks + ); return _publicDataProofs[proofKey]; } function withdrawShow(bytes32 dataMixedHash, uint256 nonce_block) public { - uint256 proofKey = _mergeMixHashAndHeight(uint256(dataMixedHash), nonce_block); + uint256 proofKey = _mergeMixHashAndHeight( + uint256(dataMixedHash), + nonce_block + ); DataProof storage proof = _publicDataProofs[proofKey]; require(proof.proofBlockHeight > 0, "proof not exist"); - require(block.number - proof.proofBlockHeight > sysConfig.showTimeout, "proof not unlock"); + require( + block.number - proof.proofBlockHeight > sysConfig.showTimeout, + "proof not unlock" + ); if (block.number - proof.proofBlockHeight > sysConfig.showTimeout) { //Last Show Proof successed! 获得奖励+增加积分 PublicData storage publicDataInfo = _publicDatas[dataMixedHash]; - _onProofSuccess(proof, publicDataInfo,dataMixedHash); - + _onProofSuccess(proof, publicDataInfo, dataMixedHash); + //防止重入:反复领取奖励 proof.proofBlockHeight = 0; } } - function showData(bytes32 dataMixedHash, uint256 nonce_block, uint32 index, bytes16[] calldata m_path, bytes calldata leafdata, ShowType showType) public { - uint256 proofKey = _mergeMixHashAndHeight(uint256(dataMixedHash), nonce_block); + function showData( + bytes32 dataMixedHash, + uint256 nonce_block, + uint32 index, + bytes16[] calldata m_path, + bytes calldata leafdata, + ShowType showType + ) public { + uint256 proofKey = _mergeMixHashAndHeight( + uint256(dataMixedHash), + nonce_block + ); DataProof storage proof = _publicDataProofs[proofKey]; - + bool isNewShow = false; address supplier = msg.sender; - if(proof.proofBlockHeight == 0) { - require(block.number - nonce_block <= sysConfig.maxNonceBlockDistance, "invalid nonce block"); + if (proof.proofBlockHeight == 0) { + require( + block.number - nonce_block <= sysConfig.maxNonceBlockDistance, + "invalid nonce block" + ); isNewShow = true; } else { - require(block.number - proof.proofBlockHeight <= sysConfig.showTimeout, "challenge timeout"); + require( + block.number - proof.proofBlockHeight <= sysConfig.showTimeout, + "challenge timeout" + ); } - - (bytes32 root_hash,) = _verifyDataProof(dataMixedHash,nonce_block,index,m_path,leafdata); - if(isNewShow) { + (bytes32 root_hash, ) = _verifyDataProof( + dataMixedHash, + nonce_block, + index, + m_path, + leafdata + ); + + if (isNewShow) { //根据showType决定锁定金额 PublicData storage publicDataInfo = _publicDatas[dataMixedHash]; - (uint256 lockAmount, bool isImmediately) = _LockSupplierPledge(supplier, dataMixedHash, showType); + (uint256 lockAmount, bool isImmediately) = _LockSupplierPledge( + supplier, + dataMixedHash, + showType + ); proof.lockedAmount = lockAmount; proof.nonceBlockHeight = nonce_block; @@ -604,26 +863,41 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable proof.prover = msg.sender; //proof.showType = showType; - if(isImmediately) { + if (isImmediately) { _onProofSuccess(proof, publicDataInfo, dataMixedHash); } - } else { // 已经有挑战存在:判断是否结果更好,如果更好,更新结果,并更新区块高度 - if(root_hash < proof.proofResult) { - _supplierInfos[proof.prover].lockedBalance -= proof.lockedAmount; + if (root_hash < proof.proofResult) { + _supplierInfos[proof.prover].lockedBalance -= proof + .lockedAmount; - uint256 rewardFromPunish = proof.lockedAmount * 8 / 10; + uint256 rewardFromPunish = (proof.lockedAmount * 8) / 10; gwtToken.transfer(msg.sender, rewardFromPunish); - gwtToken.transfer(foundationAddress, proof.lockedAmount - rewardFromPunish); - - emit SupplierPunished(proof.prover, dataMixedHash, proof.lockedAmount); - emit SupplierBalanceChanged(proof.prover, _supplierInfos[proof.prover].avalibleBalance, _supplierInfos[proof.prover].lockedBalance); + gwtToken.transfer( + foundationAddress, + proof.lockedAmount - rewardFromPunish + ); + + emit SupplierPunished( + proof.prover, + dataMixedHash, + proof.lockedAmount + ); + emit SupplierBalanceChanged( + proof.prover, + _supplierInfos[proof.prover].avalibleBalance, + _supplierInfos[proof.prover].lockedBalance + ); PublicData storage publicDataInfo = _publicDatas[dataMixedHash]; - (uint256 lockAmount, bool isImmediately) = _LockSupplierPledge(supplier, dataMixedHash, showType); + (uint256 lockAmount, bool isImmediately) = _LockSupplierPledge( + supplier, + dataMixedHash, + showType + ); - if(isImmediately) { + if (isImmediately) { _onProofSuccess(proof, publicDataInfo, dataMixedHash); } @@ -632,50 +906,65 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable proof.proofBlockHeight = block.number; proof.prover = msg.sender; //proof.showType = showType; - } + } } emit ShowDataProof(msg.sender, dataMixedHash, nonce_block); } - function _getDataOwner(bytes32 dataMixedHash, PublicData memory publicDataInfo) internal view returns(address) { - return IERCPublicDataContract(publicDataInfo.dataContract).getDataOwner(dataMixedHash); + function _getDataOwner( + bytes32 dataMixedHash, + PublicData storage publicDataInfo + ) internal view returns (address) { + return + IERCPublicDataContract(publicDataInfo.dataContract).getDataOwner( + dataMixedHash + ); } function withdrawReward(uint256 cycleNumber, bytes32 dataMixedHash) public { // 判断这次的cycle已经结束 //require(_currectCycle > cycleNumber, "cycle not finish"); - require(block.number > cycleNumber * sysConfig.blocksPerCycle + _startBlock, "cycle not finish"); + require( + block.number > cycleNumber * sysConfig.blocksPerCycle + _startBlock, + "cycle not finish" + ); CycleInfo storage cycleInfo = _cycleInfos[cycleNumber]; CycleDataInfo storage dataInfo = cycleInfo.dataInfos[dataMixedHash]; //REVIEW:一次排序并保存的GAS和32次内存排序的成本问题? - uint256 scoreListRanking = cycleInfo.scoreList.getRanking(dataMixedHash); + uint256 scoreListRanking = cycleInfo.scoreList.getRanking( + dataMixedHash + ); require(scoreListRanking > 0, "data not in rank"); // 无论谁来取,一次性提取所有奖励,并更新积分 require(dataInfo.score > 0, "already withdraw"); // 计算该得到多少奖励 - uint256 totalReward = cycleInfo.totalAward * 8 / 10; + uint256 totalReward = (cycleInfo.totalAward * 8) / 10; uint8 score = _getRewardScore(scoreListRanking); // 如果数据总量不足32,那么多余的奖励沉淀在合约账户中 - uint256 dataReward = totalReward * score / totalRewardScore; + uint256 dataReward = (totalReward * score) / totalRewardScore; // memory无法创建动态数组和map,直接算一个转一个了 // owner - gwtToken.transfer(_getDataOwner(dataMixedHash, _publicDatas[dataMixedHash]), dataReward / 5); + gwtToken.transfer( + _getDataOwner(dataMixedHash, _publicDatas[dataMixedHash]), + dataReward / 5 + ); // sponser gwtToken.transfer(_publicDatas[dataMixedHash].sponsor, dataReward / 2); // last showers - uint256 showerReward = (dataReward - dataReward / 2 - dataReward / 5) / dataInfo.lastShowers.length; + uint256 showerReward = (dataReward - dataReward / 2 - dataReward / 5) / + dataInfo.lastShowers.length; for (uint8 i = 0; i < dataInfo.lastShowers.length; i++) { gwtToken.transfer(dataInfo.lastShowers[i], showerReward); } - + // 设置已取标志 dataInfo.score = 0;