Skip to content

Commit 9d447a6

Browse files
committed
Fix index out of bound, refactor part of sqlchain code
1 parent bb34914 commit 9d447a6

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

sqlchain/chain.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ func NewChain(c *Config) (chain *Chain, err error) {
175175

176176
// LoadChain loads the chain state from the specified database and rebuilds a memory index.
177177
func LoadChain(c *Config) (chain *Chain, err error) {
178-
179178
// Open LevelDB for block and state
180179
bdbFile := c.DataFile + "-block-state.ldb"
181180
bdb, err := leveldb.OpenFile(bdbFile, &leveldbConf)
@@ -228,55 +227,55 @@ func LoadChain(c *Config) (chain *Chain, err error) {
228227
}).Debug("Loading state from database")
229228

230229
// 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+
)
236235
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{}
240241

241-
block := &ct.Block{}
242+
current, parent *blockNode
243+
)
242244

243245
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))
245248
return
246249
}
247-
248250
log.WithFields(log.Fields{
249251
"peer": chain.rt.getPeerInfoString(),
250252
"block": block.BlockHash().String(),
251253
}).Debug("Loading block from database")
252-
parent := (*blockNode)(nil)
253254

254255
if last == nil {
255256
if err = block.VerifyAsGenesis(); err != nil {
257+
err = errors.Wrapf(err, "genesis verification failed")
256258
return
257259
}
258-
259260
// Set constant fields from genesis block
260261
chain.rt.setGenesis(block)
261262
} else if block.ParentHash().IsEqual(&last.hash) {
262263
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))
263266
return
264267
}
265-
266268
parent = last
267269
} else {
268-
parent = chain.bi.lookupNode(block.ParentHash())
269-
270-
if parent == nil {
270+
if parent = chain.bi.lookupNode(block.ParentHash()); parent == nil {
271271
return nil, ErrParentNotFound
272272
}
273273
}
274274

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
280279
}
281280
if err = blockIter.Error(); err != nil {
282281
err = errors.Wrap(err, "load block")

0 commit comments

Comments
 (0)