Skip to content

Commit 9a40c26

Browse files
JukLee0iragzliudan
authored andcommitted
core,eth,light: remove duplicated functions
1 parent 75b18c8 commit 9a40c26

12 files changed

+54
-174
lines changed

core/bench_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,12 @@ func makeChainForBench(db ethdb.Database, full bool, count uint64) {
235235
ReceiptHash: types.EmptyRootHash,
236236
}
237237
hash = header.Hash()
238-
WriteHeader(db, header)
239-
WriteCanonicalHash(db, hash, n)
238+
rawdb.WriteHeader(db, header)
239+
rawdb.WriteCanonicalHash(db, hash, n)
240240
WriteTd(db, hash, n, big.NewInt(int64(n+1)))
241241
if full || n == 0 {
242242
block := types.NewBlockWithHeader(header)
243-
WriteBody(db, hash, n, block.Body())
243+
rawdb.WriteBody(db, hash, n, block.Body())
244244
WriteBlockReceipts(db, hash, n, nil)
245245
}
246246
}

core/blockchain.go

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
4040
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
4141
contractValidator "github.com/XinFinOrg/XDPoSChain/contracts/validator/contract"
42+
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
4243
"github.com/XinFinOrg/XDPoSChain/core/state"
4344
"github.com/XinFinOrg/XDPoSChain/core/types"
4445
"github.com/XinFinOrg/XDPoSChain/core/vm"
@@ -437,9 +438,7 @@ func (bc *BlockChain) SetHead(head uint64) error {
437438
}
438439
currentBlock := bc.CurrentBlock()
439440
currentFastBlock := bc.CurrentFastBlock()
440-
if err := WriteHeadBlockHash(bc.db, currentBlock.Hash()); err != nil {
441-
log.Crit("Failed to reset head full block", "err", err)
442-
}
441+
rawdb.WriteHeadBlockHash(bc.db, currentBlock.Hash())
443442
if err := WriteHeadFastBlockHash(bc.db, currentFastBlock.Hash()); err != nil {
444443
log.Crit("Failed to reset head fast block", "err", err)
445444
}
@@ -586,9 +585,7 @@ func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error {
586585
if err := bc.hc.WriteTd(genesis.Hash(), genesis.NumberU64(), genesis.Difficulty()); err != nil {
587586
log.Crit("Failed to write genesis block TD", "err", err)
588587
}
589-
if err := WriteBlock(bc.db, genesis); err != nil {
590-
log.Crit("Failed to write genesis block", "err", err)
591-
}
588+
rawdb.WriteBlock(bc.db, genesis)
592589
bc.genesisBlock = genesis
593590
bc.insert(bc.genesisBlock, false)
594591
bc.currentBlock.Store(bc.genesisBlock)
@@ -685,17 +682,9 @@ func (bc *BlockChain) insert(block *types.Block, writeBlock bool) {
685682
updateHeads := GetCanonicalHash(bc.db, block.NumberU64()) != block.Hash()
686683

687684
// Add the block to the canonical chain number scheme and mark as the head
688-
if err := WriteCanonicalHash(bc.db, block.Hash(), block.NumberU64()); err != nil {
689-
log.Crit("Failed to insert block number", "err", err)
690-
}
691-
if err := WriteHeadBlockHash(bc.db, block.Hash()); err != nil {
692-
log.Crit("Failed to insert head block hash", "err", err)
693-
}
694-
if writeBlock {
695-
if err := WriteBlock(bc.db, block); err != nil {
696-
log.Crit("Failed to insert block", "err", err)
697-
}
698-
}
685+
rawdb.WriteCanonicalHash(bc.db, block.Hash(), block.NumberU64())
686+
rawdb.WriteHeadBlockHash(bc.db, block.Hash())
687+
rawdb.WriteBlock(bc.db, block)
699688
bc.currentBlock.Store(block)
700689

701690
// save cache BlockSigners
@@ -1044,7 +1033,7 @@ func (bc *BlockChain) Rollback(chain []common.Hash) {
10441033
if currentBlock := bc.CurrentBlock(); currentBlock.Hash() == hash {
10451034
newBlock := bc.GetBlock(currentBlock.ParentHash(), currentBlock.NumberU64()-1)
10461035
bc.currentBlock.Store(newBlock)
1047-
WriteHeadBlockHash(bc.db, newBlock.Hash())
1036+
rawdb.WriteHeadBlockHash(bc.db, newBlock.Hash())
10481037
}
10491038
}
10501039
}
@@ -1134,9 +1123,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
11341123
return i, fmt.Errorf("failed to set receipts data: %v", err)
11351124
}
11361125
// Write all the data out into the database
1137-
if err := WriteBody(batch, block.Hash(), block.NumberU64(), block.Body()); err != nil {
1138-
return i, fmt.Errorf("failed to write block body: %v", err)
1139-
}
1126+
rawdb.WriteBody(batch, block.Hash(), block.NumberU64(), block.Body())
11401127
if err := WriteBlockReceipts(batch, block.Hash(), block.NumberU64(), receipts); err != nil {
11411128
return i, fmt.Errorf("failed to write block receipts: %v", err)
11421129
}
@@ -1196,9 +1183,7 @@ func (bc *BlockChain) WriteBlockWithoutState(block *types.Block, td *big.Int) (e
11961183
if err := bc.hc.WriteTd(block.Hash(), block.NumberU64(), td); err != nil {
11971184
return err
11981185
}
1199-
if err := WriteBlock(bc.db, block); err != nil {
1200-
return err
1201-
}
1186+
rawdb.WriteBlock(bc.db, block)
12021187
return nil
12031188
}
12041189

