Skip to content

Commit 89dbf0a

Browse files
authored
correct zero hash in log (iotexproject#2389)
1 parent fdf5043 commit 89dbf0a

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

blockchain/blockchain.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,14 @@ func (bc *blockchain) commitBlock(blk *block.Block) error {
492492
putTimer := bc.timerFactory.NewTimer("putBlock")
493493
err = bc.dao.PutBlock(ctx, blk)
494494
putTimer.End()
495-
if err != nil {
495+
switch {
496+
case errors.Cause(err) == blockdao.ErrAlreadyExist:
497+
return nil
498+
case err != nil:
496499
return err
497500
}
501+
blkHash := blk.HashBlock()
502+
blk.HeaderLogger(log.L()).Info("Committed a block.", log.Hex("tipHash", blkHash[:]))
498503
blockMtc.WithLabelValues("numActions").Set(float64(len(blk.Actions)))
499504
// emit block to all block subscribers
500505
bc.emitToSubscribers(blk)

blockchain/blockdao/blockdao.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ var (
6060
heightToFileBucket = []byte("h2f")
6161
)
6262

63+
// vars
6364
var (
6465
cacheMtc = prometheus.NewCounterVec(
6566
prometheus.CounterOpts{
@@ -68,12 +69,9 @@ var (
6869
},
6970
[]string{"result"},
7071
)
71-
patternLen = len("00000000.db")
72-
suffixLen = len(".db")
73-
// ErrNotOpened indicates db is not opened
74-
ErrNotOpened = errors.New("DB is not opened")
75-
76-
// ErrNotSupported indicates not supported
72+
patternLen = len("00000000.db")
73+
suffixLen = len(".db")
74+
ErrAlreadyExist = errors.New("block already exist")
7775
ErrNotSupported = errors.New("feature not supported")
7876
)
7977

@@ -403,7 +401,7 @@ func (dao *blockDAO) PutBlock(ctx context.Context, blk *block.Block) error {
403401
blkHash, err := dao.getBlockHash(blkHeight)
404402
if err == nil && blkHash != hash.ZeroHash256 {
405403
log.L().Debug("Block already exists.", zap.Uint64("height", blkHeight))
406-
return nil
404+
return ErrAlreadyExist
407405
}
408406
// early exit if it's a db io error
409407
if err != nil && errors.Cause(err) != db.ErrNotExist && errors.Cause(err) != db.ErrBucketNotExist {
@@ -431,8 +429,6 @@ func (dao *blockDAO) PutBlock(ctx context.Context, blk *block.Block) error {
431429
return err
432430
}
433431
}
434-
435-
blk.HeaderLogger(log.L()).Info("Committed a block.", log.Hex("tipHash", blkHash[:]))
436432
return nil
437433
}
438434

blockchain/blockdao/blockdao_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ func TestBlockDAO(t *testing.T) {
199199
require.Equal(blks[i], blk)
200200
}
201201

202+
// commit an existing block
203+
require.Equal(ErrAlreadyExist, dao.PutBlock(ctx, blks[2]))
204+
202205
// Test getReceiptByActionHash
203206
for j := range daoTests[0].hashTotal {
204207
h := hash.BytesToHash256(daoTests[0].hashTotal[j])

0 commit comments

Comments
 (0)