Exemplo n.º 1
0
 public string ToAddress()
 {
     return(Wallet.ToAddress(publickey));
 }
Exemplo n.º 2
0
        // console 支持

        public void OnStats(HttpMessage httpMessage)
        {
            if (!GetParam(httpMessage, "1", "style", out string style))
            {
                style = "";
            }
            Dictionary <string, object> result = new Dictionary <string, object>();
            string  address = Wallet.GetWallet().GetCurWallet().ToAddress();
            Account account = null;

            using (DbSnapshot dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot(0))
            {
                account = dbSnapshot.Accounts.Get(address);
            }

            var rule   = Entity.Root.GetComponent <Rule>();
            var pool   = Entity.Root.GetComponent <Pool>();
            var amount = account != null ? account.amount : "0";

            long.TryParse(Entity.Root.GetComponent <LevelDBStore>().Get("UndoHeight"), out long UndoHeight);
            long   PoolHeight = rule.height;
            string power1     = rule.calculatePower.GetPower();
            string power2     = Entity.Root.GetComponent <Consensus>().calculatePower.GetPower();

            int NodeCount = Entity.Root.GetComponent <NodeManager>().GetNodeCount();

            if (style == "")
            {
                result.Add("AlppyHeight", UndoHeight);
                result.Add("PoolHeight", PoolHeight);
                result.Add("Calculate Power", $"{power1}of{power2}");
                result.Add("Account", $"{ address},{amount}");
                result.Add("Node", NodeCount);
                result.Add("Rule.Transfer", rule?.GetTransfersCount());
                result.Add("Pool.Transfer", pool?.GetTransfersCount());
                httpMessage.result = JsonHelper.ToJson(result);
                //httpMessage.result = $"      AlppyHeight: {UndoHeight}\n" +
                //                        $"       PoolHeight: {PoolHeight}\n" +
                //                        $"  Calculate Power: {power1} of {power2}\n" +
                //                        $"          Account: {address}, {amount}\n" +
                //                        $"             Node: {NodeCount}\n" +
                //                        $"    Rule.Transfer: {rule?.GetTransfersCount()}\n" +
                //                        $"    Pool.Transfer: {pool?.GetTransfersCount()}";
            }
            else
            if (style == "1")
            {
                result.Add("H", UndoHeight);
                result.Add("P", power2);
                httpMessage.result = JsonHelper.ToJson(result);
                //httpMessage.result = $"H:{UndoHeight} P:{power2}";
            }
            else
            if (style == "2")
            {
                var httpPool = Entity.Root.GetComponent <HttpPool>();
                var miners   = httpPool.GetMinerReward(out long miningHeight);
                result.Add("H", UndoHeight);
                result.Add("P", power2);
                result.Add("P1", power1);
                result.Add("Miner", miners?.Count);
                httpMessage.result = JsonHelper.ToJson(result);
                //httpMessage.result = $"H:{UndoHeight} P:{power2} Miner:{miners?.Count}";
            }
        }
Exemplo n.º 3
0
        public void Test(HttpMessage httpMessage)
        {
            if (!GetParam(httpMessage, "1", "password", out string password))
            {
                httpMessage.result = "command error! \nexample: test 1 Address C:\\Address.csv";
                return;
            }
            if (!Wallet.GetWallet().IsPassword(password))
            {
                return;
            }

            if (!GetParam(httpMessage, "2", "style", out string style))
            {
                httpMessage.result = "command error! \nexample: test 1 Address C:\\Address.csv";
                return;
            }

            httpMessage.result = "";
            if (style == "1")
            {
                if (!GetParam(httpMessage, "3", "Address", out string Address))
                {
                    httpMessage.result = "command error! \nexample: test 1 Address C:\\Address.csv";
                    return;
                }
                if (!GetParam(httpMessage, "4", "file", out string file))
                {
                    httpMessage.result = "command error! \nexample: test 1 Address C:\\Address.csv";
                    return;
                }

                LevelDBStore.Export2CSV_Transfer($"{file}", Address);
            }
            else
            if (style == "2")
            {
                OneThreadSynchronizationContext.Instance.Post(this.Test2Async, null);
            }
            else
            if (style == "3")
            {
                OneThreadSynchronizationContext.Instance.Post(this.Test3Async, null);
            }
            else
            if (style == "4")
            {
                LevelDBStore.Export2CSV_Accounts($"C:\\Accounts_test4.csv");
            }
            else
            if (style == "5")
            {
                if (!GetParam(httpMessage, "3", "Address", out string Address))
                {
                    httpMessage.result = "command error! \nexample: test 5 Address C:\\Address.csv";
                    return;
                }
                if (!GetParam(httpMessage, "4", "file", out string file))
                {
                    httpMessage.result = "command error! \nexample: test 5 Address C:\\Address.csv";
                    return;
                }
                LevelDBStore.Export2CSV_Account($"{file}", Address);
            }
            else
            if (style == "rule")
            {
                TestBeRule(httpMessage);
            }
        }
