Skip to content

Commit da71c1a

Browse files
committed
[config] add DB.CompressLegacy to deprecate Chain.CompressBlock
1 parent 3713e8f commit da71c1a

File tree

13 files changed

+58
-44
lines changed

13 files changed

+58
-44
lines changed

blockchain/blockchain.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ func BoltDBDaoOption(indexers ...blockdao.BlockIndexer) Option {
154154
return nil
155155
}
156156
cfg.DB.DbPath = cfg.Chain.ChainDBPath // TODO: remove this after moving TrieDBPath from cfg.Chain to cfg.DB
157-
bc.dao = blockdao.NewBlockDAO(indexers, cfg.Chain.CompressBlock, cfg.DB)
157+
cfg.DB.CompressLegacy = cfg.Chain.CompressBlock
158+
bc.dao = blockdao.NewBlockDAO(indexers, cfg.DB)
158159
return nil
159160
}
160161
}

blockchain/blockdao/blockdao.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ type (
7777
)
7878

7979
// NewBlockDAO instantiates a block DAO
80-
func NewBlockDAO(indexers []BlockIndexer, compressLegacy bool, cfg config.DB) BlockDAO {
81-
blkStore, err := filedao.NewFileDAO(compressLegacy, cfg)
80+
func NewBlockDAO(indexers []BlockIndexer, cfg config.DB) BlockDAO {
81+
blkStore, err := filedao.NewFileDAO(cfg)
8282
if err != nil {
8383
return nil
8484
}

blockchain/blockdao/blockdao_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -426,19 +426,15 @@ func createTestBlockDAO(inMemory, legacy bool, compressBlock string, cfg config.
426426
}
427427

428428
if legacy {
429-
file, err := filedao.NewFileDAOLegacy(compressBlock != "", cfg)
430-
if err != nil {
431-
return nil, err
432-
}
433-
fileDAO, err := filedao.CreateFileDAO(file, nil, cfg)
429+
fileDAO, err := filedao.CreateFileDAO(true, nil, cfg)
434430
if err != nil {
435431
return nil, err
436432
}
437433
return createBlockDAO(fileDAO, nil, cfg), nil
438434
}
439435

440436
cfg.Compressor = compressBlock
441-
return NewBlockDAO(nil, false, cfg), nil
437+
return NewBlockDAO(nil, cfg), nil
442438
}
443439

444440
func BenchmarkBlockCache(b *testing.B) {
@@ -458,10 +454,8 @@ func BenchmarkBlockCache(b *testing.B) {
458454
}()
459455
cfg.DbPath = indexPath
460456
cfg.DbPath = testPath
461-
462-
db := config.Default.DB
463-
db.MaxCacheSize = cacheSize
464-
blkDao := NewBlockDAO([]BlockIndexer{}, false, db)
457+
cfg.MaxCacheSize = cacheSize
458+
blkDao := NewBlockDAO([]BlockIndexer{}, cfg)
465459
require.NoError(b, blkDao.Start(context.Background()))
466460
defer func() {
467461
require.NoError(b, blkDao.Stop(context.Background()))

blockchain/filedao/filedao.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type (
6969
)
7070

7171
// NewFileDAO creates an instance of FileDAO
72-
func NewFileDAO(compressLegacy bool, cfg config.DB) (FileDAO, error) {
72+
func NewFileDAO(cfg config.DB) (FileDAO, error) {
7373
header, v2Files, err := checkChainDBFiles(cfg)
7474
if err == ErrFileInvalid {
7575
return nil, err
@@ -80,33 +80,30 @@ func NewFileDAO(compressLegacy bool, cfg config.DB) (FileDAO, error) {
8080
if err := createNewV2File(1, cfg); err != nil {
8181
return nil, err
8282
}
83-
return CreateFileDAO(nil, []string{cfg.DbPath}, cfg)
83+
return CreateFileDAO(false, []string{cfg.DbPath}, cfg)
8484
}
8585

8686
switch header.Version {
8787
case FileLegacyMaster:
8888
// default file is legacy format
89-
legacyFd, err := NewFileDAOLegacy(compressLegacy, cfg)
90-
if err != nil {
91-
return nil, err
92-
}
93-
return CreateFileDAO(legacyFd, v2Files, cfg)
89+
return CreateFileDAO(true, v2Files, cfg)
9490
case FileV2:
9591
// default file is v2 format, add it to filenames
9692
v2Files = append(v2Files, cfg.DbPath)
97-
return CreateFileDAO(nil, v2Files, cfg)
93+
return CreateFileDAO(false, v2Files, cfg)
9894
default:
9995
panic(fmt.Errorf("corrupted file version: %s", header.Version))
10096
}
10197
}
10298

10399
// NewFileDAOInMemForTest creates an in-memory FileDAO for testing
104100
func NewFileDAOInMemForTest(cfg config.DB) (FileDAO, error) {
105-
legacyFd, err := newFileDAOLegacyInMem(false, cfg)
101+
legacyFd, err := newFileDAOLegacyInMem(cfg.CompressLegacy, cfg)
106102
if err != nil {
107103
return nil, err
108104
}
109-
return CreateFileDAO(legacyFd, nil, cfg)
105+
106+
return &fileDAO{legacyFd: legacyFd}, nil
110107
}
111108

112109
func (fd *fileDAO) Start(ctx context.Context) error {
@@ -255,11 +252,22 @@ func (fd *fileDAO) DeleteTipBlock() error {
255252
}
256253

257254
// CreateFileDAO creates FileDAO from legacy and new files
258-
func CreateFileDAO(legacy FileDAO, v2Files []string, cfg config.DB) (FileDAO, error) {
259-
if legacy == nil && len(v2Files) == 0 {
255+
func CreateFileDAO(legacy bool, v2Files []string, cfg config.DB) (FileDAO, error) {
256+
if legacy == false && len(v2Files) == 0 {
260257
return nil, ErrNotSupported
261258
}
262259

260+
var (
261+
legacyFd FileDAO
262+
err error
263+
)
264+
if legacy {
265+
legacyFd, err = newFileDAOLegacy(cfg)
266+
if err != nil {
267+
return nil, err
268+
}
269+
}
270+
263271
var v2Fd FileV2Manager
264272
if len(v2Files) > 0 {
265273
fds := make([]*fileDAOv2, len(v2Files))
@@ -271,7 +279,7 @@ func CreateFileDAO(legacy FileDAO, v2Files []string, cfg config.DB) (FileDAO, er
271279
}
272280

273281
return &fileDAO{
274-
legacyFd: legacy,
282+
legacyFd: legacyFd,
275283
v2Fd: v2Fd,
276284
}, nil
277285
}

blockchain/filedao/filedao_legacy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ type (
5959
}
6060
)
6161

62-
// NewFileDAOLegacy creates a new legacy file
63-
func NewFileDAOLegacy(compressBlock bool, cfg config.DB) (FileDAO, error) {
62+
// newFileDAOLegacy creates a new legacy file
63+
func newFileDAOLegacy(cfg config.DB) (FileDAO, error) {
6464
return &fileDAOLegacy{
65-
compressBlock: compressBlock,
65+
compressBlock: cfg.CompressLegacy,
6666
cfg: cfg,
6767
kvStore: db.NewBoltDB(cfg),
6868
}, nil

blockchain/filedao/filedao_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestReadFileHeader(t *testing.T) {
7777
r.Equal(ErrFileNotExist, err)
7878

7979
// empty legacy file is invalid
80-
legacy, err := NewFileDAOLegacy(false, cfg)
80+
legacy, err := newFileDAOLegacy(cfg)
8181
r.NoError(err)
8282
ctx := context.Background()
8383
r.NoError(legacy.Start(ctx))
@@ -152,7 +152,7 @@ func TestNewFileDAO(t *testing.T) {
152152
r.Nil(v2files)
153153

154154
// test empty db file, this will create new v2 file
155-
fd, err := NewFileDAO(false, cfg)
155+
fd, err := NewFileDAO(cfg)
156156
r.NoError(err)
157157
r.NotNil(fd)
158158
h, err = readFileHeader(cfg, FileAll)
@@ -168,7 +168,7 @@ func TestNewFileDAO(t *testing.T) {
168168
os.RemoveAll(cfg.DbPath)
169169
cfg.SplitDBHeight = 5
170170
cfg.SplitDBSizeMB = 200
171-
legacy, err := NewFileDAOLegacy(false, cfg)
171+
legacy, err := newFileDAOLegacy(cfg)
172172
r.NoError(err)
173173
r.NoError(legacy.Start(ctx))
174174
testCommitBlocks(t, legacy, 1, 10, hash.ZeroHash256)
@@ -197,7 +197,7 @@ func TestNewFileDAO(t *testing.T) {
197197
// block 6~10 in legacy file-000000001.db
198198
// block 11~32 in v2 file-000000002.db
199199
cfg.DbPath = "./filedao_v2.db"
200-
fd, err = NewFileDAO(false, cfg)
200+
fd, err = NewFileDAO(cfg)
201201
r.NoError(err)
202202
r.NotNil(fd)
203203
h, err = readFileHeader(cfg, FileAll)

blockchain/integrity/integrity_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,8 @@ func TestConstantinople(t *testing.T) {
741741
require.NoError(err)
742742
// create BlockDAO
743743
cfg.DB.DbPath = cfg.Chain.ChainDBPath
744-
dao := blockdao.NewBlockDAO([]blockdao.BlockIndexer{sf, indexer}, cfg.Chain.CompressBlock, cfg.DB)
744+
cfg.DB.CompressLegacy = cfg.Chain.CompressBlock
745+
dao := blockdao.NewBlockDAO([]blockdao.BlockIndexer{sf, indexer}, cfg.DB)
745746
require.NotNil(dao)
746747
bc := blockchain.NewBlockchain(
747748
cfg,
@@ -978,7 +979,8 @@ func TestLoadBlockchainfromDB(t *testing.T) {
978979
cfg.Genesis.InitBalanceMap[identityset.Address(27).String()] = unit.ConvertIotxToRau(10000000000).String()
979980
// create BlockDAO
980981
cfg.DB.DbPath = cfg.Chain.ChainDBPath
981-
dao := blockdao.NewBlockDAO(indexers, cfg.Chain.CompressBlock, cfg.DB)
982+
cfg.DB.CompressLegacy = cfg.Chain.CompressBlock
983+
dao := blockdao.NewBlockDAO(indexers, cfg.DB)
982984
require.NotNil(dao)
983985
bc := blockchain.NewBlockchain(
984986
cfg,
@@ -1675,7 +1677,8 @@ func newChain(t *testing.T, stateTX bool) (blockchain.Blockchain, factory.Factor
16751677
cfg.Genesis.InitBalanceMap[identityset.Address(27).String()] = unit.ConvertIotxToRau(10000000000).String()
16761678
// create BlockDAO
16771679
cfg.DB.DbPath = cfg.Chain.ChainDBPath
1678-
dao := blockdao.NewBlockDAO(indexers, cfg.Chain.CompressBlock, cfg.DB)
1680+
cfg.DB.CompressLegacy = cfg.Chain.CompressBlock
1681+
dao := blockdao.NewBlockDAO(indexers, cfg.DB)
16791682
require.NotNil(dao)
16801683
bc := blockchain.NewBlockchain(
16811684
cfg,

blockindex/indexbuilder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func TestIndexBuilder(t *testing.T) {
173173
blockdao.NewBlockDAOInMemForTest(nil, cfg), true,
174174
},
175175
{
176-
blockdao.NewBlockDAO(nil, false, cfg), false,
176+
blockdao.NewBlockDAO(nil, cfg), false,
177177
},
178178
} {
179179
t.Run("test indexbuilder", func(t *testing.T) {

chainservice/chainservice.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ func New(
188188
dao = blockdao.NewBlockDAOInMemForTest(indexers, cfg.DB)
189189
} else {
190190
cfg.DB.DbPath = cfg.Chain.ChainDBPath
191-
dao = blockdao.NewBlockDAO(indexers, cfg.Chain.CompressBlock, cfg.DB)
191+
cfg.DB.CompressLegacy = cfg.Chain.CompressBlock
192+
dao = blockdao.NewBlockDAO(indexers, cfg.DB)
192193
}
193194

194195
// Create ActPool

config/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ var (
189189
MaxCacheSize: 64,
190190
BlockStoreBatchSize: 16,
191191
Compressor: "Snappy",
192+
CompressLegacy: false,
192193
SQLITE3: SQLITE3{
193194
SQLite3File: "./explorer.db",
194195
},
@@ -256,7 +257,7 @@ type (
256257
EnableStakingProtocol bool `yaml:"enableStakingProtocol"`
257258
// EnableStakingIndexer enables staking indexer
258259
EnableStakingIndexer bool `yaml:"enableStakingIndexer"`
259-
// CompressBlock enables gzip compression on block data, used by legacy DB file
260+
// deprecated by DB.CompressBlock
260261
CompressBlock bool `yaml:"compressBlock"`
261262
// AllowedBlockGasResidue is the amount of gas remained when block producer could stop processing more actions
262263
AllowedBlockGasResidue uint64 `yaml:"allowedBlockGasResidue"`
@@ -368,6 +369,8 @@ type (
368369
BlockStoreBatchSize int `yaml:"blockStoreBatchSize"`
369370
// Compressor is the compression used on block data, used by new DB file after Ithaca
370371
Compressor string `yaml:"compressor"`
372+
// CompressLegacy enables gzip compression on block data, used by legacy DB file before Ithaca
373+
CompressLegacy bool `yaml:"compressLegacy"`
371374
// RDS is the config for rds
372375
RDS RDS `yaml:"RDS"`
373376
// SQLite3 is the config for SQLITE3

e2etest/local_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,8 @@ func TestStartExistingBlockchain(t *testing.T) {
548548

549549
// Recover to height 3 from empty state DB
550550
cfg.DB.DbPath = cfg.Chain.ChainDBPath
551-
dao = blockdao.NewBlockDAO(nil, cfg.Chain.CompressBlock, cfg.DB)
551+
cfg.DB.CompressLegacy = cfg.Chain.CompressBlock
552+
dao = blockdao.NewBlockDAO(nil, cfg.DB)
552553
require.NoError(dao.Start(protocol.WithBlockchainCtx(ctx,
553554
protocol.BlockchainCtx{
554555
Genesis: cfg.Genesis,
@@ -571,7 +572,8 @@ func TestStartExistingBlockchain(t *testing.T) {
571572
// Recover to height 2 from an existing state DB with Height 3
572573
require.NoError(svr.Stop(ctx))
573574
cfg.DB.DbPath = cfg.Chain.ChainDBPath
574-
dao = blockdao.NewBlockDAO(nil, cfg.Chain.CompressBlock, cfg.DB)
575+
cfg.DB.CompressLegacy = cfg.Chain.CompressBlock
576+
dao = blockdao.NewBlockDAO(nil, cfg.DB)
575577
require.NoError(dao.Start(protocol.WithBlockchainCtx(ctx,
576578
protocol.BlockchainCtx{
577579
Genesis: cfg.Genesis,

tools/iomigrater/cmds/check-height.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func checkDbFileHeight(filePath string) (uint64, error) {
5353
}
5454

5555
cfg.DB.DbPath = filePath
56-
blockDao := blockdao.NewBlockDAO(nil, cfg.Chain.CompressBlock, cfg.DB)
56+
cfg.DB.CompressLegacy = cfg.Chain.CompressBlock
57+
blockDao := blockdao.NewBlockDAO(nil, cfg.DB)
5758

5859
// Load height value.
5960
ctx := context.Background()

tools/iomigrater/cmds/migrate-db.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ func migrateDbFile() error {
113113
}
114114

115115
cfg.DB.DbPath = oldFile
116-
oldDAO := blockdao.NewBlockDAO(nil, cfg.Chain.CompressBlock, cfg.DB)
116+
cfg.DB.CompressLegacy = cfg.Chain.CompressBlock
117+
oldDAO := blockdao.NewBlockDAO(nil, cfg.DB)
117118

118119
cfg.DB.DbPath = newFile
119-
newDAO := blockdao.NewBlockDAO(nil, cfg.Chain.CompressBlock, cfg.DB)
120+
newDAO := blockdao.NewBlockDAO(nil, cfg.DB)
120121

121122
ctx := context.Background()
122123
if err := oldDAO.Start(ctx); err != nil {

0 commit comments

Comments
 (0)