github.com/Gessiux/neatchain@v1.3.1/utilities/utils/neatchain_p2p_server.go (about) 1 package utils 2 3 import ( 4 "github.com/Gessiux/neatchain/network/node" 5 "github.com/Gessiux/neatchain/network/p2p" 6 "github.com/Gessiux/neatchain/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 // Load Default P2P config 18 config := &node.Config{ 19 GeneralDataDir: MakeDataDir(ctx), 20 DataDir: MakeDataDir(ctx), // Just for pass the check, P2P always use GeneralDataDir 21 P2P: node.DefaultConfig.P2P, 22 } 23 24 // Setup the config from context 25 SetP2PConfig(ctx, &config.P2P) 26 27 // Initialize the intp2p server. This creates the node key and 28 // discovery databases. 29 serverConfig := config.P2P 30 serverConfig.PrivateKey = config.NodeKey() 31 serverConfig.Name = config.NodeName() 32 serverConfig.EnableMsgEvents = true 33 34 if serverConfig.StaticNodes == nil { 35 serverConfig.StaticNodes = config.StaticNodes() 36 } 37 if serverConfig.TrustedNodes == nil { 38 serverConfig.TrustedNodes = config.TrustedNodes() 39 } 40 if serverConfig.NodeDatabase == "" { 41 serverConfig.NodeDatabase = config.NodeDB() 42 } 43 serverConfig.LocalValidators = make([]p2p.P2PValidator, 0) 44 serverConfig.Validators = make(map[p2p.P2PValidator]*p2p.P2PValidatorNodeInfo) 45 46 running := &p2p.Server{Config: serverConfig} 47 //log.Info("Creating peer-to-peer neatnode", "instance", serverConfig.Name) 48 49 return &NeatChainP2PServer{ 50 serverConfig: serverConfig, 51 server: running, 52 } 53 } 54 55 func (srv *NeatChainP2PServer) Server() *p2p.Server { 56 return srv.server 57 } 58 59 func (srv *NeatChainP2PServer) Stop() { 60 srv.server.Stop() 61 } 62 63 func (srv *NeatChainP2PServer) BroadcastNewSideChainMsg(sideId string) { 64 srv.server.BroadcastMsg(p2p.BroadcastNewSideChainMsg, sideId) 65 } 66 67 func (srv *NeatChainP2PServer) AddLocalValidator(chainId string, address common.Address) { 68 srv.server.AddLocalValidator(chainId, address) 69 } 70 71 func (srv *NeatChainP2PServer) RemoveLocalValidator(chainId string, address common.Address) { 72 srv.server.RemoveLocalValidator(chainId, address) 73 }