forked from hyperledger-archives/burrow
/
dump.go
260 lines (228 loc) · 6.35 KB
/
dump.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package dump
import (
"encoding/json"
"fmt"
"io"
"time"
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/acm/acmstate"
"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/encoding"
"github.com/hyperledger/burrow/execution/exec"
"github.com/hyperledger/burrow/execution/names"
"github.com/hyperledger/burrow/execution/state"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/storage"
)
const (
// Whether to send/receive these classes of data
Accounts Option = 1 << iota
Names
Events
)
// Chunk account storage into rows that are less than 1 MiB
const thresholdAccountStorageBytesPerRow = 1 << 20
type Sink interface {
Send(*Dump) error
}
type Blockchain interface {
ChainID() string
LastBlockHeight() uint64
}
type Dumper struct {
state *state.State
blockchain Blockchain
logger *logging.Logger
}
// Return a Dumper that can Transmit Dump rows to a Sink by pulling them out of the the provided State
func NewDumper(state *state.State, blockchain Blockchain) *Dumper {
return &Dumper{
state: state,
blockchain: blockchain,
logger: logging.NewNoopLogger(),
}
}
type Option uint64
const (
None Option = 0
All = Accounts | Names | Events
)
func (options Option) Enabled(option Option) bool {
return options&option > 0
}
// Transmit Dump rows to the provided Sink over the inclusive range of heights provided, if endHeight is 0 the latest
// height is used.
func (ds *Dumper) Transmit(sink Sink, startHeight, endHeight uint64, options Option) error {
lastHeight := ds.blockchain.LastBlockHeight()
if endHeight == 0 || endHeight > lastHeight {
endHeight = lastHeight
}
st, err := ds.state.AtHeight(endHeight)
if err != nil {
return err
}
if options.Enabled(Accounts) {
ds.logger.InfoMsg("Dumping accounts")
err = st.IterateAccounts(func(acc *acm.Account) error {
// Since we tend to want to handle accounts and their storage as a single unit we multiplex account
// and storage within the same row. If the storage gets too large we chunk it and send in separate rows
// (so that we stay well below the 4MiB GRPC message size limit and generally maintain stream-ability)
row := &Dump{
Height: endHeight,
Account: acc,
AccountStorage: &AccountStorage{
Address: acc.Address,
Storage: make([]*Storage, 0),
},
}
for _, m := range acc.ContractMeta {
var metahash acmstate.MetadataHash
copy(metahash[:], m.MetadataHash.Bytes())
meta, err := ds.state.GetMetadata(metahash)
if err != nil {
return err
}
m.Metadata = meta
m.MetadataHash = []byte{}
}
var storageBytes int
err = st.IterateStorage(acc.Address, func(key binary.Word256, value []byte) error {
if storageBytes > thresholdAccountStorageBytesPerRow {
// Send the current row
err = sink.Send(row)
if err != nil {
return err
}
// Start a new pure storage row
row = &Dump{
Height: endHeight,
AccountStorage: &AccountStorage{
Address: acc.Address,
Storage: make([]*Storage, 0),
},
}
}
row.AccountStorage.Storage = append(row.AccountStorage.Storage, &Storage{Key: key, Value: value})
storageBytes += len(key) + len(value)
return nil
})
if err != nil {
return err
}
// Don't send empty storage
if len(row.AccountStorage.Storage) == 0 {
row.AccountStorage = nil
// Don't send an empty row
if row.Account == nil {
// We started a new storage row, but there was no subsequent storage to go in it
return nil
}
}
err = sink.Send(row)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
}
if options.Enabled(Names) {
ds.logger.InfoMsg("Dumping names")
err = st.IterateNames(func(entry *names.Entry) error {
return sink.Send(&Dump{Height: endHeight, Name: entry})
})
if err != nil {
return err
}
}
if options.Enabled(Events) {
ds.logger.InfoMsg("Dumping events")
var blockTime time.Time
var origin *exec.Origin
// Only return events from specified start height - allows for resume
err = ds.state.IterateStreamEvents(&startHeight, &endHeight, storage.AscendingSort,
func(ev *exec.StreamEvent) error {
switch {
case ev.BeginBlock != nil:
ds.logger.TraceMsg("BeginBlock", "height", ev.BeginBlock.Height)
blockTime = ev.BeginBlock.Header.GetTime()
case ev.BeginTx != nil:
origin = ev.BeginTx.TxHeader.Origin
case ev.Event != nil && ev.Event.Log != nil:
row := &Dump{EVMEvent: &EVMEvent{Event: ev.Event.Log}}
if origin != nil {
// this event was already restored
row.EVMEvent.ChainID = origin.ChainID
row.EVMEvent.Time = origin.Time
row.EVMEvent.Index = origin.Index
row.Height = origin.Height
} else {
// this event was generated on this chain
row.EVMEvent.ChainID = ds.blockchain.ChainID()
row.EVMEvent.Time = blockTime
row.EVMEvent.Index = ev.Event.Header.Index
row.Height = ev.Event.Header.Height
}
err := sink.Send(row)
if err != nil {
return err
}
case ev.EndTx != nil:
origin = nil
}
return nil
})
if err != nil {
return err
}
}
return nil
}
// Return a Source that is a Pipe fed from this Dumper's Transmit function
func (ds *Dumper) Source(startHeight, endHeight uint64, options Option) Source {
p := make(Pipe)
go func() {
err := ds.Transmit(p, startHeight, endHeight, options)
if err != nil {
p <- msg{err: err}
}
close(p)
}()
return p
}
func (ds *Dumper) WithLogger(logger *logging.Logger) *Dumper {
ds.logger = logger
return ds
}
// Write a dump to the Writer out by pulling rows from stream
func Write(out io.Writer, source Source, useBinaryEncoding bool, options Option) error {
for {
resp, err := source.Recv()
if err != nil {
if err == io.EOF {
return nil
}
return fmt.Errorf("failed to recv dump: %v", err)
}
if useBinaryEncoding {
_, err := encoding.WriteMessage(out, resp)
if err != nil {
return fmt.Errorf("failed write to binary dump message: %v", err)
}
return nil
}
bs, err := json.Marshal(resp)
if err != nil {
return fmt.Errorf("failed to marshal dump: %v", err)
}
if len(bs) > 0 {
bs = append(bs, []byte("\n")...)
n, err := out.Write(bs)
if err == nil && n < len(bs) {
return fmt.Errorf("failed to write dump: %v", err)
}
}
}
}