-
Notifications
You must be signed in to change notification settings - Fork 126
/
badger_validation.go
164 lines (155 loc) · 4.56 KB
/
badger_validation.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package storage
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"sort"
"time"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/logger"
)
func (s *BadgerStore) ValidateGraphEntries(networkId crypto.Hash, depth uint64) (int, int, error) {
nodes := s.ReadAllNodes(uint64(time.Now().UnixNano()), false)
stats := make(chan [2]int, len(nodes))
errchan := make(chan error, len(nodes))
for _, n := range nodes {
go func(nodeId crypto.Hash) {
total, invalid, err := s.validateSnapshotEntriesForNode(nodeId, depth)
if err != nil {
logger.Printf("SNAPSHOT VALIDATION ERROR FOR NODE %s %s\n", nodeId, err.Error())
errchan <- err
}
stats <- [2]int{total, invalid}
}(n.IdForNetwork(networkId))
}
var total, invalid int
for i := 0; i < len(nodes); i++ {
select {
case stat := <-stats:
total += stat[0]
invalid += stat[1]
case err := <-errchan:
return total, invalid, err
}
}
return total, invalid, nil
}
func (s *BadgerStore) validateSnapshotEntriesForNode(nodeId crypto.Hash, depth uint64) (int, int, error) {
logger.Printf("SNAPSHOT VALIDATE NODE %s BEGIN\n", nodeId)
txn := s.snapshotsDB.NewTransaction(false)
defer func() {
txn.Discard()
logger.Printf("SNAPSHOT VALIDATE NODE %s DONE\n", nodeId)
}()
head, err := readRound(txn, nodeId)
if err != nil {
return 0, 0, err
}
if head == nil {
logger.Printf("SNAPSHOT VALIDATE NODE %s 0 ROUND\n", nodeId)
return 0, 0, nil
}
logger.Printf("SNAPSHOT VALIDATE NODE %s %d ROUNDS\n", nodeId, head.Number)
start := head.Number - depth
if head.Number < depth {
start = 0
}
invalid, total := 0, 0
for i := start; i < head.Number; i++ {
snapshots, err := readSnapshotsForNodeRound(txn, nodeId, i)
if err != nil {
return total, invalid, err
}
for _, s := range snapshots {
total += 1
item, err := txn.Get(graphTransactionKey(s.SoleTransaction()))
if err != nil {
return total, invalid, err
}
val, err := item.ValueCopy(nil)
if err != nil {
return total, invalid, err
}
ver, err := common.UnmarshalVersionedTransaction(val)
if err != nil {
return total, invalid, err
}
if s.SoleTransaction().String() != ver.PayloadHash().String() {
logger.Printf("MALFORMED TRANSACTION %s %s %#v\n", s.SoleTransaction(), ver.PayloadHash(), ver)
invalid += 1
}
item, err = txn.Get(graphFinalizationKey(s.SoleTransaction()))
if err != nil {
return total, invalid, err
}
val, err = item.ValueCopy(nil)
if err != nil {
return total, invalid, err
}
if s.Hash.String() != hex.EncodeToString(val) {
logger.Printf("DUPLICATED FINALIZATION %s %s\n", s.Hash, hex.EncodeToString(val))
}
dup, _ := crypto.HashFromString(hex.EncodeToString(val))
topo, err := readSnapshotWithTopo(txn, dup)
if err != nil {
return total, invalid, err
}
if topo.SoleTransaction().String() != s.SoleTransaction().String() {
logger.Printf("MALFORMED FINALIZATION %s %s\n", s.Hash, topo.Hash)
invalid += 1
}
}
_, _, hash := computeRoundHash(nodeId, i, snapshots)
round, err := readRound(txn, hash)
if err != nil {
return total, invalid, err
}
if round == nil {
logger.Printf("MISSING ROUND %s %d %s\n", nodeId, i, hash)
invalid += 1
} else if round.NodeId != nodeId || round.Number != i {
logger.Printf("MALFORMED ROUND %s %d %s %s %d\n", nodeId, i, hash, round.NodeId, round.Number)
invalid += 1
}
}
return total, invalid, nil
}
func computeRoundHash(nodeId crypto.Hash, number uint64, snapshots []*common.SnapshotWithTopologicalOrder) (uint64, uint64, crypto.Hash) {
sort.Slice(snapshots, func(i, j int) bool {
if snapshots[i].Timestamp < snapshots[j].Timestamp {
return true
}
if snapshots[i].Timestamp > snapshots[j].Timestamp {
return false
}
a, b := snapshots[i].Hash, snapshots[j].Hash
return bytes.Compare(a[:], b[:]) < 0
})
start := snapshots[0].Timestamp
end := snapshots[len(snapshots)-1].Timestamp
if end >= start+config.SnapshotRoundGap {
err := fmt.Errorf("ComputeRoundHash(%s, %d) %d %d %d", nodeId, number, start, end, start+config.SnapshotRoundGap)
panic(err)
}
version := snapshots[0].Version
for _, s := range snapshots {
if s.Version > version {
version = s.Version
}
}
buf := binary.BigEndian.AppendUint64(nodeId[:], number)
hash := crypto.Blake3Hash(buf)
for _, s := range snapshots {
if s.Version > version {
panic(nodeId)
}
if s.Timestamp > end {
panic(nodeId)
}
hash = crypto.Blake3Hash(append(hash[:], s.Hash[:]...))
}
return start, end, hash
}