-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodescan.go
50 lines (45 loc) · 1.27 KB
/
nodescan.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
package veracity
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/datatrails/go-datatrails-merklelog/massifs"
"github.com/urfave/cli/v2"
)
// NewNodeScan implements a sub command which linearly scans for a node in a blob
// This is a debugging tool
func NewNodeScanCmd() *cli.Command {
return &cli.Command{Name: "nodescan",
Usage: "scan a log for a particular node value. this is a debugging tool",
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "massif", Aliases: []string{"m"},
},
&cli.StringFlag{
Name: "value", Aliases: []string{"v"},
},
&cli.BoolFlag{Name: "massif-relative", Aliases: []string{"r"}},
},
Action: func(cCtx *cli.Context) error {
var err error
cmd := &CmdCtx{}
if err = cfgMassif(cmd, cCtx); err != nil {
return err
}
targetValue, err := hex.DecodeString(cCtx.String("value"))
if err != nil {
return err
}
start := cmd.massif.LogStart()
count := cmd.massif.Count()
for i := uint64(0); i < count; i++ {
entry := cmd.massif.Data[start+i*massifs.ValueBytes : start+i*massifs.ValueBytes+massifs.ValueBytes]
if bytes.Compare(entry, targetValue) == 0 {
fmt.Printf("%d\n", i+cmd.massif.Start.FirstIndex)
return nil
}
}
return fmt.Errorf("'%s' not found", cCtx.String("value"))
},
}
}