aboutsummaryrefslogtreecommitdiffstats
path: root/eth
diff options
context:
space:
mode:
authorBas van Kervel <basvankervel@gmail.com>2015-04-22 18:46:41 +0800
committerBas van Kervel <basvankervel@gmail.com>2015-04-23 23:58:58 +0800
commitc273ed7d82f3d5beab7c213fbe1f5c0942adf0bd (patch)
tree623972174faa894c499a7ad808668cb64e17cbcd /eth
parent94f2adb80a54c9bddacf146138d8e218a95629c9 (diff)
downloaddexon-c273ed7d82f3d5beab7c213fbe1f5c0942adf0bd.tar.gz
dexon-c273ed7d82f3d5beab7c213fbe1f5c0942adf0bd.tar.zst
dexon-c273ed7d82f3d5beab7c213fbe1f5c0942adf0bd.zip
Moved leveldb update loop to eth/backend
change order of block insert and update LastBlock bugfix, wrong hash stored in blockDb
Diffstat (limited to 'eth')
-rw-r--r--eth/backend.go67
1 files changed, 49 insertions, 18 deletions
diff --git a/eth/backend.go b/eth/backend.go
index 646a4eaf2..0e671f1bf 100644
--- a/eth/backend.go
+++ b/eth/backend.go
@@ -5,6 +5,7 @@ import (
"fmt"
"path"
"strings"
+ "time"
"github.com/ethereum/ethash"
"github.com/ethereum/go-ethereum/accounts"
@@ -123,6 +124,8 @@ type Ethereum struct {
blockDb common.Database // Block chain database
stateDb common.Database // State changes database
extraDb common.Database // Extra database (txs, etc)
+ // Closed when databases are flushed and closed
+ databasesClosed chan bool
//*** SERVICES ***
// State manager for processing new blocks and managing the over all states
@@ -197,18 +200,19 @@ func New(config *Config) (*Ethereum, error) {
glog.V(logger.Info).Infof("Blockchain DB Version: %d", config.BlockChainVersion)
eth := &Ethereum{
- shutdownChan: make(chan bool),
- blockDb: blockDb,
- stateDb: stateDb,
- extraDb: extraDb,
- eventMux: &event.TypeMux{},
- accountManager: config.AccountManager,
- DataDir: config.DataDir,
- etherbase: common.HexToAddress(config.Etherbase),
- clientVersion: config.Name, // TODO should separate from Name
- ethVersionId: config.ProtocolVersion,
- netVersionId: config.NetworkId,
- NatSpec: config.NatSpec,
+ shutdownChan: make(chan bool),
+ databasesClosed: make(chan bool),
+ blockDb: blockDb,
+ stateDb: stateDb,
+ extraDb: extraDb,
+ eventMux: &event.TypeMux{},
+ accountManager: config.AccountManager,
+ DataDir: config.DataDir,
+ etherbase: common.HexToAddress(config.Etherbase),
+ clientVersion: config.Name, // TODO should separate from Name
+ ethVersionId: config.ProtocolVersion,
+ netVersionId: config.NetworkId,
+ NatSpec: config.NatSpec,
}
eth.chainManager = core.NewChainManager(blockDb, stateDb, eth.EventMux())
@@ -376,6 +380,9 @@ func (s *Ethereum) Start() error {
}
}
+ // periodically flush databases
+ go s.syncDatabases()
+
// Start services
go s.txPool.Start()
s.protocolManager.Start()
@@ -392,6 +399,34 @@ func (s *Ethereum) Start() error {
return nil
}
+func (s *Ethereum) syncDatabases() {
+ ticker := time.NewTicker(1 * time.Minute)
+done:
+ for {
+ select {
+ case <-ticker.C:
+ // don't change the order of database flushes
+ if err := s.extraDb.Flush(); err != nil {
+ glog.V(logger.Error).Infof("error: flush extraDb: %v\n", err)
+ }
+ if err := s.stateDb.Flush(); err != nil {
+ glog.V(logger.Error).Infof("error: flush stateDb: %v\n", err)
+ }
+ if err := s.blockDb.Flush(); err != nil {
+ glog.V(logger.Error).Infof("error: flush blockDb: %v\n", err)
+ }
+ case <-s.shutdownChan:
+ break done
+ }
+ }
+
+ s.blockDb.Close()
+ s.stateDb.Close()
+ s.extraDb.Close()
+
+ close(s.databasesClosed)
+}
+
func (s *Ethereum) StartForTest() {
jsonlogger.LogJson(&logger.LogStarting{
ClientString: s.net.Name,
@@ -412,12 +447,7 @@ func (self *Ethereum) SuggestPeer(nodeURL string) error {
}
func (s *Ethereum) Stop() {
- // Close the database
- defer s.blockDb.Close()
- defer s.stateDb.Close()
- defer s.extraDb.Close()
-
- s.txSub.Unsubscribe() // quits txBroadcastLoop
+ s.txSub.Unsubscribe() // quits txBroadcastLoop
s.protocolManager.Stop()
s.txPool.Stop()
@@ -432,6 +462,7 @@ func (s *Ethereum) Stop() {
// This function will wait for a shutdown and resumes main thread execution
func (s *Ethereum) WaitForShutdown() {
+ <-s.databasesClosed
<-s.shutdownChan
}