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

change logic #107

Merged
merged 1 commit into from
Feb 29, 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
24 changes: 9 additions & 15 deletions Discreet/DB/BlockBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,15 @@ public async Task ForceFlush()
public async Task Start()
{
_pIndex = DataView.GetView().GetOutputIndex();
DateTime lastFlush = DateTime.MinValue;

_ = Task.Factory.StartNew(async () =>
{
var timer = new PeriodicTimer(_flushInterval);
while (await timer.WaitForNextTickAsync())
{
await _buffer.Writer.WriteAsync(_signaler);
}
});

await foreach(var block in _buffer.Reader.ReadAllAsync())
{
Expand Down Expand Up @@ -304,20 +312,6 @@ public async Task Start()
}

UpdateBuffers(block);

// check received
if (DateTime.Now.Subtract(lastFlush) > _flushInterval)
{
// flush
Flush(buffer);

lock (buffer)
{
buffer.Clear();
}

lastFlush = DateTime.Now;
}
}
}
}
Expand Down
58 changes: 49 additions & 9 deletions Discreet/Network/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public async Task NeededInventoryStart(CancellationToken token)
}
}

public void RegisterNeeded(IPacketBody packet, IPEndPoint req, long durMilliseconds = 0, Action<IPEndPoint, InventoryVector, bool, RequestCallbackContext> callback = null)
public bool RegisterNeeded(IPacketBody packet, IPEndPoint req, long durMilliseconds = 0, Action<IPEndPoint, InventoryVector, bool, RequestCallbackContext> callback = null)
{
bool success = NeededInventory.TryGetValue(req, out var reqset);
if (!success || reqset == null)
Expand All @@ -139,53 +139,93 @@ public void RegisterNeeded(IPacketBody packet, IPEndPoint req, long durMilliseco

var timestamp = DateTime.UtcNow.Ticks;
var durTicks = durMilliseconds * 10_000L;
bool needed = false;

if (packet.GetType() == typeof(GetTransactionsPacket))
{
var gettx = packet as GetTransactionsPacket;
var newGettxs = new List<Cipher.SHA256>();
foreach (var tx in gettx.Transactions)
{
var ivref = new InventoryVectorRef(new InventoryVector(ObjectType.Transaction, tx), reqset, callback, req);
reqset.Add(ivref);
InventoryTimeouts.TryAdd(ivref, (timestamp, durTicks));
if (!reqset.Contains(ivref))
{
newGettxs.Add(tx);
needed = true;
reqset.Add(ivref);
InventoryTimeouts.TryAdd(ivref, (timestamp, durTicks));
}
}

gettx.Transactions = newGettxs.ToArray();
}
else if (packet.GetType() == typeof(GetBlocksPacket))
{
var gettx = packet as GetBlocksPacket;
var newGettxs = new List<Cipher.SHA256>();
foreach (var block in gettx.Blocks)
{
var ivref = new InventoryVectorRef(new InventoryVector(ObjectType.Block, block), reqset, callback, req);
reqset.Add(ivref);
InventoryTimeouts.TryAdd(ivref, (timestamp, durTicks));
if (!reqset.Contains(ivref))
{
newGettxs.Add(block);
needed = true;
reqset.Add(ivref);
InventoryTimeouts.TryAdd(ivref, (timestamp, durTicks));
}
}

gettx.Blocks = newGettxs.ToArray();
}
else if (packet.GetType() == typeof(GetHeadersPacket))
{
var gettx = packet as GetHeadersPacket;
var newGettxs = new List<Cipher.SHA256>();
if (gettx.Headers == null)
{
for (long i = gettx.StartingHeight; i < gettx.StartingHeight + gettx.Count; i++)
{
var ivref = new InventoryVectorRef(new InventoryVector(ObjectType.BlockHeader, new Cipher.SHA256(i)), reqset, callback, req);
reqset.Add(ivref);
InventoryTimeouts.TryAdd(ivref, (timestamp, durTicks));
if (!reqset.Contains(ivref))
{
newGettxs.Add(new Cipher.SHA256(i));
needed = true;
reqset.Add(ivref);
InventoryTimeouts.TryAdd(ivref, (timestamp, durTicks));
}
}

if (newGettxs.Count == gettx.Count)
{
newGettxs = null;
}
}
else
{
foreach (var header in gettx.Headers)
{
var ivref = new InventoryVectorRef(new InventoryVector(ObjectType.BlockHeader, header), reqset, callback, req);
reqset.Add(ivref);
InventoryTimeouts.TryAdd(ivref, (timestamp, durTicks));
if (!reqset.Contains(ivref))
{
newGettxs.Add(header);
needed = true;
reqset.Add(ivref);
InventoryTimeouts.TryAdd(ivref, (timestamp, durTicks));
}
}
}

if (newGettxs != null)
{
gettx.Headers = newGettxs.ToArray();
}
}
else
{
Daemon.Logger.Error($"Handler.RegisterNeeded: cannot accept packet of type {packet.GetType()}");
}

return needed;
}

internal (bool, List<InventoryVectorRef>) CheckFulfillment(IPacketBody packet, IPEndPoint resp)
Expand Down
11 changes: 6 additions & 5 deletions Discreet/Network/Peerbloom/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -749,22 +749,23 @@ public bool SendRequest(Connection conn, Core.Packet packet, long durationMillis
Daemon.Logger.Info($"Network.SendRequest: Sending request {packet.Header.Command} to {conn.Receiver}", verbose: 2);

// hacky; make specific functions for sending packets which call this instead (in the future)
bool success = false;
if (packet.Header.Command == Core.PacketType.GETBLOCKS)
{
handler.RegisterNeeded((Core.Packets.GetBlocksPacket)packet.Body, conn.Receiver, durationMilliseconds, callback);
success = handler.RegisterNeeded((Core.Packets.GetBlocksPacket)packet.Body, conn.Receiver, durationMilliseconds, callback);
}
else if (packet.Header.Command == Core.PacketType.GETTXS)
{
handler.RegisterNeeded((Core.Packets.GetTransactionsPacket)packet.Body, conn.Receiver, durationMilliseconds, callback);
success = handler.RegisterNeeded((Core.Packets.GetTransactionsPacket)packet.Body, conn.Receiver, durationMilliseconds, callback);
}
else if (packet.Header.Command == Core.PacketType.GETHEADERS)
{
handler.RegisterNeeded((Core.Packets.GetHeadersPacket)packet.Body, conn.Receiver, durationMilliseconds, callback);
success = handler.RegisterNeeded((Core.Packets.GetHeadersPacket)packet.Body, conn.Receiver, durationMilliseconds, callback);
}

conn.Send(packet);
if (success) conn.Send(packet);

return true;
return success;
}

public bool Send(IPEndPoint endpoint, Core.Packet packet)
Expand Down
Loading