@@ -175,7 +175,6 @@ func NewChain(c *Config) (chain *Chain, err error) {
175
175
176
176
// LoadChain loads the chain state from the specified database and rebuilds a memory index.
177
177
func LoadChain (c * Config ) (chain * Chain , err error ) {
178
-
179
178
// Open LevelDB for block and state
180
179
bdbFile := c .DataFile + "-block-state.ldb"
181
180
bdb , err := leveldb .OpenFile (bdbFile , & leveldbConf )
@@ -228,55 +227,55 @@ func LoadChain(c *Config) (chain *Chain, err error) {
228
227
}).Debug ("Loading state from database" )
229
228
230
229
// Read blocks and rebuild memory index
231
- var last * blockNode
232
- var index int32
233
- // TODO(lambda): select a better init length
234
- nodes := make ([] blockNode , 100 )
235
- blockIter := chain . bdb . NewIterator ( util . BytesPrefix ( metaBlockIndex [:]), nil )
230
+ var (
231
+ index int32
232
+ last * blockNode
233
+ blockIter = chain . bdb . NewIterator ( util . BytesPrefix ( metaBlockIndex [:]), nil )
234
+ )
236
235
defer blockIter .Release ()
237
- for blockIter .Next () {
238
- k := blockIter .Key ()
239
- v := blockIter .Value ()
236
+ for index = 0 ; blockIter .Next (); index ++ {
237
+ var (
238
+ k = blockIter .Key ()
239
+ v = blockIter .Value ()
240
+ block = & ct.Block {}
240
241
241
- block := & ct.Block {}
242
+ current , parent * blockNode
243
+ )
242
244
243
245
if err = utils .DecodeMsgPack (v , block ); err != nil {
244
- err = errors .Wrapf (err , "block height %d, key index %s" , keyWithSymbolToHeight (k ), string (k ))
246
+ err = errors .Wrapf (err , "decoding failed at height %d with key %s" ,
247
+ keyWithSymbolToHeight (k ), string (k ))
245
248
return
246
249
}
247
-
248
250
log .WithFields (log.Fields {
249
251
"peer" : chain .rt .getPeerInfoString (),
250
252
"block" : block .BlockHash ().String (),
251
253
}).Debug ("Loading block from database" )
252
- parent := (* blockNode )(nil )
253
254
254
255
if last == nil {
255
256
if err = block .VerifyAsGenesis (); err != nil {
257
+ err = errors .Wrapf (err , "genesis verification failed" )
256
258
return
257
259
}
258
-
259
260
// Set constant fields from genesis block
260
261
chain .rt .setGenesis (block )
261
262
} else if block .ParentHash ().IsEqual (& last .hash ) {
262
263
if err = block .Verify (); err != nil {
264
+ err = errors .Wrapf (err , "block verification failed at height %d with key %s" ,
265
+ keyWithSymbolToHeight (k ), string (k ))
263
266
return
264
267
}
265
-
266
268
parent = last
267
269
} else {
268
- parent = chain .bi .lookupNode (block .ParentHash ())
269
-
270
- if parent == nil {
270
+ if parent = chain .bi .lookupNode (block .ParentHash ()); parent == nil {
271
271
return nil , ErrParentNotFound
272
272
}
273
273
}
274
274
275
- height := chain .rt .getHeightFromTime (block .Timestamp ())
276
- nodes [index ].initBlockNode (height , block , parent )
277
- chain .bi .addBlock (& nodes [index ])
278
- last = & nodes [index ]
279
- index ++
275
+ current = & blockNode {}
276
+ current .initBlockNode (chain .rt .getHeightFromTime (block .Timestamp ()), block , parent )
277
+ chain .bi .addBlock (current )
278
+ last = current
280
279
}
281
280
if err = blockIter .Error (); err != nil {
282
281
err = errors .Wrap (err , "load block" )
0 commit comments