Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 38 additions & 1 deletion Lib9c/Action/ValidatorDelegation/AllocateGuildReward.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Libplanet.Types.Blocks;
using Lib9c;
using Nekoyume.Action.Guild.Migration.LegacyModels;
using Serilog;

namespace Nekoyume.Action.ValidatorDelegation
{
Expand All @@ -29,16 +30,23 @@ public override void LoadPlainValue(IValue plainValue)
public override IWorld Execute(IActionContext context)
{
var world = context.PreviousState;
Log.Information(
"[AllocateGuildReward] Start block #{BlockIndex}",
context.BlockIndex);

if (world.GetDelegationMigrationHeight() is not { } migrationHeight
|| context.BlockIndex < migrationHeight)
{
Log.Information("[AllocateGuildReward] Skipped (migration height not reached)");
return world;
}

Log.Information("[AllocateGuildReward] Creating ValidatorRepository...");
var repository = new ValidatorRepository(world, context);
Log.Information("[AllocateGuildReward] ValidatorRepository created");
var rewardCurrency = Currencies.Mead;
var proposerInfo = repository.GetProposerInfo();
Log.Information("[AllocateGuildReward] ProposerInfo retrieved");

if (context.LastCommit is BlockCommit lastCommit)
{
Expand All @@ -47,21 +55,31 @@ public override IWorld Execute(IActionContext context)
(total, next)
=> total + (next.ValidatorPower ?? BigInteger.Zero));

Log.Information(
"[AllocateGuildReward] DistributeProposerReward start, votes={VoteCount}, power={Power}",
lastCommit.Votes.Count(),
validatorSetPower);
DistributeProposerReward(
repository,
rewardCurrency,
proposerInfo,
validatorSetPower,
lastCommit.Votes);
Log.Information("[AllocateGuildReward] DistributeProposerReward done");

Log.Information("[AllocateGuildReward] DistributeValidatorReward start");
DistributeValidatorReward(
repository,
rewardCurrency,
validatorSetPower,
lastCommit.Votes);
Log.Information("[AllocateGuildReward] DistributeValidatorReward done");
}

var communityFund = repository.GetBalance(Addresses.RewardPool, rewardCurrency);
Log.Information(
"[AllocateGuildReward] CommunityFund={CommunityFund}",
communityFund);

if (communityFund.Sign > 0)
{
Expand All @@ -71,6 +89,7 @@ public override IWorld Execute(IActionContext context)
communityFund);
}

Log.Information("[AllocateGuildReward] Complete block #{BlockIndex}", context.BlockIndex);
return repository.World;
}

Expand Down Expand Up @@ -140,24 +159,35 @@ FungibleAssetValue rewardToAllocate

if (rewardToAllocate.Sign <= 0)
{
Log.Information("[AllocateGuildReward.DistributeValidatorReward] No reward to allocate");
return;
}

if (validatorSetPower == BigInteger.Zero)
{
Log.Information("[AllocateGuildReward.DistributeValidatorReward] ValidatorSetPower is zero");
return;
}

int index = 0;
foreach (Vote vote in lastVotes)
{
if (vote.Flag == VoteFlag.Null || vote.Flag == VoteFlag.Unknown)
{
continue;
}

var validatorAddress = vote.ValidatorPublicKey.Address;
Log.Information(
"[AllocateGuildReward.DistributeValidatorReward] Processing validator {Index} {Address}",
index, validatorAddress);

if (!repository.TryGetDelegatee(
vote.ValidatorPublicKey.Address, out var validatorDelegatee))
validatorAddress, out var validatorDelegatee))
{
Log.Information(
"[AllocateGuildReward.DistributeValidatorReward] Delegatee not found for {Address}",
validatorAddress);
continue;
}

Expand All @@ -167,12 +197,19 @@ FungibleAssetValue rewardToAllocate
continue;
}

Log.Information(
"[AllocateGuildReward.DistributeValidatorReward] AllocateReward for {Address}, power={Power}",
validatorAddress, validatorPower);
validatorDelegatee.AllocateReward(
rewardToAllocate,
validatorPower,
validatorSetPower,
Addresses.RewardPool,
blockHeight);
Log.Information(
"[AllocateGuildReward.DistributeValidatorReward] AllocateReward done for {Address}",
validatorAddress);
index++;
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions Lib9c/Action/ValidatorDelegation/AllocateReward.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Nekoyume.ValidatorDelegation;
using Nekoyume.Module.ValidatorDelegation;
using Nekoyume.Action.Guild.Migration.LegacyModels;
using Serilog;

