github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/consensus/raft/backend/backend.go (about)

     1  package backend
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"errors"
     6  	"github.com/bigzoro/my_simplechain/accounts"
     7  	"github.com/bigzoro/my_simplechain/consensus/raft"
     8  	"github.com/bigzoro/my_simplechain/core"
     9  	"github.com/bigzoro/my_simplechain/eth"
    10  	"github.com/bigzoro/my_simplechain/eth/downloader"
    11  	"github.com/bigzoro/my_simplechain/ethdb"
    12  	"github.com/bigzoro/my_simplechain/event"
    13  	"github.com/bigzoro/my_simplechain/log"
    14  	"github.com/bigzoro/my_simplechain/miner"
    15  	"github.com/bigzoro/my_simplechain/node"
    16  	"github.com/bigzoro/my_simplechain/p2p"
    17  	"github.com/bigzoro/my_simplechain/p2p/enode"
    18  	"github.com/bigzoro/my_simplechain/rpc"
    19  	"sync"
    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   *miner.Miner
    36  	nodeKey  *ecdsa.PrivateKey
    37  }
    38  
    39  func New(ctx *node.ServiceContext, raftId, raftPort uint16, joinExisting bool, e *eth.SimpleService, startPeers []*enode.Node, datadir string) (*RaftService, error) {
    40  	service := &RaftService{
    41  		eventMux:       ctx.EventMux,
    42  		chainDb:        e.ChainDb(),
    43  		blockchain:     e.BlockChain(),
    44  		txPool:         e.TxPool(),
    45  		accountManager: e.AccountManager(),
    46  		downloader:     e.Downloader(),
    47  		startPeers:     startPeers,
    48  		nodeKey:        ctx.NodeKey(),
    49  	}
    50  
    51  	engine, ok := e.Engine().(*raft.Raft)
    52  	if !ok {
    53  		return nil, errors.New("raft service require raft engine") // never occur
    54  	}
    55  	engine.SetId(raftId)
    56  
    57  	service.minter = miner.New(service, &e.GetEthConfig().Miner, e.APIBackend.ChainConfig(), service.eventMux, e.Engine(), nil, nil)
    58  
    59  	var err error
    60  	if service.RaftProtocolManager, err = NewProtocolManager(raftId, raftPort, service.blockchain, service.eventMux, startPeers, joinExisting, datadir, service.minter, service.downloader); err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	return service, nil
    65  }
    66  
    67  // Backend interface methods:
    68  
    69  func (service *RaftService) AccountManager() *accounts.Manager { return service.accountManager }
    70  func (service *RaftService) BlockChain() *core.BlockChain      { return service.blockchain }
    71  func (service *RaftService) ChainDb() ethdb.Database           { return service.chainDb }
    72  func (service *RaftService) EventMux() *event.TypeMux          { return service.eventMux }
    73  func (service *RaftService) TxPool() *core.TxPool              { return service.txPool }
    74  
    75  // node.Service interface methods:
    76  
    77  func (service *RaftService) Protocols() []p2p.Protocol { return []p2p.Protocol{} }
    78  func (service *RaftService) APIs() []rpc.API {
    79  	return []rpc.API{
    80  		{
    81  			Namespace: "raft",
    82  			Version:   "1.0",
    83  			Service:   NewPublicRaftAPI(service),
    84  			Public:    true,
    85  		},
    86  	}
    87  }
    88  
    89  // Start implements node.Service, starting the background data propagation thread
    90  // of the protocol.
    91  func (service *RaftService) Start(p2pServer *p2p.Server) error {
    92  	service.RaftProtocolManager.Start(p2pServer)
    93  	return nil
    94  }
    95  
    96  // Stop implements node.Service, stopping the background data propagation thread
    97  // of the protocol.
    98  func (service *RaftService) Stop() error {
    99  	service.blockchain.Stop()
   100  	service.RaftProtocolManager.Stop()
   101  	service.minter.Stop()
   102  	service.eventMux.Stop()
   103  
   104  	//service.chainDb.Close()
   105  
   106  	log.Info("Raft stopped")
   107  	return nil
   108  }