這個函式意味著什麼?為什麼是aAdjusted和bAdjusted?
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 time
yin the past when compared as 32-bit integer, even though
xis chronologically after
y`.The intention of this Solidity code is to make the comparisons of two time values
a
andb
safe even after the overflow. The value of the current, referencetime
is expected to be chronologically after or equal to the values ofa
andb
. (This is the assumption this code makes, it will fail it it’s not true.) If the value ofa
or/andb
is greater than the value oftime
, thentime
has overflown, buta
and/orb
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
. ThenaAdjusted = 2**32
,bAdjusted = 2**32-1
, andaAdjusted
is smaller thanbAdjusted
when compared as 256-bit integers, as expected.`