Skip to content

Commit ed594f8

Browse files
Merge pull request richardschneider#89 from richardschneider/88-bitswap-ledger
Bitswap ledger with another peer
2 parents 8c9be91 + e96ae45 commit ed594f8

File tree

3 files changed

+150
-4
lines changed

3 files changed

+150
-4
lines changed

src/CoreApi/BitswapLedger.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Ipfs.CoreApi
6+
{
7+
/// <summary>
8+
/// Statistics on the <see cref="IBitswapApi">bitswap</see> blocks exchanged with another <see cref="Peer"/>.
9+
/// </summary>
10+
/// <seealso cref="IBitswapApi.LedgerAsync"/>
11+
public class BitswapLedger
12+
{
13+
/// <summary>
14+
/// The <see cref="Peer"/> that pertains to this ledger.
15+
/// </summary>
16+
/// <value>
17+
/// The peer that is being monitored.
18+
/// </value>
19+
public Peer Peer { get; set; }
20+
21+
/// <summary>
22+
/// The number of blocks sent by the <see cref="Peer"/> to us.
23+
/// </summary>
24+
/// <value>
25+
/// The number of blocks.
26+
/// </value>
27+
public ulong BlocksReceived { get; set; }
28+
29+
/// <summary>
30+
/// The number of blocks sent by us to the <see cref="Peer"/>.
31+
/// </summary>
32+
/// <value>
33+
/// The number of blocks.
34+
/// </value>
35+
public ulong BlocksSent { get; set; }
36+
37+
/// <summary>
38+
/// The number of bytes sent by the <see cref="Peer"/> to us.
39+
/// </summary>
40+
/// <value>
41+
/// The number of bytes.
42+
/// </value>
43+
public ulong DataReceived { get; set; }
44+
45+
46+
/// <summary>
47+
/// The number of bytes sent by us to the <see cref="Peer"/>
48+
/// </summary>
49+
/// <value>
50+
/// The number of bytes.
51+
/// </value>
52+
public ulong DataSent { get; set; }
53+
54+
/// <summary>
55+
/// The calculated debt to the peer.
56+
/// </summary>
57+
/// <value>
58+
/// <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"/>.
61+
/// </value>
62+
public float DebtRatio
63+
{
64+
get
65+
{
66+
return (float)DataSent / (float)(DataReceived + 1); // +1 is to prevent division by zero
67+
}
68+
}
69+
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+
78+
}
79+
}

src/CoreApi/IBitswapApi.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ namespace Ipfs.CoreApi
1111
/// send blocks to other peers in the network.
1212
/// </summary>
1313
/// <remarks>
14-
/// Bitswap has two primary jobs (1) Attempt to acquire blocks from the network that
15-
/// have been requested by the client and (2) Judiciously(though strategically)
16-
/// send blocks in its possession to other peers who want them.
14+
/// Bitswap has two primary jobs
15+
/// <list type="bullet">
16+
/// <item>
17+
/// <description>
18+
/// Attempt to acquire blocks from the network that have been requested by the client
19+
/// </description>
20+
/// </item>
21+
/// <item>
22+
/// <description>
23+
/// Judiciously (though strategically) send blocks in its possession to other peers who want them
24+
/// </description>
25+
/// </item>
26+
/// </list>
1727
/// </remarks>
1828
/// <seealso href="https://github.com/ipfs/specs/tree/master/bitswap">Bitswap spec</seealso>
1929
public interface IBitswapApi
@@ -68,6 +78,22 @@ public interface IBitswapApi
6878
/// Any outstanding <see cref="GetAsync(Cid, CancellationToken)"/> for the
6979
/// <paramref name="id"/> are cancelled.
7080
/// </remarks>
71-
Task UnwantAsync(Cid id, CancellationToken cancel = default(CancellationToken));
81+
Task UnwantAsync(Cid id, CancellationToken cancel = default(CancellationToken));
82+
83+
/// <summary>
84+
/// Gets information on the blocks exchanged with a specific <see cref="Peer"/>.
85+
/// </summary>
86+
/// <param name="peer">
87+
/// The peer to get information on. If the peer is unknown, then a ledger
88+
/// with zeros is returned.
89+
/// </param>
90+
/// <param name="cancel">
91+
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
92+
/// </param>
93+
/// <returns>
94+
/// A task that represents the asynchronous operation. The task's value
95+
/// contains the <see cref="BitswapLedger"/> for the <paramref name="peer"/>.
96+
/// </returns>
97+
Task<BitswapLedger> LedgerAsync(Peer peer, CancellationToken cancel = default(CancellationToken));
7298
}
7399
}

test/CoreApi/BitswapLedgerTest.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
6+
namespace Ipfs.CoreApi
7+
{
8+
[TestClass]
9+
public class BitswapLedgerTest
10+
{
11+
[TestMethod]
12+
public void Defaults()
13+
{
14+
var ledger = new BitswapLedger();
15+
16+
Assert.IsNull(ledger.Peer);
17+
Assert.AreEqual(0ul, ledger.BlocksReceived);
18+
Assert.AreEqual(0ul, ledger.BlocksSent);
19+
Assert.AreEqual(0ul, ledger.DataReceived);
20+
Assert.AreEqual(0ul, ledger.DataSent);
21+
Assert.AreEqual(0f, ledger.DebtRatio);
22+
Assert.IsTrue(ledger.IsInDebt);
23+
}
24+
25+
[TestMethod]
26+
public void DebtRatio_Positive()
27+
{
28+
var ledger = new BitswapLedger { DataSent = 1024 * 1024 };
29+
Assert.IsTrue(ledger.DebtRatio >= 1);
30+
Assert.IsFalse(ledger.IsInDebt);
31+
}
32+
33+
[TestMethod]
34+
public void DebtRatio_Negative()
35+
{
36+
var ledger = new BitswapLedger { DataReceived = 1024 * 1024 };
37+
Assert.IsTrue(ledger.DebtRatio < 1);
38+
Assert.IsTrue(ledger.IsInDebt);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)