github.com/Night-mk/quorum@v21.1.0+incompatible/raft/backend.go (about)

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