| 1 | package hostbox |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/registry" |
| 7 | ) |
| 8 | |
| 9 | // GenerationHandover is retained transfer evidence, read from root's journal. |
| 10 | // It cannot authorize a new handover or discard a launch reservation. |
| 11 | type GenerationHandover coldJournal |
| 12 | |
| 13 | func DecodeGenerationHandover(raw []byte) (GenerationHandover, error) { |
| 14 | var j coldJournal |
| 15 | if decodeStrict(raw, &j) != nil || j.V != 1 || j.Phase != "complete" || j.IntendedAbsent || j.ServingInstallation == nil { |
| 16 | return GenerationHandover{}, ErrTypedJournal |
| 17 | } |
| 18 | p, n, in := j.Prior, j.Successor, j.Inventory |
| 19 | if p.Host == "" || p.Incarnation == "" || n.Host != p.Host || n.Incarnation != p.Incarnation || n.Box != p.Box || n.Enrollment != p.Enrollment || n.Stamp <= p.Stamp || n.Session != n.Stamp || n.Session <= p.Session || n.Generation == "" || n.ETag == "" || n.Op != "" || n.Phase != "" || p.Op != "" || p.Phase != "" || p.ObligationID != "" || j.ID != "serving-handover-"+ownershipFor(n.Host, n.Incarnation, n.Stamp, n.Box) || !in.Complete || in.Host != p.Host || in.Incarnation != p.Incarnation || in.InstallationStamp != p.Stamp || in.Box != p.Box || in.Enrollment != p.Enrollment || in.DirectoryRevision != p.DirectoryRevision { |
| 20 | return GenerationHandover{}, ErrTypedJournal |
| 21 | } |
| 22 | barrier := p |
| 23 | barrier.FenceID, barrier.Stamp, barrier.Session, barrier.ClaimETag, barrier.DirectoryRevision = j.ID, n.Stamp, n.Session, n.ETag, n.DirectoryRevision |
| 24 | if j.Barrier != barrier { |
| 25 | return GenerationHandover{}, ErrTypedJournal |
| 26 | } |
| 27 | after := j.Before |
| 28 | after.Timers = append([]registry.Timer(nil), j.Before.Timers...) |
| 29 | owner := ownershipFor(n.Host, n.Incarnation, n.Stamp, n.Box) |
| 30 | after.Route.ClaimOwnership = registry.ClaimOwnership{ClaimIncarnation: n.Incarnation, ClaimStamp: n.Stamp, ClaimOwner: owner, ClaimRowID: rowIDFor(owner, "route", n.Host)} |
| 31 | for i := range after.Timers { |
| 32 | after.Timers[i].ClaimOwnership = after.Route.ClaimOwnership |
| 33 | after.Timers[i].ClaimRowID = rowIDFor(owner, "timer", after.Timers[i].Name) |
| 34 | } |
| 35 | if !reflect.DeepEqual(after, j.After) { |
| 36 | return GenerationHandover{}, ErrTypedJournal |
| 37 | } |
| 38 | return GenerationHandover(j), nil |
| 39 | } |
| 40 | |