-
Notifications
You must be signed in to change notification settings - Fork 0
/
ediag.go
184 lines (163 loc) · 5.86 KB
/
ediag.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package veracity
import (
"context"
"crypto/sha256"
"encoding/binary"
"fmt"
"reflect"
"time"
"github.com/datatrails/go-datatrails-merklelog/massifs"
"github.com/datatrails/go-datatrails-merklelog/massifs/snowflakeid"
"github.com/datatrails/go-datatrails-merklelog/mmr"
"github.com/datatrails/go-datatrails-simplehash/simplehash"
"github.com/urfave/cli/v2"
)
// NewEventDiagCmd provides diagnostic support for event verification
//
//nolint:gocognit
func NewEventDiagCmd() *cli.Command {
return &cli.Command{Name: "event-log-info",
Aliases: []string{"ediag"},
Usage: "print diagnostics about an events entry in the log",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "bendump", Aliases: []string{"b"}, Value: false},
&cli.StringFlag{
Name: "json", Aliases: []string{"j"},
},
},
Action: func(cCtx *cli.Context) error {
decodedEvents, err := stdinToDecodedEvents()
if err != nil {
return err
}
cmd := &CmdCtx{}
if err = cfgMassifReader(cmd, cCtx); err != nil {
return err
}
cmpPrint := func(fmtEq, fmtNe string, a, b any) bool {
if reflect.DeepEqual(a, b) {
fmt.Printf(fmtEq, a)
return true
}
fmt.Printf(fmtNe, a, b)
return false
}
for _, decodedEvent := range decodedEvents {
if decodedEvent.MerkleLog == nil || decodedEvent.MerkleLog.Commit == nil {
continue
}
// Get the mmrIndex from the request and then compute the massif
// it implies based on the massifHeight command line option.
mmrIndex := decodedEvent.MerkleLog.Commit.Index
massifIndex := massifs.MassifIndexFromMMRIndex(cmd.massifHeight, mmrIndex)
tenantIdentity := cCtx.String("tenant")
if tenantIdentity == "" {
// The tenant identity on the event is the original tenant
// that created the event. For public assets and shared
// assets, this is true regardless of which tenancy the
// record is fetched from. Those same events will appear in
// the logs of all tenants they were shared with.
tenantIdentity = decodedEvent.V3Event.TenantIdentity
}
// read the massif blob
cmd.massif, err = cmd.massifReader.GetMassif(context.Background(), tenantIdentity, massifIndex)
if err != nil {
return err
}
// Get the human time from the idtimestamp committed on the event.
eventIDTimestamp, _, err := massifs.SplitIDTimestampHex(decodedEvent.MerkleLog.Commit.Idtimestamp)
if err != nil {
return err
}
eventIDTimestampMS, err := snowflakeid.IDUnixMilli(eventIDTimestamp, uint8(cmd.massif.Start.CommitmentEpoch))
if err != nil {
return err
}
leafIndex := mmr.LeafIndex(mmrIndex)
// Note that the banner info is all from the event response
fmt.Printf("%d %s %s\n", leafIndex, time.UnixMilli(eventIDTimestampMS).Format(time.RFC3339Nano), decodedEvent.V3Event.Identity)
leafIndexMassif, err := cmd.massif.GetMassifLeafIndex(leafIndex)
if err != nil {
return fmt.Errorf("when expecting %d for %d: %v", leafIndexMassif, mmrIndex, err)
}
fmt.Printf(" |%8d leaf-index-massif\n", leafIndexMassif)
// Read the trie entry from the log
logTrieEntry := massifs.GetTrieEntry(cmd.massif.Data, cmd.massif.IndexStart(), leafIndexMassif)
logNodeValue, err := cmd.massif.Get(mmrIndex)
if err != nil {
return err
}
logTrieIDTimestampBytes := logTrieEntry[massifs.TrieEntryIdTimestampStart:massifs.TrieEntryIdTimestampEnd]
logTrieIDTimestamp := binary.BigEndian.Uint64(logTrieIDTimestampBytes)
unixMS, err := snowflakeid.IDUnixMilli(logTrieIDTimestamp, uint8(cmd.massif.Start.CommitmentEpoch))
if err != nil {
return err
}
idTime := time.UnixMilli(unixMS)
trieKey := massifs.NewTrieKey(
massifs.KeyTypeApplicationContent,
[]byte(tenantIdentity),
[]byte(decodedEvent.V3Event.Identity))
if len(trieKey) != massifs.TrieKeyBytes {
return massifs.ErrIndexEntryBadSize
}
cmpPrint(
" |%x trie-key\n",
" |%x != log-trie-key %x\n", trieKey[:32], logTrieEntry[:32])
fmt.Printf(" |%x %s log-idtimestamp\n", logTrieIDTimestampBytes, idTime.Format(time.DateTime))
cmpPrint(
" |%x idtimestamp\n",
" |%x != log-idtimestamp %x\n", eventIDTimestamp, logTrieIDTimestamp)
// Compute the event data hash, independent of domain and idtimestamp
eventHasher := sha256.New()
if err = simplehash.V3HashEvent(eventHasher, decodedEvent.V3Event); err != nil {
return err
}
eventHash := eventHasher.Sum(nil)
fmt.Printf(" |%x v3hash (just the schema fields hashed)\n", eventHash)
if cCtx.Bool("bendump") {
bencode, err2 := bencodeEvent(decodedEvent.V3Event)
if err2 != nil {
return err2
}
fmt.Printf(" |%s\n", string(bencode))
}
leafHasher := simplehash.NewHasherV3()
err = leafHasher.HashEventFromV3(
decodedEvent.V3Event,
simplehash.WithPrefix([]byte{LeafTypePlain}),
simplehash.WithIDCommitted(eventIDTimestamp))
if err != nil {
return err
}
leafHash := leafHasher.Sum(nil)
ok := cmpPrint(
" |%x leaf\n",
" |%x leaf != log-leaf %x\n", leafHash, logNodeValue)
if !ok {
// if the leaf doesn't match we definitely cant verify it
continue
}
// Generate the proof for the mmrIndex and get the root. We use
// the mmrSize from the end of the blob in which the leaf entry
// was recorded. Any size > than the leaf index would work.
mmrSize := cmd.massif.RangeCount()
proof, err := mmr.InclusionProof(&cmd.massif, mmrSize, mmrIndex)
if err != nil {
return err
}
verified, err := mmr.VerifyInclusion(&cmd.massif, eventHasher, mmrSize, logNodeValue, mmrIndex, proof)
if verified {
fmt.Printf("OK|%d %d\n", mmrIndex, leafIndex)
continue
}
if err != nil {
fmt.Printf("XX|%d %d|%s\n", mmrIndex, leafIndex, err.Error())
continue
}
fmt.Printf("XX|%d %d\n", mmrIndex, leafIndex)
}
return nil
},
}
}