From 4a002228f43464204c0a907c75fad51dff8954c7 Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 11:51:35 +0800 Subject: [PATCH 01/16] [Fix] Fix release profit method. --- src/AElf.Contracts.Profit/ProfitContract.cs | 23 ++++++++++++--------- src/AElf.Contracts.Profit/ViewMethods.cs | 3 ++- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/AElf.Contracts.Profit/ProfitContract.cs b/src/AElf.Contracts.Profit/ProfitContract.cs index 2c7b891307..f51887647d 100644 --- a/src/AElf.Contracts.Profit/ProfitContract.cs +++ b/src/AElf.Contracts.Profit/ProfitContract.cs @@ -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), @@ -75,7 +76,7 @@ public override Hash CreateProfitItem(CreateProfitItemInput input) } State.CreatedProfitItemsMap[Context.Sender] = createdProfitItems; - + Context.LogDebug(() => $"Created profit item {profitId}"); return profitId; } @@ -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; @@ -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; @@ -320,7 +321,7 @@ 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(); } @@ -328,7 +329,8 @@ public override Empty ReleaseProfit(ReleaseProfitInput input) // 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); @@ -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) @@ -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++) { @@ -526,4 +529,4 @@ public override Empty Profit(ProfitInput input) } } -} +} \ No newline at end of file diff --git a/src/AElf.Contracts.Profit/ViewMethods.cs b/src/AElf.Contracts.Profit/ViewMethods.cs index 726fab9584..39273eb4e9 100644 --- a/src/AElf.Contracts.Profit/ViewMethods.cs +++ b/src/AElf.Contracts.Profit/ViewMethods.cs @@ -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) From ac4c54da85a12e72ed111907aad660c1ee1da542 Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 12:04:35 +0800 Subject: [PATCH 02/16] [Fix] Fix GetMinersCount. --- src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs b/src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs index 12d7ac4ec3..12ed0fd573 100644 --- a/src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs +++ b/src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs @@ -621,8 +621,15 @@ private string GetNextAvailableMinerPublicKey(Round round) private int GetMinersCount() { - return ((int) Context.CurrentBlockTime.Subtract(State.BlockchainStartTimestamp.Value.ToDateTime()) - .TotalDays).Div(7); + if (TryToGetRoundInformation(1, out var firstRound)) + { + var initialMinersCount = firstRound.RealTimeMinersInformation.Count; + return initialMinersCount.Add(((int) Context.CurrentBlockTime + .Subtract(State.BlockchainStartTimestamp.Value.ToDateTime()) + .TotalDays).Div(State.TimeEachTerm.Value)); + } + + return 0; } } } \ No newline at end of file From 724cd33eeea3244fd60a487f10622e374e4ef154 Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 12:06:06 +0800 Subject: [PATCH 03/16] [Fix] Fix GetMinersCount. --- src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs b/src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs index 12ed0fd573..645f7223b1 100644 --- a/src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs +++ b/src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs @@ -626,7 +626,7 @@ private int GetMinersCount() var initialMinersCount = firstRound.RealTimeMinersInformation.Count; return initialMinersCount.Add(((int) Context.CurrentBlockTime .Subtract(State.BlockchainStartTimestamp.Value.ToDateTime()) - .TotalDays).Div(State.TimeEachTerm.Value)); + .TotalDays).Div(365)); } return 0; From f00930adbf479267308d62d0af24a0294c0c2d1c Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 13:46:48 +0800 Subject: [PATCH 04/16] [Fix] Recover. --- src/AElf.OS/Jobs/BlockSyncJob.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/AElf.OS/Jobs/BlockSyncJob.cs b/src/AElf.OS/Jobs/BlockSyncJob.cs index 38862a5971..5c2b32304b 100644 --- a/src/AElf.OS/Jobs/BlockSyncJob.cs +++ b/src/AElf.OS/Jobs/BlockSyncJob.cs @@ -17,7 +17,6 @@ public class BlockSyncJob { private const long InitialSyncLimit = 10; private const int BlockSyncJobLimit = 200; - private const int BlockSyncWaitTime = 5000; private readonly IBlockchainService _blockchainService; private readonly INetworkService _networkService; @@ -84,8 +83,7 @@ public async Task ExecuteAsync(BlockSyncJobArgs args) if (chain.LongestChainHeight < blockHeight - BlockSyncJobLimit) { Logger.LogWarning($"Pause sync task and wait for synced block to be processed, best chain height: {chain.BestChainHeight}"); - await Task.Delay(BlockSyncWaitTime); - continue; + break; } Logger.LogDebug($"Request blocks start with {blockHash}"); From 371dc8f116fa29ff0893b35087489b75cc6e7e0d Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 14:58:32 +0800 Subject: [PATCH 05/16] [Fix] Fix a test case about releasing profits. --- .../BVT/TreasuryTests.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/AElf.Contracts.Election.Tests/BVT/TreasuryTests.cs b/test/AElf.Contracts.Election.Tests/BVT/TreasuryTests.cs index e40f82a6a0..a3c9964caf 100644 --- a/test/AElf.Contracts.Election.Tests/BVT/TreasuryTests.cs +++ b/test/AElf.Contracts.Election.Tests/BVT/TreasuryTests.cs @@ -147,8 +147,8 @@ public async Task> ElectionContract_ReleaseTreasuryProfits_Relea ProfitId = ProfitItemsIds[ProfitType.CitizenWelfare], Period = 1 }); - releasedProfitsInformation.TotalWeight.ShouldBe(0); - releasedProfitsInformation.ProfitsAmount.ShouldBe(0); + releasedProfitsInformation.TotalWeight.ShouldBe(-1); + releasedProfitsInformation.ProfitsAmount.ShouldBe(-1); releasedProfitsInformation.IsReleased.ShouldBe(false); // Check balance of virtual address. @@ -190,8 +190,8 @@ await ProfitContractStub.GetReleasedProfitsInformation.CallAsync( ProfitId = ProfitItemsIds[ProfitType.BasicMinerReward], Period = 1 }); - releasedProfitsInformation.TotalWeight.ShouldBe(0); - releasedProfitsInformation.ProfitsAmount.ShouldBe(0); + releasedProfitsInformation.TotalWeight.ShouldBe(-1); + releasedProfitsInformation.ProfitsAmount.ShouldBe(-1); releasedProfitsInformation.IsReleased.ShouldBe(false); } @@ -234,8 +234,8 @@ await ProfitContractStub.GetReleasedProfitsInformation.CallAsync( ProfitId = ProfitItemsIds[ProfitType.VotesWeightReward], Period = 1 }); - releasedProfitsInformation.TotalWeight.ShouldBe(0); - releasedProfitsInformation.ProfitsAmount.ShouldBe(0); + releasedProfitsInformation.TotalWeight.ShouldBe(-1); + releasedProfitsInformation.ProfitsAmount.ShouldBe(-1); releasedProfitsInformation.IsReleased.ShouldBe(false); } @@ -277,8 +277,8 @@ await ProfitContractStub.GetReleasedProfitsInformation.CallAsync( ProfitId = ProfitItemsIds[ProfitType.ReElectionReward], Period = 1 }); - releasedProfitsInformation.TotalWeight.ShouldBe(0); - releasedProfitsInformation.ProfitsAmount.ShouldBe(0); + releasedProfitsInformation.TotalWeight.ShouldBe(-1); + releasedProfitsInformation.ProfitsAmount.ShouldBe(-1); releasedProfitsInformation.IsReleased.ShouldBe(false); } @@ -291,8 +291,8 @@ await ProfitContractStub.GetReleasedProfitsInformation.CallAsync( ProfitId = ProfitItemsIds[ProfitType.ReElectionReward], Period = 1 }); - releasedProfitsInformation.TotalWeight.ShouldBe(0); - releasedProfitsInformation.ProfitsAmount.ShouldBe(0); + releasedProfitsInformation.TotalWeight.ShouldBe(-1); + releasedProfitsInformation.ProfitsAmount.ShouldBe(-1); releasedProfitsInformation.IsReleased.ShouldBe(false); } From d0b47a74dd640a2a43b7742a3c012d1cbf538ccf Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 15:01:32 +0800 Subject: [PATCH 06/16] [Fix] Fix getting miners count. --- src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs b/src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs index 645f7223b1..71690583cc 100644 --- a/src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs +++ b/src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs @@ -623,10 +623,11 @@ private int GetMinersCount() { if (TryToGetRoundInformation(1, out var firstRound)) { + // TODO: Maybe this should according to date, like every July 1st we increase 2 miners. var initialMinersCount = firstRound.RealTimeMinersInformation.Count; return initialMinersCount.Add(((int) Context.CurrentBlockTime .Subtract(State.BlockchainStartTimestamp.Value.ToDateTime()) - .TotalDays).Div(365)); + .TotalDays).Div(365).Mul(2)); } return 0; From 201176e6972b5b28595bac90d855bffee1dcacb2 Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 16:33:16 +0800 Subject: [PATCH 07/16] [Fix] Add logs to debug. --- src/AElf.Contracts.Profit/ProfitContract.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/AElf.Contracts.Profit/ProfitContract.cs b/src/AElf.Contracts.Profit/ProfitContract.cs index f51887647d..293e6ad237 100644 --- a/src/AElf.Contracts.Profit/ProfitContract.cs +++ b/src/AElf.Contracts.Profit/ProfitContract.cs @@ -182,6 +182,8 @@ public override Empty AddWeight(AddWeightInput input) } State.ProfitDetailsMap[profitId][input.Receiver] = currentProfitDetails; + + Context.LogDebug(() => $"Add {input.Weight} weights to profit item {input.ProfitId.ToHex()}"); return new Empty(); } @@ -354,6 +356,10 @@ public override Empty ReleaseProfit(ReleaseProfitInput input) State.ReleasedProfitsMap[profitsReceivingVirtualAddress] = releasedProfitInformation; + Context.LogDebug(() => + $"Released profit information of {input.ProfitId.ToHex()} in period {input.Period}, " + + $"total weight {releasedProfitInformation.TotalWeight}, total amount {releasedProfitInformation.ProfitsAmount}"); + // Start releasing. var remainAmount = input.Amount; @@ -527,6 +533,5 @@ public override Empty Profit(ProfitInput input) return new Empty(); } - } } \ No newline at end of file From 637e0dcaa9fa615e9fd76bcbb51b3ea2f9c4cd36 Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 16:35:59 +0800 Subject: [PATCH 08/16] [Fix] Add logs to debug. --- src/AElf.Contracts.Profit/ProfitContract.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/AElf.Contracts.Profit/ProfitContract.cs b/src/AElf.Contracts.Profit/ProfitContract.cs index 293e6ad237..7029064ab5 100644 --- a/src/AElf.Contracts.Profit/ProfitContract.cs +++ b/src/AElf.Contracts.Profit/ProfitContract.cs @@ -511,15 +511,17 @@ public override Empty Profit(ProfitInput input) var releasedProfitsVirtualAddress = GetReleasedPeriodProfitsVirtualAddress(profitVirtualAddress, period); var releasedProfitsInformation = State.ReleasedProfitsMap[releasedProfitsVirtualAddress]; - if (releasedProfitsInformation.IsReleased) + var amount = profitDetail.Weight.Mul(releasedProfitsInformation.ProfitsAmount) + .Div(releasedProfitsInformation.TotalWeight); + Context.LogDebug(() => $"{Context.Sender} is profiting {amount} tokens from {input.ProfitId.ToHex()} in period {profitItem.CurrentPeriod}"); + if (releasedProfitsInformation.IsReleased && amount > 0) { State.TokenContract.TransferFrom.Send(new TransferFromInput { From = releasedProfitsVirtualAddress, To = Context.Sender, Symbol = profitItem.TokenSymbol, - Amount = profitDetail.Weight.Mul(releasedProfitsInformation.ProfitsAmount) - .Div(releasedProfitsInformation.TotalWeight) + Amount = amount }); } From f479c29b6a3f75a3e50b966d238f1c7ccb26645e Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 16:44:31 +0800 Subject: [PATCH 09/16] [Fix] For testing. --- .../AElfConsensusContractConstants.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs b/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs index a6164599a9..574fdb9f29 100644 --- a/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs +++ b/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs @@ -2,6 +2,6 @@ namespace AElf.Contracts.Consensus.AEDPoS { public static class AElfConsensusContractConstants { - public const int TinyBlocksNumber = 8; + public const int TinyBlocksNumber = 4; } } \ No newline at end of file From 0fd75b3d00bc0effd50a16e45cd8e3544974464f Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 17:31:44 +0800 Subject: [PATCH 10/16] [Fix] Fix welfare releasing problem. --- protobuf/profit_contract.proto | 1 + src/AElf.Contracts.Election/ElectionContractState.cs | 3 ++- .../ElectionContract_Treasury.cs | 6 +++++- src/AElf.Contracts.Profit/ProfitContract.cs | 10 ++++++---- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/protobuf/profit_contract.proto b/protobuf/profit_contract.proto index 12fb4bef47..445b981142 100644 --- a/protobuf/profit_contract.proto +++ b/protobuf/profit_contract.proto @@ -120,6 +120,7 @@ message ReleaseProfitInput { Hash profit_id = 1; sint64 period = 2; sint64 amount = 3; + sint64 total_weight = 4; } message ProfitDetails { diff --git a/src/AElf.Contracts.Election/ElectionContractState.cs b/src/AElf.Contracts.Election/ElectionContractState.cs index 6e118c4754..d772d509e4 100644 --- a/src/AElf.Contracts.Election/ElectionContractState.cs +++ b/src/AElf.Contracts.Election/ElectionContractState.cs @@ -50,6 +50,7 @@ public partial class ElectionContractState : ContractState public SingletonState TimeEachTerm { get; set; } public SingletonState MinerElectionVotingItemId { get; set; } - + + public SingletonState CachedWelfareWeight { get; set; } } } \ No newline at end of file diff --git a/src/AElf.Contracts.Election/ElectionContract_Treasury.cs b/src/AElf.Contracts.Election/ElectionContract_Treasury.cs index 633a12aa97..73e9ece517 100644 --- a/src/AElf.Contracts.Election/ElectionContract_Treasury.cs +++ b/src/AElf.Contracts.Election/ElectionContract_Treasury.cs @@ -205,8 +205,12 @@ private void ReleaseTreasurySubProfitItems(long termNumber) State.ProfitContract.ReleaseProfit.Send(new ReleaseProfitInput { ProfitId = State.WelfareHash.Value, - Period = termNumber > 1 ? termNumber - 1 : -1 + Period = termNumber > 1 ? termNumber - 1 : -1, + TotalWeight = State.CachedWelfareWeight.Value }); + + State.CachedWelfareWeight.Value = + State.ProfitContract.GetProfitItem.Call(State.WelfareHash.Value).TotalWeight; } private void UpdateTreasurySubItemsWeights(long termNumber) diff --git a/src/AElf.Contracts.Profit/ProfitContract.cs b/src/AElf.Contracts.Profit/ProfitContract.cs index 7029064ab5..592ca8d5bb 100644 --- a/src/AElf.Contracts.Profit/ProfitContract.cs +++ b/src/AElf.Contracts.Profit/ProfitContract.cs @@ -280,6 +280,8 @@ public override Empty ReleaseProfit(ReleaseProfitInput input) return new Empty(); } + var totalWeight = input.TotalWeight == 0 ? profitItem.TotalWeight : input.TotalWeight; + Assert(Context.Sender == profitItem.Creator, "Only creator can release profits."); var profitVirtualAddress = Context.ConvertVirtualAddressToContractAddress(input.ProfitId); @@ -297,7 +299,7 @@ public override Empty ReleaseProfit(ReleaseProfitInput input) input.Amount = balance; } - if (input.Period < 0 || profitItem.TotalWeight <= 0) + if (input.Period < 0 || totalWeight <= 0) { profitItem.CurrentPeriod = input.Period > 0 ? input.Period.Add(1) : profitItem.CurrentPeriod; @@ -342,14 +344,14 @@ public override Empty ReleaseProfit(ReleaseProfitInput input) { releasedProfitInformation = new ReleasedProfitsInformation { - TotalWeight = profitItem.TotalWeight, + TotalWeight = totalWeight, ProfitsAmount = input.Amount, IsReleased = true }; } else { - releasedProfitInformation.TotalWeight = profitItem.TotalWeight; + releasedProfitInformation.TotalWeight = totalWeight; releasedProfitInformation.ProfitsAmount += input.Amount; releasedProfitInformation.IsReleased = true; } @@ -368,7 +370,7 @@ public override Empty ReleaseProfit(ReleaseProfitInput input) { var subItemVirtualAddress = Context.ConvertVirtualAddressToContractAddress(subProfitItem.ProfitId); - var amount = subProfitItem.Weight.Mul(input.Amount).Div(profitItem.TotalWeight); + var amount = subProfitItem.Weight.Mul(input.Amount).Div(totalWeight); if (amount != 0) { State.TokenContract.TransferFrom.Send(new TransferFromInput From f87711986deb4b967c2f816abe42dc5e45ec0ef3 Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 18:04:04 +0800 Subject: [PATCH 11/16] [Fix] Fix bugs. --- src/AElf.Contracts.Profit/ProfitContract.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/AElf.Contracts.Profit/ProfitContract.cs b/src/AElf.Contracts.Profit/ProfitContract.cs index 592ca8d5bb..a0efcaa1a7 100644 --- a/src/AElf.Contracts.Profit/ProfitContract.cs +++ b/src/AElf.Contracts.Profit/ProfitContract.cs @@ -205,7 +205,7 @@ public override Empty SubWeight(SubWeightInput input) } var expiryDetails = currentDetail.Details - .Where(d => d.EndPeriod < profitItem.CurrentPeriod).ToList(); + .Where(d => d.EndPeriod < profitItem.CurrentPeriod && d.LastProfitPeriod != 0).ToList(); if (!expiryDetails.Any()) { @@ -478,6 +478,8 @@ public override Empty AddProfits(AddProfitsInput input) public override Empty Profit(ProfitInput input) { + Context.LogDebug(() => $"{Context.Sender} is trying to profit from {input.ProfitId.ToHex()}."); + var profitItem = State.ProfitItemsMap[input.ProfitId]; Assert(profitItem != null, "Profit item not found."); @@ -515,7 +517,8 @@ public override Empty Profit(ProfitInput input) var releasedProfitsInformation = State.ReleasedProfitsMap[releasedProfitsVirtualAddress]; var amount = profitDetail.Weight.Mul(releasedProfitsInformation.ProfitsAmount) .Div(releasedProfitsInformation.TotalWeight); - Context.LogDebug(() => $"{Context.Sender} is profiting {amount} tokens from {input.ProfitId.ToHex()} in period {profitItem.CurrentPeriod}"); + var period1 = period; + Context.LogDebug(() => $"{Context.Sender} is profiting {amount} tokens from {input.ProfitId.ToHex()} in period {period1}"); if (releasedProfitsInformation.IsReleased && amount > 0) { State.TokenContract.TransferFrom.Send(new TransferFromInput From 7c1613cd57b80b62932c7ead49693b02910e9544 Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 18:54:52 +0800 Subject: [PATCH 12/16] [Fix] Fix bugs. --- .../AElfConsensusContractConstants.cs | 2 +- src/AElf.Contracts.Profit/ProfitContract.cs | 18 +++++++----------- .../BVT/TreasuryTests.cs | 2 +- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs b/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs index 574fdb9f29..a6164599a9 100644 --- a/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs +++ b/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs @@ -2,6 +2,6 @@ namespace AElf.Contracts.Consensus.AEDPoS { public static class AElfConsensusContractConstants { - public const int TinyBlocksNumber = 4; + public const int TinyBlocksNumber = 8; } } \ No newline at end of file diff --git a/src/AElf.Contracts.Profit/ProfitContract.cs b/src/AElf.Contracts.Profit/ProfitContract.cs index a0efcaa1a7..7eedff82e5 100644 --- a/src/AElf.Contracts.Profit/ProfitContract.cs +++ b/src/AElf.Contracts.Profit/ProfitContract.cs @@ -204,16 +204,15 @@ public override Empty SubWeight(SubWeightInput input) return new Empty(); } - var expiryDetails = currentDetail.Details - .Where(d => d.EndPeriod < profitItem.CurrentPeriod && d.LastProfitPeriod != 0).ToList(); + var expiryDetails = currentDetail.Details.Where(d => d.EndPeriod + 1 == profitItem.CurrentPeriod).ToList(); + var weights = expiryDetails.Sum(d => d.Weight); + profitItem.TotalWeight -= weights; + State.ProfitItemsMap[input.ProfitId] = profitItem; - if (!expiryDetails.Any()) - { - return new Empty(); - } + var expiryButNotProfitedDetails = currentDetail.Details + .Where(d => d.EndPeriod < profitItem.CurrentPeriod && d.LastProfitPeriod < d.EndPeriod).ToList(); - var weights = expiryDetails.Sum(d => d.Weight); - foreach (var profitDetail in expiryDetails) + foreach (var profitDetail in expiryButNotProfitedDetails) { currentDetail.Details.Remove(profitDetail); } @@ -230,9 +229,6 @@ public override Empty SubWeight(SubWeightInput input) // State.ProfitDetailsMap[input.ProfitId][input.Receiver] = null; // } - profitItem.TotalWeight -= weights; - State.ProfitItemsMap[input.ProfitId] = profitItem; - return new Empty(); } diff --git a/test/AElf.Contracts.Election.Tests/BVT/TreasuryTests.cs b/test/AElf.Contracts.Election.Tests/BVT/TreasuryTests.cs index a3c9964caf..6005c9d733 100644 --- a/test/AElf.Contracts.Election.Tests/BVT/TreasuryTests.cs +++ b/test/AElf.Contracts.Election.Tests/BVT/TreasuryTests.cs @@ -468,7 +468,7 @@ await ProfitContractStub.GetReleasedProfitsInformation.CallAsync( ProfitId = ProfitItemsIds[ProfitType.BasicMinerReward], Period = 2 }); - releasedProfitsInformation.TotalWeight.ShouldBe(profitItems[ProfitType.BasicMinerReward].TotalWeight); + releasedProfitsInformation.TotalWeight.ShouldBeLessThan(candidatesKeyPairs.Count); releasedProfitsInformation.ProfitsAmount.ShouldBe(releasedAmount .Mul(ElectionContractConstants.MinerRewardWeight) .Div(totalWeightsOfTreasury) From 0932dce39d1d5f18f61b17328d64cf9efc8288a0 Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 18:55:15 +0800 Subject: [PATCH 13/16] [Fix] For testing. --- .../AElfConsensusContractConstants.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs b/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs index a6164599a9..574fdb9f29 100644 --- a/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs +++ b/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs @@ -2,6 +2,6 @@ namespace AElf.Contracts.Consensus.AEDPoS { public static class AElfConsensusContractConstants { - public const int TinyBlocksNumber = 8; + public const int TinyBlocksNumber = 4; } } \ No newline at end of file From 1517114560d010c29d2dad75e85dd8c0d8a5e2b5 Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 19:03:06 +0800 Subject: [PATCH 14/16] [Fix] Tune test cases. --- ...ElfConsensusContract.cs => AEDPoSContract.cs} | 2 +- ...ctConstants.cs => AEDPoSContractConstants.cs} | 2 +- .../HelpMethods.cs | 16 ++++++++-------- .../AElfConsensusContractTestBase.cs | 2 +- .../ConsensusCommonMethodsTests.cs | 8 ++++---- 5 files changed, 15 insertions(+), 15 deletions(-) rename src/AElf.Contracts.Consensus.AEDPoS/{AElfConsensusContract.cs => AEDPoSContract.cs} (99%) rename src/AElf.Contracts.Consensus.AEDPoS/{AElfConsensusContractConstants.cs => AEDPoSContractConstants.cs} (65%) diff --git a/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContract.cs b/src/AElf.Contracts.Consensus.AEDPoS/AEDPoSContract.cs similarity index 99% rename from src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContract.cs rename to src/AElf.Contracts.Consensus.AEDPoS/AEDPoSContract.cs index 0a9fb5f5d3..90d3a29a0c 100644 --- a/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContract.cs +++ b/src/AElf.Contracts.Consensus.AEDPoS/AEDPoSContract.cs @@ -212,7 +212,7 @@ private void TryToFindLastIrreversibleBlock() Context.LogDebug(() => $"LIB found, offset is {offset}"); Context.Fire(new IrreversibleBlockFound() { - Offset = offset.Mul(AElfConsensusContractConstants.TinyBlocksNumber) + Offset = offset.Mul(AEDPoSContractConstants.TinyBlocksNumber) }); } } diff --git a/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs b/src/AElf.Contracts.Consensus.AEDPoS/AEDPoSContractConstants.cs similarity index 65% rename from src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs rename to src/AElf.Contracts.Consensus.AEDPoS/AEDPoSContractConstants.cs index 574fdb9f29..1a14d51a72 100644 --- a/src/AElf.Contracts.Consensus.AEDPoS/AElfConsensusContractConstants.cs +++ b/src/AElf.Contracts.Consensus.AEDPoS/AEDPoSContractConstants.cs @@ -1,6 +1,6 @@ namespace AElf.Contracts.Consensus.AEDPoS { - public static class AElfConsensusContractConstants + public static class AEDPoSContractConstants { public const int TinyBlocksNumber = 4; } diff --git a/src/AElf.Contracts.Consensus.AEDPoS/HelpMethods.cs b/src/AElf.Contracts.Consensus.AEDPoS/HelpMethods.cs index 7e9a7f4042..438c3fafe1 100644 --- a/src/AElf.Contracts.Consensus.AEDPoS/HelpMethods.cs +++ b/src/AElf.Contracts.Consensus.AEDPoS/HelpMethods.cs @@ -50,7 +50,7 @@ private AElfConsensusBehaviour GetBehaviour(string publicKey, DateTime dateTime, if (currentRound.ExtraBlockProducerOfPreviousRound == publicKey && dateTime < currentRound.GetStartTime() && - minerInRound.ProducedTinyBlocks < AElfConsensusContractConstants.TinyBlocksNumber) + minerInRound.ProducedTinyBlocks < AEDPoSContractConstants.TinyBlocksNumber) { return AElfConsensusBehaviour.TinyBlock; } @@ -61,12 +61,12 @@ private AElfConsensusBehaviour GetBehaviour(string publicKey, DateTime dateTime, return AElfConsensusBehaviour.UpdateValue; } } - else if (minerInRound.ProducedTinyBlocks < AElfConsensusContractConstants.TinyBlocksNumber) + else if (minerInRound.ProducedTinyBlocks < AEDPoSContractConstants.TinyBlocksNumber) { return AElfConsensusBehaviour.TinyBlock; } else if (currentRound.ExtraBlockProducerOfPreviousRound == publicKey && - minerInRound.ProducedTinyBlocks < AElfConsensusContractConstants.TinyBlocksNumber.Mul(2)) + minerInRound.ProducedTinyBlocks < AEDPoSContractConstants.TinyBlocksNumber.Mul(2)) { return AElfConsensusBehaviour.TinyBlock; } @@ -143,22 +143,22 @@ private ConsensusCommand GetConsensusCommand(AElfConsensusBehaviour behaviour, R if (currentRound.ExtraBlockProducerOfPreviousRound != publicKey) { expectedMiningTime = expectedMiningTime.ToDateTime().AddMilliseconds(producedTinyBlocks - .Mul(miningInterval).Div(AElfConsensusContractConstants.TinyBlocksNumber)) + .Mul(miningInterval).Div(AEDPoSContractConstants.TinyBlocksNumber)) .ToTimestamp(); } else { // EBP of previous round will produce double tiny blocks. This is for normal time slot of current round. expectedMiningTime = expectedMiningTime.ToDateTime().AddMilliseconds(producedTinyBlocks - .Sub(AElfConsensusContractConstants.TinyBlocksNumber) - .Mul(miningInterval).Div(AElfConsensusContractConstants.TinyBlocksNumber)).ToTimestamp(); + .Sub(AEDPoSContractConstants.TinyBlocksNumber) + .Mul(miningInterval).Div(AEDPoSContractConstants.TinyBlocksNumber)).ToTimestamp(); } } else if (previousRound != null) { // EBP of previous round will produce double tiny blocks. This is for extra time slot of previous round. expectedMiningTime = previousRound.GetExtraBlockMiningTime().AddMilliseconds(producedTinyBlocks - .Mul(miningInterval).Div(AElfConsensusContractConstants.TinyBlocksNumber)).ToTimestamp(); + .Mul(miningInterval).Div(AEDPoSContractConstants.TinyBlocksNumber)).ToTimestamp(); } nextBlockMiningLeftMilliseconds = @@ -192,7 +192,7 @@ private ConsensusCommand GetConsensusCommand(AElfConsensusBehaviour behaviour, R { ExpectedMiningTime = expectedMiningTime, NextBlockMiningLeftMilliseconds = nextBlockMiningLeftMilliseconds, - LimitMillisecondsOfMiningBlock = miningInterval / AElfConsensusContractConstants.TinyBlocksNumber, + LimitMillisecondsOfMiningBlock = miningInterval / AEDPoSContractConstants.TinyBlocksNumber, Hint = hint }; } diff --git a/test/AElf.Contracts.Consensus.AEDPoS.Tests/AElfConsensusContractTestBase.cs b/test/AElf.Contracts.Consensus.AEDPoS.Tests/AElfConsensusContractTestBase.cs index 26c17c74bf..5f26d2bc69 100644 --- a/test/AElf.Contracts.Consensus.AEDPoS.Tests/AElfConsensusContractTestBase.cs +++ b/test/AElf.Contracts.Consensus.AEDPoS.Tests/AElfConsensusContractTestBase.cs @@ -40,7 +40,7 @@ public class AElfConsensusContractTestBase : ContractTestBase Date: Wed, 15 May 2019 19:30:34 +0800 Subject: [PATCH 15/16] [Fix] Fix SubWeight. --- src/AElf.Contracts.Profit/ProfitContract.cs | 18 +++++++++++------- .../BVT/TreasuryTests.cs | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/AElf.Contracts.Profit/ProfitContract.cs b/src/AElf.Contracts.Profit/ProfitContract.cs index 7eedff82e5..957240eb60 100644 --- a/src/AElf.Contracts.Profit/ProfitContract.cs +++ b/src/AElf.Contracts.Profit/ProfitContract.cs @@ -204,15 +204,16 @@ public override Empty SubWeight(SubWeightInput input) return new Empty(); } - var expiryDetails = currentDetail.Details.Where(d => d.EndPeriod + 1 == profitItem.CurrentPeriod).ToList(); - var weights = expiryDetails.Sum(d => d.Weight); - profitItem.TotalWeight -= weights; - State.ProfitItemsMap[input.ProfitId] = profitItem; + var expiryDetails = currentDetail.Details + .Where(d => d.EndPeriod < profitItem.CurrentPeriod).ToList(); - var expiryButNotProfitedDetails = currentDetail.Details - .Where(d => d.EndPeriod < profitItem.CurrentPeriod && d.LastProfitPeriod < d.EndPeriod).ToList(); + if (!expiryDetails.Any()) + { + return new Empty(); + } - foreach (var profitDetail in expiryButNotProfitedDetails) + var weights = expiryDetails.Sum(d => d.Weight); + foreach (var profitDetail in expiryDetails) { currentDetail.Details.Remove(profitDetail); } @@ -229,6 +230,9 @@ public override Empty SubWeight(SubWeightInput input) // State.ProfitDetailsMap[input.ProfitId][input.Receiver] = null; // } + profitItem.TotalWeight -= weights; + State.ProfitItemsMap[input.ProfitId] = profitItem; + return new Empty(); } diff --git a/test/AElf.Contracts.Election.Tests/BVT/TreasuryTests.cs b/test/AElf.Contracts.Election.Tests/BVT/TreasuryTests.cs index 6005c9d733..0c5260f717 100644 --- a/test/AElf.Contracts.Election.Tests/BVT/TreasuryTests.cs +++ b/test/AElf.Contracts.Election.Tests/BVT/TreasuryTests.cs @@ -456,7 +456,7 @@ await VoteToCandidates(VotersKeyPairs.Take(1).ToList(), // Already burned. profitItems[ProfitType.BasicMinerReward].TotalAmount.ShouldBe(0); // Each new miner takes 1 weight. - profitItems[ProfitType.BasicMinerReward].TotalWeight.ShouldBeLessThan(candidatesKeyPairs.Count); + profitItems[ProfitType.BasicMinerReward].TotalWeight.ShouldBe(5); // Check released profit information. // We don't give initial miners rewards. @@ -721,7 +721,7 @@ public async Task ElectionContract_ReleaseTreasuryProfits_ReleaseThirdTerm() // Already burned. profitItems[ProfitType.BasicMinerReward].TotalAmount.ShouldBe(0); // Each new miner takes 1 weight. - profitItems[ProfitType.BasicMinerReward].TotalWeight.ShouldBeLessThan(candidatesKeyPairs.Count); + profitItems[ProfitType.BasicMinerReward].TotalWeight.ShouldBe(5); // Check released profit information. // We don't give initial miners rewards. From 7cec5c0fea08c93d5d6fbf05491a44fe0cfb6a82 Mon Sep 17 00:00:00 2001 From: EanCuznaivy Date: Wed, 15 May 2019 19:38:59 +0800 Subject: [PATCH 16/16] [Fix] TinyBlocksNumber to 8. --- src/AElf.Contracts.Consensus.AEDPoS/AEDPoSContractConstants.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AElf.Contracts.Consensus.AEDPoS/AEDPoSContractConstants.cs b/src/AElf.Contracts.Consensus.AEDPoS/AEDPoSContractConstants.cs index 1a14d51a72..b882e0c3cd 100644 --- a/src/AElf.Contracts.Consensus.AEDPoS/AEDPoSContractConstants.cs +++ b/src/AElf.Contracts.Consensus.AEDPoS/AEDPoSContractConstants.cs @@ -2,6 +2,6 @@ namespace AElf.Contracts.Consensus.AEDPoS { public static class AEDPoSContractConstants { - public const int TinyBlocksNumber = 4; + public const int TinyBlocksNumber = 8; } } \ No newline at end of file