package managed import ( "context" "encoding/json" "errors" "fmt" "strings" "sync" "testing" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/ec2" ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/dashpay/dash-network-go/internal/bootstrap" "github.com/dashpay/dash-network-go/internal/journal" "github.com/dashpay/dash-network-go/internal/spec" "github.com/dashpay/dash-network-go/internal/testutil" ) func clone[T any](v T) T { b, _ := json.Marshal(v); var out T; _ = json.Unmarshal(b, &out); return out } func h64(s string) string { return hash(s) } type memory struct { r Record owner string } func (m *memory) Read(_ context.Context, _ Fleet) (Record, string, error) { if m.r.FleetID == "" { return Record{}, "", journal.ErrNotFound } return clone(m.r), m.owner, nil } func (m *memory) Acquire(_ context.Context, f Fleet, s Snapshot, o string) (Record, error) { if m.owner != "" { return Record{}, errors.New("claimed") } m.owner = o if m.r.FleetID == "" { m.r = Record{FleetID: f.Authority(), SnapshotID: s.ID, Phase: "enrolling", Enrolled: map[string]bool{}} for _, t := range f.Targets { m.r.Enrolled[t.Name] = false } } return clone(m.r), nil } func (m *memory) Save(_ context.Context, f Fleet, r Record, o string) error { if m.owner != o || r.Revision != m.r.Revision+1 { return errors.New("stale write") } if e := r.Validate(f); e != nil { return e } m.r = clone(r) return nil } func (m *memory) Release(_ context.Context, _ Fleet, o string) error { if m.owner != o { return errors.New("wrong owner") } m.owner = "" return nil } type cloud struct { f Fleet foreign bool } func (c *cloud) DescribeInstances(_ context.Context, in *ec2.DescribeInstancesInput, _ ...func(*ec2.Options)) (*ec2.DescribeInstancesOutput, error) { o := &ec2.DescribeInstancesOutput{} r := ec2types.Reservation{} for _, t := range c.f.Targets { tag := c.f.Metadata.Name if c.foreign { tag = "other" } arch := ec2types.ArchitectureValuesX8664 if t.Architecture == "arm64" { arch = ec2types.ArchitectureValuesArm64 } r.Instances = append(r.Instances, ec2types.Instance{InstanceId: aws.String(t.InstanceID), PublicIpAddress: aws.String(t.Address), Architecture: arch, State: &ec2types.InstanceState{Name: ec2types.InstanceStateNameRunning}, Tags: []ec2types.Tag{{Key: aws.String(c.f.NetworkTagKey), Value: aws.String(tag)}}}) } o.Reservations = []ec2types.Reservation{r} return clone(o), nil } type backend struct { mu sync.Mutex nodes map[string]Observation calls []Request fail string failOnce bool after func(Request) error observe func(Request, *Observation) } func (b *backend) Call(ctx context.Context, q Request) (Observation, error) { b.mu.Lock() defer b.mu.Unlock() if e := ctx.Err(); e != nil { return Observation{}, e } b.calls = append(b.calls, q) o := clone(b.nodes[q.Target.Name]) if q.Action == b.fail && b.failOnce { b.failOnce = false return Observation{}, errors.New("lost-before-apply") } if q.Action == "observe" { o.Chain.CoreHeight++ o.Chain.ChainLockHeight++ o.Chain.PlatformHeight++ o.Chain.DAPIHeight++ if q.ReferenceHeight > 0 { o.Chain.PlatformHash = h64("same-block") } if b.observe != nil { b.observe(q, &o) } b.nodes[q.Target.Name] = o } if q.Action == "apply" { for c, pin := range q.Pins { v := o.Components[c] if v.Image != pin { v.ID = h64(q.Target.Name + c + pin) v.ImageID = "sha256:" + h64(pin) v.Image = pin v.Digests = []string{pin} } v.Running = true o.Components[c] = v } b.nodes[q.Target.Name] = o } if b.after != nil { if e := b.after(q); e != nil { return Observation{}, e } } return clone(o), nil } func setup(t *testing.T) (Snapshot, Runner, *memory, *backend) { t.Helper() f := Fleet{APIVersion: spec.Version, Kind: "ExistingNetwork", Metadata: spec.Metadata{Name: "devnet-example", DisplayName: "Example", Visibility: "private"}, ChainType: "devnet", CoreNetwork: "devnet-example", AccountID: "123456789012", Region: "us-west-2", NetworkTagKey: "DashNetwork", StateTable: "managed-state", Access: bootstrap.Access{User: "ubuntu", Port: 22, Address: "public"}} powers := map[string]int64{} for i := 1; i <= 12; i++ { powers[h64(fmt.Sprint(i))] = 100 } nodes := map[string]Observation{} for i := 1; i <= 13; i++ { name := fmt.Sprintf("validator-%02d", i) target := Target{Name: name, InstanceID: fmt.Sprintf("i-%017x", i), Address: fmt.Sprintf("192.0.2.%d", i), Architecture: "amd64", Role: "validator", Containers: map[string]string{}} o := Observation{InstanceID: target.InstanceID, At: time.Now().UTC(), Components: map[string]Container{}, Companions: map[string]Container{}, FilesHash: h64("files"), Problems: []string{}, Chain: Chain{CoreNetwork: f.CoreNetwork, CoreGenesis: h64("genesis"), CoreHeight: 100, CoreSynced: true, ChainLockHeight: 99, MasternodeState: "READY", ProTxHash: h64(fmt.Sprint(i)), PlatformChainID: "dash-devnet-example", PlatformHeight: 100, PlatformProtocol: 14, PlatformNodeID: h64(name)[:40], VotingPower: clone(powers), DAPIHeight: 100, DAPIHealthy: true}} for _, c := range Components { target.Containers[c] = name + "-" + c pin := "docker.io/dashpay/" + c + "@sha256:" + strings.Repeat("a", 64) o.Components[c] = Container{ID: h64(name + c), ImageID: "sha256:" + h64(pin), Image: pin, Digests: []string{pin}, ConfigHash: h64("config" + c), Running: true, StartedAt: "2026-09-24T00:00:00Z"} } f.Targets = append(f.Targets, target) nodes[name] = o } b := &backend{nodes: nodes} s := Observe(context.Background(), f, b, 0) if e := s.Complete(); e != nil { t.Fatal(e) } m := &memory{} r := Runner{Identity: testutil.Identity{Account: f.AccountID}, Cloud: &cloud{f: f}, Store: m, Remote: b, Owner: "runner", Window: time.Second, Wait: func(ctx context.Context, _ time.Duration) error { return ctx.Err() }} return s, r, m, b } func pins(s Snapshot, scope, letter string) Images { out := Images{} for _, t := range s.Fleet.Targets { out[t.Name] = map[string]string{} for c := range t.Containers { if Selected(scope, c) { out[t.Name][c] = "docker.io/dashpay/" + c + "@sha256:" + strings.Repeat(letter, 64) } } } return out } func deadline() (context.Context, context.CancelFunc) { return context.WithTimeout(context.Background(), 15*time.Second) } func enroll(t *testing.T, s Snapshot, r Runner) { t.Helper() ctx, c := deadline() defer c() if _, e := r.Enroll(ctx, s); e != nil { t.Fatal(e) } } func TestManagedImportEnrollmentAndSerialUpgrade(t *testing.T) { s, r, m, b := setup(t) if len(b.calls) != 13 { t.Fatal("import lost targets") } for _, q := range b.calls { if q.Action != "observe" { t.Fatal("import mutated") } } enroll(t, s, r) p, e := Build(s, "upgrade", "platform", "", pins(s, "platform", "b"), time.Now()) if e != nil { t.Fatal(e) } last := "" b.after = func(q Request) error { if q.Action == "apply" { if m.r.Current != q.Target.Name { t.Error("apply preceded journal intent") } if last != "" && !m.r.Completed[last] { t.Error("withdrew next node before gate") } last = q.Target.Name } return nil } ctx, c := deadline() defer c() result, e := r.Execute(ctx, p) if e != nil { t.Fatal(e) } if result.Phase != "complete" || m.owner != "" { t.Fatal("incomplete operation") } for name, o := range b.nodes { if hash(o.Components["core"]) != hash(s.Nodes[name].Components["core"]) || hash(o.Components["helper"]) != hash(s.Nodes[name].Components["helper"]) { t.Fatal("unselected workload changed") } } count := len(b.calls) if _, e = r.Execute(ctx, p); e != nil { t.Fatal(e) } for _, q := range b.calls[count:] { if q.Action != "observe" { t.Fatal("completed replay mutated") } } } func TestManagedLostResponseResumesSameNode(t *testing.T) { s, r, m, b := setup(t) enroll(t, s, r) p, e := Build(s, "upgrade", "tenderdash", "", pins(s, "tenderdash", "b"), time.Now()) if e != nil { t.Fatal(e) } lost := false b.after = func(q Request) error { if q.Action == "apply" && !lost { lost = true return errors.New("lost-after-apply") } return nil } ctx, c := deadline() defer c() _, e = r.Execute(ctx, p) if e == nil || m.r.Phase != "interrupted" || m.r.Current != s.Fleet.Targets[0].Name { t.Fatal("lost pending target", e) } pending := m.r.Current id := b.nodes[pending].Components["tenderdash"].ID b.after = nil start := len(b.calls) result, e := r.Execute(ctx, p) if e != nil { t.Fatal(e) } if result.Phase != "complete" || b.nodes[pending].Components["tenderdash"].ID != id { t.Fatal("resume replaced accepted image") } for _, q := range b.calls[start:] { if q.Action == "apply" { if q.Target.Name != pending { t.Fatal("did not reconcile pending first") } break } } } func TestManagedScopeOwnershipStagingAndQuorumGates(t *testing.T) { for _, fault := range []string{"cloud", "claimed", "stage", "member", "core"} { t.Run(fault, func(t *testing.T) { s, r, m, b := setup(t) enroll(t, s, r) p, e := Build(s, "upgrade", "platform", "", pins(s, "platform", "b"), time.Now()) if e != nil { t.Fatal(e) } switch fault { case "cloud": r.Cloud.(*cloud).foreign = true case "claimed": m.owner = "other" case "stage": b.fail = "stage" b.failOnce = true case "member": for n, o := range b.nodes { o.Chain.VotingPower = map[string]int64{h64("unmanaged"): 100} b.nodes[n] = o } case "core": n := s.Fleet.Targets[0].Name o := b.nodes[n] v := o.Components["core"] v.StartedAt = "2026-09-24T01:00:00Z" o.Components["core"] = v b.nodes[n] = o } ctx, c := deadline() defer c() start := len(b.calls) if _, e = r.Execute(ctx, p); e == nil { t.Fatal("unsafe change accepted") } for _, q := range b.calls[start:] { if q.Action == "apply" { t.Fatal("mutated after gate failure") } } }) } } func TestManagedDeployRestoresStoppedImagesAndRejectsStalePlan(t *testing.T) { s, r, m, b := setup(t) enroll(t, s, r) p, e := Build(s, "deploy", "platform", "", pins(s, "platform", "a"), time.Now()) if e != nil { t.Fatal(e) } for n, o := range b.nodes { for c, v := range o.Components { if Selected("platform", c) { v.Running = false o.Components[c] = v } } b.nodes[n] = o } ctx, c := deadline() defer c() result, e := r.Execute(ctx, p) if e != nil { t.Fatal(e) } if result.Phase != "complete" { t.Fatal("restore incomplete") } old, e := Build(s, "upgrade", "core", "", pins(s, "core", "b"), time.Now()) if e != nil { t.Fatal(e) } if _, e = r.Execute(ctx, old); e == nil { t.Fatal("stale predecessor accepted") } if m.r.OperationID != p.ID { t.Fatal("old intent overwrote current runtime") } } func TestManagedDeployRejectsReplacedSourceBeforeAnyStaging(t *testing.T) { s, r, m, b := setup(t) enroll(t, s, r) p, err := Build(s, "deploy", "platform", "", pins(s, "platform", "a"), time.Now()) if err != nil { t.Fatal(err) } // Place drift on the final host so earlier hosts cannot be partially restored. name := s.Fleet.Targets[len(s.Fleet.Targets)-1].Name o := b.nodes[name] v := o.Components["tenderdash"] v.ID = h64("foreign same-name replacement") v.Running = false o.Components["tenderdash"] = v b.nodes[name] = o start := len(b.calls) ctx, cancel := deadline() defer cancel() if _, err = r.Execute(ctx, p); err == nil || m.r.OperationID != "" { t.Fatal("replacement was adopted or operation started", err) } for _, q := range b.calls[start:] { if q.Action != "observe" { t.Fatal("started mutating despite a replaced source") } } } func TestManagedSnapshotAndPlanValidation(t *testing.T) { s, _, _, _ := setup(t) p, e := Build(s, "upgrade", "platform", "", pins(s, "platform", "b"), time.Now()) if e != nil { t.Fatal(e) } p.Images[s.Fleet.Targets[0].Name]["core"] = "docker.io/dashpay/core@sha256:" + strings.Repeat("b", 64) p.ID = "" p.ID = hash(p) if p.Validate() == nil { t.Fatal("scope escape accepted") } s.Nodes[s.Fleet.Targets[0].Name] = Observation{InstanceID: s.Fleet.Targets[0].InstanceID, Error: "unreachable"} s.ID = "" s.ID = hash(s) if s.Complete() == nil { t.Fatal("unknown target accepted") } } func TestCoreUpgradeDoesNotPermitNativeTorRestart(t *testing.T) { base := Observation{NativeProcesses: map[string]string{"dashd:123": "old-core", "tor:456": "unchanged"}} current := clone(base) current.NativeProcesses["dashd:123"] = "new-core" if e := Preserved(base, current, []string{"core"}); e != nil { t.Fatal(e) } current.NativeProcesses["tor:456"] = "restarted" if Preserved(base, current, []string{"core"}) == nil { t.Fatal("Core scope also permitted Tor restart") } } func TestReadOnlyImportPreservesUnknownTarget(t *testing.T) { s, r, _, b := setup(t) b.fail = "observe" b.failOnce = true ctx, c := deadline() defer c() observed := Observe(ctx, s.Fleet, r.Remote, 0) if len(observed.Nodes) != 13 || observed.Complete() == nil { t.Fatal("unknown target silently dropped") } if observed.Validate() != nil { t.Fatal("partial evidence could not be retained") } } // A new Core release may spend minutes migrating its indexes on first start // (v24 on testnet). The gate waits on the replaced node alone, then verifies. func TestManagedUpgradeWaitsForCoreStartup(t *testing.T) { s, r, _, b := setup(t) enroll(t, s, r) p, e := Build(s, "upgrade", "core", "", pins(s, "core", "b"), time.Now()) if e != nil { t.Fatal(e) } starting := map[string]int{} b.after = func(q Request) error { if q.Action == "apply" { starting[q.Target.Name] = 5 } return nil } b.observe = func(q Request, o *Observation) { if starting[q.Target.Name] > 0 { starting[q.Target.Name]-- o.Problems = []string{"core-starting: Loading block index", "platform-health-unavailable"} } else { o.Problems = []string{} } } var progress []string r.Progress = func(line string) { progress = append(progress, line) } ctx, c := deadline() defer c() if _, e = r.Execute(ctx, p); e != nil { t.Fatal(e) } log := strings.Join(progress, "\n") if !strings.Contains(log, "managed target validator-01 starting: Core starting (Loading block index)") || !strings.Contains(log, "managed target validator-01 ready") { t.Fatal("startup not reported:\n" + log) } if strings.Contains(log, "health gate waiting") { t.Fatal("fleet gate ran before the replaced node served again:\n" + log) } } func TestManagedUpgradeReadyTimeout(t *testing.T) { s, r, m, b := setup(t) enroll(t, s, r) p, e := Build(s, "upgrade", "core", "", pins(s, "core", "b"), time.Now()) if e != nil { t.Fatal(e) } applied := false b.after = func(q Request) error { applied = applied || q.Action == "apply" return nil } b.observe = func(q Request, o *Observation) { if applied && q.Target.Name == "validator-01" { o.Problems = []string{"core-starting: Verifying blocks"} } } r.ReadyTimeout = 50 * time.Millisecond r.Wait = func(ctx context.Context, _ time.Duration) error { return sleep(ctx, 5*time.Millisecond) } ctx, c := deadline() defer c() _, e = r.Execute(ctx, p) if e == nil || !strings.Contains(e.Error(), "validator-01 not ready within 50ms: Core starting (Verifying blocks)") { t.Fatal(e) } if m.r.Phase != "interrupted" || m.r.Current != "validator-01" { t.Fatal("interruption not journaled for resume", m.r.Phase, m.r.Current) } // The operation deadline ends the wait with a message naming it. r.ReadyTimeout = time.Hour short, c2 := context.WithTimeout(context.Background(), 300*time.Millisecond) defer c2() _, e = r.Execute(short, p) if !errors.Is(e, context.DeadlineExceeded) || !strings.Contains(e.Error(), "validator-01 was still starting when the operation stopped (Core starting (Verifying blocks))") { t.Fatal(e) } } // Public testnet can go several minutes without a block: a window in which no // node advanced proves nothing, so Doctor extends it instead of failing all. func TestDoctorExtendsWindowWithoutNewBlock(t *testing.T) { s, r, _, b := setup(t) sweeps := map[string]int{} idle := 2 b.observe = func(q Request, o *Observation) { sweeps[q.Target.Name]++ if sweeps[q.Target.Name] <= idle { o.Chain.CoreHeight-- o.Chain.ChainLockHeight-- } } waits := 0 wait := func(ctx context.Context, _ time.Duration) error { waits++; return ctx.Err() } ctx, c := deadline() defer c() h, e := Doctor(ctx, s, r.Remote, time.Second, wait) if e != nil || !h.Healthy || waits != 2 { t.Fatal(e, h.Problems, waits) } // A chain that stays stalled past every extension still fails. sweeps, idle, waits = map[string]int{}, 100, 0 h, e = Doctor(ctx, s, r.Remote, time.Second, wait) if e != nil || h.Healthy || waits != 1+idleExtensions || !strings.Contains(strings.Join(h.Problems, ";"), "validator-01: Core not advancing") { t.Fatal(e, h.Problems, waits) } } // A block that lands mid-sweep reaches only the nodes sampled after it: only // the nodes that have not advanced are sampled again, and only focus targets. func TestDoctorResamplesOnlyLaggingFocusNodes(t *testing.T) { s, r, _, b := setup(t) sweeps := map[string]int{} b.observe = func(q Request, o *Observation) { sweeps[q.Target.Name]++ // validator-01..06 miss the block in the second sweep; validator-13 is stuck. n := q.Target.Name if (n <= "validator-06" && sweeps[n] == 2) || n == "validator-13" { o.Chain.CoreHeight-- o.Chain.ChainLockHeight-- } } wait := func(ctx context.Context, _ time.Duration) error { return ctx.Err() } ctx, c := deadline() defer c() h, e := doctor(ctx, s, r.Remote, time.Second, wait, []string{"validator-02"}) if e != nil || !h.Nodes["validator-02"].Healthy || sweeps["validator-02"] != 3 || sweeps["validator-01"] != 2 || sweeps["validator-13"] != 2 { t.Fatal(e, h.Nodes["validator-02"], sweeps) } if h.Nodes["validator-13"].Healthy || h.Snapshot.Validate() != nil { t.Fatal("stuck node hidden or snapshot not rehashed") } // Whole-fleet doctor: every lagging node is re-sampled; the stuck one still fails. sweeps = map[string]int{} h, e = Doctor(ctx, s, r.Remote, time.Second, wait) if e != nil || !h.Nodes["validator-01"].Healthy || h.Nodes["validator-13"].Healthy || sweeps["validator-13"] != 2+idleExtensions || sweeps["validator-07"] != 2 { t.Fatal(e, h.Problems, sweeps) } } func TestManagedUpgradeFailsFastOnPoSeBan(t *testing.T) { s, r, _, b := setup(t) enroll(t, s, r) p, e := Build(s, "upgrade", "core", "", pins(s, "core", "b"), time.Now()) if e != nil { t.Fatal(e) } applied := false b.after = func(q Request) error { applied = applied || q.Action == "apply"; return nil } b.observe = func(q Request, o *Observation) { if applied && q.Target.Name == "validator-01" { o.Chain.MasternodeState = "POSE_BANNED" } } ctx, c := deadline() defer c() if _, e = r.Execute(ctx, p); e == nil || !strings.Contains(e.Error(), "validator-01: masternode POSE_BANNED after the change") { t.Fatal(e) } }