carlos / platform Public

system recover: defer post-journal census refusals; hold acquisitions refused

Astra (fix round 2, finding 1):

a. After recovery durably removed the journal, closeGuardJournal's
   settlement (endGuard, clearUpdateBegun) ran unwrapped, so a census
   refusal there failed recover and the edge's dependency job. It now
   goes through settleWithoutJournal, forward and backward, guarded and
   unarmed, and Run consumes the deferral from recoverIfNeeded under
   --recover: no fatal error kept, no settlement retried at the end of
   the run. Journal-removal, image and other non-census failures stay
   fatal; the apply still returns them.
b. With no update record (a prepared transaction never begun),
   EndParticipation's BeginUpdate withdrew the record it wrote when its
   census refused, and the deferral left acquisitions open. Before it
   defers, settleWithoutJournal now holds them refused under the
   transaction lock with Ledger.HoldUpdate: root's update record kept,
   or written under the transaction's id, with no census of its own
   (it only adds a refusal; EndUpdateWith, with its census, still
   removes it). A barrier that cannot be written leaves the refusal
   fatal. An already-completed transaction with no update record left is
   settled: its replay returns at once and runs no census.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 7faac6ebae6c9cda506377d159a8583ae4de39f8 parent ec057b9
6 files changed, +390 −17
  • internal/host/guardtransaction.go +57 −14
  • internal/host/recover_settlement_test.go +223 −1
  • internal/host/systemapply.go +7 −1
  • internal/host/systemparticipation.go +1 −1
  • internal/rootparticipation/holdupdate_test.go +68 −0
  • internal/rootparticipation/ledger.go +34 −0
