This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
explore.go
210 lines (180 loc) · 5.93 KB
/
explore.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package commands
import (
"encoding/json"
"fmt"
"os"
"github.com/hyperledger/burrow/config"
"github.com/hyperledger/burrow/forensics"
"github.com/hyperledger/burrow/bcm"
"github.com/hyperledger/burrow/txs"
cli "github.com/jawher/mow.cli"
dbm "github.com/tendermint/tm-db"
)
// Explore chain state(s)
func Explore(output Output) func(cmd *cli.Cmd) {
return func(cmd *cli.Cmd) {
configOpts := addConfigOptions(cmd)
var conf *config.BurrowConfig
var explorer *bcm.BlockStore
var err error
cmd.Before = func() {
conf, err = configOpts.obtainBurrowConfig()
if err != nil {
output.Fatalf("could not obtain config: %v", err)
}
tmConf, err := conf.TendermintConfig()
if err != nil {
output.Fatalf("could not build Tendermint config:", err)
}
if conf.GenesisDoc == nil {
output.Fatalf("genesis doc is required")
}
explorer = bcm.NewBlockExplorer(dbm.DBBackendType(tmConf.DBBackend), tmConf.DBDir())
}
cmd.Command("dump", "pretty print the state tree at the given height", func(cmd *cli.Cmd) {
heightOpt := cmd.IntOpt("height", 0, "The height to read, defaults to latest")
stateDir := cmd.StringArg("STATE", "", "Directory containing burrow state")
cmd.Spec = "[--height] [STATE]"
cmd.Before = func() {
if err := isDir(*stateDir); err != nil {
output.Fatalf("could not obtain state: %v", err)
}
}
cmd.Action = func() {
replay := forensics.NewReplayFromDir(conf.GenesisDoc, *stateDir)
height := uint64(*heightOpt)
if height == 0 {
height, err = replay.LatestHeight()
if err != nil {
output.Fatalf("could not read latest height: %v", err)
}
}
err := replay.LoadAt(height)
if err != nil {
output.Fatalf("could not load state: %v", err)
}
fmt.Println(replay.State.Dump())
}
})
cmd.Command("compare", "diff the state of two .burrow directories", func(cmd *cli.Cmd) {
goodDir := cmd.StringArg("GOOD", "", "Directory containing expected state")
badDir := cmd.StringArg("BAD", "", "Directory containing invalid state")
heightOpt := cmd.IntOpt("height", 0, "The height to read, defaults to latest")
cmd.Spec = "[--height] [GOOD] [BAD]"
cmd.Before = func() {
if err := isDir(*goodDir); err != nil {
output.Fatalf("could not obtain state: %v", err)
}
if err := isDir(*badDir); err != nil {
output.Fatalf("could not obtain state: %v", err)
}
}
cmd.Action = func() {
replay1 := forensics.NewReplayFromDir(conf.GenesisDoc, *goodDir)
replay2 := forensics.NewReplayFromDir(conf.GenesisDoc, *badDir)
h1, err := replay1.LatestHeight()
if err != nil {
output.Fatalf("could not get height for first replay: %v", err)
}
h2, err := replay2.LatestHeight()
if err != nil {
output.Fatalf("could not get height for second replay: %v", err)
}
height := h1
if *heightOpt != 0 {
height = uint64(*heightOpt)
} else if h2 < h1 {
height = h2
output.Printf("States do not agree on last height, using min: %d", h2)
} else {
output.Printf("Using default last height: %d", h1)
}
recap1, err := replay1.Blocks(1, height)
if err != nil {
output.Fatalf("could not replay first state: %v", err)
}
recap2, err := replay2.Blocks(1, height)
if err != nil {
output.Fatalf("could not replay second state: %v", err)
}
if height, err := forensics.CompareCaptures(recap1, recap2); err != nil {
output.Printf("difference in capture: %v", err)
// TODO: compare at every height?
if err := forensics.CompareStateAtHeight(replay1.State, replay2.State, height); err != nil {
output.Fatalf("difference in state: %v", err)
}
}
output.Printf("States match!")
}
})
cmd.Command("blocks", "dump blocks to stdout", func(cmd *cli.Cmd) {
rangeArg := cmd.StringArg("RANGE", "", "Range as START_HEIGHT:END_HEIGHT where omitting "+
"either endpoint implicitly describes the start/end and a negative index counts back from the last block")
cmd.Spec = "[RANGE]"
cmd.Action = func() {
start, end, err := parseRange(*rangeArg)
if err != nil {
output.Fatalf("could not parse range '%s': %v", *rangeArg, err)
}
err = explorer.Blocks(start, end,
func(block *bcm.Block) error {
bs, err := json.Marshal(block)
if err != nil {
output.Fatalf("Could not serialise block: %v", err)
}
output.Printf(string(bs))
return nil
})
if err != nil {
output.Fatalf("Error iterating over blocks: %v", err)
}
}
})
cmd.Command("txs", "dump transactions to stdout", func(cmd *cli.Cmd) {
rangeArg := cmd.StringArg("RANGE", "", "Range as START_HEIGHT:END_HEIGHT where omitting "+
"either endpoint implicitly describes the start/end and a negative index counts back from the last block")
cmd.Spec = "[RANGE]"
cmd.Action = func() {
start, end, err := parseRange(*rangeArg)
if err != nil {
output.Fatalf("could not parse range '%s': %v", *rangeArg, err)
}
err = explorer.Blocks(start, end,
func(block *bcm.Block) error {
err := block.Transactions(func(txEnv *txs.Envelope) error {
wrapper := struct {
Height int64
Tx *txs.Envelope
}{
Height: block.Height,
Tx: txEnv,
}
bs, err := json.Marshal(wrapper)
if err != nil {
output.Fatalf("Could not deserialise transaction: %v", err)
}
output.Printf(string(bs))
return nil
})
if err != nil {
output.Fatalf("Error iterating over transactions: %v", err)
}
// If we stopped transactions stop everything
return nil
})
if err != nil {
output.Fatalf("Error iterating over blocks: %v", err)
}
}
})
}
}
func isDir(path string) error {
file, err := os.Stat(path)
if err != nil {
return fmt.Errorf("could not read state directory: %v", err)
} else if !file.IsDir() {
return fmt.Errorf("%s is not a directory", path)
}
return nil
}