Skip to content

Commit

Permalink
Merge f479c29 into fa7c718
Browse files Browse the repository at this point in the history
  • Loading branch information
EanCuznaivy authored May 15, 2019
2 parents fa7c718 + f479c29 commit e629851
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace AElf.Contracts.Consensus.AEDPoS
{
public static class AElfConsensusContractConstants
{
public const int TinyBlocksNumber = 8;
public const int TinyBlocksNumber = 4;
}
}
12 changes: 10 additions & 2 deletions src/AElf.Contracts.Consensus.AEDPoS/ViewMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,16 @@ 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))
{
// 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).Mul(2));
}

return 0;
}
}
}
38 changes: 24 additions & 14 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 @@ -181,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();
}
Expand Down Expand Up @@ -296,15 +299,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 +323,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 @@ -352,8 +356,12 @@ 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;

foreach (var subProfitItem in profitItem.SubProfitItems)
Expand Down Expand Up @@ -482,7 +490,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 All @@ -502,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
});
}

Expand All @@ -524,6 +535,5 @@ public override Empty Profit(ProfitInput input)

return new Empty();
}

}
}
}
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
4 changes: 1 addition & 3 deletions src/AElf.OS/Jobs/BlockSyncJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}");
Expand Down
20 changes: 10 additions & 10 deletions test/AElf.Contracts.Election.Tests/BVT/TreasuryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public async Task<List<ECKeyPair>> 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.
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down

0 comments on commit e629851

Please sign in to comment.