public Module_Node(AllPet.Common.ILogger logger, Newtonsoft.Json.Linq.JObject configJson) : base(true) { this.guid = Helper_NEO.CalcHash256(Guid.NewGuid().ToByteArray()); this.logger = logger; this.config = new Config_Module(configJson); this.chainHash = Helper_NEO.CalcHash256(this.config.ChainInfo.ToInitScript()); //this.config = new Config_ChainInit(configJson); this.linkNodes = new System.Collections.Concurrent.ConcurrentDictionary <ulong, LinkObj>(); this.provedNodes = new System.Collections.Concurrent.ConcurrentDictionary <ulong, LinkObj>(); this.linkIDs = new System.Collections.Concurrent.ConcurrentDictionary <string, ulong>(); this.listCanlink = new Struct.ThreadSafeQueueWithKey <CanLinkObj>(); try { if (configJson.ContainsKey("Key_Nep2") && configJson.ContainsKey("Key_Password")) { var nep2 = configJson["Key_Nep2"].AsString(); var password = configJson["Key_Password"].AsString(); this.prikey = Helper_NEO.GetPrivateKeyFromNEP2(nep2, password); this.pubkey = Helper_NEO.GetPublicKey_FromPrivateKey(prikey); //区分记账人 var address = Helper_NEO.GetAddress_FromPublicKey(pubkey); //证明人的地址 //如果证明人的地址和初始记账人的地址相同即为记账人 if (this.config.ChainInfo.InitOwner.Contains(address)) { this.isProved = true; this.pLevel = 0;//记账节点 } } //return; blockChain = new BlockChain(); blockChain.InitChain(this.config.SimpleDbPath, this.config.ChainInfo); this.blockIndex = blockChain.GetBlockCount(); //记账节点才需要出块 if (this.isProved) { this.blockTimer = new System.Timers.Timer(); this.blockTimer.Interval = blockTime * 1000; //毫秒 this.blockTimer.Enabled = true; this.blockTimer.AutoReset = true; //一直执行true this.blockTimer.Elapsed += new System.Timers.ElapsedEventHandler(MakeBlock); this.blockTimer.Start(); } } catch (Exception err) { logger.Error("Error in Get Prikey:" + err.ToString()); throw new Exception("error in get prikey.", err); } this.txpool = new Node.TXPool(); ResetCanlinkList(); }
public Node(string EndPoint = null, string initpeer = null, string ListenEndPoint = null, bool beEnableQueryPeers = true, string jsonFile = "config.json") { logger = new AllPet.Common.Logger(); logger.Warn("Allpet.Node v0.001 Peer 01"); var config = new Config(logger); //init current path. //把当前目录搞对,怎么启动都能找到dll了 var lastpath = System.IO.Path.GetDirectoryName(typeof(Program).Assembly.Location);; Console.WriteLine("exepath=" + lastpath); Environment.CurrentDirectory = lastpath; var system = AllPet.Pipeline.PipelineSystem.CreatePipelineSystemV1(new AllPet.Common.Logger()); var config_node = config.GetJson(jsonFile, ".ModulesConfig.Node") as JObject; //-----------------配置 publicendpoint /initpeer if (EndPoint != null) { config_node["PublicEndPoint"] = EndPoint; } if (initpeer != null) { config_node["InitPeer"] = new JArray(initpeer); } if (Config.IsOpen(config_node)) { this.actor = new AllPet.Module.Module_Node(logger, config_node); system.RegistModule("node", this.actor); this.actor.beEnableQueryPeers = beEnableQueryPeers; } else { logger.Error("cant find config for node"); return; } system.OpenNetwork(new AllPet.peer.tcp.PeerOption() { }); var endpoint = config.GetIPEndPoint(jsonFile, ".ListenEndPoint"); if (ListenEndPoint != null) { endpoint = ListenEndPoint.AsIPEndPoint(); } if (endpoint != null && endpoint.Port != 0) { try { system.OpenListen(endpoint); } catch (Exception err) { logger.Error("listen error:" + err.ToString()); } } system.Start(); //等待cli结束才退出 var pipeline = system.GetPipeline(null, "this/node"); //while (pipeline.IsVaild) //{ // var line = Console.ReadLine(); // if (string.IsNullOrEmpty(line) == false) // { // if (line == "exit") // { // break; // } // var cmds = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); // var dict = new MsgPack.MessagePackObjectDictionary(); // dict["cmd"] = (UInt16)AllPet.Module.CmdList.Local_Cmd; // var list = new MsgPack.MessagePackObject[cmds.Length]; // for (var i = 0; i < cmds.Length; i++) // { // list[i] = cmds[i]; // } // dict["params"] = list; // pipeline.Tell(new MsgPack.MessagePackObject(dict)); // } //} //system.Dispose(); this.sys = system; }
static void Main(string[] args) { logger = new AllPet.Common.Logger(); logger.Warn("Allpet.Node v0.001 Peer 01"); var config = new Config(logger); //init current path. //把当前目录搞对,怎么启动都能找到dll了 var lastpath = System.IO.Path.GetDirectoryName(typeof(Program).Assembly.Location);; Console.WriteLine("exepath=" + lastpath); Environment.CurrentDirectory = lastpath; var system = AllPet.Pipeline.PipelineSystem.CreatePipelineSystemV1(new AllPet.Common.Logger()); var config_node = config.GetJson("config.json", ".ModulesConfig.Node") as JObject; if (Config.IsOpen(config_node)) { system.RegistModule("node", new AllPet.Module.Module_Node(logger, config_node)); } else { logger.Error("cant find config for node"); return; } system.OpenNetwork(new AllPet.peer.tcp.PeerOption() { }); var endpoint = config.GetIPEndPoint("config.json", ".ListenEndPoint"); if (endpoint != null && endpoint.Port != 0) { try { system.OpenListen(endpoint); } catch (Exception err) { logger.Error("listen error:" + err.ToString()); } } system.Start(); //等待cli结束才退出 var pipeline = system.GetPipeline(null, "this/node"); while (pipeline.IsVaild) { var line = Console.ReadLine(); if (string.IsNullOrEmpty(line) == false) { if (line == "exit") { break; } var cmds = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); var dict = new MsgPack.MessagePackObjectDictionary(); dict["cmd"] = (UInt16)AllPet.Module.CmdList.Local_Cmd; var list = new MsgPack.MessagePackObject[cmds.Length]; for (var i = 0; i < cmds.Length; i++) { list[i] = cmds[i]; } dict["params"] = list; pipeline.Tell(new MsgPack.MessagePackObject(dict)); } } system.Dispose(); }
static void Main(string[] args) { logger = new AllPet.Common.Logger(); logger.Warn("Allpet.Node v0.001"); var config = new Config(logger); //init current path. //把当前目录搞对,怎么启动都能找到dll了 var lastpath = System.IO.Path.GetDirectoryName(typeof(Program).Assembly.Location);; Console.WriteLine("exepath=" + lastpath); Environment.CurrentDirectory = lastpath; var system = AllPet.Pipeline.PipelineSystem.CreatePipelineSystemV1(logger); var config_cli = config.GetJson("config.json", ".ModulesConfig.Cli") as JObject; var config_node = config.GetJson("config.json", ".ModulesConfig.Node") as JObject; var config_rpc = config.GetJson("config.json", ".ModulesConfig.RPC") as JObject; if (config_node.ContainsKey("Key_Nep2") && config_node.ContainsKey("Key_Password") == false) { Console.Write("input Key for Nep2>"); var pass = Console.ReadLine(); config_node["Key_Password"] = pass; } if (Config.IsOpen(config_cli)) { system.RegistModule("cli", new Module_Cli(logger, config_cli)); } if (Config.IsOpen(config_node)) { system.RegistModule("node", new AllPet.Module.Module_Node(logger, config_node)); } if (Config.IsOpen(config_rpc)) { system.RegistModule("rpc", new AllPet.Module.Module_RPC(logger, config_rpc)); } system.OpenNetwork(new AllPet.peer.tcp.PeerOption() { }); var endpoint = config.GetIPEndPoint("config.json", ".ListenEndPoint"); if (endpoint != null && endpoint.Port != 0) { try { system.OpenListen(endpoint); } catch (Exception err) { logger.Error("open listen err:" + err); } } //是不是开listen 这个事情可以留给Module system.Start(); //等待cli结束才退出 var pipeline = system.GetPipeline(null, "this/cli"); while (pipeline.IsVaild) { var line = Console.ReadLine(); if (line == "exit") { break; } System.Threading.Thread.Sleep(100); } system.Dispose(); }