namespace Nekoyume.Action.ValidatorDelegation
{
Expand All @@ -31,15 +32,19 @@ public override void LoadPlainValue(IValue plainValue)
public override IWorld Execute(IActionContext context)
{
var world = context.PreviousState;
Log.Information("[AllocateReward] Start block #{BlockIndex}", context.BlockIndex);

if (world.GetDelegationMigrationHeight() is not { } migrationHeight
|| context.BlockIndex < migrationHeight)
{
Log.Information("[AllocateReward] Skipped (migration height not reached)");
return world;
}

var rewardCurrency = world.GetGoldCurrency();
Log.Information("[AllocateReward] Creating GuildRepository...");
var repository = new GuildRepository(world, context);
Log.Information("[AllocateReward] GuildRepository created");

if (context.LastCommit is BlockCommit lastCommit)
{
Expand All @@ -48,11 +53,20 @@ public override IWorld Execute(IActionContext context)
(total, next)
=> total + (next.ValidatorPower ?? BigInteger.Zero));

Log.Information("[AllocateReward] DistributeValidator start");
DistributeValidator(repository, rewardCurrency, validatorSetPower, lastCommit.Votes);
Log.Information("[AllocateReward] DistributeValidator done");

Log.Information("[AllocateReward] DistributeGuild start");
var validatorRepository = new ValidatorRepository(repository.World, context);
DistributeGuild(validatorRepository, rewardCurrency, validatorSetPower, lastCommit.Votes);
Log.Information("[AllocateReward] DistributeGuild done, UpdateWorld...");
repository.UpdateWorld(validatorRepository.World);
Log.Information("[AllocateReward] UpdateWorld done");

Log.Information("[AllocateReward] DistributeGuildParticipant start");
DistributeGuildParticipant(repository, rewardCurrency, validatorSetPower, lastCommit.Votes);
Log.Information("[AllocateReward] DistributeGuildParticipant done");
}

var communityFund = repository.GetBalance(Addresses.RewardPool, rewardCurrency);
Expand All @@ -65,6 +79,7 @@ public override IWorld Execute(IActionContext context)
communityFund);
}

Log.Information("[AllocateReward] Complete block #{BlockIndex}", context.BlockIndex);
return repository.World;
}

Expand Down
18 changes: 18 additions & 0 deletions Lib9c/Action/ValidatorDelegation/SlashValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Nekoyume.Model.Guild;
using Nekoyume.Module.ValidatorDelegation;
using Nekoyume.ValidatorDelegation;
using Serilog;

namespace Nekoyume.Action.ValidatorDelegation
{
Expand All @@ -34,6 +35,7 @@
public override IWorld Execute(IActionContext context)
{
var world = context.PreviousState;
Log.Information("[SlashValidator] Start block #{BlockIndex}", context.BlockIndex);

if (world.GetDelegationMigrationHeight() is null)
{
Expand All @@ -48,12 +50,19 @@
.Select(vote => vote.ValidatorPublicKey),
context.BlockIndex);
repository.SetAbstainHistory(abstainHistory);
Log.Information(
"[SlashValidator] AbstainsToSlash count={Count}",
abstainsToSlash.Count());

Check warning on line 55 in Lib9c/Action/ValidatorDelegation/SlashValidator.cs

View workflow job for this annotation

GitHub Actions / build-for-unity

Use 'Count' property here instead.

Check warning on line 55 in Lib9c/Action/ValidatorDelegation/SlashValidator.cs

View workflow job for this annotation

GitHub Actions / build-for-unity

Use 'Count' property here instead.

Check warning on line 55 in Lib9c/Action/ValidatorDelegation/SlashValidator.cs

View workflow job for this annotation

GitHub Actions / build-js

Use 'Count' property here instead.

Check warning on line 55 in Lib9c/Action/ValidatorDelegation/SlashValidator.cs

View workflow job for this annotation

GitHub Actions / build-js

Use 'Count' property here instead.

Check warning on line 55 in Lib9c/Action/ValidatorDelegation/SlashValidator.cs

View workflow job for this annotation

GitHub Actions / build-and-test (Release)

Use 'Count' property here instead.

Check warning on line 55 in Lib9c/Action/ValidatorDelegation/SlashValidator.cs

View workflow job for this annotation

GitHub Actions / build-and-test (Release)

Use 'Count' property here instead.

foreach (var abstain in abstainsToSlash)
{
Log.Information(
"[SlashValidator] Slashing abstainer {Address}",
abstain.Address);
var validatorDelegatee = repository.GetDelegatee(abstain.Address);
if (validatorDelegatee.Jailed)
{
Log.Information("[SlashValidator] Already jailed, skip {Address}", abstain.Address);
continue;
}

Expand All @@ -65,13 +74,20 @@
guildDelegatee.Slash(LivenessSlashFactor, context.BlockIndex, context.BlockIndex);
guildDelegatee.Jail(context.BlockIndex + AbstainJailTime);
repository.UpdateWorld(guildRepository.World);
Log.Information("[SlashValidator] Slashed and jailed {Address}", abstain.Address);
}

Log.Information(
"[SlashValidator] Processing evidence, count={Count}",
context.Evidence.Count());
foreach (var evidence in context.Evidence)
{
switch (evidence)
{
case DuplicateVoteEvidence e:
Log.Information(
"[SlashValidator] DuplicateVoteEvidence target={Address}, height={Height}",
e.TargetAddress, e.Height);
if (e.Height > context.BlockIndex)
{
throw new Exception("Evidence height is greater than block index.");
Expand All @@ -86,12 +102,14 @@
guildDelegatee.Slash(DuplicateVoteSlashFactor, e.Height, context.BlockIndex);
guildDelegatee.Tombstone();
repository.UpdateWorld(guildRepository.World);
Log.Information("[SlashValidator] DuplicateVote slashed {Address}", e.TargetAddress);
break;
default:
break;
}
}

Log.Information("[SlashValidator] Complete block #{BlockIndex}", context.BlockIndex);
return repository.World;
}
}
Expand Down
Loading