Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sendblocks spam; fix faucet crash #121

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion Discreet/Network/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public class Handler
private ConcurrentDictionary<IPEndPoint, HashSet<InventoryVectorRef>> NeededInventory = new();
private ConcurrentDictionary<InventoryVectorRef, (long, long)> InventoryTimeouts = new(Environment.ProcessorCount, 25000, new InventoryEqualityComparer());

private Dictionary<Cipher.SHA256, long> cachedSendBlocks = new(new Cipher.SHA256EqualityComparer());
private long cachedSendBlocksIndex = 0;

public async Task NeededInventoryStart(CancellationToken token)
{
while (!token.IsCancellationRequested)
Expand Down Expand Up @@ -1315,6 +1318,17 @@ public async Task HandleSendBlock(SendBlockPacket p, Peerbloom.Connection conn,
}
}

private Cipher.SHA256 CreateSendBlocksHash(SendBlocksPacket p)
{
byte[] dat = new byte[32 * p.Blocks.Length];
for (int i = 0; i < p.Blocks.Length; i++)
{
Array.Copy(p.Blocks[i].Header.BlockHash.Bytes, 0, dat, 32 * i, 32);
}

return new Cipher.SHA256(System.Security.Cryptography.SHA256.HashData(dat), false);
}

public async Task HandleSendBlocks(SendBlocksPacket p, Peerbloom.Connection conn)
{
// ensure blocks are sorted
Expand Down Expand Up @@ -1344,6 +1358,26 @@ public async Task HandleSendBlocks(SendBlocksPacket p, Peerbloom.Connection conn
await HandleSendBlock(new SendBlockPacket { Block = block }, conn, false);
}

var cachedSendBlocksHash = CreateSendBlocksHash(p);

// check against cachedSendBlocks rule for propagation
lock (cachedSendBlocks)
{
if (cachedSendBlocks.ContainsKey(cachedSendBlocksHash))
{
// don't propagate, we've seen this
return;
}

cachedSendBlocks[cachedSendBlocksHash] = cachedSendBlocksIndex;
cachedSendBlocksIndex++;
if (cachedSendBlocks.Count > 100)
{
var vmin = cachedSendBlocks.MinBy(x => x.Value);
cachedSendBlocks.Remove(vmin.Key);
}
}

if (propagate.Any()) Peerbloom.Network.GetNetwork().Broadcast(new Packet(PacketType.SENDBLOCKS, p));
}

Expand Down Expand Up @@ -1401,7 +1435,7 @@ public async Task HandleBlocks(BlocksPacket p, Peerbloom.Connection conn)

if (BlockBuffer.Instance.BlockExists(p.Blocks[0].Header.BlockHash))
{
Daemon.Logger.Error("HandleBlocks: queried peer returned an existing block; ignoring");
Daemon.Logger.Error("HandleBlocks: queried peer returned an existing block; ignoring", verbose: 3);
}
else
{
Expand Down
21 changes: 15 additions & 6 deletions Discreet/Wallets/Services/WalletFundsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -61,13 +62,21 @@ public virtual async Task StartFundsScanAsync(CancellationToken token = default)
State = ServiceState.SYNCED;
while (!token.IsCancellationRequested)
{
var chainHeight = view.GetChainHeight();
if (lastSeenHeight < chainHeight)
try
{
var chainHeight = view.GetChainHeight();
if (lastSeenHeight < chainHeight)
{
ProcessBlocks(Enumerable.Range(
(int)lastSeenHeight + 1,
(int)chainHeight - (int)lastSeenHeight)
.Select(x => view.GetBlock((long)x)));
}
}
catch
{
ProcessBlocks(Enumerable.Range(
(int)lastSeenHeight + 1,
(int)chainHeight - (int)lastSeenHeight)
.Select(x => view.GetBlock((long)x)));
await Task.Delay(100);
continue;
}

try
Expand Down
Loading