Remove remainder, particiant count from single mode

This commit is contained in:
nolash 2021-06-05 12:50:31 +02:00
parent 74ef57a6a7
commit b5421cdd4e
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
6 changed files with 8 additions and 68 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -6,17 +6,13 @@ contract DemurrageTokenSingleCap {
// Redistribution bit field, with associated shifts and masks // Redistribution bit field, with associated shifts and masks
// (Uses sub-byte boundaries) // (Uses sub-byte boundaries)
bytes32[] public redistributions; // uint1(isFractional) | uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period) bytes32[] public redistributions; // uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period)
uint8 constant shiftRedistributionPeriod = 0; uint8 constant shiftRedistributionPeriod = 0;
uint256 constant maskRedistributionPeriod = 0x00000000000000000000000000000000000000000000000000000000ffffffff; // (1 << 32) - 1 uint256 constant maskRedistributionPeriod = 0x00000000000000000000000000000000000000000000000000000000ffffffff; // (1 << 32) - 1
uint8 constant shiftRedistributionValue = 32; uint8 constant shiftRedistributionValue = 32;
uint256 constant maskRedistributionValue = 0x00000000000000000000000000000000000000ffffffffffffffffff00000000; // ((1 << 72) - 1) << 32 uint256 constant maskRedistributionValue = 0x00000000000000000000000000000000000000ffffffffffffffffff00000000; // ((1 << 72) - 1) << 32
uint8 constant shiftRedistributionParticipants = 104;
uint256 constant maskRedistributionParticipants = 0x00000000000000000000000000000fffffffff00000000000000000000000000; // ((1 << 36) - 1) << 104
uint8 constant shiftRedistributionDemurrage = 140; uint8 constant shiftRedistributionDemurrage = 140;
uint256 constant maskRedistributionDemurrage = 0x000000000000000000000000fffff00000000000000000000000000000000000; // ((1 << 20) - 1) << 140 uint256 constant maskRedistributionDemurrage = 0x000000000000000000000000fffff00000000000000000000000000000000000; // ((1 << 20) - 1) << 140
uint8 constant shiftRedistributionIsFractional = 255;
uint256 constant maskRedistributionIsFractional = 0x8000000000000000000000000000000000000000000000000000000000000000; // 1 << 255
// Account balances // Account balances
mapping (address => uint256) account; mapping (address => uint256) account;
@ -208,12 +204,11 @@ contract DemurrageTokenSingleCap {
} }
// Deserializes the redistribution word // Deserializes the redistribution word
// uint1(isFractional) | uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period) // uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period)
function toRedistribution(uint256 _participants, uint256 _demurrageModifierPpm, uint256 _value, uint256 _period) private pure returns(bytes32) { function toRedistribution(uint256 _participants, uint256 _demurrageModifierPpm, uint256 _value, uint256 _period) private pure returns(bytes32) {
bytes32 redistribution; bytes32 redistribution;
redistribution |= bytes32((_demurrageModifierPpm << shiftRedistributionDemurrage) & maskRedistributionDemurrage); redistribution |= bytes32((_demurrageModifierPpm << shiftRedistributionDemurrage) & maskRedistributionDemurrage);
redistribution |= bytes32((_participants << shiftRedistributionParticipants) & maskRedistributionParticipants);
redistribution |= bytes32((_value << shiftRedistributionValue) & maskRedistributionValue); redistribution |= bytes32((_value << shiftRedistributionValue) & maskRedistributionValue);
redistribution |= bytes32(_period & maskRedistributionPeriod); redistribution |= bytes32(_period & maskRedistributionPeriod);
return redistribution; return redistribution;
@ -229,11 +224,6 @@ contract DemurrageTokenSingleCap {
return (uint256(redistribution) & maskRedistributionValue) >> shiftRedistributionValue; return (uint256(redistribution) & maskRedistributionValue) >> shiftRedistributionValue;
} }
// Serializes the number of participants part of the redistribution word
function toRedistributionParticipants(bytes32 redistribution) public pure returns (uint256) {
return (uint256(redistribution) & maskRedistributionParticipants) >> shiftRedistributionParticipants;
}
// Serializes the number of participants part of the redistribution word // Serializes the number of participants part of the redistribution word
function toRedistributionDemurrageModifier(bytes32 redistribution) public pure returns (uint256) { function toRedistributionDemurrageModifier(bytes32 redistribution) public pure returns (uint256) {
return (uint256(redistribution) & maskRedistributionDemurrage) >> shiftRedistributionDemurrage; return (uint256(redistribution) & maskRedistributionDemurrage) >> shiftRedistributionDemurrage;
@ -244,23 +234,6 @@ contract DemurrageTokenSingleCap {
return redistributions.length; return redistributions.length;
} }
// Add number of participants for the current redistribution period by one
function incrementRedistributionParticipants() private returns (bool) {
bytes32 currentRedistribution;
uint256 tmpRedistribution;
uint256 participants;
currentRedistribution = redistributions[redistributions.length-1];
participants = toRedistributionParticipants(currentRedistribution) + 1;
tmpRedistribution = uint256(currentRedistribution);
tmpRedistribution &= (~maskRedistributionParticipants);
tmpRedistribution |= ((participants << shiftRedistributionParticipants) & maskRedistributionParticipants);
redistributions[redistributions.length-1] = bytes32(tmpRedistribution);
return true;
}
// Save the current total supply amount to the current redistribution period // Save the current total supply amount to the current redistribution period
function saveRedistributionSupply() private returns (bool) { function saveRedistributionSupply() private returns (bool) {
uint256 currentRedistribution; uint256 currentRedistribution;
@ -305,8 +278,6 @@ contract DemurrageTokenSingleCap {
if (truncatedResult < redistributionSupply) { if (truncatedResult < redistributionSupply) {
redistributionPeriod = toRedistributionPeriod(_redistribution); // since we reuse period here, can possibly be optimized by passing period instead redistributionPeriod = toRedistributionPeriod(_redistribution); // since we reuse period here, can possibly be optimized by passing period instead
redistributions[redistributionPeriod-1] &= bytes32(~maskRedistributionParticipants); // just to be safe, zero out all participant count data, in this case there will be only one
redistributions[redistributionPeriod-1] |= bytes32(maskRedistributionIsFractional | (1 << shiftRedistributionParticipants));
} }
increaseBaseBalance(sinkAddress, unit / ppmDivider); increaseBaseBalance(sinkAddress, unit / ppmDivider);
@ -347,7 +318,6 @@ contract DemurrageTokenSingleCap {
bytes32 currentRedistribution; bytes32 currentRedistribution;
bytes32 nextRedistribution; bytes32 nextRedistribution;
uint256 currentPeriod; uint256 currentPeriod;
uint256 currentParticipants;
uint256 currentDemurrageAmount; uint256 currentDemurrageAmount;
uint256 nextRedistributionDemurrage; uint256 nextRedistributionDemurrage;
uint256 demurrageCounts; uint256 demurrageCounts;

View File

@ -6,17 +6,13 @@ contract DemurrageTokenSingleNocap {
// Redistribution bit field, with associated shifts and masks // Redistribution bit field, with associated shifts and masks
// (Uses sub-byte boundaries) // (Uses sub-byte boundaries)
bytes32[] public redistributions; // uint1(isFractional) | uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period) bytes32[] public redistributions; // uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period)
uint8 constant shiftRedistributionPeriod = 0; uint8 constant shiftRedistributionPeriod = 0;
uint256 constant maskRedistributionPeriod = 0x00000000000000000000000000000000000000000000000000000000ffffffff; // (1 << 32) - 1 uint256 constant maskRedistributionPeriod = 0x00000000000000000000000000000000000000000000000000000000ffffffff; // (1 << 32) - 1
uint8 constant shiftRedistributionValue = 32; uint8 constant shiftRedistributionValue = 32;
uint256 constant maskRedistributionValue = 0x00000000000000000000000000000000000000ffffffffffffffffff00000000; // ((1 << 72) - 1) << 32 uint256 constant maskRedistributionValue = 0x00000000000000000000000000000000000000ffffffffffffffffff00000000; // ((1 << 72) - 1) << 32
uint8 constant shiftRedistributionParticipants = 104;
uint256 constant maskRedistributionParticipants = 0x00000000000000000000000000000fffffffff00000000000000000000000000; // ((1 << 36) - 1) << 104
uint8 constant shiftRedistributionDemurrage = 140; uint8 constant shiftRedistributionDemurrage = 140;
uint256 constant maskRedistributionDemurrage = 0x000000000000000000000000fffff00000000000000000000000000000000000; // ((1 << 20) - 1) << 140 uint256 constant maskRedistributionDemurrage = 0x000000000000000000000000fffff00000000000000000000000000000000000; // ((1 << 20) - 1) << 140
uint8 constant shiftRedistributionIsFractional = 255;
uint256 constant maskRedistributionIsFractional = 0x8000000000000000000000000000000000000000000000000000000000000000; // 1 << 255
// Account balances // Account balances
mapping (address => uint256) account; mapping (address => uint256) account;
@ -203,12 +199,11 @@ contract DemurrageTokenSingleNocap {
} }
// Deserializes the redistribution word // Deserializes the redistribution word
// uint1(isFractional) | uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period) // uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period)
function toRedistribution(uint256 _participants, uint256 _demurrageModifierPpm, uint256 _value, uint256 _period) private pure returns(bytes32) { function toRedistribution(uint256 _participants, uint256 _demurrageModifierPpm, uint256 _value, uint256 _period) private pure returns(bytes32) {
bytes32 redistribution; bytes32 redistribution;
redistribution |= bytes32((_demurrageModifierPpm << shiftRedistributionDemurrage) & maskRedistributionDemurrage); redistribution |= bytes32((_demurrageModifierPpm << shiftRedistributionDemurrage) & maskRedistributionDemurrage);
redistribution |= bytes32((_participants << shiftRedistributionParticipants) & maskRedistributionParticipants);
redistribution |= bytes32((_value << shiftRedistributionValue) & maskRedistributionValue); redistribution |= bytes32((_value << shiftRedistributionValue) & maskRedistributionValue);
redistribution |= bytes32(_period & maskRedistributionPeriod); redistribution |= bytes32(_period & maskRedistributionPeriod);
return redistribution; return redistribution;
@ -224,11 +219,6 @@ contract DemurrageTokenSingleNocap {
return (uint256(redistribution) & maskRedistributionValue) >> shiftRedistributionValue; return (uint256(redistribution) & maskRedistributionValue) >> shiftRedistributionValue;
} }
// Serializes the number of participants part of the redistribution word
function toRedistributionParticipants(bytes32 redistribution) public pure returns (uint256) {
return (uint256(redistribution) & maskRedistributionParticipants) >> shiftRedistributionParticipants;
}
// Serializes the number of participants part of the redistribution word // Serializes the number of participants part of the redistribution word
function toRedistributionDemurrageModifier(bytes32 redistribution) public pure returns (uint256) { function toRedistributionDemurrageModifier(bytes32 redistribution) public pure returns (uint256) {
return (uint256(redistribution) & maskRedistributionDemurrage) >> shiftRedistributionDemurrage; return (uint256(redistribution) & maskRedistributionDemurrage) >> shiftRedistributionDemurrage;
@ -239,23 +229,6 @@ contract DemurrageTokenSingleNocap {
return redistributions.length; return redistributions.length;
} }
// Add number of participants for the current redistribution period by one
function incrementRedistributionParticipants() private returns (bool) {
bytes32 currentRedistribution;
uint256 tmpRedistribution;
uint256 participants;
currentRedistribution = redistributions[redistributions.length-1];
participants = toRedistributionParticipants(currentRedistribution) + 1;
tmpRedistribution = uint256(currentRedistribution);
tmpRedistribution &= (~maskRedistributionParticipants);
tmpRedistribution |= ((participants << shiftRedistributionParticipants) & maskRedistributionParticipants);
redistributions[redistributions.length-1] = bytes32(tmpRedistribution);
return true;
}
// Save the current total supply amount to the current redistribution period // Save the current total supply amount to the current redistribution period
function saveRedistributionSupply() private returns (bool) { function saveRedistributionSupply() private returns (bool) {
uint256 currentRedistribution; uint256 currentRedistribution;
@ -300,8 +273,6 @@ contract DemurrageTokenSingleNocap {
if (truncatedResult < redistributionSupply) { if (truncatedResult < redistributionSupply) {
redistributionPeriod = toRedistributionPeriod(_redistribution); // since we reuse period here, can possibly be optimized by passing period instead redistributionPeriod = toRedistributionPeriod(_redistribution); // since we reuse period here, can possibly be optimized by passing period instead
redistributions[redistributionPeriod-1] &= bytes32(~maskRedistributionParticipants); // just to be safe, zero out all participant count data, in this case there will be only one
redistributions[redistributionPeriod-1] |= bytes32(maskRedistributionIsFractional | (1 << shiftRedistributionParticipants));
} }
increaseBaseBalance(sinkAddress, unit / ppmDivider); increaseBaseBalance(sinkAddress, unit / ppmDivider);
@ -342,7 +313,6 @@ contract DemurrageTokenSingleNocap {
bytes32 currentRedistribution; bytes32 currentRedistribution;
bytes32 nextRedistribution; bytes32 nextRedistribution;
uint256 currentPeriod; uint256 currentPeriod;
uint256 currentParticipants;
uint256 currentDemurrageAmount; uint256 currentDemurrageAmount;
uint256 nextRedistributionDemurrage; uint256 nextRedistributionDemurrage;
uint256 demurrageCounts; uint256 demurrageCounts;