Exemplo n.º 4
0
        private static void Main(string[] args)
        {
            Dictionary <string, string> param = new Dictionary <string, string>();

            for (int i = 0; i < args.Length; i++)
            {
                int jj = args[i].IndexOf(':');
                if (jj != -1)
                {
                    param.TryAdd(args[i].Substring(0, jj).Replace("-", ""), args[i].Substring(jj + 1, args[i].Length - (jj + 1)));
                }
                else
                {
                    param.TryAdd(args[i].Replace("-", ""), "true");
                }
            }

            param.TryAdd("configure", "");
            param.TryAdd("node", "All");
            param.TryAdd("wallet", "./Data/wallet.json");
            param.TryAdd("db", "./Data/LevelDB");

            if (param.TryGetValue("index", out string index))
            {
                param["wallet"] = param["wallet"].Replace(".json", $"{index}.json");
                param["db"]     = param["db"] + index;
            }

            if (param.TryGetValue("miner", out string tmp1))
            {
                Entity.Root.AddComponent <Miner>().Init(param);
                Update();
                return;
            }

            //// DAG 创建测试
            //char[] dag = new char[1024*1024*10];
            //var hash = CryptoHelper.Sha256("sat dag");
            //var count = dag.Length / hash.Length;
            //int dagIndex = 0;
            //for (int ii = 0; ii < count; ii++)
            //{
            //    hash = CryptoHelper.Sha256(hash);
            //    for (int jj = 0; jj < hash.Length; jj++)
            //    {
            //        dag[dagIndex++] = hash[jj];
            //    }
            //}

            //BigHelper.Test();
            //return;

            //CalculatePower.Test();
            //return;

            //Wallet.Test2();
            //return;

            //Wallet.Test3();
            //return;

            // 测试代码
            //LuaVMEnv.TestRapidjson(args);
            //LuaVMEnv.TestLib(args);
            //LuaVMEnv.TestCoroutine(args);
            //LuaVMEnv.Test_number(args);
            //LevelDBStore.test_delete(args);
            //LevelDBStore.test_undo(args);
            //LevelDBStore.Export2CSV_Block(args);
            //LevelDBStore.test_ergodic(args);
            //return;

            //Log.Info(Environment.CurrentDirectory);
            string walletFile = param["wallet"];
            Wallet wallet     = Wallet.GetWallet(walletFile);

            if (wallet == null)
            {
                return;
            }

            if (param.TryGetValue("makeGenesis", out string tmp2))
            {
                Consensus.MakeGenesis();
                return;
            }

            //DisbleQuickEditMode();
            Console.Clear();
            Console.CursorVisible = false;
            Console.Title         = $"SmartX 配置: {param["configure"]} {index} Address: {wallet.GetCurWallet().ToAddress()}";
            Log.Debug($"address: {wallet.GetCurWallet().ToAddress()}");

            string NodeKey = param["node"];

            // 异步方法全部会回调到主线程
            SynchronizationContext.SetSynchronizationContext(OneThreadSynchronizationContext.Instance);
            AssemblyHelper.AddAssembly("Base", typeof(AssemblyHelper).Assembly);
            AssemblyHelper.AddAssembly("App", typeof(Program).Assembly);

            // 读取配置文件
            try
            {
                StreamReader sr     = new StreamReader(new FileStream(param["configure"], FileMode.Open, FileAccess.Read, FileShare.ReadWrite), System.Text.Encoding.UTF8);
                string       strTxt = sr.ReadToEnd();
                strTxt = strTxt.Replace("0.0.0.0", NodeManager.GetIpV4());
                sr.Close(); sr.Dispose();
                JToken jd = JToken.Parse(strTxt);
                jdNode = jd[NodeKey];
            }
            catch (Exception e)
            {
                Log.Info(e.ToString());
                Log.Error($"configure file: {param["configure"]} on exists ro json foramt error.");
                Console.ReadKey();
                return;
            }

            if (jdNode != null)
            {
                Log.Debug("启动: " + jdNode["appType"]);

                // DNS
                List <string> list = JsonHelper.FromJson <List <string> >(jdNode["NodeSessions"].ToString());
                for (int ii = 0; ii < list.Count; ii++)
                {
                    list[ii] = NetworkHelper.DnsToIPEndPoint(list[ii]).ToString();
                }
                jdNode["NodeSessions"] = JsonHelper.ToJson(list);

                if (!string.IsNullOrEmpty(index))
                {
                    if (jdNode["HttpRpc"] != null)
                    {
                        jdNode["HttpRpc"]["ComponentNetworkHttp"]["address"] = ((string)jdNode["HttpRpc"]["ComponentNetworkHttp"]["address"]).Replace("8101", (8100 + int.Parse(index)).ToString());
                    }
                    if (jdNode["HttpRpc"] != null)
                    {
                        jdNode["HttpPool"]["ComponentNetworkHttp"]["address"] = ((string)jdNode["HttpPool"]["ComponentNetworkHttp"]["address"]).Replace("9101", (9100 + int.Parse(index)).ToString());
                    }
                    if (jdNode["HttpRpc"] != null)
                    {
                        jdNode["SmartxRpc"]["ComponentNetworkHttp"]["address"] = ((string)jdNode["SmartxRpc"]["ComponentNetworkHttp"]["address"]).Replace("5000", ((5000 - 1) + int.Parse(index)).ToString());
                    }
                    if (jdNode["HttpRpc"] != null)
                    {
                        jdNode["ComponentNetworkInner"]["address"] = ((string)jdNode["ComponentNetworkInner"]["address"]).Replace("58601", (58600 + int.Parse(index)).ToString());
                    }
                    if (jdNode["RelayNetwork"] != null)
                    {
                        jdNode["RelayNetwork"]["ComponentNetworkInner"]["address"] = ((string)jdNode["RelayNetwork"]["ComponentNetworkInner"]["address"]).Replace("57601", (57600 + int.Parse(index)).ToString());
                    }
                    if (jdNode["Pool"] != null)
                    {
                        jdNode["Pool"]["db_path"] = ((string)jdNode["Pool"]["db_path"]) + index;
                    }
                }

                // 数据库路径
                if (jdNode["LevelDBStore"] != null && args.Length >= 3)
                {
                    jdNode["LevelDBStore"]["db_path"] = param["db"];
                }

                Entity.Root.AddComponent <ComponentStart>(jdNode);
            }

            Update();
        }