github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/raft/backend.go (about) 1 package raft 2 3 import ( 4 "crypto/ecdsa" 5 "sync" 6 "time" 7 8 "github.com/kisexp/xdchain/accounts" 9 "github.com/kisexp/xdchain/core" 10 "github.com/kisexp/xdchain/core/types" 11 "github.com/kisexp/xdchain/eth" 12 "github.com/kisexp/xdchain/eth/downloader" 13 "github.com/kisexp/xdchain/ethdb" 14 "github.com/kisexp/xdchain/event" 15 "github.com/kisexp/xdchain/log" 16 "github.com/kisexp/xdchain/node" 17 "github.com/kisexp/xdchain/p2p/enode" 18 "github.com/kisexp/xdchain/params" 19 "github.com/kisexp/xdchain/rpc" 20 ) 21 22 type RaftService struct { 23 blockchain *core.BlockChain 24 chainDb ethdb.Database // Block chain database 25 txMu sync.Mutex 26 txPool *core.TxPool 27 accountManager *accounts.Manager 28 downloader *downloader.Downloader 29 30 raftProtocolManager *ProtocolManager 31 startPeers []*enode.Node 32 33 // we need an event mux to instantiate the blockchain 34 eventMux *event.TypeMux 35 minter *minter 36 nodeKey *ecdsa.PrivateKey 37 calcGasLimitFunc func(block *types.Block) uint64 38 39 pendingLogsFeed *event.Feed 40 } 41 42 func New(stack *node.Node, chainConfig *params.ChainConfig, raftId, raftPort uint16, joinExisting bool, blockTime time.Duration, e *eth.Ethereum, startPeers []*enode.Node, raftLogDir string, useDns bool) (*RaftService, error) { 43 service := &RaftService{ 44 eventMux: stack.EventMux(), 45 chainDb: e.ChainDb(), 46 blockchain: e.BlockChain(), 47 txPool: e.TxPool(), 48 accountManager: e.AccountManager(), 49 downloader: e.Downloader(), 50 startPeers: startPeers, 51 nodeKey: stack.GetNodeKey(), 52 calcGasLimitFunc: e.CalcGasLimit, 53 pendingLogsFeed: e.ConsensusServicePendingLogsFeed(), 54 } 55 56 service.minter = newMinter(chainConfig, service, blockTime) 57 58 var err error 59 if service.raftProtocolManager, err = NewProtocolManager(raftId, raftPort, service.blockchain, service.eventMux, startPeers, joinExisting, raftLogDir, service.minter, service.downloader, useDns, stack.Server()); err != nil { 60 return nil, err 61 } 62 63 stack.RegisterAPIs(service.apis()) 64 stack.RegisterLifecycle(service) 65 66 return service, nil 67 } 68 69 // Utility methods 70 71 func (service *RaftService) apis() []rpc.API { 72 return []rpc.API{ 73 { 74 Namespace: "raft", 75 Version: "1.0", 76 Service: NewPublicRaftAPI(service), 77 Public: true, 78 }, 79 } 80 } 81 82 // Backend interface methods: 83 84 func (service *RaftService) AccountManager() *accounts.Manager { return service.accountManager } 85 func (service *RaftService) BlockChain() *core.BlockChain { return service.blockchain } 86 func (service *RaftService) ChainDb() ethdb.Database { return service.chainDb } 87 func (service *RaftService) DappDb() ethdb.Database { return nil } 88 func (service *RaftService) EventMux() *event.TypeMux { return service.eventMux } 89 func (service *RaftService) TxPool() *core.TxPool { return service.txPool } 90 91 // node.Lifecycle interface methods: 92 93 // Start implements node.Service, starting the background data propagation thread 94 // of the protocol. 95 func (service *RaftService) Start() error { 96 service.raftProtocolManager.Start() 97 return nil 98 } 99 100 // Stop implements node.Service, stopping the background data propagation thread 101 // of the protocol. 102 func (service *RaftService) Stop() error { 103 service.blockchain.Stop() 104 service.raftProtocolManager.Stop() 105 service.minter.stop() 106 service.eventMux.Stop() 107 108 // handles gracefully if freezedb process is already stopped 109 service.chainDb.Close() 110 111 log.Info("Raft stopped") 112 return nil 113 }