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

V0.3.7 patch17 reduce compile size by compressing errors #63

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
9f3a0df
(hub): non-Reentrant modifier had unused error code; remove
benjaminbollen Sep 25, 2024
1db4d65
(circles): correct order of check and effect in _claimIssuance to pre…
benjaminbollen Sep 25, 2024
3a209d7
(hub): on update v1 status, never reduce lastMintTime to avoid resett…
benjaminbollen Sep 26, 2024
fbc7824
Merge branch 'v0.3.7-patch02-check-lastminttime-before-updating-v1-st…
benjaminbollen Sep 26, 2024
fcde687
(Hub, InflatCRC, Circles): tidy up unused variable, docstring, comment
benjaminbollen Sep 26, 2024
2c8363d
Merge branch 'v0.3.7-patch03-wrong-value-welcome-bonus-docs' into rc-…
benjaminbollen Sep 26, 2024
7efb87f
(erc20Lift): only deploy erc20 wrapper for humans or groups (not for …
benjaminbollen Sep 26, 2024
99220e6
Merge branch 'v0.3.7-patch04-deploy-erc20-only-for-humans-and-orgs' i…
benjaminbollen Sep 26, 2024
c2a520f
(erc20Lift): quick extra cleanup of comments
benjaminbollen Sep 26, 2024
e25ede4
(Circles): correct issuance function for calculating exactly on a com…
benjaminbollen Sep 30, 2024
678a4dd
Merge branch 'v0.3.7-patch05-missing-hour-when-minting-exactly-on-the…
benjaminbollen Sep 30, 2024
a6cd3ea
(hub): correct trivial error in error in _matchNettedFlows
benjaminbollen Sep 30, 2024
b505892
Merge branch 'v0.3.7-patch06-trivial-error-correction-in-matchNettedF…
benjaminbollen Sep 30, 2024
61be206
(hub, circles): suppress group mint's acceptance call if along a path…
benjaminbollen Sep 30, 2024
5a58fa1
Merge branch 'v0.3.7-patch07-dont-make-explicit-acceptance-call-in-pa…
benjaminbollen Sep 30, 2024
5a55b0c
(demurrage): small possible gas optimisation
benjaminbollen Sep 30, 2024
8337c73
Merge branch 'v0.3.7-patch08-issue44-gas-optimisation-demurrage' into…
benjaminbollen Sep 30, 2024
8c857ce
(migration): require migration amount to be greater than zero
benjaminbollen Oct 1, 2024
212783b
Merge branch 'v0.3.7-patch09-zero-amount-migration' into rc-v0.3.7
benjaminbollen Oct 1, 2024
f8a5e08
(circles): improve the impl so save compile size and improve readability
benjaminbollen Oct 1, 2024
d2c1a41
Merge branch 'v0.3.7-patch05-missing-hour-when-minting-exactly-on-the…
benjaminbollen Oct 1, 2024
10bb27a
(InflationaryOperator): initialise inflationDayZero from Hub
benjaminbollen Oct 1, 2024
d6ebb20
Merge branch 'v0.3.7-patch10-initialise-inflationDayZero-for-operator…
benjaminbollen Oct 1, 2024
78cc375
(StandardTreasury): improve accuracy of name of events and func
benjaminbollen Oct 1, 2024
cf4ba33
(hub): adding explicit GroupMint event to Hub, TBD if we can find spa…
benjaminbollen Oct 1, 2024
8a50969
Merge branch 'v0.3.7-patch11-improve-events-in-standardTreasury' into…
benjaminbollen Oct 1, 2024
7ca31f9
(hub): simplify GroupMint event to reduce code size
benjaminbollen Oct 1, 2024
670839c
Merge branch 'v0.3.7-patch11-improve-events-in-standardTreasury' into…
benjaminbollen Oct 1, 2024
d7406b5
(errors): rename variable in error for correctness
benjaminbollen Oct 1, 2024
4da6217
Merge branch 'v0.3.7-patch12-correct-error-name' into rc-v0.3.7
benjaminbollen Oct 1, 2024
3954035
(nameRegistry): check short name is not zero
benjaminbollen Oct 1, 2024
51ef19f
Merge branch 'v0.3.7-patch13-protect-shortName-zero' into rc-v0.3.7
benjaminbollen Oct 1, 2024
ae3e110
(nameRegistry): remove restriction only registered address can regist…
benjaminbollen Oct 1, 2024
3755c3a
Merge branch 'v0.3.7-patch14-open-name-registry' into rc-v0.3.7
benjaminbollen Oct 1, 2024
ce7d2ad
(circles): update check for calculate issuance to more accurately che…
benjaminbollen Oct 2, 2024
b58261d
Merge branch 'v0.3.7-patch15-improve-hourly-check' into rc-v0.3.7
benjaminbollen Oct 2, 2024
408b7ce
(circles): remove commented out code
benjaminbollen Oct 2, 2024
faea261
Merge branch 'v0.3.7-patch15-improve-hourly-check' into rc-v0.3.7
benjaminbollen Oct 2, 2024
5998835
Merge branch 'v0.3.7-patch04-deploy-erc20-only-for-humans-and-orgs' i…
benjaminbollen Oct 2, 2024
b00072c
(StandardTreasury): correct data passed to be user data to policy for…
benjaminbollen Oct 2, 2024
50c784b
Merge branch 'v0.3.7-patch16-fix-group-redeem' into rc-v0.3.7
benjaminbollen Oct 2, 2024
4b44c29
(error): CirclesErrorNoArgs <- 0: CirclesAddressCannotBeZero
benjaminbollen Oct 2, 2024
0889ad3
(error): CirclesErrorNoArgs <- 1: CirclesArrayMustNotBeEmpty
benjaminbollen Oct 2, 2024
2fdacd7
(errors): adopt hex error code to reduce code size
benjaminbollen Oct 2, 2024
caaca32
(errors): uint8 conversion saves compile size... who knew?
benjaminbollen Oct 2, 2024
eb5ae88
(errors): first signs of nett benefit
benjaminbollen Oct 2, 2024
3d8323b
(errors): continue replacing errors, but only limited benefit so far
benjaminbollen Oct 3, 2024
353601d
(errors): churning along, also starting to reduce some params in adva…
benjaminbollen Oct 3, 2024
7e4fbe3
(circles): start collapsing if-statements of separate condition check…
benjaminbollen Oct 3, 2024
1135f83
(errors): reduce error detail on RejectBurn by mint policy
benjaminbollen Oct 3, 2024
98d9af3
(errors): repeat the trick! another 80 bytes down
benjaminbollen Oct 3, 2024
7025fef
(errors): covering more green territory
benjaminbollen Oct 3, 2024
320a002
(errors): simplify revert cases in registerGroup
benjaminbollen Oct 3, 2024
8f46b36
(errors): further compaction of errors, esp relevant for hub
benjaminbollen Oct 4, 2024
e28ef69
(errors): compact errors
benjaminbollen Oct 4, 2024
b94d28e
(hub): collapsing if-cond in upsert avatar
benjaminbollen Oct 4, 2024
9143ed7
(hub): more collapsing if clauses
benjaminbollen Oct 4, 2024
8c703d0
(hub): more if collapsing
benjaminbollen Oct 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions src/circles/Circles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ contract Circles is ERC1155, ICirclesErrors {

// Constants

/**
* @notice Issue one Circle per hour for each human in demurraged units.
* So per second issue 10**18 / 3600 = 277777777777778 attoCircles.
*/
uint256 private constant ISSUANCE_PER_SECOND = uint256(277777777777778);

/**
* @notice Upon claiming, the maximum claim is upto two weeks
* of history. Unclaimed older Circles are unclaimable.
Expand Down Expand Up @@ -88,12 +82,16 @@ contract Circles is ERC1155, ICirclesErrors {
MintTime memory mintTime = mintTimes[_human];
if (mintTime.mintV1Status != address(0) && mintTime.mintV1Status != CIRCLES_STOPPED_V1) {
// Circles v1 contract cannot be active.
revert CirclesERC1155MintBlocked(_human, mintTime.mintV1Status);
// revert CirclesERC1155MintBlocked(_human, mintTime.mintV1Status);
revert CirclesErrorOneAddressArg(_human, 0xC0);
}

if (uint256(mintTime.lastMintTime) + 1 hours > block.timestamp) {
// Mint time is set to indefinite future for stopped mints in v2
// and only complete hours get minted, so shortcut the calculation
// Check if at least one new completed hour is mintable
uint256 lastCompletedHour = mintTime.lastMintTime / 1 hours;
uint256 currentCompletedHour = block.timestamp / 1 hours;

if (lastCompletedHour >= currentCompletedHour || mintTime.lastMintTime == INDEFINITE_FUTURE) {
// No new completed hour to mint, or stopped
return (0, 0, 0);
}

Expand All @@ -112,10 +110,17 @@ contract Circles is ERC1155, ICirclesErrors {
// calculate the number of completed hours in day A until `startMint`
int128 k = Math64x64.fromUInt((startMint - (dA * 1 days + inflationDayZero)) / 1 hours);

// Calculate the number of incompleted hours remaining in day B from current timestamp
int128 l = Math64x64.fromUInt(((dB + 1) * 1 days + inflationDayZero - block.timestamp) / 1 hours + 1);
// Calculate the number of seconds remaining in the current day (dB)
uint256 secondsRemainingInB = ((dB + 1) * 1 days + inflationDayZero - block.timestamp);
// Calculate the number of complete hours remaining
uint256 hoursRemainingInB = secondsRemainingInB / 1 hours;
// Calculate l:
// If there are any seconds beyond complete hours, add 1 to account for the incomplete hour
// Convert the result to int128 using Math64x64.fromUInt
int128 l = Math64x64.fromUInt(hoursRemainingInB + (secondsRemainingInB % 1 hours > 0 ? 1 : 0));

// calculate the overcounted (demurraged) k (in day A) and l (in day B) hours
// note that the hours l are not demurraged as it is current day by construction
int128 overcount = Math64x64.add(Math64x64.mul(R[n], k), l);

// subtract the overcount from the total issuance, and convert to attoCircles
Expand All @@ -138,24 +143,33 @@ contract Circles is ERC1155, ICirclesErrors {
// No issuance to claim, simply return without reverting
return;
}
// mint personal Circles to the human
_mintAndUpdateTotalSupply(_human, toTokenId(_human), issuance, "");
// update the last mint time

// update the last mint time, before minting as mint time determines the check (guard for reeentrancy attack)
mintTimes[_human].lastMintTime = uint96(block.timestamp);

// mint personal Circles to the human; ERC1155 mint will perform acceptance call
_mintAndUpdateTotalSupply(_human, toTokenId(_human), issuance, "", true);

emit PersonalMint(_human, issuance, startPeriod, endPeriod);
}

function _mintAndUpdateTotalSupply(address _account, uint256 _id, uint256 _value, bytes memory _data) internal {
_mint(_account, _id, _value, _data);
function _mintAndUpdateTotalSupply(
address _account,
uint256 _id,
uint256 _value,
bytes memory _data,
bool _doAcceptanceCheck
) internal {
_mint(_account, _id, _value, _data, _doAcceptanceCheck);

uint64 today = day(block.timestamp);
DiscountedBalance memory totalSupplyBalance = discountedTotalSupplies[_id];
uint256 newTotalSupply =
_calculateDiscountedBalance(totalSupplyBalance.balance, today - totalSupplyBalance.lastUpdatedDay) + _value;
if (newTotalSupply > MAX_VALUE) {
// DiscountedBalances: balance exceeds maximum value
revert CirclesDemurrageAmountExceedsMaxUint190(_account, _id, newTotalSupply, 2);
// revert CirclesDemurrageAmountExceedsMaxUint192(_account, _id, newTotalSupply, 2);
revert CirclesErrorAddressUintArgs(_account, _id, 0x80);
}
totalSupplyBalance.balance = uint192(newTotalSupply);
totalSupplyBalance.lastUpdatedDay = today;
Expand All @@ -176,7 +190,8 @@ contract Circles is ERC1155, ICirclesErrors {
// and the total supply might differ on the least significant bits.
// There is no good way to handle this, so user should burn a few attoCRC less,
// or wait a day for the total supply to be discounted to zero automatically.
revert CirclesLogicAssertion(4);
// revert CirclesLogicAssertion(4);
revert CirclesErrorNoArgs(0x84);
}
unchecked {
totalSupplyBalance.balance = uint192(discountedTotalSupply - _value);
Expand All @@ -185,14 +200,12 @@ contract Circles is ERC1155, ICirclesErrors {
discountedTotalSupplies[_id] = totalSupplyBalance;
}

// Private functions

/**
* @dev Max function to compare two values.
* @param a Value a
* @param b Value b
*/
function _max(uint256 a, uint256 b) private pure returns (uint256) {
function _max(uint256 a, uint256 b) internal pure returns (uint256) {
return a >= b ? a : b;
}
}
7 changes: 4 additions & 3 deletions src/circles/Demurrage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity >=0.8.24;
import {ABDKMath64x64 as Math64x64} from "lib/abdk-libraries-solidity/ABDKMath64x64.sol";
import "../errors/Errors.sol";

contract Demurrage is ICirclesDemurrageErrors {
contract Demurrage is ICirclesCompactErrors, ICirclesDemurrageErrors {
// Type declarations

/**
Expand Down Expand Up @@ -256,9 +256,10 @@ contract Demurrage is ICirclesDemurrageErrors {
R[_dayDifference] = demurrageFactor;
}
return demurrageFactor;
} else {
return Math64x64.pow(GAMMA_64x64, _dayDifference);
}
// if the day difference is for older than 14 days, calculate the value
// and do not cache it
return Math64x64.pow(GAMMA_64x64, _dayDifference);
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/circles/DiscountedBalances.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ contract DiscountedBalances is Demurrage {
DiscountedBalance memory discountedBalance = discountedBalances[_id][_account];
if (_day < discountedBalance.lastUpdatedDay) {
// DiscountedBalances: day is before last updated day
revert CirclesDemurrageDayBeforeLastUpdatedDay(_account, _id, _day, discountedBalance.lastUpdatedDay, 0);
// revert CirclesDemurrageDayBeforeLastUpdatedDay(_account, _id, _day, discountedBalance.lastUpdatedDay, 0);
revert CirclesErrorAddressUintArgs(_account, discountedBalance.lastUpdatedDay, 0xA0);
}
uint256 dayDifference;
unchecked {
Expand Down Expand Up @@ -93,7 +94,8 @@ contract DiscountedBalances is Demurrage {
function _updateBalance(address _account, uint256 _id, uint256 _balance, uint64 _day) internal {
if (_balance > MAX_VALUE) {
// DiscountedBalances: balance exceeds maximum value
revert CirclesDemurrageAmountExceedsMaxUint190(_account, _id, _balance, 0);
// revert CirclesDemurrageAmountExceedsMaxUint192(_account, _id, _balance, 0);
revert CirclesErrorAddressUintArgs(_account, _id, 0x81);
}
DiscountedBalance memory discountedBalance = discountedBalances[_id][_account];
discountedBalance.balance = uint192(_balance);
Expand All @@ -112,7 +114,8 @@ contract DiscountedBalances is Demurrage {
DiscountedBalance memory discountedBalance = discountedBalances[_id][_account];
if (_day < discountedBalance.lastUpdatedDay) {
// DiscountedBalances: day is before last updated day
revert CirclesDemurrageDayBeforeLastUpdatedDay(_account, _id, _day, discountedBalance.lastUpdatedDay, 1);
// revert CirclesDemurrageDayBeforeLastUpdatedDay(_account, _id, _day, discountedBalance.lastUpdatedDay, 1);
revert CirclesErrorAddressUintArgs(_account, discountedBalance.lastUpdatedDay, 0xA1);
}
uint256 dayDifference;
unchecked {
Expand All @@ -129,7 +132,8 @@ contract DiscountedBalances is Demurrage {
uint256 updatedBalance = discountedBalanceOnDay + _value;
if (updatedBalance > MAX_VALUE) {
// DiscountedBalances: balance exceeds maximum value
revert CirclesDemurrageAmountExceedsMaxUint190(_account, _id, updatedBalance, 1);
// revert CirclesDemurrageAmountExceedsMaxUint190(_account, _id, updatedBalance, 1);
revert CirclesErrorAddressUintArgs(_account, _id, 0x82);
}
discountedBalance.balance = uint192(updatedBalance);
discountedBalance.lastUpdatedDay = _day;
Expand Down
9 changes: 7 additions & 2 deletions src/circles/ERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,20 @@ abstract contract ERC1155 is DiscountedBalances, Context, ERC165, IERC1155, IERC
* Requirements:
*
* - `to` cannot be the zero address.
* - If `_doAcceptanceCheck` is true, it will perform ERC1155 acceptance check, otherwise only update
* - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
* acceptance magic value.
*/
function _mint(address to, uint256 id, uint256 value, bytes memory data) internal {
function _mint(address to, uint256 id, uint256 value, bytes memory data, bool _doAcceptanceCheck) internal {
if (to == address(0)) {
revert ERC1155InvalidReceiver(address(0));
}
(uint256[] memory ids, uint256[] memory values) = _asSingletonArrays(id, value);
_updateWithAcceptanceCheck(address(0), to, ids, values, data);
if (_doAcceptanceCheck) {
_updateWithAcceptanceCheck(address(0), to, ids, values, data);
} else {
_update(address(0), to, ids, values);
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/circles/InflationaryOperator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ contract InflationaryCirclesOperator is BatchedDemurrage {

constructor(IHubV2 _hub) {
hub = _hub;
// read inflation day zero from hub
inflationDayZero = hub.inflationDayZero();
}

// Public functions
Expand Down
Loading
Loading