cs-codex-dist-tests/Tools/TranscriptAnalysis/Receivers/DuplicateBlocksReceived.cs

68 lines
2.2 KiB
C#
Raw Normal View History

2024-08-06 13:56:42 +00:00
using CodexPlugin.OverwatchSupport;
using OverwatchTranscript;
2024-08-07 09:18:40 +00:00
namespace TranscriptAnalysis.Receivers
2024-08-06 13:56:42 +00:00
{
2024-08-07 09:18:40 +00:00
public class DuplicateBlocksReceived : BaseReceiver<OverwatchCodexEvent>
2024-08-06 13:56:42 +00:00
{
2024-08-07 09:18:40 +00:00
public override string Name => "BlocksReceived";
2024-08-06 13:56:42 +00:00
2024-08-07 09:18:40 +00:00
public override void Receive(ActivateEvent<OverwatchCodexEvent> @event)
2024-08-06 13:56:42 +00:00
{
2024-08-07 09:18:40 +00:00
if (@event.Payload.BlockReceived != null)
{
Handle(@event.Payload, @event.Payload.BlockReceived);
}
2024-08-06 13:56:42 +00:00
}
2024-08-07 09:18:40 +00:00
public override void Finish()
2024-08-06 13:56:42 +00:00
{
Log("Number of BlockReceived events seen: " + seen);
var totalReceived = peerIdBlockAddrCount.Sum(a => a.Value.Sum(p => p.Value));
var maxRepeats = peerIdBlockAddrCount.Max(a => a.Value.Max(p => p.Value));
var occurances = new int[maxRepeats + 1];
foreach (var peerPair in peerIdBlockAddrCount)
{
foreach (var pair in peerPair.Value)
{
occurances[pair.Value]++;
2024-08-07 09:18:40 +00:00
}
2024-08-06 13:56:42 +00:00
}
float t = totalReceived;
for (var i = 1; i < occurances.Length; i++)
{
float n = occurances[i];
float p = 100.0f * (n / t);
Log($"Block received {i} times = {occurances[i]}x ({p}%)");
}
}
private int seen = 0;
private readonly Dictionary<string, Dictionary<string, int>> peerIdBlockAddrCount = new Dictionary<string, Dictionary<string, int>>();
private void Handle(OverwatchCodexEvent payload, BlockReceivedEvent blockReceived)
{
var receiverPeerId = payload.PeerId;
var blockAddress = blockReceived.BlockAddress;
seen++;
if (!peerIdBlockAddrCount.ContainsKey(receiverPeerId))
{
peerIdBlockAddrCount.Add(receiverPeerId, new Dictionary<string, int>());
}
var blockAddCount = peerIdBlockAddrCount[receiverPeerId];
if (!blockAddCount.ContainsKey(blockAddress))
{
blockAddCount.Add(blockAddress, 1);
}
else
{
blockAddCount[blockAddress]++;
}
}
}
}