@@ -49,21 +49,24 @@ type DumpCollector interface {
49
49
50
50
// DumpAccount represents an account in the state.
51
51
type DumpAccount struct {
52
- Balance string `json:"balance"`
53
- Nonce uint64 `json:"nonce"`
54
- Root hexutil.Bytes `json:"root"`
55
- CodeHash hexutil.Bytes `json:"codeHash"`
56
- Code hexutil.Bytes `json:"code,omitempty"`
57
- Storage map [common.Hash ]string `json:"storage,omitempty"`
58
- Address * common.Address `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode
59
- SecureKey hexutil.Bytes `json:"key,omitempty"` // If we don't have address, we can output the key
52
+ Balance string `json:"balance"`
53
+ Nonce uint64 `json:"nonce"`
54
+ Root hexutil.Bytes `json:"root"`
55
+ CodeHash hexutil.Bytes `json:"codeHash"`
56
+ Code hexutil.Bytes `json:"code,omitempty"`
57
+ Storage map [common.Hash ]string `json:"storage,omitempty"`
58
+ Address * common.Address `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode
59
+ AddressHash hexutil.Bytes `json:"key,omitempty"` // If we don't have address, we can output the key
60
60
61
61
}
62
62
63
63
// Dump represents the full dump in a collected format, as one large map.
64
64
type Dump struct {
65
- Root string `json:"root"`
66
- Accounts map [common.Address ]DumpAccount `json:"accounts"`
65
+ Root string `json:"root"`
66
+ Accounts map [string ]DumpAccount `json:"accounts"`
67
+ // Next can be set to represent that this dump is only partial, and Next
68
+ // is where an iterator should be positioned in order to continue the dump.
69
+ Next []byte `json:"next,omitempty"` // nil if no more accounts
67
70
}
68
71
69
72
// OnRoot implements DumpCollector interface
@@ -73,27 +76,11 @@ func (d *Dump) OnRoot(root common.Hash) {
73
76
74
77
// OnAccount implements DumpCollector interface
75
78
func (d * Dump ) OnAccount (addr * common.Address , account DumpAccount ) {
76
- if addr ! = nil {
77
- d .Accounts [* addr ] = account
79
+ if addr = = nil {
80
+ d .Accounts [fmt . Sprintf ( "pre(%s)" , account . AddressHash ) ] = account
78
81
}
79
- }
80
-
81
- // IteratorDump is an implementation for iterating over data.
82
- type IteratorDump struct {
83
- Root string `json:"root"`
84
- Accounts map [common.Address ]DumpAccount `json:"accounts"`
85
- Next []byte `json:"next,omitempty"` // nil if no more accounts
86
- }
87
-
88
- // OnRoot implements DumpCollector interface
89
- func (d * IteratorDump ) OnRoot (root common.Hash ) {
90
- d .Root = fmt .Sprintf ("%x" , root )
91
- }
92
-
93
- // OnAccount implements DumpCollector interface
94
- func (d * IteratorDump ) OnAccount (addr * common.Address , account DumpAccount ) {
95
82
if addr != nil {
96
- d .Accounts [* addr ] = account
83
+ d .Accounts [( * addr ). String () ] = account
97
84
}
98
85
}
99
86
@@ -105,14 +92,14 @@ type iterativeDump struct {
105
92
// OnAccount implements DumpCollector interface
106
93
func (d iterativeDump ) OnAccount (addr * common.Address , account DumpAccount ) {
107
94
dumpAccount := & DumpAccount {
108
- Balance : account .Balance ,
109
- Nonce : account .Nonce ,
110
- Root : account .Root ,
111
- CodeHash : account .CodeHash ,
112
- Code : account .Code ,
113
- Storage : account .Storage ,
114
- SecureKey : account .SecureKey ,
115
- Address : addr ,
95
+ Balance : account .Balance ,
96
+ Nonce : account .Nonce ,
97
+ Root : account .Root ,
98
+ CodeHash : account .CodeHash ,
99
+ Code : account .Code ,
100
+ Storage : account .Storage ,
101
+ AddressHash : account .AddressHash ,
102
+ Address : addr ,
116
103
}
117
104
d .Encode (dumpAccount )
118
105
}
@@ -150,26 +137,27 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
150
137
if err := rlp .DecodeBytes (it .Value , & data ); err != nil {
151
138
panic (err )
152
139
}
153
- account := DumpAccount {
154
- Balance : data .Balance .String (),
155
- Nonce : data .Nonce ,
156
- Root : data .Root [:],
157
- CodeHash : data .CodeHash ,
158
- SecureKey : it .Key ,
159
- }
160
140
var (
161
- addrBytes = s .trie .GetKey (it .Key )
162
- addr = common .BytesToAddress (addrBytes )
141
+ account = DumpAccount {
142
+ Balance : data .Balance .String (),
143
+ Nonce : data .Nonce ,
144
+ Root : data .Root [:],
145
+ CodeHash : data .CodeHash ,
146
+ AddressHash : it .Key ,
147
+ }
163
148
address * common.Address
149
+ addr common.Address
150
+ addrBytes = s .trie .GetKey (it .Key )
164
151
)
165
152
if addrBytes == nil {
166
- // Preimage missing
167
153
missingPreimages ++
168
154
if conf .OnlyWithAddresses {
169
155
continue
170
156
}
171
157
} else {
158
+ addr = common .BytesToAddress (addrBytes )
172
159
address = & addr
160
+ account .Address = address
173
161
}
174
162
obj := newObject (s , addr , & data )
175
163
if ! conf .SkipCode {
@@ -220,12 +208,13 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
220
208
return nextKey
221
209
}
222
210
223
- // RawDump returns the entire state an a single large object
211
+ // RawDump returns the state. If the processing is aborted e.g. due to options
212
+ // reaching Max, the `Next` key is set on the returned Dump.
224
213
func (s * StateDB ) RawDump (opts * DumpConfig ) Dump {
225
214
dump := & Dump {
226
- Accounts : make (map [common. Address ]DumpAccount ),
215
+ Accounts : make (map [string ]DumpAccount ),
227
216
}
228
- s .DumpToCollector (dump , opts )
217
+ dump . Next = s .DumpToCollector (dump , opts )
229
218
return * dump
230
219
}
231
220
@@ -234,7 +223,7 @@ func (s *StateDB) Dump(opts *DumpConfig) []byte {
234
223
dump := s .RawDump (opts )
235
224
json , err := json .MarshalIndent (dump , "" , " " )
236
225
if err != nil {
237
- fmt . Println ( "Dump err" , err )
226
+ log . Error ( "Error dumping state" , " err" , err )
238
227
}
239
228
return json
240
229
}
@@ -243,12 +232,3 @@ func (s *StateDB) Dump(opts *DumpConfig) []byte {
243
232
func (s * StateDB ) IterativeDump (opts * DumpConfig , output * json.Encoder ) {
244
233
s .DumpToCollector (iterativeDump {output }, opts )
245
234
}
246
-
247
- // IteratorDump dumps out a batch of accounts starts with the given start key
248
- func (s * StateDB ) IteratorDump (opts * DumpConfig ) IteratorDump {
249
- iterator := & IteratorDump {
250
- Accounts : make (map [common.Address ]DumpAccount ),
251
- }
252
- iterator .Next = s .DumpToCollector (iterator , opts )
253
- return * iterator
254
- }
0 commit comments