-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from richardschneider/88-bitswap-ledger
Bitswap ledger with another peer
- Loading branch information
Showing
3 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Ipfs.CoreApi | ||
{ | ||
/// <summary> | ||
/// Statistics on the <see cref="IBitswapApi">bitswap</see> blocks exchanged with another <see cref="Peer"/>. | ||
/// </summary> | ||
/// <seealso cref="IBitswapApi.LedgerAsync"/> | ||
public class BitswapLedger | ||
{ | ||
/// <summary> | ||
/// The <see cref="Peer"/> that pertains to this ledger. | ||
/// </summary> | ||
/// <value> | ||
/// The peer that is being monitored. | ||
/// </value> | ||
public Peer Peer { get; set; } | ||
|
||
/// <summary> | ||
/// The number of blocks sent by the <see cref="Peer"/> to us. | ||
/// </summary> | ||
/// <value> | ||
/// The number of blocks. | ||
/// </value> | ||
public ulong BlocksReceived { get; set; } | ||
|
||
/// <summary> | ||
/// The number of blocks sent by us to the <see cref="Peer"/>. | ||
/// </summary> | ||
/// <value> | ||
/// The number of blocks. | ||
/// </value> | ||
public ulong BlocksSent { get; set; } | ||
|
||
/// <summary> | ||
/// The number of bytes sent by the <see cref="Peer"/> to us. | ||
/// </summary> | ||
/// <value> | ||
/// The number of bytes. | ||
/// </value> | ||
public ulong DataReceived { get; set; } | ||
|
||
|
||
/// <summary> | ||
/// The number of bytes sent by us to the <see cref="Peer"/> | ||
/// </summary> | ||
/// <value> | ||
/// The number of bytes. | ||
/// </value> | ||
public ulong DataSent { get; set; } | ||
|
||
/// <summary> | ||
/// The calculated debt to the peer. | ||
/// </summary> | ||
/// <value> | ||
/// <see cref="DataSent"/> divided by <see cref="DataReceived"/>. | ||
/// A value less than 1 indicates that we are in debt to the | ||
/// <see cref="Peer"/>. | ||
/// </value> | ||
public float DebtRatio | ||
{ | ||
get | ||
{ | ||
return (float)DataSent / (float)(DataReceived + 1); // +1 is to prevent division by zero | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Determines if we owe the <see cref="Peer"/> some blocks. | ||
/// </summary> | ||
/// <value> | ||
/// <b>true</b> if we owe data to the peer; otherwise, <b>false</b>. | ||
/// </value> | ||
public bool IsInDebt => DebtRatio < 1; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Ipfs.CoreApi | ||
{ | ||
[TestClass] | ||
public class BitswapLedgerTest | ||
{ | ||
[TestMethod] | ||
public void Defaults() | ||
{ | ||
var ledger = new BitswapLedger(); | ||
|
||
Assert.IsNull(ledger.Peer); | ||
Assert.AreEqual(0ul, ledger.BlocksReceived); | ||
Assert.AreEqual(0ul, ledger.BlocksSent); | ||
Assert.AreEqual(0ul, ledger.DataReceived); | ||
Assert.AreEqual(0ul, ledger.DataSent); | ||
Assert.AreEqual(0f, ledger.DebtRatio); | ||
Assert.IsTrue(ledger.IsInDebt); | ||
} | ||
|
||
[TestMethod] | ||
public void DebtRatio_Positive() | ||
{ | ||
var ledger = new BitswapLedger { DataSent = 1024 * 1024 }; | ||
Assert.IsTrue(ledger.DebtRatio >= 1); | ||
Assert.IsFalse(ledger.IsInDebt); | ||
} | ||
|
||
[TestMethod] | ||
public void DebtRatio_Negative() | ||
{ | ||
var ledger = new BitswapLedger { DataReceived = 1024 * 1024 }; | ||
Assert.IsTrue(ledger.DebtRatio < 1); | ||
Assert.IsTrue(ledger.IsInDebt); | ||
} | ||
} | ||
} |