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

Fix economic system bugs. #1705

Merged
merged 17 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
[Fix] Fix release profit method.
  • Loading branch information
EanCuznaivy committed May 15, 2019
commit 4a002228f43464204c0a907c75fad51dff8954c7
23 changes: 13 additions & 10 deletions src/AElf.Contracts.Profit/ProfitContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public override Hash CreateProfitItem(CreateProfitItemInput input)
{
profitId = Hash.FromTwoHashes(profitId, createdProfitIds.Last());
}

State.ProfitItemsMap[profitId] = new ProfitItem
{
VirtualAddress = Context.ConvertVirtualAddressToContractAddress(profitId),
Expand All @@ -75,7 +76,7 @@ public override Hash CreateProfitItem(CreateProfitItemInput input)
}

State.CreatedProfitItemsMap[Context.Sender] = createdProfitItems;

Context.LogDebug(() => $"Created profit item {profitId}");
return profitId;
}
Expand Down Expand Up @@ -145,7 +146,7 @@ public override Empty AddWeight(AddWeightInput input)
{
return new Empty();
}

Assert(input.EndPeriod >= profitItem.CurrentPeriod, "Invalid end period.");

profitItem.TotalWeight += input.Weight;
Expand Down Expand Up @@ -296,15 +297,15 @@ public override Empty ReleaseProfit(ReleaseProfitInput input)

if (input.Period < 0 || profitItem.TotalWeight <= 0)
{
profitItem.CurrentPeriod = input.Period > 0 ? input.Period.Add(1) : profitItem.CurrentPeriod;

// Release to an address no one can receive profits.

if (input.Amount <= 0)
{
State.ProfitItemsMap[input.ProfitId] = profitItem;
return new Empty();
}

profitItem.CurrentPeriod = input.Period > 0 ? input.Period.Add(1) : profitItem.CurrentPeriod;

if (input.Period >= 0)
{
input.Period = -1;
Expand All @@ -320,15 +321,16 @@ public override Empty ReleaseProfit(ReleaseProfitInput input)
Amount = input.Amount,
Symbol = profitItem.TokenSymbol
});
profitItem.TotalAmount -= input.Amount;
profitItem.TotalAmount = profitItem.TotalAmount.Sub(input.Amount);
State.ProfitItemsMap[input.ProfitId] = profitItem;
return new Empty();
}

// Update current_period.
var releasingPeriod = profitItem.CurrentPeriod;

Assert(input.Period == releasingPeriod, $"Invalid period. When release profit item {input.ProfitId.ToHex()} of period {input.Period}");
Assert(input.Period == releasingPeriod,
$"Invalid period. When release profit item {input.ProfitId.ToHex()} of period {input.Period}. Current period is {releasingPeriod}");

var profitsReceivingVirtualAddress =
GetReleasedPeriodProfitsVirtualAddress(profitVirtualAddress, releasingPeriod);
Expand All @@ -353,7 +355,7 @@ public override Empty ReleaseProfit(ReleaseProfitInput input)
State.ReleasedProfitsMap[profitsReceivingVirtualAddress] = releasedProfitInformation;

// Start releasing.

var remainAmount = input.Amount;

foreach (var subProfitItem in profitItem.SubProfitItems)
Expand Down Expand Up @@ -482,7 +484,8 @@ public override Empty Profit(ProfitInput input)

var profitVirtualAddress = Context.ConvertVirtualAddressToContractAddress(input.ProfitId);

var availableDetails = profitDetails.Details.Where(d => d.LastProfitPeriod != profitItem.CurrentPeriod).ToList();
var availableDetails = profitDetails.Details.Where(d => d.LastProfitPeriod != profitItem.CurrentPeriod)
.ToList();

for (var i = 0; i < Math.Min(ProfitContractConsts.ProfitLimit, availableDetails.Count); i++)
{
Expand Down Expand Up @@ -526,4 +529,4 @@ public override Empty Profit(ProfitInput input)
}

}
}
}
3 changes: 2 additions & 1 deletion src/AElf.Contracts.Profit/ViewMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public override ReleasedProfitsInformation GetReleasedProfitsInformation(
{
var virtualAddress = Context.ConvertVirtualAddressToContractAddress(input.ProfitId);
var releasedProfitsVirtualAddress = GetReleasedPeriodProfitsVirtualAddress(virtualAddress, input.Period);
return State.ReleasedProfitsMap[releasedProfitsVirtualAddress];
return State.ReleasedProfitsMap[releasedProfitsVirtualAddress] ?? new ReleasedProfitsInformation
{ProfitsAmount = -1, TotalWeight = -1};
}

public override ProfitDetails GetProfitDetails(GetProfitDetailsInput input)
Expand Down