Download this file
| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // MoveRollbackLossRebind moves a lost target's reference from a declaration |
| 9 | // whose claim CAS never landed (its signed loss record conflicted) to a |
| 10 | // fresh declaration (§7.10, claim Plan 10 J4 B1). Legal only from lost with |
| 11 | // exactly oldRef; the phase, Dirty and the rollback id are kept, so the |
| 12 | // rollback the first declaration decided stands. The caller proves oldRef |
| 13 | // is this move's conflicted declaration and binds newRef in its signed |
| 14 | // intent. The same newRef again is a replay; anything else is a conflict. |
| 15 | func MoveRollbackLossRebind(g MoveGuard, oldRef, newRef string, now time.Time) func(*Record) error { |
| 16 | return func(rec *Record) error { |
| 17 | if oldRef == "" || newRef == "" || oldRef == newRef || now.IsZero() { |
| 18 | return fmt.Errorf("provision: malformed loss declaration") |
| 19 | } |
| 20 | rb, err := rollingBack(g, rec) |
| 21 | if err != nil { |
| 22 | return err |
| 23 | } |
| 24 | switch { |
| 25 | case rb.TargetPhase == RbTargetLost && rb.LossRef == newRef: |
| 26 | return nil |
| 27 | case rb.TargetPhase == RbTargetLost && rb.LossRef == oldRef: |
| 28 | default: |
| 29 | return fmt.Errorf("%w: another loss is recorded for this target", ErrMoveConflict) |
| 30 | } |
| 31 | t := now.UTC() |
| 32 | rb.LossRef, rb.LostAt = newRef, &t |
| 33 | return nil |
| 34 | } |
| 35 | } |
| 36 | |