~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
84 lines · 2.4 KB Raw History
package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"log"
)

// runCompare checks that two firehose summaries agree on every repo: a live
// consumer against a cursor-0 replay, or a PDS against a relay downstream.
func runCompare(args []string) error {
	fs := flag.NewFlagSet("compare", flag.ExitOnError)
	counts := fs.Bool("counts", true, "also require the same number of #commit events per repo")
	fs.Parse(args)
	if fs.NArg() != 2 {
		return fmt.Errorf("usage: interop-check compare [-counts=false] A.json B.json")
	}
	load := func(path string) (*summary, error) {
		b, err := readFile(path)
		if err != nil {
			return nil, err
		}
		var s summary
		return &s, json.Unmarshal(b, &s)
	}
	a, err := load(fs.Arg(0))
	if err != nil {
		return err
	}
	b, err := load(fs.Arg(1))
	if err != nil {
		return err
	}
	fail := &failures{}
	for _, did := range sortedKeys(a.Repos) {
		x := a.Repos[did]
		y, ok := b.Repos[did]
		if !ok {
			if x.Commits > 0 {
				fail.add("%s: in %s, missing from %s", did, a.Source, b.Source)
			}
			continue
		}
		if x.Rev != y.Rev || x.Commit != y.Commit || x.Data != y.Data {
			fail.add("%s: %s ends at %s/%s, %s at %s/%s", did, a.Source, x.Rev, x.Commit, b.Source, y.Rev, y.Commit)
		}
		if *counts && x.Commits != y.Commits {
			fail.add("%s: %d commits from %s, %d from %s", did, x.Commits, a.Source, y.Commits, b.Source)
		}
		if x.Complete && y.Complete {
			for _, path := range sortedKeys(x.Records) {
				if x.Records[path] != y.Records[path] {
					fail.add("%s: record %s is %s in %s, %q in %s", did, path, x.Records[path], a.Source, y.Records[path], b.Source)
				}
			}
			for path := range y.Records {
				if _, ok := x.Records[path]; !ok {
					fail.add("%s: record %s only in %s", did, path, b.Source)
				}
			}
		}
		if activeOf(x) != activeOf(y) || x.Status != y.Status {
			fail.add("%s: account %v/%q in %s, %v/%q in %s", did, activeOf(x), x.Status, a.Source, activeOf(y), y.Status, b.Source)
		}
	}
	for did, y := range b.Repos {
		if _, ok := a.Repos[did]; !ok && y.Commits > 0 {
			fail.add("%s: in %s, missing from %s", did, b.Source, a.Source)
		}
	}
	log.Printf("compare %s vs %s: %d repos, %d failures", a.Source, b.Source, len(a.Repos), fail.count())
	if n := fail.count(); n > 0 {
		return fmt.Errorf("%d differences", n)
	}
	return nil
}

func activeOf(s *repoState) string {
	if s.Active == nil {
		return "unknown"
	}
	return fmt.Sprint(*s.Active)
}