Resynchronize a log with the other stations, and rescore it

A station that was away — a laptop that lost the network, one that was
switched off — comes back missing whatever the others logged while it was
gone. N1MM fills that in by walking the log an hour at a time, and this is
that walk, on the same messages:

| Message | What it says |
|---|---|
| `CHECKSUM` | here is what I hold for your contacts in this hour, and the contest and callsign I am logging |
| `DELETEQS` | drop your copies of my contacts in that hour, I am sending them again |
| `RESYNCQSO` | one of those contacts |
| `CONFIRMED` | here is what the hour hashes to at my end — which also asks for the next hour |

Each station is the authority on the contacts it made, so an hour that
disagrees is replaced rather than merged. The answer to one block asks for
the next, and the walk ends when the block starts after the end of the
period. The Resynchronize tab counts the hours that agreed, the hours that
were sent again and the hours that still disagreed, which are N1MM's
columns.

**`LogChecksum`** is N1MM's: a CRC32 over the `DXLOG` columns of the block,
ordered by time and then callsign, leaving out what each station works out
for itself — points, the three multiplier flags, the country prefix, the
continent, the WPX prefix and `IsOriginal`. Two stations may disagree about
those and still hold the same log. A checksum that disagrees for any other
reason costs one block of traffic, which is what a resync is for.

**The link still holds no log.** It asks one through `ContactLog` for the
contacts of a computer in a block of time, and `SessionContactLog` answers
on the window thread, because that is where the log is touched.

**Every contact logged here now carries the computer that made it.** N1MM
stamps `NetBiosName` and `NetworkedCompNr` on every row and this program
did not, so its own contacts belonged to no station and a resync could not
ask for them.

**Rescore** writes the points and multiplier flags of a period back to the
database. The log in memory scores itself whenever it changes; the stored
rows do not, and N1MM reads them rather than working them out. A contact
that arrives from another station after the one that took a multiplier
leaves those columns behind. A station can also ask the others to rescore
when its resync finishes, which is the flag on the end of `CHECKSUM`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PdAYHcdRktqKry7nk414TU
This commit is contained in:
2026-09-03 21:52:09 +00:00
co-authored by Claude Opus 5
parent 68b29d722a
commit 2a21c772ad
18 changed files with 1061 additions and 15 deletions
@@ -0,0 +1,93 @@
using Nonemm.Core;
namespace Nonemm.Network.Tests;
/// The checksum two stations compare during a resync. What matters is that the
/// same contacts hash the same whatever order they are handed over in, that a
/// change anywhere in a contact changes it, and that the columns each station
/// works out for itself do not.
public class LogChecksumTests
{
private static readonly DateTime Start = new(2026, 5, 30, 12, 0, 0, DateTimeKind.Utc);
[Fact]
public void TheSameContactsHashTheSameInAnyOrder()
{
Qso one = Contact("G3XYZ", 0);
Qso two = Contact("DL1ABC", 1);
Assert.Equal(LogChecksum.Of([one, two]), LogChecksum.Of([two, one]));
}
/// Two contacts at the same second are ordered by callsign, the way N1MM
/// reads them out of the database.
[Fact]
public void ContactsAtTheSameSecondAreOrderedByCall()
{
Qso one = Contact("G3XYZ", 0);
Qso two = Contact("DL1ABC", 0);
Assert.Equal(LogChecksum.Of([one, two]), LogChecksum.Of([two, one]));
}
[Fact]
public void AChangedExchangeChangesTheChecksum()
{
Qso qso = Contact("G3XYZ", 0);
Assert.NotEqual(
LogChecksum.Of([qso]),
LogChecksum.Of([qso with { ReceivedNumber = 42 }]));
}
/// Points and the multiplier flags are worked out from the rules at each
/// station, and the two are allowed to disagree about them while holding
/// the same contact.
[Fact]
public void ScoringDoesNotChangeTheChecksum()
{
Qso qso = Contact("G3XYZ", 0);
Assert.Equal(
LogChecksum.Of([qso]),
LogChecksum.Of([qso with { Points = 3, IsMultiplier1 = true, IsMultiplier2 = true }]));
}
[Fact]
public void AnEmptyBlockHashesToTheEmptyChecksum()
{
Assert.Equal(LogChecksum.Of([]), LogChecksum.Of([]));
}
/// A block is the contacts of one computer inside one stretch of time. The
/// end of the block is not in it, so two blocks side by side neither share
/// a contact nor drop one.
[Fact]
public void ABlockHoldsOneStationsContactsInsideTheHour()
{
List<Qso> log =
[
Contact("G3XYZ", 0) with { StationName = "RUN-PC" },
Contact("DL1ABC", 30) with { StationName = "RUN-PC" },
Contact("OK1AB", 45) with { StationName = "MULT-PC" },
Contact("F5XY", 60) with { StationName = "RUN-PC" },
];
IReadOnlyList<Qso> block = LogChecksum.Block(log, "run-pc", Start, TimeSpan.FromHours(1));
Assert.Equal(["DL1ABC", "G3XYZ"], block.Select(q => q.Call.Text).Order());
}
private static Qso Contact(string call, int minutes) => new()
{
Id = $"{call}-{minutes}",
TimestampUtc = Start.AddMinutes(minutes),
Call = Callsign.Parse(call),
Frequency = Frequency.FromKilohertz(14_025),
Mode = Modes.Cw,
ContestName = "CQWW",
ContestNumber = 2,
SentNumber = 41,
StationName = "RUN-PC",
};
}