diff --git a/internal/host/guardtransaction.go b/internal/host/guardtransaction.go
index 4baa14909..047d0133e 100644
--- a/internal/host/guardtransaction.go
+++ b/internal/host/guardtransaction.go
@@ -9,6 +9,7 @@ import (
execlease "github.com/carlosframework/platform/internal/hostexec"
"github.com/carlosframework/platform/internal/hostfloor"
"github.com/carlosframework/platform/internal/release"
+ "github.com/carlosframework/platform/internal/rootparticipation"
"os"
"path/filepath"
"slices"
@@ -253,12 +254,19 @@ func (a *systemApplier) checkGuardRecovery(ctx context.Context) error {
}
completed, readErr := os.ReadFile(filepath.Join(a.floor.Dir, "completed.json"))
if readErr == nil && string(completed) == tx.ID {
- return a.settleWithoutJournal(ctx, a.floor.EndParticipation(ctx, tx.ID, nil))
+ // Completion recorded and root's marker gone. With root's update
+ // record gone too the transaction is settled: a replay has
+ // nothing left to close, and runs no census.
+ open, err := a.updateRecordOpen(ctx)
+ if err != nil || !open {
+ return err
+ }
+ return a.settleWithoutJournal(ctx, tx.ID, a.floor.EndParticipation(ctx, tx.ID, nil))
}
if readErr != nil && !errors.Is(readErr, os.ErrNotExist) {
return readErr
}
- return a.settleWithoutJournal(ctx, a.settleGuard(ctx, tx, func() error { return a.controllersRecovered(ctx) }))
+ return a.settleWithoutJournal(ctx, tx.ID, a.settleGuard(ctx, tx, func() error { return a.controllersRecovered(ctx) }))
}
if err != nil {
return err
@@ -274,7 +282,7 @@ func (a *systemApplier) checkGuardRecovery(ctx context.Context) error {
return hostfloor.ErrFloor
}
if j == nil {
- return a.settleWithoutJournal(ctx, a.settleGuard(ctx, tx, func() error { return a.controllersRecovered(ctx) }))
+ return a.settleWithoutJournal(ctx, tx.ID, a.settleGuard(ctx, tx, func() error { return a.controllersRecovered(ctx) }))
}
if err := a.controllersRecovered(ctx); err != nil {
return err
@@ -296,15 +304,20 @@ func (a *systemApplier) checkGuardRecovery(ctx context.Context) error {
// treats a refused census as a deferral too (systemparticipation.go).
var errSettlementDeferred = errors.New("participation settlement deferred")
-// settleWithoutJournal is the result of one no-journal settlement: the
-// pending-controller recovery, the completion or withdrawal, and the end of
-// root's update record, whose census runs again inside EndParticipation. A
-// result that is only a census refusal, on a box with root participation,
-// is wrapped in errSettlementDeferred. Anything else is returned unchanged:
-// an image that does not verify, malformed state, a live or unreadable
-// controller, a durable write that failed, and every refusal on a box
+// settleWithoutJournal is the result of one settlement made with no
+// journal open: the pending-controller recovery, the completion or
+// withdrawal, and the end of root's update record, whose census runs again
+// inside EndParticipation; and, after an apply's journal is durably
+// removed, the settlement that follows it (closeGuardJournal). A result
+// that is only a census refusal, on a box with root participation, is
+// wrapped in errSettlementDeferred, once acquisitions are held refused:
+// root's update record is kept, or, where the refused census was the one
+// that would have begun it, written under barrier's id (Ledger.HoldUpdate).
+// Anything else is returned unchanged: an image that does not verify,
+// malformed state, a live or unreadable controller, a durable write that
+// failed, a barrier that could not be written, and every refusal on a box
// without a census, which sees exactly main's recovery (ruling 6).
-func (a *systemApplier) settleWithoutJournal(ctx context.Context, err error) error {
+func (a *systemApplier) settleWithoutJournal(ctx context.Context, barrier string, err error) error {
if err == nil || !onlyCensusRefusal(err) || ctx.Err() != nil {
return err
}
@@ -312,7 +325,27 @@ func (a *systemApplier) settleWithoutJournal(ctx context.Context, err error) err
if checkErr != nil || !required {
return err
}
- return fmt.Errorf("%w: no journal is open, so the files are whole; the participation transaction stays open and claim acquisitions stay refused: %w", errSettlementDeferred, err)
+ if holdErr := a.participationDisk().WithParticipation(ctx, func(ctx context.Context, l rootparticipation.Ledger) error {
+ return l.HoldUpdate(ctx, barrier)
+ }, func() error { return rootparticipation.ErrUnavailable }); holdErr != nil {
+ return errors.Join(err, fmt.Errorf("acquisition barrier: %w", holdErr))
+ }
+ return fmt.Errorf("%w: no journal is open, so the files are whole; root's update record holds claim acquisitions refused until the settlement passes: %w", errSettlementDeferred, err)
+}
+
+// updateRecordOpen reports whether root's update record is there, on a box
+// with root participation. Without one it is false: main's legacy path.
+func (a *systemApplier) updateRecordOpen(ctx context.Context) (bool, error) {
+ open := false
+ err := a.participationDisk().WithParticipation(ctx, func(_ context.Context, l rootparticipation.Ledger) error {
+ _, err := l.UpdateID()
+ if errors.Is(err, os.ErrNotExist) {
+ return nil
+ }
+ open = err == nil
+ return err
+ }, func() error { open = true; return nil })
+ return open, err
}
// deferSettlement reports whether Run, under --recover, leaves err's
@@ -399,10 +432,20 @@ func (a *systemApplier) closeGuardJournal(ctx context.Context) error {
if err := hostfloor.RemoveDurable(a.file("journal.json")); err != nil {
return err
}
+ // The journal is gone and the files are whole: from here a settlement
+ // the census refuses is deferred as with no journal open.
if !active {
- return a.clearUpdateBegun(ctx)
+ return a.settleWithoutJournal(ctx, "recover-legacy", a.clearUpdateBegun(ctx))
+ }
+ err = a.endGuard(ctx)
+ if err == nil || !onlyCensusRefusal(err) {
+ return err
+ }
+ tx, txErr := a.guardTransaction()
+ if txErr != nil {
+ return errors.Join(err, txErr)
}
- return a.endGuard(ctx)
+ return a.settleWithoutJournal(ctx, tx.ID, err)
}
func (a *systemApplier) guardRun(ctx context.Context, wait time.Duration, name string, args ...string) (string, error) {
if id, required, err := a.rootMoves.read(); err != nil {
diff --git a/internal/host/recover_settlement_test.go b/internal/host/recover_settlement_test.go
index f59278012..5fc2d47ea 100644
--- a/internal/host/recover_settlement_test.go
+++ b/internal/host/recover_settlement_test.go
@@ -204,7 +204,7 @@ func TestRuling6NoCensusRecoverIsUnchanged(t *testing.T) {
}
// No census refusal can reach a no-census box's settlement, and one
// that did would not defer either.
- if err := a.settleWithoutJournal(t.Context(), harnessRefusal()); errors.Is(err, errSettlementDeferred) {
+ if err := a.settleWithoutJournal(t.Context(), "recover-system", harnessRefusal()); errors.Is(err, errSettlementDeferred) {
t.Fatalf("a no-census box deferred: %v", err)
}
}
@@ -437,3 +437,225 @@ func TestRecoverWithoutAnArmedFloorKeepsALateRefusalNonFatal(t *testing.T) {
t.Fatalf("a refused settlement closed the update record: %v", err)
}
}
+
+func TestRecoverHoldsAcquisitionsWhenNoUpdateRecordWasBegun(t *testing.T) {
+ // A prepared transaction: the reservation persisted, root's marker and
+ // update record never written. The census refuses the settlement's
+ // BeginUpdate, which withdraws its own record; recover must still
+ // leave acquisitions refused.
+ for _, finished := range []bool{true, false} {
+ t.Run(map[bool]string{true: "finished", false: "unfinished"}[finished], func(t *testing.T) {
+ f, id := guardRecoveryFixture(t)
+ a := f.a
+ if !finished {
+ unfinishedGuard(t, f, id)
+ }
+ if _, err := a.participation.UpdateID(); !os.IsNotExist(err) {
+ t.Fatalf("fixture has an update record: %v", err)
+ }
+ refuseCensus(t, f)
+ if err := a.Run(t.Context(), true); err != nil {
+ t.Fatalf("recover = %v, want a deferral that exits 0", err)
+ }
+ openTransaction(t, f, id, false)
+ a.participation.VerifyLaunches = func(context.Context, uint64) error { return nil }
+ if err := a.Run(t.Context(), true); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := a.participation.UpdateID(); !os.IsNotExist(err) {
+ t.Fatalf("barrier left after settlement: %v", err)
+ }
+ if err := a.participation.Acquire(t.Context(), func() error { return nil }); err != nil {
+ t.Fatalf("acquisition after settlement = %v", err)
+ }
+ })
+ }
+}
+
+func TestRecoverReplaysASettledTransactionWithoutACensus(t *testing.T) {
+ // Completion recorded, marker and update record gone: settled. A
+ // replay closes nothing, runs no census and blocks nothing.
+ f, id := guardRecoveryFixture(t)
+ a := f.a
+ if err := a.floor.Begin(t.Context(), id, a.dataDir); err != nil {
+ t.Fatal(err)
+ }
+ if err := a.Run(t.Context(), true); err != nil {
+ t.Fatal(err)
+ }
+ if raw, err := os.ReadFile(filepath.Join(a.floor.Dir, "completed.json")); err != nil || string(raw) != id {
+ t.Fatalf("fixture not settled: %q %v", raw, err)
+ }
+ calls := refuseCensus(t, f)
+ if err := a.Run(t.Context(), true); err != nil {
+ t.Fatalf("replay = %v", err)
+ }
+ if *calls != 0 {
+ t.Fatalf("a settled replay ran the census %d times", *calls)
+ }
+ if _, err := a.participation.UpdateID(); !os.IsNotExist(err) {
+ t.Fatalf("a settled replay left an update record: %v", err)
+ }
+}
+
+// journalOpen writes an apply's journal for a recovery to finish.
+func journalOpen(t *testing.T, f *applyFixture, j systemJournal) {
+ t.Helper()
+ j.StartedAt = f.now
+ if err := f.a.journal(j); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func TestRecoverDefersARefusalAfterTheJournalIsRemoved(t *testing.T) {
+ // Recovery's own census passes; the settlement after the journal is
+ // durably removed refuses. Unarmed forward (from gated) and backward
+ // (from installed); guarded through the journal close a guarded
+ // fixture can run, the legacy backing-up journal (the guarded forward
+ // and backward restores run a guarded image, which it cannot; the
+ // guarded backward close is TestGuardedRollbackCloseDefers...).
+ for _, tc := range []struct {
+ name string
+ guarded bool
+ forward bool
+ }{{"guarded-backing-up", true, true}, {"unarmed-forward", false, true}, {"unarmed-backward", false, false}} {
+ t.Run(tc.name, func(t *testing.T) {
+ var f *applyFixture
+ id := ""
+ if tc.guarded {
+ f, id = guardRecoveryFixture(t)
+ if err := f.a.floor.Begin(t.Context(), id, f.a.dataDir); err != nil {
+ t.Fatal(err)
+ }
+ } else {
+ f = newApplyFixture(t, "stable", "old-build")
+ if err := f.a.beginExecutableChange(t.Context(), "new-build", f.now); err != nil {
+ t.Fatal(err)
+ }
+ id, _ = f.a.participation.UpdateID()
+ }
+ a := f.a
+ // carlos.prev is the same signed build: the guard verifies it
+ // before a recovery with a journal open.
+ body, _ := os.ReadFile(systemBinaryPath(a.optDir))
+ if err := os.WriteFile(systemPrevPath(a.optDir), body, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ prev := systemPrevPath(a.optDir)
+ f.answer["version:"+prev] = "carlos old-build\n"
+ f.answer["plan:"+prev] = f.answer["plan:"+systemBinaryPath(a.optDir)]
+ f.answer["units:"+prev] = f.answer["units:"+systemBinaryPath(a.optDir)]
+ if tc.guarded {
+ journalOpen(t, f, systemJournal{Phase: systemPhaseBackingUp, Target: "new-build", PrevBuild: "old-build"})
+ } else if tc.forward {
+ journalOpen(t, f, systemJournal{Phase: systemPhaseGated, Target: "old-build", PrevBuild: "old-build"})
+ } else {
+ journalOpen(t, f, systemJournal{Phase: systemPhaseInstalled, Target: "new-build", PrevBuild: "old-build"})
+ }
+ old := recoverPendingControllers
+ t.Cleanup(func() { recoverPendingControllers = old })
+ recoverPendingControllers = func(context.Context, string, string) error { return nil }
+ removed := false
+ a.participation.VerifyLaunches = func(context.Context, uint64) error {
+ if j, _ := readSystemJournal(a.optDir); j == nil {
+ removed = true
+ return harnessRefusal()
+ }
+ return nil
+ }
+ if err := a.Run(t.Context(), true); err != nil {
+ t.Fatalf("recover = %v, want a deferral that exits 0", err)
+ }
+ if !removed {
+ t.Fatal("the census never ran after the journal was removed")
+ }
+ if j, _ := readSystemJournal(a.optDir); j != nil {
+ t.Fatal("journal kept")
+ }
+ want := systemOutcomeRolledBack
+ if tc.forward {
+ want = systemOutcomeApplied
+ }
+ if r := f.result(); !tc.guarded && (r == nil || r.Outcome != want) {
+ t.Fatalf("result = %+v, want %s", r, want)
+ }
+ if got, err := a.participation.UpdateID(); err != nil || got != id {
+ t.Fatalf("update record = %q, %v; want %s held", got, err, id)
+ }
+ if err := a.participation.Acquire(t.Context(), func() error { return nil }); err == nil {
+ t.Fatal("a claim acquisition started while the settlement was deferred")
+ }
+ // The next recover, with the census passing, settles it.
+ a.participation.VerifyLaunches = func(context.Context, uint64) error { return nil }
+ if err := a.Run(t.Context(), true); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := a.participation.UpdateID(); !os.IsNotExist(err) {
+ t.Fatalf("update record after settlement: %v", err)
+ }
+ })
+ }
+}
+
+func TestRecoverStillFailsANonCensusErrorAfterTheJournalIsRemoved(t *testing.T) {
+ f, id := guardRecoveryFixture(t)
+ a := f.a
+ if err := a.floor.Begin(t.Context(), id, a.dataDir); err != nil {
+ t.Fatal(err)
+ }
+ journalOpen(t, f, systemJournal{Phase: systemPhaseGated, Target: "old-build", PrevBuild: "old-build"})
+ a.participation.VerifyLaunches = func(context.Context, uint64) error {
+ if j, _ := readSystemJournal(a.optDir); j == nil {
+ return rootparticipation.ErrUnavailable
+ }
+ return nil
+ }
+ err := a.Run(t.Context(), true)
+ if err == nil || errors.Is(err, errSettlementDeferred) {
+ t.Fatalf("recover = %v, want the settlement's own failure", err)
+ }
+}
+
+func TestGuardedRollbackCloseDefersARefusalAfterTheJournalIsRemoved(t *testing.T) {
+ // The backward close of a guarded transaction (closeGuardRollback, as
+ // recovery's rollback and the apply's rollback call it): the rollback is
+ // recorded, the journal removed, and the settlement's census refuses.
+ // Restoring carlos.prev runs a guarded image, which this fixture
+ // cannot, so the close is driven directly; Run's consumption of the
+ // deferral is the forward case's.
+ f, id := guardRecoveryFixture(t)
+ a := f.a
+ if err := a.floor.Begin(t.Context(), id, a.dataDir); err != nil {
+ t.Fatal(err)
+ }
+ journalOpen(t, f, systemJournal{Phase: systemPhaseInstalled, Target: "new-build", PrevBuild: "old-build"})
+ refuseCensus(t, f)
+ err := a.closeGuardRollback(t.Context())
+ if !errors.Is(err, errSettlementDeferred) || !a.deferSettlement(true, err) {
+ t.Fatalf("rollback close = %v, want a deferral", err)
+ }
+ if a.deferSettlement(false, err) {
+ t.Fatal("the apply consumed a deferral")
+ }
+ if j, _ := readSystemJournal(a.optDir); j != nil {
+ t.Fatal("journal kept")
+ }
+ if begun, err := hostfloor.ReadBegun(a.floor.Dir); err != nil || !begun.RolledBack {
+ t.Fatalf("rollback not recorded: %+v %v", begun, err)
+ }
+ openTransaction(t, f, id, true)
+}
+
+func TestRecoverDoesNotDeferWithoutABarrier(t *testing.T) {
+ // A barrier that cannot be written (here an id the ledger refuses)
+ // leaves the census refusal fatal: recover never exits 0 with
+ // acquisitions open.
+ f, _ := guardRecoveryFixture(t)
+ err := f.a.settleWithoutJournal(t.Context(), "bad id", harnessRefusal())
+ if err == nil || errors.Is(err, errSettlementDeferred) || !strings.Contains(err.Error(), "acquisition barrier") {
+ t.Fatalf("settlement without a barrier = %v", err)
+ }
+ if err := f.a.participation.Acquire(t.Context(), func() error { return nil }); err != nil {
+ t.Fatalf("fixture: %v", err)
+ }
+}
diff --git a/internal/host/systemapply.go b/internal/host/systemapply.go
index 62d6d8eef..ff29f79aa 100644
--- a/internal/host/systemapply.go
+++ b/internal/host/systemapply.go
@@ -369,9 +369,15 @@ func (a *systemApplier) Run(ctx context.Context, recover bool) (retErr error) {
// main leaves it, and the triggers wait for the run that gets past it.
a.rearmPending()
+ // A settlement the census refused after recovery durably removed the
+ // journal is deferred like one with no journal open: --recover exits 0
+ // and does not try it again at the end of the run.
recovered, err := a.recoverIfNeeded(ctx)
if err != nil {
- return err
+ if !a.deferSettlement(recover, err) {
+ return err
+ }
+ deferred = true
}
if recover {
return nil
diff --git a/internal/host/systemparticipation.go b/internal/host/systemparticipation.go
index 9d14514ee..380a61827 100644
--- a/internal/host/systemparticipation.go
+++ b/internal/host/systemparticipation.go
@@ -226,7 +226,7 @@ func (a *systemApplier) checkUnarmedParticipationRecovery(ctx context.Context) e
if recover {
if err := a.controllersRecovered(ctx); err != nil {
if j == nil {
- err = a.settleWithoutJournal(ctx, err)
+ err = a.settleWithoutJournal(ctx, "recover-system", err)
}
return err
}
diff --git a/internal/rootparticipation/holdupdate_test.go b/internal/rootparticipation/holdupdate_test.go
new file mode 100644
index 000000000..fd30aec17
--- /dev/null
+++ b/internal/rootparticipation/holdupdate_test.go
@@ -0,0 +1,68 @@
+package rootparticipation
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "testing"
+)
+
+func TestHoldUpdateRefusesAcquisitionsWithoutACensus(t *testing.T) {
+ l := censusFixture(t)
+ refused := errors.New("census refused")
+ l.VerifyLaunches = func(context.Context, uint64) error { return refused }
+ // BeginUpdate cannot hold: its census refuses and it withdraws the record.
+ if err := l.BeginUpdate(t.Context(), "tx-1"); !errors.Is(err, refused) {
+ t.Fatalf("BeginUpdate = %v", err)
+ }
+ if err := l.Acquire(t.Context(), func() error { return nil }); err != nil {
+ t.Fatalf("fixture: acquisition refused before the hold: %v", err)
+ }
+ if err := l.HoldUpdate(t.Context(), "tx-1"); err != nil {
+ t.Fatal(err)
+ }
+ if id, err := l.UpdateID(); err != nil || id != "tx-1" {
+ t.Fatalf("held record = %q, %v", id, err)
+ }
+ if err := l.Acquire(t.Context(), func() error { return nil }); !errors.Is(err, ErrUnavailable) {
+ t.Fatalf("acquisition under the hold = %v, want refused", err)
+ }
+ // A second hold keeps the record as it is; only EndUpdateWith, with its
+ // census, removes it.
+ if err := l.HoldUpdate(t.Context(), "tx-2"); err != nil {
+ t.Fatal(err)
+ }
+ if id, _ := l.UpdateID(); id != "tx-1" {
+ t.Fatalf("a second hold replaced the record with %q", id)
+ }
+ if err := l.EndUpdateWith(t.Context(), "tx-1", nil); !errors.Is(err, refused) {
+ t.Fatalf("EndUpdateWith under a refusing census = %v", err)
+ }
+ l.VerifyLaunches = func(context.Context, uint64) error { return nil }
+ if err := l.EndUpdateWith(t.Context(), "tx-1", nil); err != nil {
+ t.Fatal(err)
+ }
+ if err := l.Acquire(t.Context(), func() error { return nil }); err != nil {
+ t.Fatalf("acquisition after settlement = %v", err)
+ }
+ if err := l.HoldUpdate(t.Context(), "bad id"); !errors.Is(err, ErrUnavailable) {
+ t.Fatalf("invalid id = %v", err)
+ }
+}
+
+func TestHoldUpdateLeavesAPendingAcquisitionCensusAlone(t *testing.T) {
+ l := censusFixture(t)
+ b, _ := json.Marshal(Census{Schema: 1, Box: "box", Enrollment: "en", Migration: 1, Revision: 2, Complete: true, LaunchRevision: 1, Pending: "acquisition"})
+ if err := l.State.Write("census.json", b); err != nil {
+ t.Fatal(err)
+ }
+ if err := l.HoldUpdate(t.Context(), "tx-1"); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := l.State.Read("update.json"); err == nil {
+ t.Fatal("hold wrote a record over a pending acquisition")
+ }
+ if err := l.Acquire(t.Context(), func() error { return nil }); err == nil {
+ t.Fatal("a pending acquisition census accepted a new acquisition")
+ }
+}
diff --git a/internal/rootparticipation/ledger.go b/internal/rootparticipation/ledger.go
index 66617f575..87d55d7aa 100644
--- a/internal/rootparticipation/ledger.go
+++ b/internal/rootparticipation/ledger.go
@@ -215,6 +215,40 @@ func (l Ledger) EndUpdateWith(ctx context.Context, id string, commit func() erro
return l.State.Remove("update.json")
}
+// HoldUpdate is the acquisition barrier recovery leaves when it defers a
+// settlement its census refused: root's update record under id, written
+// under the transaction lock, which refuses every new acquisition. Unlike
+// BeginUpdate it runs no idle check, since it only adds a refusal and
+// starts nothing; EndUpdateWith, with its census, is still the only way
+// the record goes. An existing record is kept as it is, and a census with
+// an acquisition pending already refuses acquisitions, so neither writes.
+func (l Ledger) HoldUpdate(ctx context.Context, id string) error {
+ if l.State == nil || !validID(id) {
+ return ErrUnavailable
+ }
+ unlock, e := l.lockTransaction(ctx)
+ if e != nil {
+ return e
+ }
+ defer unlock()
+ c, e := l.RecoveryCensus()
+ if e != nil {
+ return e
+ }
+ if c.Pending != "" {
+ return nil
+ }
+ if _, e = l.State.Read("update.json"); !errors.Is(e, os.ErrNotExist) {
+ return e
+ }
+ raw, _ := json.Marshal(struct {
+ Schema int
+ ID string
+ Revision uint64
+ }{1, id, c.Revision})
+ return l.State.Write("update.json", raw)
+}
+
func (l Ledger) UpdateID() (string, error) {
if l.State == nil {
return "", ErrUnavailable