reeve 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +106 -1
  3. data/README.md +113 -6
  4. data/lib/generators/reeve/install/templates/create_audit_entries.rb.tt +41 -3
  5. data/lib/generators/reeve/install/templates/initializer.rb.tt +20 -0
  6. data/lib/generators/reeve/upgrade/templates/add_ledger_chain_to_reeve_audit_entries.rb.tt +51 -0
  7. data/lib/generators/reeve/upgrade/upgrade_generator.rb +40 -2
  8. data/lib/reeve/audit/chain/canonical.rb +129 -0
  9. data/lib/reeve/audit/chain/head.rb +94 -0
  10. data/lib/reeve/audit/chain/keyring.rb +53 -0
  11. data/lib/reeve/audit/chain/normalizer.rb +88 -0
  12. data/lib/reeve/audit/chain/report.rb +134 -0
  13. data/lib/reeve/audit/chain/verification.rb +110 -0
  14. data/lib/reeve/audit/chain/verifier.rb +160 -0
  15. data/lib/reeve/audit/chain/walk.rb +123 -0
  16. data/lib/reeve/audit/chain.rb +168 -0
  17. data/lib/reeve/audit/chain_record.rb +61 -0
  18. data/lib/reeve/audit/isolated_recorder.rb +5 -0
  19. data/lib/reeve/audit/recorder.rb +32 -19
  20. data/lib/reeve/audit.rb +24 -4
  21. data/lib/reeve/configuration.rb +66 -2
  22. data/lib/reeve/errors.rb +15 -0
  23. data/lib/reeve/ledger_key.rb +47 -0
  24. data/lib/reeve/railtie.rb +15 -0
  25. data/lib/reeve/tasks.rb +62 -0
  26. data/lib/reeve/testing/checks/contract_version.rb +2 -1
  27. data/lib/reeve/testing/checks/ledger_chain.rb +116 -0
  28. data/lib/reeve/testing/checks.rb +29 -4
  29. data/lib/reeve/testing/compliance_assertions.rb +3 -2
  30. data/lib/reeve/testing/compliance_suite.rb +2 -2
  31. data/lib/reeve/testing/ledger.rb +11 -1
  32. data/lib/reeve/testing/report.rb +8 -0
  33. data/lib/reeve/testing.rb +1 -1
  34. data/lib/reeve/version.rb +1 -1
  35. data/lib/reeve.rb +2 -0
  36. metadata +17 -2
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Reeve
4
+ module Audit
5
+ class Chain
6
+ # The chain record's head, advanced under its lock (research R3).
7
+ #
8
+ # One instance per append, bound to the connection that append runs on; every
9
+ # statement here goes through that connection, so the lock, the row and the head move
10
+ # share one transaction and commit or roll back together.
11
+ #
12
+ # UPDATE head_sequence by one -- the lock, taken first
13
+ # SELECT the new head_sequence and the old head_digest
14
+ # ... the caller inserts the row ...
15
+ # UPDATE head_digest to the row's digest
16
+ #
17
+ # Taking the lock with a write is what makes this behave the same on all three
18
+ # engines: SQLite cannot upgrade a read lock while another writer waits, and ignores
19
+ # SELECT ... FOR UPDATE.
20
+ class Head
21
+ def initialize(connection:, entry_class:)
22
+ @connection = connection
23
+ @entry_class = entry_class
24
+ end
25
+
26
+ # Creates the chain record with its genesis — link 0, head 0 — unless it exists.
27
+ #
28
+ # Deliberately not part of the write that follows. Run first inside it, an UPDATE that
29
+ # matched no row took a gap lock on MySQL's unique index, and two writers starting the
30
+ # chain at once each tried to INSERT into the gap the other held: a deadlock, found
31
+ # by CI, invisible on PostgreSQL, which has no gap locks. On its own, two concurrent
32
+ # INSERTs of one unique name serialize: one wins and the other gets a duplicate key,
33
+ # which means the chain has started and is all it needs to know.
34
+ #
35
+ # So a started chain can exist with nothing in it yet (head 0) — if the first chained
36
+ # write then fails, say. The verifier reports that as no chain, not as a pass.
37
+ def start!(signer)
38
+ return if connection.select_value("SELECT 1 FROM #{TABLE} WHERE #{named}")
39
+
40
+ genesis = genesis_for(signer)
41
+ digest = Canonical.genesis_digest(signer.signing_secret, genesis)
42
+ values = genesis.merge(name: ChainRecord::LEDGER, genesis_digest: digest,
43
+ head_sequence: 0, head_digest: digest)
44
+ quoted = values.values.map { |value| connection.quote(value) }
45
+
46
+ entry_class.transaction(requires_new: true) do
47
+ connection.insert("INSERT INTO #{TABLE} (#{values.keys.join(', ')}) " \
48
+ "VALUES (#{quoted.join(', ')})")
49
+ end
50
+ rescue ActiveRecord::RecordNotUnique
51
+ nil
52
+ end
53
+
54
+ # Locks the head and returns the next sequence and the digest it links to. The chain
55
+ # must have been started.
56
+ def advance!
57
+ vanished = increment.zero?
58
+ raise ActiveRecord::StatementInvalid, "the ledger chain record vanished" if vanished
59
+
60
+ head = connection.select_one("SELECT head_sequence, head_digest FROM #{TABLE} " \
61
+ "WHERE #{named}")
62
+ [Integer(head["head_sequence"]), head["head_digest"]]
63
+ end
64
+
65
+ def point_at!(digest)
66
+ connection.update("UPDATE #{TABLE} SET head_digest = #{connection.quote(digest)} " \
67
+ "WHERE #{named}")
68
+ end
69
+
70
+ private
71
+
72
+ attr_reader :connection, :entry_class
73
+
74
+ def increment
75
+ connection.update("UPDATE #{TABLE} SET head_sequence = head_sequence + 1 WHERE #{named}")
76
+ end
77
+
78
+ def genesis_for(signer)
79
+ {
80
+ genesis_at: Time.now.utc.floor(6),
81
+ pre_chain_count: entry_class.where("contract_version < 3").count,
82
+ pre_chain_unchained_count:
83
+ entry_class.where(UNCHAINED).count,
84
+ genesis_key_id: signer.signing_id
85
+ }
86
+ end
87
+
88
+ def named
89
+ "name = #{connection.quote(ChainRecord::LEDGER)}"
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Reeve
4
+ module Audit
5
+ class Chain
6
+ # The keys one chained write or one verification may use (specs/002 FR-011, R7).
7
+ #
8
+ # One key signs; any number verify. Rows name their key by fingerprint, so lookup is
9
+ # by fingerprint too, and a key the ring does not hold is simply absent — the verifier
10
+ # turns that into "unverifiable", never into "tampered".
11
+ #
12
+ # Keyring.from(Reeve.config) # what the recorder signs with
13
+ # Keyring.new(signing: nil, retired: [auditor_key]) # verify-only, e.g. an auditor
14
+ class Keyring
15
+ def self.from(config)
16
+ new(signing: config.ledger_key, retired: config.retired_ledger_keys)
17
+ end
18
+
19
+ def initialize(signing:, retired: [])
20
+ @signing_secret = signing
21
+ @by_id = {}
22
+ [signing, *retired].compact.each do |secret|
23
+ @by_id[LedgerKey.fingerprint(secret)] ||= secret
24
+ end
25
+ @by_id.freeze
26
+ freeze
27
+ end
28
+
29
+ attr_reader :signing_secret
30
+
31
+ def signing_id
32
+ signing_secret && LedgerKey.fingerprint(signing_secret)
33
+ end
34
+
35
+ def signing?
36
+ !signing_secret.nil?
37
+ end
38
+
39
+ def for(key_id)
40
+ @by_id[key_id]
41
+ end
42
+
43
+ def empty?
44
+ @by_id.empty?
45
+ end
46
+
47
+ def inspect
48
+ "#<#{self.class.name} signing=#{signing_id.inspect} keys=#{@by_id.keys.inspect}>"
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Reeve
4
+ module Audit
5
+ class Chain
6
+ # Puts a row into the form the database will hand back, before it is written
7
+ # (specs/002 FR-003, research R2 step 1).
8
+ #
9
+ # The digest is computed at write time and recomputed at verification from what the
10
+ # database returns. The two only agree if nothing is left for the database to change,
11
+ # so anything it would change is changed here first:
12
+ #
13
+ # * JSON values go through the column's own ActiveRecord type, encode and decode — the
14
+ # exact path a write takes. Symbol keys become strings, a Time inside the arguments
15
+ # becomes the string ActiveRecord stores, NaN becomes nil.
16
+ # * `occurred_at` is floored to the precision the column keeps, so the database has
17
+ # no fraction to round (MySQL rounds; it does not truncate), and moved to UTC.
18
+ #
19
+ # What remains — JSON key order and how a number is spelled — is the database's to
20
+ # change, and Canonical makes it irrelevant.
21
+ #
22
+ # It cannot recover a value the database alters for good: an integer beyond double
23
+ # precision in a MySQL JSON column is stored rounded. That shows up as a false break,
24
+ # never a false pass, and is documented.
25
+ class Normalizer
26
+ JSON_FIELDS = %i[arguments record_ids metadata].freeze
27
+ TIME_FIELD = :occurred_at
28
+
29
+ # Undeclared precision: MySQL's DATETIME is whole seconds, the others keep
30
+ # microseconds.
31
+ WHOLE_SECOND_ADAPTERS = /mysql|trilogy/i
32
+ DEFAULT_PRECISION = 6
33
+
34
+ def self.effective_precision(column, adapter_name)
35
+ return column.precision unless column.precision.nil?
36
+
37
+ adapter_name.to_s.match?(WHOLE_SECOND_ADAPTERS) ? 0 : DEFAULT_PRECISION
38
+ end
39
+
40
+ def initialize(entry_class)
41
+ @entry_class = entry_class
42
+ end
43
+
44
+ # A new hash; the row passed in is not modified.
45
+ def call(row)
46
+ # Symbol keys throughout, whatever the caller built: a host recorder (FR-007) may use
47
+ # strings, and every lookup below — and the replay check after it — is by symbol.
48
+ normalized = row.to_h.transform_keys(&:to_sym)
49
+ JSON_FIELDS.each do |field|
50
+ next unless normalized.key?(field)
51
+
52
+ normalized[field] = round_trip(field, normalized[field])
53
+ end
54
+ normalized[TIME_FIELD] = floor(normalized[TIME_FIELD]) if normalized[TIME_FIELD]
55
+ normalized
56
+ end
57
+
58
+ private
59
+
60
+ attr_reader :entry_class
61
+
62
+ def round_trip(field, value)
63
+ return nil if value.nil?
64
+
65
+ type = json_types[field]
66
+ type.deserialize(type.serialize(value))
67
+ end
68
+
69
+ def floor(time)
70
+ time.to_time.getutc.floor(precision)
71
+ end
72
+
73
+ def json_types
74
+ @json_types ||= JSON_FIELDS.to_h do |field|
75
+ [field, entry_class.type_for_attribute(field.to_s)]
76
+ end
77
+ end
78
+
79
+ def precision
80
+ @precision ||= self.class.effective_precision(
81
+ entry_class.columns_hash.fetch(TIME_FIELD.to_s),
82
+ entry_class.connection_db_config.adapter
83
+ )
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Reeve
4
+ module Audit
5
+ class Chain
6
+ # A Verification in words (contracts/ledger-chain.md "Output").
7
+ #
8
+ # Every summary line carries its own coverage. A reader who copies only the first line
9
+ # into a ticket still copies how much of the ledger the word in it covers — "INTACT"
10
+ # never travels without "sequences 1–n" and the count of what was not covered.
11
+ #
12
+ # Unknown-key breaks are summarized, not listed: there is one per row a missing key
13
+ # signed, and the summary already names the key and the count.
14
+ class Report
15
+ LABEL_WIDTH = 14
16
+ KIND_WIDTH = 17
17
+ PREFIX = "reeve ledger:"
18
+
19
+ def self.number(value)
20
+ digits = value.abs.to_s.reverse.scan(/\d{1,3}/).join(",").reverse
21
+ value.negative? ? "-#{digits}" : digits
22
+ end
23
+
24
+ def initialize(verification)
25
+ @v = verification
26
+ end
27
+
28
+ def to_s(limit:)
29
+ listed = @v.breaks.reject { |found| found.kind == :unknown_key }
30
+ return summary if listed.empty?
31
+
32
+ lines = listed.first(limit).map { |found| line_for(found) }
33
+ [summary, totals, *lines, more(listed, limit)].compact.join("\n")
34
+ end
35
+
36
+ private
37
+
38
+ def summary
39
+ case @v.status
40
+ when :intact then intact
41
+ when :broken then broken
42
+ when :no_chain then no_chain
43
+ else @v.reason == :no_key ? no_key : unverifiable
44
+ end
45
+ end
46
+
47
+ def intact
48
+ "#{PREFIX} INTACT — sequences #{range(@v.covered)} verified " \
49
+ "(head at start: #{number(@v.head_at_start)}); #{uncovered}"
50
+ end
51
+
52
+ def broken
53
+ count = @v.breaks.size
54
+ where = @v.covered ? "in sequences #{range(@v.covered)}" : "in the ledger"
55
+ "#{PREFIX} BROKEN — #{number(count)} break#{'s' unless count == 1} #{where}; #{uncovered}"
56
+ end
57
+
58
+ def no_chain
59
+ "#{PREFIX} NO CHAIN — #{number(@v.uncovered_now + @v.unchained_now)} entries, " \
60
+ "none chained; nothing was verified"
61
+ end
62
+
63
+ def no_key
64
+ "#{PREFIX} UNVERIFIABLE — no ledger key configured; nothing was verified"
65
+ end
66
+
67
+ def unverifiable
68
+ unknown = @v.breaks.select { |found| found.kind == :unknown_key }
69
+ rows = unknown.count { |found| entry_position?(found.sequence) }
70
+ checked = @v.covered ? @v.covered.size - rows : 0
71
+ "#{PREFIX} UNVERIFIABLE — #{number(rows)} entries signed by #{missing_keys(unknown)} " \
72
+ "not configured; the other #{number(checked)} in #{range(@v.covered)} show no " \
73
+ "breaks, which is not a pass; #{number(@v.uncovered_now)} earlier entries not covered"
74
+ end
75
+
76
+ # The genesis is position 0 and is not an entry.
77
+ def entry_position?(sequence)
78
+ sequence.is_a?(Integer) && sequence.positive?
79
+ end
80
+
81
+ def missing_keys(unknown)
82
+ keys = unknown.map(&:key_id).compact.uniq
83
+ keys.one? ? "key #{keys.first}, which is" : "keys #{keys.join(', ')}, which are"
84
+ end
85
+
86
+ def uncovered
87
+ parts = ["#{number(@v.uncovered_now)} earlier entries not covered"]
88
+ at_genesis = @v.uncovered_at_genesis
89
+ parts << " (#{number(at_genesis)} when the chain started)" if at_genesis
90
+ unless @v.unchained_at_genesis.to_i.zero?
91
+ parts << "; #{number(@v.unchained_at_genesis)} unchained entries from before the " \
92
+ "chain started, not covered"
93
+ end
94
+ parts.join
95
+ end
96
+
97
+ def totals
98
+ " totals: #{@v.totals.map { |kind, count| "#{kind} #{count}" }.join(', ')}"
99
+ end
100
+
101
+ def more(listed, limit)
102
+ remaining = listed.size - limit
103
+ return nil unless limit.positive? && remaining.positive?
104
+
105
+ " … and #{number(remaining)} more (the totals above are complete)"
106
+ end
107
+
108
+ def line_for(found)
109
+ kind = found.kind.to_s.ljust(KIND_WIDTH)
110
+ " #{label(found).ljust(LABEL_WIDTH)}#{kind}#{found.detail}".rstrip
111
+ end
112
+
113
+ def label(found)
114
+ sequence = found.sequence
115
+ return "invocation #{found.invocation_id}" if found.invocation_id
116
+ return "—" if sequence.nil?
117
+ return "##{range(sequence)}" if sequence.is_a?(Range)
118
+
119
+ sequence.zero? ? "genesis" : "##{number(sequence)}"
120
+ end
121
+
122
+ def range(value)
123
+ return "—" if value.nil?
124
+
125
+ "#{number(value.first)}–#{number(value.last)}"
126
+ end
127
+
128
+ def number(value)
129
+ self.class.number(value.to_i)
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Reeve
4
+ module Audit
5
+ class Chain
6
+ # What a verification established, and — as importantly — what it did not
7
+ # (specs/002 FR-012–FR-014, data-model "Verification result").
8
+ #
9
+ # The status says whether the chain held; the coverage says over what. They are
10
+ # separate fields so that no reader can take one for the other: an intact chain that
11
+ # covers the last 300 rows of a ledger of 5,300 is intact *for those 300*.
12
+ class Verification
13
+ STATUSES = %i[intact broken unverifiable no_chain].freeze
14
+
15
+ # `sequence` is an Integer or a Range (a gap), or nil for a break that has no
16
+ # position; an unchained row has none and is named by `invocation_id` instead.
17
+ # `key_id` is set for unknown_key: the fingerprint of the key the verifier lacked.
18
+ Break = Struct.new(:kind, :sequence, :detail, :invocation_id, :key_id, keyword_init: true)
19
+
20
+ DEFAULT_LIMIT = 100
21
+
22
+ KINDS = %i[
23
+ digest_mismatch missing duplicate unchained head_mismatch beyond_head
24
+ genesis_altered chain_record_missing unknown_key
25
+ ].freeze
26
+
27
+ # The status rules (data-model), for a verification that had a key and found a
28
+ # chain; the no-key and no-chain answers are given before a walk ever starts. Any
29
+ # break but an unknown key is a broken chain. Rows only an absent key could check
30
+ # leave it unverifiable — never intact, and never called tampered.
31
+ def self.status_for(breaks)
32
+ kinds = breaks.map(&:kind)
33
+ return :broken if (kinds - [:unknown_key]).any?
34
+ return :unverifiable if kinds.include?(:unknown_key)
35
+
36
+ :intact
37
+ end
38
+
39
+ attr_reader :status, :covered, :breaks, :head_at_start, :head_at_end,
40
+ :uncovered_now, :uncovered_at_genesis, :unchained_now, :unchained_at_genesis,
41
+ :reason
42
+
43
+ def initialize(status:, covered: nil, breaks: [], head_at_start: nil, head_at_end: nil,
44
+ uncovered_now: 0, uncovered_at_genesis: nil, unchained_now: 0,
45
+ unchained_at_genesis: nil, reason: nil)
46
+ raise ArgumentError, "unknown status #{status.inspect}" unless STATUSES.include?(status)
47
+
48
+ @status = status
49
+ @covered = covered
50
+ @breaks = breaks.freeze
51
+ @head_at_start = head_at_start
52
+ @head_at_end = head_at_end
53
+ @uncovered_now = uncovered_now
54
+ @uncovered_at_genesis = uncovered_at_genesis
55
+ @unchained_now = unchained_now
56
+ @unchained_at_genesis = unchained_at_genesis
57
+ @reason = reason
58
+ freeze
59
+ end
60
+
61
+ def intact?
62
+ status == :intact
63
+ end
64
+
65
+ # Never capped, whatever the display does with the list.
66
+ def totals
67
+ breaks.each_with_object(Hash.new(0)) { |b, counts| counts[b.kind] += 1 }
68
+ end
69
+
70
+ def unverifiable_count
71
+ totals[:unknown_key]
72
+ end
73
+
74
+ # The report a person reads (contracts/ledger-chain.md "Output"). The first line
75
+ # states the status and what it covers together; no line says "intact" or "pass"
76
+ # without the covered range and the uncovered count beside it (SC-006). `limit`
77
+ # caps the breaks listed, never the totals.
78
+ def to_s(limit: DEFAULT_LIMIT)
79
+ Report.new(self).to_s(limit: limit)
80
+ end
81
+
82
+ def as_json(*)
83
+ {
84
+ "status" => status.to_s, "covered" => range_json(covered),
85
+ "head_at_start" => head_at_start, "head_at_end" => head_at_end,
86
+ "uncovered_now" => uncovered_now, "uncovered_at_genesis" => uncovered_at_genesis,
87
+ "unchained_now" => unchained_now, "unchained_at_genesis" => unchained_at_genesis,
88
+ "unverifiable_count" => unverifiable_count,
89
+ "totals" => totals.transform_keys(&:to_s), "reason" => reason&.to_s,
90
+ "breaks" => breaks.map { |b| break_json(b) }
91
+ }
92
+ end
93
+
94
+ private
95
+
96
+ def break_json(found)
97
+ {
98
+ "kind" => found.kind.to_s, "sequence" => range_json(found.sequence),
99
+ "detail" => found.detail, "invocation_id" => found.invocation_id,
100
+ "key_id" => found.key_id
101
+ }
102
+ end
103
+
104
+ def range_json(value)
105
+ value.is_a?(Range) ? { "first" => value.first, "last" => value.last } : value
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Reeve
4
+ module Audit
5
+ class Chain
6
+ # Checks the chain and reports every place it no longer holds (specs/002 FR-012–FR-015,
7
+ # research R6).
8
+ #
9
+ # The chain record is read first; its head is where the walk (Walk) stops. After the
10
+ # walk the record is checked against what was found — the genesis against its digest,
11
+ # the head against the last row — and the head is read again. Rows between the two
12
+ # reads are writes that committed during the walk: legitimate, and left for the next
13
+ # run. A row above the second read cannot be legitimate, because reeve commits a row
14
+ # and its head move together, so it is reported. Without that second read, a row
15
+ # forged past the head would never be examined by any run.
16
+ #
17
+ # == What it cannot say
18
+ #
19
+ # A failing digest means the row's content, its position or its predecessor changed;
20
+ # which one is not knowable from the digest, and the report does not guess. Rows
21
+ # written before the chain began are counted, never verified.
22
+ class Verifier
23
+ BATCH_SIZE = 1_000
24
+
25
+ RECORD_MISSING = "chained rows exist but the chain record does not"
26
+ UNCHAINED_ROW = "a contract-3 row outside the chain, written after it started (by clock)"
27
+
28
+ def initialize(entry_class:, keyring:, batch_size: BATCH_SIZE)
29
+ @entry_class = entry_class
30
+ @keyring = keyring
31
+ @batch_size = batch_size
32
+ end
33
+
34
+ def call
35
+ return unwalked(:unverifiable, :no_key) if keyring.empty?
36
+
37
+ ChainRecord.require_same_database!(entry_class)
38
+
39
+ record = read_head
40
+ return unwalked(:no_chain) unless record&.head_sequence&.positive? || chained_rows?
41
+
42
+ verify_chain(record)
43
+ end
44
+
45
+ private
46
+
47
+ attr_reader :entry_class, :keyring, :batch_size, :breaks
48
+
49
+ def verify_chain(record)
50
+ @breaks = []
51
+ walk = walk(record)
52
+ check_record(record, walk)
53
+ head_at_end = record && read_head
54
+ check_beyond_head(head_at_end || record)
55
+ check_unchained(record)
56
+
57
+ result(record, head_at_end, walk.last_sequence)
58
+ end
59
+
60
+ def walk(record)
61
+ Walk.new(entry_class: entry_class, keyring: keyring, batch_size: batch_size,
62
+ link: record&.genesis_digest, report: method(:add))
63
+ .call(record&.head_sequence)
64
+ end
65
+
66
+ # A seam as well as a read: the specs wrap it to land a write mid-walk.
67
+ def read_head
68
+ ChainRecord.uncached { ChainRecord.find_by(name: ChainRecord::LEDGER) }
69
+ end
70
+
71
+ def check_record(record, walk)
72
+ return add(:chain_record_missing, nil, RECORD_MISSING) if record.nil?
73
+
74
+ check_genesis(record)
75
+ head = record.head_sequence
76
+ last_digest = walk.last_sequence.zero? ? record.genesis_digest : walk.last_digest
77
+ return if walk.last_sequence == head && last_digest == record.head_digest
78
+
79
+ add(:head_mismatch, head,
80
+ "the head is at #{head}, the last chained row found is #{walk.last_sequence}")
81
+ end
82
+
83
+ def check_genesis(record)
84
+ key_id = record.genesis_key_id
85
+ secret = keyring.for(key_id)
86
+ if secret.nil?
87
+ return add(:unknown_key, 0, "the genesis is signed by key #{key_id}", nil, key_id)
88
+ end
89
+ return if Canonical.genesis_digest(secret, record) == record.genesis_digest
90
+
91
+ add(:genesis_altered, 0, "the genesis does not match its digest")
92
+ end
93
+
94
+ def check_beyond_head(record)
95
+ return if record.nil?
96
+
97
+ candidates = entry_class.where("chain_sequence > ?", record.head_sequence)
98
+ .order(:chain_sequence).pluck(:chain_sequence)
99
+ return if candidates.empty?
100
+
101
+ # Read the head once more, after the search. A row committed between the last read
102
+ # and the search moved the head with it, so the head now covers it; a row above
103
+ # this read cannot have been written by reeve.
104
+ head = read_head&.head_sequence || record.head_sequence
105
+ detail = "numbered past the head (#{Report.number(head)} at the end of the walk); " \
106
+ "reeve did not write it"
107
+ candidates.select { |sequence| sequence > head }
108
+ .each { |sequence| add(:beyond_head, sequence, detail) }
109
+ end
110
+
111
+ # Contract-3 rows with no digest, beyond the number that already existed when the
112
+ # chain began (research R5). Which ones are new is known only by clock, and the
113
+ # detail says so.
114
+ def check_unchained(record)
115
+ return if count_unchained <= (record ? record.pre_chain_unchained_count : 0)
116
+
117
+ scope = entry_class.where(UNCHAINED)
118
+ scope = scope.where("occurred_at > ?", record.genesis_at) if record
119
+ scope.order(:occurred_at).pluck(:invocation_id).each do |invocation_id|
120
+ add(:unchained, nil, UNCHAINED_ROW, invocation_id)
121
+ end
122
+ end
123
+
124
+ def add(kind, sequence, detail = nil, invocation_id = nil, key_id = nil)
125
+ breaks << Verification::Break.new(kind: kind, sequence: sequence, detail: detail,
126
+ invocation_id: invocation_id, key_id: key_id)
127
+ end
128
+
129
+ def result(record, head_at_end, last_sequence)
130
+ Verification.new(
131
+ status: Verification.status_for(breaks), breaks: breaks,
132
+ covered: covered(record, last_sequence),
133
+ head_at_start: record&.head_sequence, head_at_end: head_at_end&.head_sequence,
134
+ uncovered_now: count_uncovered, uncovered_at_genesis: record&.pre_chain_count,
135
+ unchained_now: count_unchained, unchained_at_genesis: record&.pre_chain_unchained_count
136
+ )
137
+ end
138
+
139
+ def covered(record, last_sequence)
140
+ last = record ? record.head_sequence : last_sequence
141
+ last.positive? ? 1..last : nil
142
+ end
143
+
144
+ # An answer given before any walk: no key to walk with, or no chain to walk.
145
+ def unwalked(status, reason = nil)
146
+ Verification.new(status: status, reason: reason,
147
+ uncovered_now: count_uncovered, unchained_now: count_unchained)
148
+ end
149
+
150
+ def chained_rows?
151
+ entry_class.where.not(chain_sequence: nil).exists?
152
+ end
153
+
154
+ def count_uncovered = (@count_uncovered ||= entry_class.where("contract_version < 3").count)
155
+
156
+ def count_unchained = (@count_unchained ||= entry_class.where(UNCHAINED).count)
157
+ end
158
+ end
159
+ end
160
+ end