Exemplo n.º 1
0
        public void SaveRecentPeers()
        {
            List <IPEndPoint> seedlist = new List <IPEndPoint>();

            foreach (string hostAndPort in SeedList)
            {
                try
                {
                    string[]   p    = hostAndPort.Split(':');
                    IPEndPoint seed = Zoro.Helper.GetIPEndpointFromHostPort(p[0], int.Parse(p[1]));
                    seedlist.Add(seed);
                }
                catch (Exception)
                {
                    continue;
                }
            }

            IPEndPoint[] peers = ConnectedPeers.Values.Where(p => !seedlist.Contains(p)).ToArray();
            if (peers.Length == 0)
            {
                return;
            }

            string path = string.Format(RecentPeersFile, ChainHash.ToString());

            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (BinaryWriter writer = new BinaryWriter(fs, Encoding.ASCII, true))
                {
                    writer.Write(peers.Length);
                    foreach (IPEndPoint endpoint in peers)
                    {
                        writer.Write(endpoint.Address.MapToIPv4().GetAddressBytes());
                        writer.Write((ushort)endpoint.Port);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void LoadRecentPeers()
        {
            string path = string.Format(RecentPeersFile, ChainHash.ToString());

            if (!File.Exists(path))
            {
                return;
            }

            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                UnconnectedPeers.Clear();
                using (BinaryReader reader = new BinaryReader(fs, Encoding.ASCII, true))
                {
                    int count = reader.ReadInt32();
                    for (int i = 0; i < count; i++)
                    {
                        IPAddress address = new IPAddress(reader.ReadBytes(4));
                        int       port    = reader.ReadUInt16();
                        ImmutableInterlocked.Update(ref UnconnectedPeers, p => p.Add(new IPEndPoint(address.MapToIPv6(), port)));
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void Save(MySqlConnection conn, JObject jObject, uint blockHeight, uint blockTime)
        {
            List <string> slist = new List <string>();

            slist.Add(SpiderHelper.getString(jObject["txid"].ToString()));
            slist.Add(SpiderHelper.getString(jObject["size"].ToString()));
            slist.Add(SpiderHelper.getString(jObject["type"].ToString()));
            slist.Add(SpiderHelper.getString(jObject["version"].ToString()));
            slist.Add(SpiderHelper.getString(jObject["attributes"].ToString()));
            slist.Add(SpiderHelper.getString(jObject["sys_fee"].ToString()));
            slist.Add(SpiderHelper.getString(jObject["blockindex"].ToString()));
            slist.Add(SpiderHelper.getString(jObject["gas_limit"] == null?"": jObject["gas_limit"].ToString()));
            slist.Add(SpiderHelper.getString(jObject["gas_price"] == null ? "" : jObject["gas_price"].ToString()));
            slist.Add(UInt160.Parse(StringRemoveZoro(SpiderHelper.getString(jObject["account"].ToString())).HexToBytes().Reverse().ToHexString()).ToAddress());

            if (jObject["script"] != null)
            {
                txScriptMethod.Save(conn, SpiderHelper.getString(jObject["script"].ToString()), blockHeight, SpiderHelper.getString(jObject["txid"].ToString()));
            }

            if (ChainSpider.checkHeight == int.Parse(blockHeight.ToString()))
            {
                Dictionary <string, string> where = new Dictionary <string, string>();
                where.Add("txid", SpiderHelper.getString(jObject["txid"].ToString()));
                where.Add("blockindex", blockHeight.ToString());
                MysqlConn.Delete(conn, DataTableName, where);
            }
            {
                MysqlConn.ExecuteDataInsert(conn, DataTableName, slist);
            }

            LogConfig.Log($"SaveTransaction {ChainHash} {blockHeight}", LogConfig.LogLevel.Info, ChainHash.ToString());

            utxo.Save(conn, jObject, blockHeight);

            //if (SpiderHelper.getString(jObject["type"].ToString()) == "InvocationTransaction")
            //{
            //    notify.Save(conn, jObject, blockHeight, blockTime, SpiderHelper.getString(jObject["script"].ToString()));
            //}
        }
Exemplo n.º 4
0
        public void getNativeNEP5Asset(MySqlConnection conn, UInt160 Contract)
        {
            try
            {
                ScriptBuilder sb = new ScriptBuilder();

                sb.EmitSysCall("Zoro.NativeNEP5.Call", "TotalSupply", Contract);
                sb.EmitSysCall("Zoro.NativeNEP5.Call", "Name", Contract);
                sb.EmitSysCall("Zoro.NativeNEP5.Call", "Symbol", Contract);
                sb.EmitSysCall("Zoro.NativeNEP5.Call", "Decimals", Contract);

                IO.Json.JObject jObject;

                Blockchain        blockchain = ZoroChainSystem.Singleton.GetBlockchain(ChainHash);;
                ApplicationEngine engine     = ApplicationEngine.Run(sb.ToArray(), blockchain, testMode: true);
                if (engine.State.HasFlag(VMState.FAULT))
                {
                    return;
                }

                JArray jStack = new JArray(engine.ResultStack.Select(p => p.ToParameter().ToJson()));

                string totalSupply = jStack[0]["type"].AsString() == "ByteArray" ? new BigInteger(SpiderHelper.HexString2Bytes(jStack[0]["value"].AsString())).ToString() : jStack[0]["value"].AsString();
                string name        = jStack[1]["type"].AsString() == "ByteArray" ? Encoding.UTF8.GetString(SpiderHelper.HexString2Bytes(jStack[1]["value"].AsString())) : jStack[1]["value"].AsString();
                string symbol      = jStack[2]["type"].AsString() == "ByteArray" ? Encoding.UTF8.GetString(SpiderHelper.HexString2Bytes(jStack[2]["value"].AsString())) : jStack[2]["value"].AsString();
                string decimals    = jStack[3]["type"].AsString() == "ByteArray" ? BigInteger.Parse(jStack[3]["value"].AsString()).ToString() : jStack[3]["value"].AsString();

                //BCT没有限制,可以随意增发
                if (symbol == "BCT")
                {
                    totalSupply = "0";
                }

                List <string> slist = new List <string>();
                slist.Add(Contract.ToString());
                slist.Add(totalSupply);
                slist.Add(name);
                slist.Add(symbol);
                slist.Add(decimals);


                //这里有个bug,我们的bcp会因为转账而增长
                {
                    MysqlConn.ExecuteDataInsert(conn, DataTableName, slist);
                }

                LogConfig.Log($"SaveNEP5Asset {ChainHash} {Contract}", LogConfig.LogLevel.Info, ChainHash.ToString());
            }
            catch (Exception e)
            {
                LogConfig.Log($"error occured when call invokescript, chainhash:{ChainHash}, nep5contract:{Contract.ToString()}, reason:{e.Message}", LogConfig.LogLevel.Error);
                throw e;
            }
        }
Exemplo n.º 5
0
        public void getNEP5Asset(MySqlConnection conn, UInt160 Contract)
        {
            try
            {
                ScriptBuilder sb = new ScriptBuilder();

                sb.EmitAppCall(Contract, "totalSupply");
                sb.EmitAppCall(Contract, "name");
                sb.EmitAppCall(Contract, "symbol");
                sb.EmitAppCall(Contract, "decimals");

                Blockchain        blockchain = ZoroChainSystem.Singleton.GetBlockchain(ChainHash);;
                ApplicationEngine engine     = ApplicationEngine.Run(sb.ToArray(), blockchain, testMode: true);
                if (engine.State.HasFlag(VMState.FAULT))
                {
                    return;
                }

                JArray jStack = new JArray(engine.ResultStack.Select(p => p.ToParameter().ToJson()));

                string totalSupply = new BigInteger(SpiderHelper.HexString2Bytes(jStack[0]["value"].ToString())).ToString();
                string name        = Encoding.UTF8.GetString(SpiderHelper.HexString2Bytes(jStack[1]["value"].ToString()));
                string symbol      = Encoding.UTF8.GetString(SpiderHelper.HexString2Bytes(jStack[2]["value"].ToString()));
                string decimals    = BigInteger.Parse(jStack[3]["value"].ToString()).ToString();

                List <string> slist = new List <string>();
                slist.Add(Contract.ToString());
                slist.Add(totalSupply);
                slist.Add(name);
                slist.Add(symbol);
                slist.Add(decimals);

                //Dictionary<string, string> dictionary = new Dictionary<string, string>();
                //dictionary.Add("assetid", Contract.ToString());
                //bool exist = MysqlConn.CheckExist(DataTableName, dictionary);
                //if (!exist)
                //这里有个bug,我们的bcp会因为转账而增长
                {
                    MysqlConn.ExecuteDataInsert(conn, DataTableName, slist);
                }

                LogConfig.Log($"SaveNEP5Asset {ChainHash} {Contract}", LogConfig.LogLevel.Info, ChainHash.ToString());
            }
            catch (Exception e)
            {
                LogConfig.Log($"error occured when call invokescript, chainhash:{ChainHash}, nep5contract:{Contract.ToString()}, reason:{e.Message}", LogConfig.LogLevel.Error);
                throw e;
            }
        }
Exemplo n.º 6
0
        public void Save(MySqlConnection conn, JObject jToken)
        {
            JObject executions = null;

            try
            {
                if (jToken != null)
                {
                    executions = jToken["executions"];
                }
            }
            catch (Exception e)
            {
            }
            //try
            //{
            //    if (!TryGetDB(ChainHash, out DB db)) return;
            //    if (db.IsDisposed) return;
            //    UInt256 hash = UInt256.Parse(SpiderHelper.getString(jToken["txid"].ToString()));
            //    if (!db.TryGet(ReadOptions.Default, hash.ToArray(), out Slice value))
            //        throw new RpcException(-100, "Unknown transaction");
            //    result = JObject.Parse(value.ToString());
            //    if (result != null)
            //    executions = result["executions"];
            //}
            //catch (Exception e)
            //{
            //    LogConfig.Log($"error occured when call getapplicationlog, chain:{ChainHash} height:{blockHeight}, reason:{e.Message}", LogConfig.LogLevel.Error);
            //    //throw e;
            //}

            if (jToken != null && executions != null)
            {
                foreach (var execution in executions as JArray)
                {
                    List <string> slist = new List <string>();
                    slist.Add(SpiderHelper.getString(jToken["txid"].ToString()));
                    slist.Add(SpiderHelper.getString(execution["vmstate"].ToString()));
                    slist.Add(SpiderHelper.getString(execution["gas_consumed"].ToString()));
                    slist.Add(SpiderHelper.getString(execution["stack"].ToString()));
                    slist.Add(SpiderHelper.getString(execution["notifications"].ToString().Replace(@"[/n/r]", "")));

                    Dictionary <string, string> where = new Dictionary <string, string>();
                    where.Add("txid", SpiderHelper.getString(jToken["txid"].ToString()));
                    MysqlConn.Delete(conn, DataTableName, where);

                    MysqlConn.ExecuteDataInsert(conn, DataTableName, slist);

                    LogConfig.Log($"SaveNotify {ChainHash} {jToken["txid"]}", LogConfig.LogLevel.Info, ChainHash.ToString());

                    if (execution["vmstate"].ToString().Contains("FAULT"))
                    {
                        return;
                    }

                    JObject notifications = execution["notifications"];

                    foreach (JObject notify in notifications as JArray)
                    {
                        JArray values = notify["state"]["value"] as JArray;

                        if (values[0]["type"].ToString() == "ByteArray")
                        {
                            string transfer = Encoding.UTF8.GetString(SpiderHelper.HexString2Bytes(SpiderHelper.getString(values[0]["value"].ToString())));
                            string contract = SpiderHelper.getString(notify["contract"].ToString());

                            if (transfer == "transfer")
                            {
                                JObject nep5 = new JObject();
                                nep5["assetid"] = contract;
                                nep5Asset.Save(conn, nep5);

                                //存储Nep5Transfer内容
                                JObject tx = new JObject();
                                tx["txid"]  = SpiderHelper.getString(jToken["txid"].ToString());
                                tx["n"]     = 0;
                                tx["asset"] = contract;
                                if (SpiderHelper.getString(values[1]["value"].ToString()) == "")
                                {
                                    tx["from"] = "";
                                }
                                else
                                {
                                    tx["from"] = UInt160.Parse(SpiderHelper.getString(values[1]["value"].ToString())).ToAddress();
                                }

                                tx["to"] = UInt160.Parse(SpiderHelper.getString(values[2]["value"].ToString())).ToAddress();
                                if (SpiderHelper.getString(values[3]["type"].ToString()) == "ByteArray")
                                {
                                    tx["value"] = new BigInteger(SpiderHelper.HexString2Bytes(SpiderHelper.getString(values[3]["value"].ToString()))).ToString();
                                }
                                else
                                {
                                    tx["value"] = BigInteger.Parse(SpiderHelper.getString(values[3]["value"].ToString()), NumberStyles.AllowHexSpecifier).ToString();
                                }
                                JObject j = new JObject();
                                j["address"] = SpiderHelper.getString(tx["to"].ToString());
                                j["txid"]    = SpiderHelper.getString(tx["txid"].ToString());
                                address.Save(conn, j);
                                addressAsset.Save(conn, SpiderHelper.getString(tx["to"].ToString()), contract, "");
                                address_tx.Save(conn, j);
                                nep5Transfer.Save(conn, tx);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        public void Save(MySqlConnection conn, JObject jObject, string path)
        {
            JObject result = new JObject();

            result["version"]    = jObject["version"];
            result["id"]         = jObject["txid"];
            result["type"]       = jObject["asset"]["type"];
            result["name"]       = jObject["asset"]["name"];
            result["amount"]     = jObject["asset"]["amount"];
            result["available"]  = 1;
            result["precision"]  = jObject["asset"]["precision"];
            result["owner"]      = jObject["asset"]["owner"];
            result["admin"]      = jObject["asset"]["admin"];
            result["issuer"]     = 1;
            result["expiration"] = 0;
            result["frozen"]     = 0;

            List <string> slist = new List <string>();

            slist.Add(SpiderHelper.getString(result["version"].ToString()));
            slist.Add(SpiderHelper.getString(result["id"].ToString()));
            slist.Add(SpiderHelper.getString(result["type"].ToString()));
            slist.Add(SpiderHelper.getString(result["name"].ToString()));
            slist.Add(SpiderHelper.getString(result["amount"].ToString()));
            slist.Add(SpiderHelper.getString(result["available"].ToString()));
            slist.Add(SpiderHelper.getString(result["precision"].ToString()));
            slist.Add(SpiderHelper.getString(result["owner"].ToString()));
            slist.Add(SpiderHelper.getString(result["admin"].ToString()));
            slist.Add(SpiderHelper.getString(result["issuer"].ToString()));
            slist.Add(SpiderHelper.getString(result["expiration"].ToString()));
            slist.Add(SpiderHelper.getString(result["frozen"].ToString()));

            {
                MysqlConn.ExecuteDataInsert(conn, DataTableName, slist);
            }

            LogConfig.Log($"SaveAsset {ChainHash} {result["name"]}", LogConfig.LogLevel.Info, ChainHash.ToString());
        }
Exemplo n.º 8
0
        public void Save(MySqlConnection conn, JObject jObject, uint height)
        {
            List <string> slist = new List <string>();

            slist.Add(SpiderHelper.getString(jObject["hash"].ToString()));
            slist.Add(SpiderHelper.getString(jObject["size"].ToString()));
            slist.Add(SpiderHelper.getString(jObject["version"].ToString()));
            slist.Add(SpiderHelper.getString(jObject["time"].ToString()));
            slist.Add(SpiderHelper.getString(jObject["index"].ToString()));
            slist.Add(SpiderHelper.getString(jObject["script"].ToString()));
            slist.Add((jObject["tx"] as JArray).Count.ToString());

            if (ChainSpider.checkHeight == int.Parse(SpiderHelper.getString(jObject["index"].ToString())))
            {
                Dictionary <string, string> where = new Dictionary <string, string>();
                where.Add("indexx", SpiderHelper.getString(jObject["index"].ToString()));
                MysqlConn.Delete(conn, DataTableName, where);
            }
            MysqlConn.ExecuteDataInsert(conn, DataTableName, slist);

            uint blockTime = uint.Parse(SpiderHelper.getString(jObject["time"].ToString()));

            int numTx = 0;

            foreach (var tx in (jObject["tx"] as JArray))
            {
                trans.Save(conn, tx, height, blockTime);
                numTx++;
            }

            LogConfig.Log($"BlockSaved {ChainHash} height:{height} tx:{numTx}", LogConfig.LogLevel.Warning, ChainHash.ToString());
        }
Exemplo n.º 9
0
        public void Save(MySqlConnection conn, JObject jToken)
        {
            List <string> slist = new List <string>();

            slist.Add(SpiderHelper.getString(jToken["blockindex"].ToString()));
            slist.Add(SpiderHelper.getString(jToken["txid"].ToString()));
            slist.Add(SpiderHelper.getString(jToken["n"].ToString()));
            slist.Add(SpiderHelper.getString(jToken["asset"].ToString()));
            slist.Add(SpiderHelper.getString(jToken["from"].ToString()));
            slist.Add(SpiderHelper.getString(jToken["to"].ToString()));
            slist.Add(SpiderHelper.getString(jToken["value"].ToString()));

            if (ChainSpider.checkHeight == int.Parse(jToken["blockindex"].ToString()))
            {
                Dictionary <string, string> where = new Dictionary <string, string>();
                where.Add("txid", SpiderHelper.getString(jToken["txid"].ToString()));
                where.Add("blockindex", SpiderHelper.getString(jToken["blockindex"].ToString()));
                MysqlConn.Delete(conn, DataTableName, where);
            }
            {
                MysqlConn.ExecuteDataInsert(conn, DataTableName, slist);
            }

            LogConfig.Log($"SaveNEP5Transfer {ChainHash} {jToken["blockindex"]} {jToken["txid"]}", LogConfig.LogLevel.Info, ChainHash.ToString());
        }