Skip to content

Commit a8630de

Browse files
committed
store 16 blocks and use Snappy compression
1 parent 69d173c commit a8630de

11 files changed

+582
-111
lines changed

blockchain/block/blockstore.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ package block
99
import (
1010
"github.com/golang/protobuf/proto"
1111

12-
"github.com/iotexproject/iotex-proto/golang/iotexapi"
1312
"github.com/iotexproject/iotex-proto/golang/iotextypes"
1413

1514
"github.com/iotexproject/iotex-core/action"
1615
)
1716

1817
type (
19-
// Store defines block and receipts
18+
// Store defines block storage schema
2019
Store struct {
2120
Block *Block
2221
Receipts []*action.Receipt
@@ -29,19 +28,19 @@ func (in *Store) Serialize() ([]byte, error) {
2928
}
3029

3130
// ToProto converts to proto message
32-
func (in *Store) ToProto() *iotexapi.BlockInfo {
31+
func (in *Store) ToProto() *iotextypes.BlockStore {
3332
receipts := []*iotextypes.Receipt{}
3433
for _, r := range in.Receipts {
3534
receipts = append(receipts, r.ConvertToReceiptPb())
3635
}
37-
return &iotexapi.BlockInfo{
36+
return &iotextypes.BlockStore{
3837
Block: in.Block.ConvertToBlockPb(),
3938
Receipts: receipts,
4039
}
4140
}
4241

4342
// FromProto converts from proto message
44-
func (in *Store) FromProto(pb *iotexapi.BlockInfo) error {
43+
func (in *Store) FromProto(pb *iotextypes.BlockStore) error {
4544
in.Block = &Block{}
4645
if err := in.Block.ConvertFromBlockPb(pb.Block); err != nil {
4746
return err
@@ -62,9 +61,18 @@ func (in *Store) FromProto(pb *iotexapi.BlockInfo) error {
6261

6362
// Deserialize parses the byte stream into Store
6463
func (in *Store) Deserialize(buf []byte) error {
65-
pbInfo := &iotexapi.BlockInfo{}
66-
if err := proto.Unmarshal(buf, pbInfo); err != nil {
64+
pbStore := &iotextypes.BlockStore{}
65+
if err := proto.Unmarshal(buf, pbStore); err != nil {
6766
return err
6867
}
69-
return in.FromProto(pbInfo)
68+
return in.FromProto(pbStore)
69+
}
70+
71+
// DeserializeBlockStoresPb decode byte stream into BlockStores pb message
72+
func DeserializeBlockStoresPb(buf []byte) (*iotextypes.BlockStores, error) {
73+
pbStores := &iotextypes.BlockStores{}
74+
if err := proto.Unmarshal(buf, pbStores); err != nil {
75+
return nil, err
76+
}
77+
return pbStores, nil
7078
}

blockchain/blockdao/blockdao_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,11 @@ func createTestBlockDAO(inMemory, legacy, compressBlock bool, cfg config.DB) (Bl
433433
}
434434
} else {
435435
cfg.CompressData = compressBlock
436-
file, err := newFileDAONew(db.NewBoltDB(cfg), 1, cfg)
436+
file, err := newFileDAOv2(db.NewBoltDB(cfg), 1, cfg)
437437
if err != nil {
438438
return nil, err
439439
}
440-
fileDAO, err = createFileDAO(nil, map[uint64]FileDAONew{1: file})
440+
fileDAO, err = createFileDAO(nil, map[uint64]FileDAO{1: file})
441441
if err != nil {
442442
return nil, err
443443
}

blockchain/blockdao/filedao.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type (
6464
lifecycle lifecycle.Lifecycle
6565
currFd FileDAO
6666
legacyFd FileDAO
67-
newFd map[uint64]FileDAONew
67+
v2Fd map[uint64]FileDAO
6868
}
6969
)
7070

@@ -103,7 +103,7 @@ func (fd *fileDAO) GetBlockHash(height uint64) (hash.Hash256, error) {
103103
h hash.Hash256
104104
err error
105105
)
106-
for _, file := range fd.newFd {
106+
for _, file := range fd.v2Fd {
107107
if h, err = file.GetBlockHash(height); err == nil {
108108
return h, nil
109109
}
@@ -119,7 +119,7 @@ func (fd *fileDAO) GetBlockHeight(hash hash.Hash256) (uint64, error) {
119119
height uint64
120120
err error
121121
)
122-
for _, file := range fd.newFd {
122+
for _, file := range fd.v2Fd {
123123
if height, err = file.GetBlockHeight(hash); err == nil {
124124
return height, nil
125125
}
@@ -135,7 +135,7 @@ func (fd *fileDAO) GetBlock(hash hash.Hash256) (*block.Block, error) {
135135
blk *block.Block
136136
err error
137137
)
138-
for _, file := range fd.newFd {
138+
for _, file := range fd.v2Fd {
139139
if blk, err = file.GetBlock(hash); err == nil {
140140
return blk, nil
141141
}
@@ -151,7 +151,7 @@ func (fd *fileDAO) GetBlockByHeight(height uint64) (*block.Block, error) {
151151
blk *block.Block
152152
err error
153153
)
154-
for _, file := range fd.newFd {
154+
for _, file := range fd.v2Fd {
155155
if blk, err = file.GetBlockByHeight(height); err == nil {
156156
return blk, nil
157157
}
@@ -167,7 +167,7 @@ func (fd *fileDAO) GetReceipts(height uint64) ([]*action.Receipt, error) {
167167
receipts []*action.Receipt
168168
err error
169169
)
170-
for _, file := range fd.newFd {
170+
for _, file := range fd.v2Fd {
171171
if receipts, err = file.GetReceipts(height); err == nil {
172172
return receipts, nil
173173
}
@@ -188,7 +188,7 @@ func (fd *fileDAO) TransactionLogs(height uint64) (*iotextypes.TransactionLogs,
188188
log *iotextypes.TransactionLogs
189189
err error
190190
)
191-
for _, file := range fd.newFd {
191+
for _, file := range fd.v2Fd {
192192
if log, err = file.TransactionLogs(height); err == nil {
193193
return log, nil
194194
}
@@ -214,10 +214,10 @@ func (fd *fileDAO) DeleteTipBlock() error {
214214
return fd.currFd.DeleteTipBlock()
215215
}
216216

217-
func createFileDAO(legacy FileDAO, newFile map[uint64]FileDAONew) (FileDAO, error) {
217+
func createFileDAO(legacy FileDAO, newFile map[uint64]FileDAO) (FileDAO, error) {
218218
fileDAO := &fileDAO{
219219
legacyFd: legacy,
220-
newFd: newFile,
220+
v2Fd: newFile,
221221
}
222222

223223
var (

blockchain/blockdao/filedao_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
func TestChecksumNamespaceAndKeys(t *testing.T) {
20-
require := require.New(t)
20+
r := require.New(t)
2121

2222
a := []hash.Hash256{
2323
// filedao
@@ -34,16 +34,18 @@ func TestChecksumNamespaceAndKeys(t *testing.T) {
3434
hash.BytesToHash256([]byte(receiptsNS)),
3535
hash.BytesToHash256(heightPrefix),
3636
hash.BytesToHash256(heightToFileBucket),
37-
// filedao_new
37+
// filedao_v2
3838
hash.BytesToHash256([]byte{_normal}),
3939
hash.BytesToHash256([]byte{_compressed}),
40+
hash.BytesToHash256([]byte{blockStoreBatchSize}),
4041
hash.BytesToHash256([]byte(hashDataNS)),
4142
hash.BytesToHash256([]byte(blockDataNS)),
43+
hash.BytesToHash256([]byte(stagingDataNS)),
4244
hash.BytesToHash256(bottomHeightKey),
4345
}
4446

4547
checksum := crypto.NewMerkleTree(a)
46-
require.NotNil(checksum)
48+
r.NotNil(checksum)
4749
h := checksum.HashTree()
48-
require.Equal("1cc352fff5fc29d8ac7dcc186cce7b9e7d87c41ceeba41993b96b8a9566facaa", hex.EncodeToString(h[:]))
50+
r.Equal("f584356aa8ad1da8cb803162f06c96ff04e4478ad8f4a2667d44cee3e79f9d94", hex.EncodeToString(h[:]))
4951
}

0 commit comments

Comments
 (0)