Skip to content

Commit f5e0388

Browse files
committed
Merge pull request bnb-chain#778 from bas-vk/issue764
Moved database update loop to eth/backend
2 parents abe08d7 + c12180d commit f5e0388

5 files changed

Lines changed: 62 additions & 48 deletions

File tree

‎common/db.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ type Database interface {
77
Delete(key []byte) error
88
LastKnownTD() []byte
99
Close()
10+
Flush() error
1011
}

‎core/chain_manager.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,14 @@ func (self *ChainManager) Export(w io.Writer) error {
342342
}
343343

344344
func (bc *ChainManager) insert(block *types.Block) {
345-
bc.blockDb.Put([]byte("LastBlock"), block.Hash().Bytes())
346-
bc.currentBlock = block
347-
bc.lastBlockHash = block.Hash()
348-
349345
key := append(blockNumPre, block.Number().Bytes()...)
350-
bc.blockDb.Put(key, bc.lastBlockHash.Bytes())
346+
bc.blockDb.Put(key, block.Hash().Bytes())
351347
// Push block to cache
352348
bc.cache.Push(block)
349+
350+
bc.blockDb.Put([]byte("LastBlock"), block.Hash().Bytes())
351+
bc.currentBlock = block
352+
bc.lastBlockHash = block.Hash()
353353
}
354354