@@ -1226,9 +1211,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
12261211
}
12271212
// Write other block data using a batch.
12281213
batch := bc.db.NewBatch()
1229-
if err := WriteBlock(batch, block); err != nil {
1230-
return NonStatTy, err
1231-
}
1214+
rawdb.WriteBlock(batch, block)
12321215
root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number()))
12331216
if err != nil {
12341217
return NonStatTy, err

core/blockchain_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
130130
}
131131
blockchain.mu.Lock()
132132
WriteTd(blockchain.db, block.Hash(), block.NumberU64(), new(big.Int).Add(block.Difficulty(), blockchain.GetTdByHash(block.ParentHash())))
133-
WriteBlock(blockchain.db, block)
133+
rawdb.WriteBlock(blockchain.db, block)
134134
statedb.Commit(true)
135135
blockchain.mu.Unlock()
136136
}
@@ -148,7 +148,7 @@ func testHeaderChainImport(chain []*types.Header, blockchain *BlockChain) error
148148
// Manually insert the header into the database, but don't reorganise (allows subsequent testing)
149149
blockchain.mu.Lock()
150150
WriteTd(blockchain.db, header.Hash(), header.Number.Uint64(), new(big.Int).Add(header.Difficulty, blockchain.GetTdByHash(header.ParentHash)))
151-
WriteHeader(blockchain.db, header)
151+
rawdb.WriteHeader(blockchain.db, header)
152152
blockchain.mu.Unlock()
153153
}
154154
return nil

core/chain_indexer_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ package core
1818

1919
import (
2020
"fmt"
21-
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
2221
"math/big"
2322
"math/rand"
2423
"testing"
2524
"time"
2625

26+
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
27+
2728
"github.com/XinFinOrg/XDPoSChain/common"
2829
"github.com/XinFinOrg/XDPoSChain/core/types"
2930
)
@@ -94,8 +95,8 @@ func testChainIndexer(t *testing.T, count int) {
9495
if number > 0 {
9596
header.ParentHash = GetCanonicalHash(db, number-1)
9697
}
97-
WriteHeader(db, header)
98-
WriteCanonicalHash(db, header.Hash(), number)
98+
rawdb.WriteHeader(db, header)
99+
rawdb.WriteCanonicalHash(db, header.Hash(), number)
99100
}
100101
// Start indexer with an already existing chain
101102
for i := uint64(0); i <= 100; i++ {

core/database_util.go

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,6 @@ func GetBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash)
356356
return db.Get(key)
357357
}
358358

