Solidity

這個函式意味著什麼?為什麼是aAdjusted和bAdjusted?

  • March 21, 2022
This function is from uniswap-v3/core/contracts/libraries/Oracle.sol
/// @notice comparator for 32-bit timestamps
/// @dev safe for 0 or 1 overflows, a and b _must_ be chronologically before or equal to time
/// @param time A timestamp truncated to 32 bits
/// @param a A comparison timestamp from which to determine the relative position of `time`
/// @param b From which to determine the relative position of `time`
/// @return bool Whether `a` is chronologically <= `b`

function lte( uint32 time, uint32 a, uint32 b ) private pure returns (bool) { // if there hasn’t been overflow, no need to adjust if (a <= time && b <= time) return a <= b;

uint256 aAdjusted = a > time ? a : a + 232; uint256 bAdjusted = b > time ? b : b + 232;

return aAdjusted <= bAdjusted; }

The block timestamp in Ethereum is the UNIX time — time in seconds since January 1970. When stored as a 32-bit integer, it will overflow some time in the future. After the overflow happens, a UNIX time value xmay be numerically smaller than a timeyin the past when compared as 32-bit integer, even thoughxis chronologically aftery`.

The intention of this Solidity code is to make the comparisons of two time values a and b safe even after the overflow. The value of the current, reference time is expected to be chronologically after or equal to the values of a and b. (This is the assumption this code makes, it will fail it it’s not true.) If the value of a or/and b is greater than the value of time, then time has overflown, but a and/or b has not. To correct for this overflow, the max value of unsigned 32-bit integer is added to the variables that have overflown.

Let’s say time = 1, a = 0, b = 2**32-1. Then aAdjusted = 2**32, bAdjusted = 2**32-1, and aAdjusted is smaller than bAdjusted when compared as 256-bit integers, as expected.`

引用自:https://ethereum.stackexchange.com/questions/124299