Rebuild the state from the brokers after a restore and refuse an older copy

A restore, an import or an adopted database restarts protected: the engine
stays stopped, reads from the broker what the archive does not know (orders,
stops, executions, also from the OKX history up to 89 days), cancels orders
left working, protects what it finds and waits for the user. Strategies with
real money stay exit only until confirmed again. The database of before the
restore, put back by mistake, is refused by its fingerprint.

A failed migration leaves the engine off with one backup and a way to try
again; an archive is checked whole before the database is touched; a changed
secret key no longer crashes anything, and "anubis access reset" gives the
access back to whoever holds the computer. The image runs the db and access
commands without the guard.

refs #69

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-01 18:52:08 +01:00
1 parent 3da9b00f44
commit 4897594b87
40 files changed
+1871 -90

No files matched your search

+11 -7
View File
@@ -27,12 +27,15 @@ public sealed class StoresTests
Assert.IsTrue(File.Exists(data.InstallationFile));
Assert.AreEqual(IdentityVerdict.Match, (await Installation.CheckAsync(test.Db, data, clock).ConfigureAwait(false)).Verdict);
// Data folder lost: rewritten from the database.
// The file of the data folder is lost: nobody can tell this database from an old copy, so the engine does not start.
var before = Installation.ReadFile(data)!;
File.Delete(data.InstallationFile);
Assert.AreEqual(IdentityVerdict.FileRestored, (await Installation.CheckAsync(test.Db, data, clock).ConfigureAwait(false)).Verdict);
var missing = await Installation.CheckAsync(test.Db, data, clock).ConfigureAwait(false);
Assert.AreEqual(IdentityVerdict.MissingFile, missing.Verdict);
Assert.IsFalse(missing.CanStart);
Assert.IsFalse(File.Exists(data.InstallationFile));
// A stale copy of the database: same installation, older fingerprint.
var before = Installation.ReadFile(data)!;
Installation.WriteFile(data, before with { Fingerprint = "newer" });
var stale = await Installation.CheckAsync(test.Db, data, clock).ConfigureAwait(false);
Assert.AreEqual(IdentityVerdict.StaleDatabase, stale.Verdict);
@@ -43,7 +46,7 @@ public sealed class StoresTests
Assert.AreEqual(IdentityVerdict.DifferentInstallation, (await Installation.CheckAsync(test.Db, data, clock).ConfigureAwait(false)).Verdict);
// The user adopts this database: new fingerprint on both sides, protected restart on.
var renewed = await Installation.RenewAsync(test.Db, data, clock, adoptDatabaseIdentity: true).ConfigureAwait(false);
var renewed = await Installation.RenewAsync(test.Db, data, clock, "prova").ConfigureAwait(false);
Assert.AreEqual(before.InstallationId, renewed.InstallationId);
Assert.AreNotEqual(before.Fingerprint, renewed.Fingerprint);
var row = await Installation.ReadRowAsync(test.Db).ConfigureAwait(false);
@@ -124,10 +127,11 @@ public sealed class StoresTests
Assert.IsTrue((await secrets.StatusAsync("OKX_API_KEY_DEMO").ConfigureAwait(false)).FromEnvironment);
await Assert.ThrowsAsync<ReadOnlySettingException>(() => secrets.SetAsync("OKX_API_KEY_DEMO", "x-123456789", "admin")).ConfigureAwait(false);
// With another key the value cannot be read: the error says to enter it again.
// With another key the value cannot be read: whoever asks goes on without it, and the status says it must be entered again.
var otherKey = new SecretStore(test.Db, clock, Enumerable.Repeat((byte)7, 32).ToArray());
var error = await Assert.ThrowsAsync<System.Security.Cryptography.CryptographicException>(() => otherKey.RevealAsync("ALPACA_API_SECRET_DEMO")).ConfigureAwait(false);
Assert.Contains("Reinserisci", error.Message);
Assert.IsNull(await otherKey.RevealAsync("ALPACA_API_SECRET_DEMO").ConfigureAwait(false));
Assert.IsTrue((await otherKey.StatusAsync("ALPACA_API_SECRET_DEMO").ConfigureAwait(false)).Unreadable);
Assert.IsFalse((await secrets.StatusAsync("ALPACA_API_SECRET_DEMO").ConfigureAwait(false)).Unreadable);
// A value sealed under one name does not open under another.
var sealedValue = secrets.Seal("A", "x");