Skip to content

Commit 7f4dafc

Browse files
authored
Merge branch 'master' into feature-grpc-sub
2 parents a603f53 + 8d09ff1 commit 7f4dafc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2329
-2408
lines changed

blockchain/blockchain_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ package blockchain
77
import (
88
"testing"
99

10-
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
1111
)
1212

1313
func TestCalcHeightToBlockHeaderKey(t *testing.T) {
1414
key := calcHeightToBlockHeaderKey(1)
15-
assert.Equal(t, key, []byte("HH:000000000001"))
15+
require.Equal(t, key, []byte("HH:000000000001"))
1616
key = calcHeightToBlockHeaderKey(0)
17-
assert.Equal(t, key, []byte("HH:000000000000"))
17+
require.Equal(t, key, []byte("HH:000000000000"))
1818
key = calcHeightToBlockHeaderKey(10)
19-
assert.Equal(t, key, []byte("HH:000000000010"))
19+
require.Equal(t, key, []byte("HH:000000000010"))
2020
}

blockchain/blockstore.go

+20-17
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ func calcHeightToBlockHeaderKey(height int64) []byte {
8888
}
8989

9090
//存储block hash对应的block height
91-
func calcHashToHeightKey(hash []byte) []byte {
92-
return append(hashPrefix, hash...)
93-
}
91+
//func calcHashToHeightKey(hash []byte) []byte {
92+
// return append(hashPrefix, hash...)
93+
//}
9494

9595
//存储block hash对应的block总难度TD
9696
func calcHashToTdKey(hash []byte) []byte {
@@ -407,6 +407,9 @@ func (bs *BlockStore) HasTx(key []byte) (bool, error) {
407407
//通过短hash查询交易存在时,需要再通过全hash索引查询一下。
408408
//避免短hash重复,而全hash不一样的情况
409409
//return true, nil
410+
// 查重性能对比:
411+
// 先查短哈希,再查全哈希,单次查询平均耗时10000ns
412+
// 直接查全哈希,单次查询平均耗时24000ns
410413
}
411414
if _, err := bs.db.Get(cfg.CalcTxKey(key)); err != nil {
412415
if err == dbm.ErrNotFoundInDb {
@@ -570,7 +573,7 @@ func (bs *BlockStore) SaveBlock(storeBatch dbm.Batch, blockdetail *types.BlockDe
570573
storeBatch.Set(blockLastHeight, heightbytes)
571574

572575
//存储block hash和height的对应关系,便于通过hash查询block
573-
storeBatch.Set(calcHashToHeightKey(hash), heightbytes)
576+
//storeBatch.Set(calcHashToHeightKey(hash), heightbytes)
574577

575578
//存储block height和block hash的对应关系,便于通过height查询block
576579
storeBatch.Set(calcHeightToHashKey(height), hash)
@@ -618,7 +621,7 @@ func (bs *BlockStore) DelBlock(storeBatch dbm.Batch, blockdetail *types.BlockDet
618621
storeBatch.Set(blockLastHeight, bytes)
619622

620623
//删除block hash和height的对应关系
621-
storeBatch.Delete(calcHashToHeightKey(hash))
624+
//storeBatch.Delete(calcHashToHeightKey(hash))
622625

623626
//删除block height和block hash的对应关系,便于通过height查询block
624627
storeBatch.Delete(calcHeightToHashKey(height))
@@ -748,17 +751,17 @@ func (bs *BlockStore) DelTxs(storeBatch dbm.Batch, blockDetail *types.BlockDetai
748751
}
749752

750753
//GetHeightByBlockHash 从db数据库中获取指定hash对应的block高度
751-
func (bs *BlockStore) GetHeightByBlockHash(hash []byte) (int64, error) {
752-
753-
heightbytes, err := bs.db.Get(calcHashToHeightKey(hash))
754-
if heightbytes == nil || err != nil {
755-
if err != dbm.ErrNotFoundInDb {
756-
storeLog.Error("GetHeightByBlockHash", "error", err)
757-
}
758-
return -1, types.ErrHashNotExist
759-
}
760-
return decodeHeight(heightbytes)
761-
}
754+
//func (bs *BlockStore) GetHeightByBlockHash(hash []byte) (int64, error) {
755+
//
756+
// heightbytes, err := bs.db.Get(calcHashToHeightKey(hash))
757+
// if heightbytes == nil || err != nil {
758+
// if err != dbm.ErrNotFoundInDb {
759+
// storeLog.Error("GetHeightByBlockHash", "error", err)
760+
// }
761+
// return -1, types.ErrHashNotExist
762+
// }
763+
// return decodeHeight(heightbytes)
764+
//}
762765

763766
func decodeHeight(heightbytes []byte) (int64, error) {
764767
var height types.Int64
@@ -767,7 +770,7 @@ func decodeHeight(heightbytes []byte) (int64, error) {
767770
//may be old database format json...
768771
err = json.Unmarshal(heightbytes, &height.Data)
769772
if err != nil {
770-
storeLog.Error("GetHeightByBlockHash Could not unmarshal height bytes", "error", err)
773+
storeLog.Error("decodeHeight could not unmarshal height bytes", "error", err)
771774
return -1, types.ErrUnmarshal
772775
}
773776
}

0 commit comments

Comments
 (0)