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:
1 parent
3da9b00f44
commit
4897594b87
40 files changed
+1871
-90
No files matched your search
@@ -480,6 +480,61 @@ public sealed class RiskEngineTests
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Simulation)]
|
||||
public async Task AfterARestoreRealMoneyWaitsForThePasswordOfTheUser()
|
||||
{
|
||||
await using var s = await CreateAsync(Consent());
|
||||
var live = await FakeRealAccountAsync(s);
|
||||
await s.Rig.Ledger.RecordCapitalAsync(live.Account.AccountId, StrategyNames.CryptoTrend, 2_500m, "EUR", "test", _none);
|
||||
await RequirementsMetAsync(s);
|
||||
await s.Stages.ConfirmLiveAsync(new LiveConfirmation(StrategyNames.CryptoTrend, live.Account.AccountId,
|
||||
StageBook.Phrase(StrategyNames.CryptoTrend, StrategyStages.Reduced), Password, null, "utente"), _none);
|
||||
var before = (await s.Stages.GetAsync(StrategyNames.CryptoTrend, _none))!;
|
||||
Assert.AreEqual("stage", (await Assert.ThrowsExactlyAsync<LiveConfirmationException>(() => s.Stages.ReconfirmAsync(StrategyNames.CryptoTrend, Password, null, "utente", _none))).Code,
|
||||
"nothing to confirm before a restore");
|
||||
s.Rig.Refresh();
|
||||
await live.Manager.SubmitAsync(Buy(s, 0.002m) with { StartAggressive = true }, _none);
|
||||
await s.Rig.TurnsAsync(live.Manager, 3, _minute);
|
||||
var planned = Ids.NewId(s.Rig.Clock);
|
||||
await s.Rig.Db.WriteAsync(x => x.ExecuteAsync(
|
||||
"INSERT INTO planned_orders (id, decision_id, strategy, account_id, instrument_id, side, purpose, quantity, exit_all, reference_price, reason, due_at, valid_until, status, created_at, updated_at) " +
|
||||
"VALUES (@id, @id, 'crypto_trend', @a, @i, 'buy', 'entry', 0.001, @no, 60000, 'trend_entry', @t, @t, 'pending', @t, @t)", _none,
|
||||
("id", planned), ("a", live.Account.AccountId), ("i", s.Btc.Id), ("no", false), ("t", s.Rig.Clock.UtcNow)), _none);
|
||||
|
||||
await Installation.CheckAsync(s.Rig.Db, new DataDirectory(TempFolder.Create("reconfirm")), s.Rig.Clock, _none);
|
||||
await Installation.ProtectAsync(s.Rig.Db, s.Rig.Clock, _none);
|
||||
|
||||
// Stopped, exit only, with its counts untouched; what was planned does not leave; the stop stays.
|
||||
Assert.AreEqual("stopped", (await s.Settings.GetAsync(SettingsStore.BotState.Key)).Value);
|
||||
var waiting = (await s.Stages.GetAsync(StrategyNames.CryptoTrend, _none))!;
|
||||
Assert.AreEqual((StrategyStages.Reduced, true, true, before.StageSince), (waiting.Stage, waiting.ExitOnly, waiting.WaitsForReconfirmation, waiting.StageSince));
|
||||
Assert.AreEqual("dropped/protected_restart", (await s.Rig.QueryAsync("SELECT status, outcome FROM planned_orders WHERE id = @id",
|
||||
r => r.Text("status") + "/" + r.TextOrNull("outcome"), ("id", planned))).Single());
|
||||
Assert.HasCount(1, await s.Rig.QueryAsync("SELECT reason FROM strategy_stage_history WHERE reason = 'exit_only_after_restore'", r => r.Text("reason")));
|
||||
Assert.HasCount(1, await live.Paper.GetOpenOrdersAsync(_none), "the stop on the real account");
|
||||
|
||||
// The bot restarted: still no purchase with real money, while a sale goes; holding nothing does not free it.
|
||||
await s.Risk.RestartAsync("utente", _none);
|
||||
Assert.AreEqual(RiskReasons.ExitOnly, (await live.Manager.SubmitAsync(Buy(s, 0.001m), _none)).Refusal);
|
||||
var held = (await s.Rig.Ledger.PositionAsync(live.Account.AccountId, s.Btc.Id, StrategyNames.CryptoTrend, _none))!.Quantity;
|
||||
Assert.IsNull((await live.Manager.SubmitAsync(Sell(s, held) with { StartAggressive = true }, _none)).Refusal);
|
||||
await s.Rig.TurnsAsync(live.Manager, 3, _minute);
|
||||
Assert.AreEqual(0m, (await s.Rig.Ledger.PositionAsync(live.Account.AccountId, s.Btc.Id, StrategyNames.CryptoTrend, _none))?.Quantity ?? 0m);
|
||||
await s.Stages.ReleaseExitOnlyAsync(s.Rig.Ledger, _none);
|
||||
Assert.IsTrue((await s.Stages.GetAsync(StrategyNames.CryptoTrend, _none))!.WaitsForReconfirmation, "only the user takes it out");
|
||||
|
||||
Assert.AreEqual("password", (await Assert.ThrowsExactlyAsync<LiveConfirmationException>(() => s.Stages.ReconfirmAsync(StrategyNames.CryptoTrend, "sbagliata", null, "utente", _none))).Code);
|
||||
var closed = new StageBook(s.Rig.Db, s.Rig.Clock, TradingConsent.None, (_, _, _) => Task.FromResult(true));
|
||||
Assert.AreEqual(RiskReasons.NoConsent, (await Assert.ThrowsExactlyAsync<LiveConfirmationException>(() => closed.ReconfirmAsync(StrategyNames.CryptoTrend, Password, null, "utente", _none))).Code);
|
||||
await s.Stages.ReconfirmAsync(StrategyNames.CryptoTrend, Password, null, "utente", _none);
|
||||
var back = (await s.Stages.GetAsync(StrategyNames.CryptoTrend, _none))!;
|
||||
Assert.AreEqual((StrategyStages.Reduced, false, before.StageSince), (back.Stage, back.ExitOnly, back.StageSince));
|
||||
s.Rig.Refresh();
|
||||
Assert.IsNull((await live.Manager.SubmitAsync(Buy(s, 0.001m), _none)).Refusal);
|
||||
Assert.HasCount(2, await s.Rig.QueryAsync("SELECT key FROM config_history WHERE key = @k", r => r.Text("key"), ("k", "strategy.crypto_trend.stage")));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Simulation)]
|
||||
public async Task ADemotedStrategyKeepsManagingItsRealPositionsDownToZero()
|
||||
|
||||
Reference in new issue
Block a user