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+
395430func (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
414449func (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
434464func (s * Ethereum ) WaitForShutdown () {
465+ <- s .databasesClosed
435466 <- s .shutdownChan
436467}
437468
0 commit comments