Skip to content

Commit

Permalink
Merge pull request #181 from skalenetwork/enhancement/SKALE-2360-over…
Browse files Browse the repository at this point in the history
…loading

Enhancement/skale 2360 overloading
  • Loading branch information
DimaStebaev authored Apr 27, 2020
2 parents b523bd7 + 0fab949 commit c5e651b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 50 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.2
1.1.3
48 changes: 20 additions & 28 deletions contracts/delegation/DelegationController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ contract DelegationController is Permissions, ILocker {
function getAndUpdateEffectiveDelegatedByHolderToValidator(address holder, uint validatorId, uint month) external
allow("Distributor") returns (uint effectiveDelegated)
{
SlashingSignal[] memory slashingSignals = processSlashesWithoutSignals(holder);
effectiveDelegated = _effectiveDelegatedByHolderToValidator[holder][validatorId].getAndUpdateValue(month);
SlashingSignal[] memory slashingSignals = processAllSlashesWithoutSignals(holder);
effectiveDelegated = _effectiveDelegatedByHolderToValidator[holder][validatorId].getAndUpdateValueInSequence(month);
sendSlashingSignals(slashingSignals);
}

Expand Down Expand Up @@ -205,7 +205,7 @@ contract DelegationController is Permissions, ILocker {
delegationPeriodManager.isDelegationPeriodAllowed(delegationPeriod),
"This delegation period is not allowed");

SlashingSignal[] memory slashingSignals = processSlashesWithoutSignals(msg.sender);
SlashingSignal[] memory slashingSignals = processAllSlashesWithoutSignals(msg.sender);

uint delegationId = addDelegation(
msg.sender,
Expand Down Expand Up @@ -254,7 +254,7 @@ contract DelegationController is Permissions, ILocker {
DelegationPeriodManager delegationPeriodManager = DelegationPeriodManager(contractManager.getContract("DelegationPeriodManager"));
TokenLaunchLocker tokenLaunchLocker = TokenLaunchLocker(contractManager.getContract("TokenLaunchLocker"));

SlashingSignal[] memory slashingSignals = processSlashesWithoutSignals(delegations[delegationId].holder);
SlashingSignal[] memory slashingSignals = processAllSlashesWithoutSignals(delegations[delegationId].holder);

uint currentMonth = timeHelpers.getCurrentMonth();
delegations[delegationId].started = currentMonth.add(1);
Expand Down Expand Up @@ -354,8 +354,8 @@ contract DelegationController is Permissions, ILocker {

function confiscate(uint validatorId, uint amount) external allow("Punisher") {
uint currentMonth = getCurrentMonth();
FractionUtils.Fraction memory coefficient = _delegatedToValidator[validatorId].reduce(amount, currentMonth);
_effectiveDelegatedToValidator[validatorId].reduce(coefficient, currentMonth);
FractionUtils.Fraction memory coefficient = _delegatedToValidator[validatorId].reduceValue(amount, currentMonth);
_effectiveDelegatedToValidator[validatorId].reduceSequence(coefficient, currentMonth);
putToSlashingLog(_slashesOfValidator[validatorId], coefficient, currentMonth);
_slashes.push(SlashingEvent({reducingCoefficient: coefficient, validatorId: validatorId, month: currentMonth}));
}
Expand All @@ -367,7 +367,7 @@ contract DelegationController is Permissions, ILocker {
function getAndUpdateEffectiveDelegatedToValidator(uint validatorId, uint month)
external allow("Distributor") returns (uint)
{
return _effectiveDelegatedToValidator[validatorId].getAndUpdateValue(month);
return _effectiveDelegatedToValidator[validatorId].getAndUpdateValueInSequence(month);
}

function getDelegationsByValidatorLength(uint validatorId) external view returns (uint) {
Expand Down Expand Up @@ -490,31 +490,31 @@ contract DelegationController is Permissions, ILocker {
}

function addToDelegatedToValidator(uint validatorId, uint amount, uint month) internal {
_delegatedToValidator[validatorId].add(amount, month);
_delegatedToValidator[validatorId].addToValue(amount, month);
}

function addToEffectiveDelegatedToValidator(uint validatorId, uint effectiveAmount, uint month) internal {
_effectiveDelegatedToValidator[validatorId].add(effectiveAmount, month);
_effectiveDelegatedToValidator[validatorId].addToSequence(effectiveAmount, month);
}

function addToDelegatedByHolder(address holder, uint amount, uint month) internal {
_delegatedByHolder[holder].add(amount, month);
_delegatedByHolder[holder].addToValue(amount, month);
}

function addToDelegatedByHolderToValidator(
address holder, uint validatorId, uint amount, uint month) internal
{
_delegatedByHolderToValidator[holder][validatorId].add(amount, month);
_delegatedByHolderToValidator[holder][validatorId].addToValue(amount, month);
}

function removeFromDelegatedByHolder(address holder, uint amount, uint month) internal {
_delegatedByHolder[holder].subtract(amount, month);
_delegatedByHolder[holder].subtractFromValue(amount, month);
}

function removeFromDelegatedByHolderToValidator(
address holder, uint validatorId, uint amount, uint month) internal
{
_delegatedByHolderToValidator[holder][validatorId].subtract(amount, month);
_delegatedByHolderToValidator[holder][validatorId].subtractFromValue(amount, month);
}

function addToEffectiveDelegatedByHolderToValidator(
Expand All @@ -524,7 +524,7 @@ contract DelegationController is Permissions, ILocker {
uint month)
internal
{
_effectiveDelegatedByHolderToValidator[holder][validatorId].add(effectiveAmount, month);
_effectiveDelegatedByHolderToValidator[holder][validatorId].addToSequence(effectiveAmount, month);
}

function removeFromEffectiveDelegatedByHolderToValidator(
Expand All @@ -534,7 +534,7 @@ contract DelegationController is Permissions, ILocker {
uint month)
internal
{
_effectiveDelegatedByHolderToValidator[holder][validatorId].subtract(effectiveAmount, month);
_effectiveDelegatedByHolderToValidator[holder][validatorId].subtractFromSequence(effectiveAmount, month);
}

function getAndUpdateDelegatedByHolder(address holder) internal returns (uint) {
Expand Down Expand Up @@ -574,14 +574,6 @@ contract DelegationController is Permissions, ILocker {
return getAndUpdateDelegatedByHolder(wallet).add(getLockedInPendingDelegations(wallet));
}

function min(uint a, uint b) internal pure returns (uint) {
if (a < b) {
return a;
} else {
return b;
}
}

function updateFirstDelegationMonth(address holder, uint validatorId, uint month) internal {
if (_firstDelegationMonth[holder].value == 0) {
_firstDelegationMonth[holder].value = month;
Expand All @@ -597,11 +589,11 @@ contract DelegationController is Permissions, ILocker {
}

function removeFromDelegatedToValidator(uint validatorId, uint amount, uint month) internal {
_delegatedToValidator[validatorId].subtract(amount, month);
_delegatedToValidator[validatorId].subtractFromValue(amount, month);
}

function removeFromEffectiveDelegatedToValidator(uint validatorId, uint effectiveAmount, uint month) internal {
_effectiveDelegatedToValidator[validatorId].subtract(effectiveAmount, month);
_effectiveDelegatedToValidator[validatorId].subtractFromSequence(effectiveAmount, month);
}

function calculateDelegationAmountAfterSlashing(uint delegationId) internal view returns (uint) {
Expand Down Expand Up @@ -655,11 +647,11 @@ contract DelegationController is Permissions, ILocker {
uint month = _slashes[index].month;
uint oldValue = getAndUpdateDelegatedByHolderToValidator(holder, validatorId, month);
if (oldValue.muchGreater(0)) {
_delegatedByHolderToValidator[holder][validatorId].reduce(
_delegatedByHolderToValidator[holder][validatorId].reduceValueByCoefficientAndUpdateSum(
_delegatedByHolder[holder],
_slashes[index].reducingCoefficient,
month);
_effectiveDelegatedByHolderToValidator[holder][validatorId].reduce(
_effectiveDelegatedByHolderToValidator[holder][validatorId].reduceSequence(
_slashes[index].reducingCoefficient,
month);
slashingSignals[index.sub(begin)].holder = holder;
Expand All @@ -670,7 +662,7 @@ contract DelegationController is Permissions, ILocker {
}
}

function processSlashesWithoutSignals(address holder) internal returns (SlashingSignal[] memory slashingSignals) {
function processAllSlashesWithoutSignals(address holder) internal returns (SlashingSignal[] memory slashingSignals) {
return processSlashesWithoutSignals(holder, 0);
}

Expand Down
30 changes: 15 additions & 15 deletions contracts/delegation/PartialDifferences.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ library PartialDifferences {

// functions for sequence

function add(Sequence storage sequence, uint diff, uint month) internal {
function addToSequence(Sequence storage sequence, uint diff, uint month) internal {
require(sequence.firstUnprocessedMonth <= month, "Cannot add to the past");
if (sequence.firstUnprocessedMonth == 0) {
sequence.firstUnprocessedMonth = month;
Expand All @@ -63,7 +63,7 @@ library PartialDifferences {
}
}

function subtract(Sequence storage sequence, uint diff, uint month) internal {
function subtractFromSequence(Sequence storage sequence, uint diff, uint month) internal {
require(sequence.firstUnprocessedMonth <= month, "Cannot subtract from the past");
if (sequence.firstUnprocessedMonth == 0) {
sequence.firstUnprocessedMonth = month;
Expand All @@ -74,7 +74,7 @@ library PartialDifferences {
}
}

function getAndUpdateValue(Sequence storage sequence, uint month) internal returns (uint) {
function getAndUpdateValueInSequence(Sequence storage sequence, uint month) internal returns (uint) {
if (sequence.firstUnprocessedMonth == 0) {
return 0;
}
Expand All @@ -98,7 +98,7 @@ library PartialDifferences {
return sequence.value[month];
}

function reduce(
function reduceSequence(
Sequence storage sequence,
FractionUtils.Fraction memory reducingCoefficient,
uint month) internal
Expand All @@ -108,7 +108,7 @@ library PartialDifferences {
if (sequence.firstUnprocessedMonth == 0) {
return;
}
uint value = getAndUpdateValue(sequence, month);
uint value = getAndUpdateValueInSequence(sequence, month);
if (value.approximatelyEqual(0)) {
return;
}
Expand All @@ -122,7 +122,7 @@ library PartialDifferences {

// functions for value

function add(Value storage sequence, uint diff, uint month) internal {
function addToValue(Value storage sequence, uint diff, uint month) internal {
require(sequence.firstUnprocessedMonth <= month, "Cannot add to the past");
if (sequence.firstUnprocessedMonth == 0) {
sequence.firstUnprocessedMonth = month;
Expand All @@ -139,7 +139,7 @@ library PartialDifferences {
}
}

function subtract(Value storage sequence, uint diff, uint month) internal {
function subtractFromValue(Value storage sequence, uint diff, uint month) internal {
require(sequence.firstUnprocessedMonth <= month.add(1), "Cannot subtract from the past");
if (sequence.firstUnprocessedMonth == 0) {
sequence.firstUnprocessedMonth = month;
Expand Down Expand Up @@ -181,7 +181,7 @@ library PartialDifferences {
return sequence.value;
}

function reduce(Value storage sequence, uint amount, uint month) internal returns (FractionUtils.Fraction memory) {
function reduceValue(Value storage sequence, uint amount, uint month) internal returns (FractionUtils.Fraction memory) {
require(month.add(1) >= sequence.firstUnprocessedMonth, "Cannot reduce value in the past");
if (sequence.firstUnprocessedMonth == 0) {
return FractionUtils.createFraction(0);
Expand All @@ -197,34 +197,34 @@ library PartialDifferences {
}

FractionUtils.Fraction memory reducingCoefficient = FractionUtils.createFraction(value.boundedSub(_amount), value);
reduce(sequence, reducingCoefficient, month);
reduceValueByCoefficient(sequence, reducingCoefficient, month);
return reducingCoefficient;
}

function reduce(Value storage sequence, FractionUtils.Fraction memory reducingCoefficient, uint month) internal {
reduce(
function reduceValueByCoefficient(Value storage sequence, FractionUtils.Fraction memory reducingCoefficient, uint month) internal {
reduceValueByCoefficientAndUpdateSumIfNeeded(
sequence,
sequence,
reducingCoefficient,
month,
false);
}

function reduce(
function reduceValueByCoefficientAndUpdateSum(
Value storage sequence,
Value storage sumSequence,
FractionUtils.Fraction memory reducingCoefficient,
uint month) internal
{
reduce(
reduceValueByCoefficientAndUpdateSumIfNeeded(
sequence,
sumSequence,
reducingCoefficient,
month,
true);
}

function reduce(
function reduceValueByCoefficientAndUpdateSumIfNeeded(
Value storage sequence,
Value storage sumSequence,
FractionUtils.Fraction memory reducingCoefficient,
Expand All @@ -246,7 +246,7 @@ library PartialDifferences {

uint newValue = sequence.value.mul(reducingCoefficient.numerator).div(reducingCoefficient.denominator);
if (hasSumSequence) {
subtract(sumSequence, sequence.value.boundedSub(newValue), month);
subtractFromValue(sumSequence, sequence.value.boundedSub(newValue), month);
}
sequence.value = newValue;

Expand Down
4 changes: 2 additions & 2 deletions contracts/delegation/TokenLaunchLocker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ contract TokenLaunchLocker is Permissions, ILocker {
}

function addToDelegatedAmount(address holder, uint amount, uint month) internal {
_delegatedAmount[holder].add(amount, month);
_delegatedAmount[holder].addToValue(amount, month);
}

function removeFromDelegatedAmount(address holder, uint amount, uint month) internal {
_delegatedAmount[holder].subtract(amount, month);
_delegatedAmount[holder].subtractFromValue(amount, month);
}

function addToTotalDelegatedAmount(address holder, uint amount, uint month) internal {
Expand Down
8 changes: 4 additions & 4 deletions contracts/test/PartialDifferencesTester.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ contract PartialDifferencesTester {

function addToSequence(uint sequence, uint diff, uint month) external {
require(sequence < sequences.length, "Sequence does not exist");
sequences[sequence].add(diff, month);
sequences[sequence].addToSequence(diff, month);
}

function subtractFromSequence(uint sequence, uint diff, uint month) external {
require(sequence < sequences.length, "Sequence does not exist");
sequences[sequence].subtract(diff, month);
sequences[sequence].subtractFromSequence(diff, month);
}

function getAndUpdateSequenceItem(uint sequence, uint month) external returns (uint) {
require(sequence < sequences.length, "Sequence does not exist");
return sequences[sequence].getAndUpdateValue(month);
return sequences[sequence].getAndUpdateValueInSequence(month);
}

function reduceSequence(
Expand All @@ -62,6 +62,6 @@ contract PartialDifferencesTester {
{
require(sequence < sequences.length, "Sequence does not exist");
FractionUtils.Fraction memory reducingCoefficient = FractionUtils.createFraction(a, b);
return sequences[sequence].reduce(reducingCoefficient, month);
return sequences[sequence].reduceSequence(reducingCoefficient, month);
}
}

0 comments on commit c5e651b

Please sign in to comment.