Skip to content

Commit e96ae45

Browse files
feat: add IsInDebt
1 parent 8ee03ff commit e96ae45

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/CoreApi/BitswapLedger.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public class BitswapLedger
5656
/// </summary>
5757
/// <value>
5858
/// <see cref="DataSent"/> divided by <see cref="DataReceived"/>.
59+
/// A value less than 1 indicates that we are in debt to the
60+
/// <see cref="Peer"/>.
5961
/// </value>
6062
public float DebtRatio
6163
{
@@ -65,5 +67,13 @@ public float DebtRatio
6567
}
6668
}
6769

70+
/// <summary>
71+
/// Determines if we owe the <see cref="Peer"/> some blocks.
72+
/// </summary>
73+
/// <value>
74+
/// <b>true</b> if we owe data to the peer; otherwise, <b>false</b>.
75+
/// </value>
76+
public bool IsInDebt => DebtRatio < 1;
77+
6878
}
6979
}

test/CoreApi/BitswapLedgerTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@ public void Defaults()
1919
Assert.AreEqual(0ul, ledger.DataReceived);
2020
Assert.AreEqual(0ul, ledger.DataSent);
2121
Assert.AreEqual(0f, ledger.DebtRatio);
22+
Assert.IsTrue(ledger.IsInDebt);
2223
}
2324

2425
[TestMethod]
2526
public void DebtRatio_Positive()
2627
{
2728
var ledger = new BitswapLedger { DataSent = 1024 * 1024 };
2829
Assert.IsTrue(ledger.DebtRatio >= 1);
30+
Assert.IsFalse(ledger.IsInDebt);
2931
}
3032

3133
[TestMethod]
3234
public void DebtRatio_Negative()
3335
{
3436
var ledger = new BitswapLedger { DataReceived = 1024 * 1024 };
3537
Assert.IsTrue(ledger.DebtRatio < 1);
38+
Assert.IsTrue(ledger.IsInDebt);
3639
}
3740
}
3841
}

0 commit comments

Comments
 (0)