Skip to content

Commit 8ee03ff

Browse files
feat: add DebtRatio
1 parent a006401 commit 8ee03ff

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/CoreApi/BitswapLedger.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,19 @@ public class BitswapLedger
5151
/// </value>
5252
public ulong DataSent { get; set; }
5353

54+
/// <summary>
55+
/// The calculated debt to the peer.
56+
/// </summary>
57+
/// <value>
58+
/// <see cref="DataSent"/> divided by <see cref="DataReceived"/>.
59+
/// </value>
60+
public float DebtRatio
61+
{
62+
get
63+
{
64+
return (float)DataSent / (float)(DataReceived + 1); // +1 is to prevent division by zero
65+
}
66+
}
67+
5468
}
5569
}

test/CoreApi/BitswapLedgerTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,21 @@ public void Defaults()
1818
Assert.AreEqual(0ul, ledger.BlocksSent);
1919
Assert.AreEqual(0ul, ledger.DataReceived);
2020
Assert.AreEqual(0ul, ledger.DataSent);
21+
Assert.AreEqual(0f, ledger.DebtRatio);
2122
}
2223

24+
[TestMethod]
25+
public void DebtRatio_Positive()
26+
{
27+
var ledger = new BitswapLedger { DataSent = 1024 * 1024 };
28+
Assert.IsTrue(ledger.DebtRatio >= 1);
29+
}
2330

31+
[TestMethod]
32+
public void DebtRatio_Negative()
33+
{
34+
var ledger = new BitswapLedger { DataReceived = 1024 * 1024 };
35+
Assert.IsTrue(ledger.DebtRatio < 1);
36+
}
2437
}
2538
}

0 commit comments

Comments
 (0)