355355
func (bc *ChainManager) write(block *types.Block) {

‎eth/backend.go‎

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"path"
77
"strings"
8+
"time"
89

910
"github.com/ethereum/ethash"
1011
"github.com/ethereum/go-ethereum/accounts"
@@ -123,6 +124,8 @@ type Ethereum struct {
123124
blockDb common.Database // Block chain database
124125
stateDb common.Database // State changes database
125126
extraDb common.Database // Extra database (txs, etc)
127+
// Closed when databases are flushed and closed
128+
databasesClosed chan bool
126129

127130
//*** SERVICES ***
128131
// State manager for processing new blocks and managing the over all states
@@ -197,18 +200,19 @@ func New(config *Config) (*Ethereum, error) {
197200
glog.V(logger.Info).Infof("Blockchain DB Version: %d", config.BlockChainVersion)
198201

199202
eth := &Ethereum{
200-
shutdownChan: make(chan bool),
201-
blockDb: blockDb,
202-
stateDb: stateDb,
203-
extraDb: extraDb,
204-
eventMux: &event.TypeMux{},
205-
accountManager: config.AccountManager,
206-
DataDir: config.DataDir,
207-
etherbase: common.HexToAddress(config.Etherbase),
208-
clientVersion: config.Name, // TODO should separate from Name
209-
ethVersionId: config.ProtocolVersion,
210-
netVersionId: config.NetworkId,
211-
NatSpec: config.NatSpec,
203+
shutdownChan: make(chan bool),
204+
databasesClosed: make(chan bool),
205+
blockDb: blockDb,
206+
stateDb: stateDb,
207+
extraDb: extraDb,
208+
eventMux: &event.TypeMux{},
209+
accountManager: config.AccountManager,
210+
DataDir: config.DataDir,
211+
etherbase: common.HexToAddress(config.Etherbase),
212+
clientVersion: config.Name, // TODO should separate from Name
213+
ethVersionId: config.ProtocolVersion,
214+
netVersionId: config.NetworkId,
215+
NatSpec: config.NatSpec,
212216
}
213217

214218
eth.chainManager = core.NewChainManager(blockDb, stateDb, eth.EventMux())
@@ -376,6 +380,9 @@ func (s *Ethereum) Start() error {
376380
}
377381
}
378382

383+
// periodically flush databases
384+
go s.syncDatabases()
385+
379386
// Start services
380387
go s.txPool.Start()
381388
s.protocolManager.Start()
@@ -392,6 +399,34 @@ func (s *Ethereum) Start() error {
392399
return nil
393400
}
394401

402+
func (s *Ethereum) syncDatabases() {
403+
ticker := time.NewTicker(1 * time.Minute)
404+
done:
405+
for {
406+
select {
407+
case <-ticker.C:
408+
// don't change the order of database flushes
409+
if err := s.extraDb.Flush(); err != nil {
410+
glog.V(logger.Error).Infof("error: flush extraDb: %v\n", err)
411+
}
412+
if err := s.stateDb.Flush(); err != nil {
413+
glog.V(logger.Error).Infof("error: flush stateDb: %v\n", err)
414+
}
415+
if err := s.blockDb.Flush(); err != nil {
416+
glog.V(logger.Error).Infof("error: flush blockDb: %v\n", err)
417+
}
418+
case <-s.shutdownChan:
419+
break done
420+
}
421+
}
422+
423+
s.blockDb.Close()
424+
s.stateDb.Close()
425+
s.extraDb.Close()
426+
427+
close(s.databasesClosed)
428+
}
429+
395430
func (s *Ethereum) StartForTest() {
396431
jsonlogger.LogJson(&logger.LogStarting{
397432
ClientString: s.net.Name,
@@ -412,12 +447,7 @@ func (self *Ethereum) SuggestPeer(nodeURL string) error {
412447
}
413448

414449
func (s *Ethereum) Stop() {
415-
// Close the database
416-
defer s.blockDb.Close()
417-
defer s.stateDb.Close()
418-
defer s.extraDb.Close()
419-
420-
s.txSub.Unsubscribe() // quits txBroadcastLoop
450+
s.txSub.Unsubscribe() // quits txBroadcastLoop
421451

422452
s.protocolManager.Stop()
423453
s.txPool.Stop()
@@ -432,6 +462,7 @@ func (s *Ethereum) Stop() {
432462

433463
// This function will wait for a shutdown and resumes main thread execution
434464
func (s *Ethereum) WaitForShutdown() {
465+
<-s.databasesClosed
435466
<-s.shutdownChan
436467
}
437468

‎ethdb/database.go‎

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package ethdb
22

33
import (
44
"sync"
5-
"time"
65

76
"github.com/ethereum/go-ethereum/compression/rle"
87
"github.com/ethereum/go-ethereum/logger"
@@ -35,8 +34,6 @@ func NewLDBDatabase(file string) (*LDBDatabase, error) {
3534
}
3635
database.makeQueue()
3736

38-
go database.update()
39-
4037
return database, nil
4138
}
4239

@@ -111,35 +108,16 @@ func (self *LDBDatabase) Flush() error {
111108
}
112109
self.makeQueue() // reset the queue
113110

111+
glog.V(logger.Detail).Infoln("Flush database: ", self.fn)
112+
114113
return self.db.Write(batch, nil)
115114
}
116115

117116
func (self *LDBDatabase) Close() {
118-
self.quit <- struct{}{}
119-
<-self.quit
120-
glog.V(logger.Info).Infoln("flushed and closed db:", self.fn)
121-
}
122-
123-
func (self *LDBDatabase) update() {
124-
ticker := time.NewTicker(1 * time.Minute)
125-
done:
126-
for {
127-
select {
128-
case <-ticker.C:
129-
if err := self.Flush(); err != nil {
130-
glog.V(logger.Error).Infof("error: flush '%s': %v\n", self.fn, err)
131-
}
132-
case <-self.quit:
133-
break done
134-
}
135-
}
136-
137117
if err := self.Flush(); err != nil {
138118
glog.V(logger.Error).Infof("error: flush '%s': %v\n", self.fn, err)
139119
}
140120

141-
// Close the leveldb database
142121
self.db.Close()
143-
144-
self.quit <- struct{}{}
122+
glog.V(logger.Error).Infoln("flushed and closed db:", self.fn)
145123
}

‎ethdb/memory_database.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,7 @@ func (db *MemDatabase) LastKnownTD() []byte {
6565

6666
return data
6767
}
68+
69+
func (db *MemDatabase) Flush() error {
70+
return nil
71+
}

0 commit comments

Comments
 (0)