|
|
|
@ -6,6 +6,7 @@ import ( |
|
|
|
|
"math" |
|
|
|
|
"path" |
|
|
|
|
"strings" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/ethereum/ethash" |
|
|
|
|
"github.com/ethereum/go-ethereum/accounts" |
|
|
|
@ -124,6 +125,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
|
|
|
|
@ -200,6 +203,7 @@ func New(config *Config) (*Ethereum, error) { |
|
|
|
|
|
|
|
|
|
eth := &Ethereum{ |
|
|
|
|
shutdownChan: make(chan bool), |
|
|
|
|
databasesClosed: make(chan bool), |
|
|
|
|
blockDb: blockDb, |
|
|
|
|
stateDb: stateDb, |
|
|
|
|
extraDb: extraDb, |
|
|
|
@ -378,6 +382,9 @@ func (s *Ethereum) Start() error { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// periodically flush databases
|
|
|
|
|
go s.syncDatabases() |
|
|
|
|
|
|
|
|
|
// Start services
|
|
|
|
|
s.txPool.Start() |
|
|
|
|
|
|
|
|
@ -397,6 +404,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, |
|
|
|
@ -417,11 +452,6 @@ 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.minedBlockSub.Unsubscribe() // quits blockBroadcastLoop
|
|
|
|
|
|
|
|
|
@ -437,6 +467,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 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|