Skip to content

Commit

Permalink
Add trace for delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
OnedgeLee committed Dec 2, 2024
1 parent 6cda595 commit 408044d
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 1 deletion.
206 changes: 205 additions & 1 deletion Lib9c/Delegation/Delegatee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using Bencodex.Types;
Expand All @@ -14,6 +15,9 @@ public abstract class Delegatee<T, TSelf> : IDelegatee
where T : Delegator<TSelf, T>
where TSelf : Delegatee<T, TSelf>
{
private static readonly ActivitySource ActivitySource

Check warning on line 18 in Lib9c/Delegation/Delegatee.cs

View workflow job for this annotation

GitHub Actions / build-js

A static field in a generic type is not shared among instances of different close constructed types.

Check warning on line 18 in Lib9c/Delegation/Delegatee.cs

View workflow job for this annotation

GitHub Actions / build-and-test (Release)

A static field in a generic type is not shared among instances of different close constructed types.
= new ActivitySource("Lib9c.Delegation.Delegatee");

public Delegatee(
Address address,
Address accountAddress,
Expand Down Expand Up @@ -173,7 +177,14 @@ public Address LumpSumRewardsRecordAddress(long height)

public virtual BigInteger Bond(T delegator, FungibleAssetValue fav, long height)
{
using var activity = ActivitySource.StartActivity("Bond");

var distributeRewardActivity = ActivitySource.StartActivity(
"DistributeReward",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
DistributeReward(delegator, height);
distributeRewardActivity?.Dispose();

if (!fav.Currency.Equals(DelegationCurrency))
{
Expand All @@ -187,14 +198,62 @@ public virtual BigInteger Bond(T delegator, FungibleAssetValue fav, long height)
"Cannot bond to tombstoned delegatee.");
}

var getBondActivity = ActivitySource.StartActivity(
"GetBond",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Bond bond = Repository.GetBond(this, delegator.Address);
getBondActivity?.Dispose();

var shareFromFAVActivity = ActivitySource.StartActivity(
"ShareFromFAV",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
BigInteger share = ShareFromFAV(fav);
shareFromFAVActivity?.Dispose();

var addShareActivity = ActivitySource.StartActivity(
"AddShare",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
bond = bond.AddShare(share);
addShareActivity?.Dispose();

var addShareMetadataActivity = ActivitySource.StartActivity(
"AddShareMetadata",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Metadata.AddShare(share);
addShareMetadataActivity?.Dispose();

var addDelegatedFAVMetadataActivity = ActivitySource.StartActivity(
"AddDelegatedFAVMetadata",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Metadata.AddDelegatedFAV(fav);
addDelegatedFAVMetadataActivity?.Dispose();

var setBondActivity = ActivitySource.StartActivity(
"SetBond",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Repository.SetBond(bond);
setBondActivity?.Dispose();

var startNewRewardPeriodActivity = ActivitySource.StartActivity(
"StartNewRewardPeriod",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
StartNewRewardPeriod(height);
startNewRewardPeriodActivity?.Dispose();

var setDelegateeMetadataActivity = ActivitySource.StartActivity(
"SetDelegateeMetadata",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Repository.SetDelegateeMetadata(Metadata);
setDelegateeMetadataActivity?.Dispose();

DelegationChanged?.Invoke(this, height);

return share;
Expand All @@ -205,26 +264,80 @@ BigInteger IDelegatee.Bond(IDelegator delegator, FungibleAssetValue fav, long he

public FungibleAssetValue Unbond(T delegator, BigInteger share, long height)
{
using var activity = ActivitySource.StartActivity("Unbond");

var distributeRewardActivity = ActivitySource.StartActivity(
"DistributeReward",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
DistributeReward(delegator, height);
distributeRewardActivity?.Dispose();

if (TotalShares.IsZero || TotalDelegated.RawValue.IsZero)
{
throw new InvalidOperationException(
"Cannot unbond without bonding.");
}

var getBondActivity = ActivitySource.StartActivity(
"GetBond",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Bond bond = Repository!.GetBond(this, delegator.Address);
getBondActivity?.Dispose();

var favFromShareActivity = ActivitySource.StartActivity(
"FAVFromShare",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
FungibleAssetValue fav = FAVFromShare(share);
favFromShareActivity?.Dispose();

var subtractShareActivity = ActivitySource.StartActivity(
"SubtractShare",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
bond = bond.SubtractShare(share);
if (bond.Share.IsZero)
{
bond = bond.ClearLastDistributeHeight();
}
subtractShareActivity?.Dispose();

var removeShareMetadataActivity = ActivitySource.StartActivity(
"RemoveShareMetadata",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Metadata.RemoveShare(share);
removeShareMetadataActivity?.Dispose();

var removeDelegatedFAVMetadataActivity = ActivitySource.StartActivity(
"RemoveDelegatedFAVMetadata",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Metadata.RemoveDelegatedFAV(fav);

var setBondActivity = ActivitySource.StartActivity(
"SetBond",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Repository.SetBond(bond);
setBondActivity?.Dispose();

var startNewRewardPeriodActivity = ActivitySource.StartActivity(
"StartNewRewardPeriod",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
StartNewRewardPeriod(height);
startNewRewardPeriodActivity?.Dispose();

var setDelegateeMetadataActivity = ActivitySource.StartActivity(
"SetDelegateeMetadata",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Repository.SetDelegateeMetadata(Metadata);
setDelegateeMetadataActivity?.Dispose();

DelegationChanged?.Invoke(this, height);

return fav;
Expand All @@ -235,53 +348,109 @@ FungibleAssetValue IDelegatee.Unbond(IDelegator delegator, BigInteger share, lon

public void DistributeReward(T delegator, long height)
{
using var activity = ActivitySource.StartActivity("DistributeReward");

var getBondActivity = ActivitySource.StartActivity(
"GetBond",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Bond bond = Repository.GetBond(this, delegator.Address);
BigInteger share = bond.Share;
getBondActivity?.Dispose();

if (!share.IsZero && bond.LastDistributeHeight.HasValue)
{
var getLumpSumRewardsRecordsActivity = ActivitySource.StartActivity(
"GetLumpSumRewardsRecords",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
IEnumerable<LumpSumRewardsRecord> lumpSumRewardsRecords
= GetLumpSumRewardsRecords(bond.LastDistributeHeight);
getLumpSumRewardsRecordsActivity?.Dispose();

var consumeLumpSumRewardsRecordsActivity = ActivitySource.StartActivity(
"ConsumeLumpSumRewardsRecords",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
foreach (LumpSumRewardsRecord record in lumpSumRewardsRecords)
{
TransferReward(delegator, share, record);
// TransferRemainders(newRecord);
Repository.SetLumpSumRewardsRecord(record);
}
consumeLumpSumRewardsRecordsActivity?.Dispose();
}

var updateLastDistributeHeightActivity = ActivitySource.StartActivity(
"UpdateLastDistributeHeight",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
if (bond.LastDistributeHeight != height)
{
bond = bond.UpdateLastDistributeHeight(height);
}
updateLastDistributeHeightActivity?.Dispose();

var setBondActivity = ActivitySource.StartActivity(
"SetBond",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Repository.SetBond(bond);
setBondActivity?.Dispose();
}

void IDelegatee.DistributeReward(IDelegator delegator, long height)
=> DistributeReward((T)delegator, height);

public void CollectRewards(long height)
{
using var activity = ActivitySource.StartActivity("CollectRewards");

var calculateRewardActivity = ActivitySource.StartActivity(
"CalculateReward",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
var rewards = RewardCurrencies.Select(c => Repository.GetBalance(RewardPoolAddress, c));
calculateRewardActivity?.Dispose();

var getCurrentLumpSumRewardsRecordsActivity = ActivitySource.StartActivity(
"GetCurrentLumpSumRewardsRecords",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
LumpSumRewardsRecord record = Repository.GetCurrentLumpSumRewardsRecord(this)
?? new LumpSumRewardsRecord(
CurrentLumpSumRewardsRecordAddress(),
height,
TotalShares,
RewardCurrencies);
getCurrentLumpSumRewardsRecordsActivity?.Dispose();

var addLumpSumRewardsActivity = ActivitySource.StartActivity(
"AddLumpSumRewards",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
record = record.AddLumpSumRewards(rewards);
addLumpSumRewardsActivity?.Dispose();

var transferRewardsActivity = ActivitySource.StartActivity(
"TransferRewards",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
foreach (var rewardsEach in rewards)
{
if (rewardsEach.Sign > 0)
{
Repository.TransferAsset(RewardPoolAddress, record.Address, rewardsEach);
}
}

transferRewardsActivity?.Dispose();

var setLumpSumRewardsRecordActivity = ActivitySource.StartActivity(
"SetLumpSumRewardsRecord",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Repository.SetLumpSumRewardsRecord(record);
setLumpSumRewardsRecordActivity?.Dispose();
}

public virtual void Slash(BigInteger slashFactor, long infractionHeight, long height)
Expand Down Expand Up @@ -371,7 +540,19 @@ ImmutableDictionary<Currency, FungibleAssetValue> reward

private void StartNewRewardPeriod(long height)
{
using var activity = ActivitySource.StartActivity("StartNewRewardPeriod");

var getCurrentLumpSumRewardsRecordsActivity = ActivitySource.StartActivity(
"GetCurrentLumpSumRewardsRecords",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
LumpSumRewardsRecord? currentRecord = Repository.GetCurrentLumpSumRewardsRecord(this);
getCurrentLumpSumRewardsRecordsActivity?.Dispose();

var setLumpSumRewardsRecordActivity = ActivitySource.StartActivity(
"SetLumpSumRewardsRecord",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
long? lastStartHeight = null;
if (currentRecord is LumpSumRewardsRecord lastRecord)
{
Expand Down Expand Up @@ -403,26 +584,48 @@ private void StartNewRewardPeriod(long height)
lastRecord = lastRecord.MoveAddress(archiveAddress);
Repository.SetLumpSumRewardsRecord(lastRecord);
}
setLumpSumRewardsRecordActivity?.Dispose();

var newRecordActivity = ActivitySource.StartActivity(
"NewRecord",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
LumpSumRewardsRecord newRecord = new(
CurrentLumpSumRewardsRecordAddress(),
height,
TotalShares,
RewardCurrencies,
lastStartHeight);
newRecordActivity?.Dispose();

var setNewLumpSumRewardsRecordActivity = ActivitySource.StartActivity(
"SetNewLumpSumRewardsRecord",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
Repository.SetLumpSumRewardsRecord(newRecord);
setNewLumpSumRewardsRecordActivity?.Dispose();
}

private List<LumpSumRewardsRecord> GetLumpSumRewardsRecords(long? lastRewardHeight)
{
using var activity = ActivitySource.StartActivity("GetLumpSumRewardsRecords");

var getCurrentLumpSumRewardsRecordsActivity = ActivitySource.StartActivity(
"GetCurrentLumpSumRewardsRecords",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
List<LumpSumRewardsRecord> records = new();
if (lastRewardHeight is null
|| !(Repository.GetCurrentLumpSumRewardsRecord(this) is LumpSumRewardsRecord record))
{
return records;
}
getCurrentLumpSumRewardsRecordsActivity?.Dispose();

var getLumpSumRewardsRecordActivity = ActivitySource.StartActivity(
"GetLumpSumRewardsRecord",
ActivityKind.Internal,
activity?.Id ?? string.Empty);
while (record.StartHeight >= lastRewardHeight)
{
records.Add(record);
Expand All @@ -436,6 +639,7 @@ record = Repository.GetLumpSumRewardsRecord(this, lastStartHeight)
?? throw new InvalidOperationException(
$"Lump sum rewards record for #{lastStartHeight} is missing");
}
getLumpSumRewardsRecordActivity?.Dispose();

return records;
}
Expand Down
Loading

0 comments on commit 408044d

Please sign in to comment.