359-
// WriteCanonicalHash stores the canonical hash for the given block number.
360-
func WriteCanonicalHash(db ethdb.KeyValueWriter, hash common.Hash, number uint64) error {
361-
key := append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...)
362-
if err := db.Put(key, hash.Bytes()); err != nil {
363-
log.Crit("Failed to store number to hash mapping", "err", err)
364-
}
365-
return nil
366-
}
367-
368359
// WriteHeadHeaderHash stores the head header's hash.
369360
func WriteHeadHeaderHash(db ethdb.KeyValueWriter, hash common.Hash) error {
370361
if err := db.Put(headHeaderKey, hash.Bytes()); err != nil {
@@ -373,14 +364,6 @@ func WriteHeadHeaderHash(db ethdb.KeyValueWriter, hash common.Hash) error {
373364
return nil
374365
}
375366

376-
// WriteHeadBlockHash stores the head block's hash.
377-
func WriteHeadBlockHash(db ethdb.KeyValueWriter, hash common.Hash) error {
378-
if err := db.Put(headBlockKey, hash.Bytes()); err != nil {
379-
log.Crit("Failed to store last block's hash", "err", err)
380-
}
381-
return nil
382-
}
383-
384367
// WriteHeadFastBlockHash stores the fast head block's hash.
385368
func WriteHeadFastBlockHash(db ethdb.KeyValueWriter, hash common.Hash) error {
386369
if err := db.Put(headFastKey, hash.Bytes()); err != nil {
@@ -398,44 +381,6 @@ func WriteTrieSyncProgress(db ethdb.KeyValueWriter, count uint64) error {
398381
return nil
399382
}
400383

401-
// WriteHeader serializes a block header into the database.
402-
func WriteHeader(db ethdb.KeyValueWriter, header *types.Header) error {
403-
data, err := rlp.EncodeToBytes(header)
404-
if err != nil {
405-
return err
406-
}
407-
hash := header.Hash().Bytes()
408-
num := header.Number.Uint64()
409-
encNum := encodeBlockNumber(num)
410-
key := append(blockHashPrefix, hash...)
411-
if err := db.Put(key, encNum); err != nil {
412-
log.Crit("Failed to store hash to number mapping", "err", err)
413-
}
414-
key = append(append(headerPrefix, encNum...), hash...)
415-
if err := db.Put(key, data); err != nil {
416-
log.Crit("Failed to store header", "err", err)
417-
}
418-
return nil
419-
}
420-
421-
// WriteBody serializes the body of a block into the database.
422-
func WriteBody(db ethdb.KeyValueWriter, hash common.Hash, number uint64, body *types.Body) error {
423-
data, err := rlp.EncodeToBytes(body)
424-
if err != nil {
425-
return err
426-
}
427-
return WriteBodyRLP(db, hash, number, data)
428-
}
429-
430-
// WriteBodyRLP writes a serialized body of a block into the database.
431-
func WriteBodyRLP(db ethdb.KeyValueWriter, hash common.Hash, number uint64, rlp rlp.RawValue) error {
432-
key := append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
433-
if err := db.Put(key, rlp); err != nil {
434-
log.Crit("Failed to store block body", "err", err)
435-
}
436-
return nil
437-
}
438-
439384
// WriteTd serializes the total difficulty of a block into the database.
440385
func WriteTd(db ethdb.KeyValueWriter, hash common.Hash, number uint64, td *big.Int) error {
441386
data, err := rlp.EncodeToBytes(td)
@@ -449,19 +394,6 @@ func WriteTd(db ethdb.KeyValueWriter, hash common.Hash, number uint64, td *big.I
449394
return nil
450395
}
451396

452-
// WriteBlock serializes a block into the database, header and body separately.
453-
func WriteBlock(db ethdb.KeyValueWriter, block *types.Block) error {
454-
// Store the body first to retain database consistency
455-
if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil {
456-
return err
457-
}
458-
// Store the header too, signaling full block ownership
459-
if err := WriteHeader(db, block.Header()); err != nil {
460-
return err
461-
}
462-
return nil
463-
}
464-
465397
// WriteBlockReceipts stores all the transaction receipts belonging to a block
466398
// as a single receipt slice. This is used during chain reorganisations for
467399
// rescheduling dropped transactions.

core/database_util_test.go

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ func TestHeaderStorage(t *testing.T) {
3838
t.Fatalf("Non existent header returned: %v", entry)
3939
}
4040
// Write and verify the header in the database
41-
if err := WriteHeader(db, header); err != nil {
42-
t.Fatalf("Failed to write header into database: %v", err)
43-
}
41+
rawdb.WriteHeader(db, header)
4442
if entry := GetHeader(db, header.Hash(), header.Number.Uint64()); entry == nil {
4543
t.Fatalf("Stored header not found")
4644
} else if entry.Hash() != header.Hash() {
@@ -78,9 +76,7 @@ func TestBodyStorage(t *testing.T) {
7876
t.Fatalf("Non existent body returned: %v", entry)
7977
}
8078
// Write and verify the body in the database
81-
if err := WriteBody(db, hash, 0, body); err != nil {
82-
t.Fatalf("Failed to write body into database: %v", err)
83-
}
79+
rawdb.WriteBody(db, hash, 0, body)
8480
if entry := GetBody(db, hash, 0); entry == nil {
8581
t.Fatalf("Stored body not found")
8682
} else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(types.Transactions(body.Transactions)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
@@ -124,9 +120,7 @@ func TestBlockStorage(t *testing.T) {
124120
t.Fatalf("Non existent body returned: %v", entry)
125121
}
126122
// Write and verify the block in the database
127-
if err := WriteBlock(db, block); err != nil {
128-
t.Fatalf("Failed to write block into database: %v", err)
129-
}
123+
rawdb.WriteBlock(db, block)
130124
if entry := GetBlock(db, block.Hash(), block.NumberU64()); entry == nil {
131125
t.Fatalf("Stored block not found")
132126
} else if entry.Hash() != block.Hash() {
@@ -165,30 +159,22 @@ func TestPartialBlockStorage(t *testing.T) {
165159
ReceiptHash: types.EmptyRootHash,
166160
})
167161
// Store a header and check that it's not recognized as a block
168-
if err := WriteHeader(db, block.Header()); err != nil {
169-
t.Fatalf("Failed to write header into database: %v", err)
170-
}
162+
rawdb.WriteHeader(db, block.Header())
171163
if entry := GetBlock(db, block.Hash(), block.NumberU64()); entry != nil {
172164
t.Fatalf("Non existent block returned: %v", entry)
173165
}
174166
DeleteHeader(db, block.Hash(), block.NumberU64())
175167

176168
// Store a body and check that it's not recognized as a block
177-
if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil {
178-
t.Fatalf("Failed to write body into database: %v", err)
179-
}
169+
rawdb.WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
180170
if entry := GetBlock(db, block.Hash(), block.NumberU64()); entry != nil {
181171
t.Fatalf("Non existent block returned: %v", entry)
182172
}
183173
DeleteBody(db, block.Hash(), block.NumberU64())
184174

185175
// Store a header and a body separately and check reassembly
186-
if err := WriteHeader(db, block.Header()); err != nil {
187-
t.Fatalf("Failed to write header into database: %v", err)
188-
}
189-
if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil {
190-
t.Fatalf("Failed to write body into database: %v", err)
191-
}
176+
rawdb.WriteHeader(db, block.Header())
177+
rawdb.WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
192178
if entry := GetBlock(db, block.Hash(), block.NumberU64()); entry == nil {
193179
t.Fatalf("Stored block not found")
194180
} else if entry.Hash() != block.Hash() {
@@ -231,9 +217,7 @@ func TestCanonicalMappingStorage(t *testing.T) {
231217
t.Fatalf("Non existent canonical mapping returned: %v", entry)
232218
}
233219
// Write and verify the TD in the database
234-
if err := WriteCanonicalHash(db, hash, number); err != nil {
235-
t.Fatalf("Failed to write canonical mapping into database: %v", err)
236-
}
220+
rawdb.WriteCanonicalHash(db, hash, number)
237221
if entry := GetCanonicalHash(db, number); entry == (common.Hash{}) {
238222
t.Fatalf("Stored canonical mapping not found")
239223
} else if entry != hash {
@@ -268,9 +252,7 @@ func TestHeadStorage(t *testing.T) {
268252
if err := WriteHeadHeaderHash(db, blockHead.Hash()); err != nil {
269253
t.Fatalf("Failed to write head header hash: %v", err)
270254
}
271-
if err := WriteHeadBlockHash(db, blockFull.Hash()); err != nil {
272-
t.Fatalf("Failed to write head block hash: %v", err)
273-
}
255+
rawdb.WriteHeadBlockHash(db, blockFull.Hash())
274256
if err := WriteHeadFastBlockHash(db, blockFast.Hash()); err != nil {
275257
t.Fatalf("Failed to write fast head block hash: %v", err)
276258
}
@@ -304,9 +286,7 @@ func TestLookupStorage(t *testing.T) {
304286
}
305287
}
306288
// Insert all the transactions into the database, and verify contents
307-
if err := WriteBlock(db, block); err != nil {
308-
t.Fatalf("failed to write block contents: %v", err)
309-
}
289+
rawdb.WriteBlock(db, block)
310290
if err := WriteTxLookupEntries(db, block); err != nil {
311291
t.Fatalf("failed to write transactions: %v", err)
312292
}

core/genesis.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,18 +280,12 @@ func (g *Genesis) Commit(db ethdb.Database) (*types.Block, error) {
280280
if err := WriteTd(db, block.Hash(), block.NumberU64(), g.Difficulty); err != nil {
281281
return nil, err
282282
}
283-
if err := WriteBlock(db, block); err != nil {
284-
return nil, err
285-
}
283+
rawdb.WriteBlock(db, block)
286284
if err := WriteBlockReceipts(db, block.Hash(), block.NumberU64(), nil); err != nil {
287285
return nil, err
288286
}
289-
if err := WriteCanonicalHash(db, block.Hash(), block.NumberU64()); err != nil {
290-
return nil, err
291-
}
292-
if err := WriteHeadBlockHash(db, block.Hash()); err != nil {
293-
return nil, err
294-
}
287+
rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64())
288+
rawdb.WriteHeadBlockHash(db, block.Hash())
295289
if err := WriteHeadHeaderHash(db, block.Hash()); err != nil {
296290
return nil, err
297291
}

0 commit comments

Comments
 (0)