github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/utilities/utils/neatchain_p2p_server.go (about)

     1  package utils
     2  
     3  import (
     4  	"github.com/neatio-net/neatio/network/node"
     5  	"github.com/neatio-net/neatio/network/p2p"
     6  	"github.com/neatio-net/neatio/utilities/common"
     7  	"gopkg.in/urfave/cli.v1"
     8  )
     9  
    10  type NeatChainP2PServer struct {
    11  	serverConfig p2p.Config
    12  	server       *p2p.Server
    13  }
    14  
    15  func NewP2PServer(ctx *cli.Context) *NeatChainP2PServer {
    16  
    17  	config := &node.Config{
    18  		GeneralDataDir: MakeDataDir(ctx),
    19  		DataDir:        MakeDataDir(ctx),
    20  		P2P:            node.DefaultConfig.P2P,
    21  	}
    22  
    23  	SetP2PConfig(ctx, &config.P2P)
    24  
    25  	serverConfig := config.P2P
    26  	serverConfig.PrivateKey = config.NodeKey()
    27  	serverConfig.Name = config.NodeName()
    28  	serverConfig.EnableMsgEvents = true
    29  
    30  	if serverConfig.StaticNodes == nil {
    31  		serverConfig.StaticNodes = config.StaticNodes()
    32  	}
    33  	if serverConfig.TrustedNodes == nil {
    34  		serverConfig.TrustedNodes = config.TrustedNodes()
    35  	}
    36  	if serverConfig.NodeDatabase == "" {
    37  		serverConfig.NodeDatabase = config.NodeDB()
    38  	}
    39  	serverConfig.LocalValidators = make([]p2p.P2PValidator, 0)
    40  	serverConfig.Validators = make(map[p2p.P2PValidator]*p2p.P2PValidatorNodeInfo)
    41  
    42  	running := &p2p.Server{Config: serverConfig}
    43  
    44  	return &NeatChainP2PServer{
    45  		serverConfig: serverConfig,
    46  		server:       running,
    47  	}
    48  }
    49  
    50  func (srv *NeatChainP2PServer) Server() *p2p.Server {
    51  	return srv.server
    52  }
    53  
    54  func (srv *NeatChainP2PServer) Stop() {
    55  	srv.server.Stop()
    56  }
    57  
    58  func (srv *NeatChainP2PServer) BroadcastNewSideChainMsg(sideId string) {
    59  	srv.server.BroadcastMsg(p2p.BroadcastNewSideChainMsg, sideId)
    60  }
    61  
    62  func (srv *NeatChainP2PServer) AddLocalValidator(chainId string, address common.Address) {
    63  	srv.server.AddLocalValidator(chainId, address)
    64  }
    65  
    66  func (srv *NeatChainP2PServer) RemoveLocalValidator(chainId string, address common.Address) {
    67  	srv.server.RemoveLocalValidator(chainId, address)
    68  }