File size: 1,682 Bytes
518343a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
 * ER-Bridge — Einstein-Rosen Reconciliation between the Two Tablets
 *
 * Source: Einstein & Rosen (1935), "The Particle Problem in the General
 *   Theory of Relativity", Phys. Rev. 48:73-77. Wald (1984), General
 *   Relativity, §6.4.
 *
 * The ER-bridge connects the two tablet roots. If both tablets recorded
 * the same governance history, their chainHash roots are identical and
 * the bridge "closes". If not, the bridge reports the first divergent seq.
 */
import type { Tablet } from './dual_tablets';
import { tabletRoot } from './dual_tablets';

export type BridgeReport = {
  closed: boolean;
  primaryRoot: string;
  secondaryRoot: string;
  divergedAtSeq: number | null;
  recommendation: 'continue' | 'halt-and-audit';
};

export function reconcile(primary: Tablet, secondary: Tablet): BridgeReport {
  const primaryRoot = tabletRoot(primary);
  const secondaryRoot = tabletRoot(secondary);
  if (primaryRoot === secondaryRoot && primary.entries.length === secondary.entries.length) {
    return {
      closed: true,
      primaryRoot,
      secondaryRoot,
      divergedAtSeq: null,
      recommendation: 'continue',
    };
  }
  const minLen = Math.min(primary.entries.length, secondary.entries.length);
  let divergedAt: number | null = null;
  for (let i = 0; i < minLen; i++) {
    if (primary.entries[i].chainHash !== secondary.entries[i].chainHash) {
      divergedAt = i;
      break;
    }
  }
  if (divergedAt === null && primary.entries.length !== secondary.entries.length) {
    divergedAt = minLen;
  }
  return {
    closed: false,
    primaryRoot,
    secondaryRoot,
    divergedAtSeq: divergedAt,
    recommendation: 'halt-and-audit',
  };
}