Beispiel #1
0
        /// <summary>
        /// Main method for getting new blocks from node MainNet
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void ReadMainNet(object sender, ElapsedEventArgs e)
        {
            try
            {
                //do only if all blocks are processed
                if (QueueMainNet.Count == 0)
                {
                    //Get max block from node
                    JObject joe = nodeLayer.Invoke("getblockcount", Settings.Default.NodesMainNet.Nodes, "");

                    long toblock = long.Parse(joe["result"].ToString());
                    //Get max block from database
                    long maxblock = businessLayer.GetMaxBlockDB(Settings.Default.DBMainNet.Connection);

                    //Add all missing blocks to Queue
                    for (long i = maxblock + 1; i < toblock; i++)
                    {
                        try
                        {
                            QueueMainNet.TryAdd(i);
                        }
                        catch (InvalidOperationException)
                        {
                            Console.WriteLine("MainNet InvalidOperationException");
                            break;
                        }
                    }
                    Console.WriteLine("MainNet Looking for new blocks ...");
                }
                if (Settings.Default.DBMainNet.NepSyncActive)
                {
                    //check nep queue
                    if (QueueNEPMainNet.Count == 0)
                    {
                        //Get max block from node
                        JObject joe      = nodeLayer.Invoke("getblockcount", Settings.Default.NodesMainNet.Nodes, "");
                        long    toblock  = businessLayer.GetMaxBlockDB(Settings.Default.DBMainNet.Connection);
                        long    maxblock = businessLayer.GetMaxBlockNEPDB(Settings.Default.DBMainNet.Connection);
                        //Add all missing blocks to Queue
                        for (long i = maxblock + 1; i < toblock; i++)
                        {
                            try
                            {
                                QueueNEPMainNet.TryAdd(i);
                            }
                            catch (InvalidOperationException)
                            {
                                Console.WriteLine("MainNet NEP InvalidOperationException");
                                break;
                            }
                        }
                        Console.WriteLine("MainNet NEP Looking for new blocks ...");
                    }
                }
            }
            finally
            {
                //Start timer again after adding all new blocks.
                ReadMainNetTimer.Start();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Syn nep5 tokens/transfers for one block
        /// </summary>
        /// <param name="blocknep5"></param>
        /// <param name="AddressVersion"></param>
        public bool SyncNEP5(JObject data, byte AddressVersion, List <string> nodes, long BlockId, String Database, bool debug)
        {
            try
            {
                var blocknep5 = data["result"];
                var success   = false;
                var success2  = false;
                for (int j = 0; j < blocknep5.Count(); j++)
                {
                    if (blocknep5[j]["state"]["value"][0]["value"].ToString() == "7472616e73666572") //transfer
                    {
                        //get contract from db. if null, save asset and contract to db
                        //always save contract and asset both.
                        //asset needs to be saved in first instance, so the transfer can get the asset id
                        var contract     = ConvertHash(blocknep5[j]["contract"].ToString(), 40);
                        var contractorig = blocknep5[j]["contract"].ToString();
                        int precision    = 0;

                        if (!databaseLayer.ExistsContractDB(contract, Database, debug))
                        {
                            JObject contractstate = nodeLayer.Invoke("getcontractstate", nodes, contractorig, "1");
                            contractstate = (JObject)contractstate["result"];

                            JObject assetSymbol = nodeLayer.Invoke("invokefunction", nodes, contractorig, "symbol");
                            assetSymbol = (JObject)assetSymbol["result"];

                            JObject assetDecimals = nodeLayer.Invoke("invokefunction", nodes, contractorig, "decimals");
                            assetDecimals = (JObject)assetDecimals["result"];
                            if (assetDecimals["stack"][0]["value"].ToString() == "")
                            {
                                //Try "Decimals" ???
                                assetDecimals = nodeLayer.Invoke("invokefunction", nodes, contractorig, "Decimals");
                                assetDecimals = (JObject)assetDecimals["result"];
                            }
                            if (assetDecimals["stack"][0]["value"].ToString() != "")
                            {
                                if (assetDecimals["stack"][0]["type"].ToString() == "ByteArray")
                                {
                                    //check for unknown operation
                                    if (assetDecimals["stack"][0]["value"].ToString() == "756e6b6e6f776e206f7065726174696f6e")
                                    {
                                        precision = 0;
                                    }
                                    else
                                    {
                                        var hexval = ReverseHex(assetDecimals["stack"][0]["value"].ToString());
                                        precision = (int)Convert.ToInt64(hexval, 16);
                                    }
                                }
                                else
                                {
                                    precision = (int)long.Parse(assetDecimals["stack"][0]["value"].ToString());
                                }
                            }
                            else
                            {
                                precision = 0;
                            }

                            JObject assetName = nodeLayer.Invoke("invokefunction", nodes, contractorig, "name");
                            assetName = (JObject)assetName["result"];

                            JObject assetSupply = nodeLayer.Invoke("invokefunction", nodes, contractorig, "totalSupply");
                            assetSupply = (JObject)assetSupply["result"];

                            contractstate["hash"]          = ConvertHash(contractstate["hash"].ToString(), 40);
                            contractstate["assetname"]     = HexToString(assetName["stack"][0]["value"].ToString());
                            contractstate["assetdecimals"] = precision;
                            contractstate["assetSymbol"]   = HexToString(assetSymbol["stack"][0]["value"].ToString());
                            contractstate["assetSupply"]   = (Decimal)Convert.ToInt64(ReverseHex(assetSupply["stack"][0]["value"].ToString()), 16) / (Decimal)Math.Pow(10, Convert.ToDouble(precision));

                            contractstate["storage"] = null;
                            if (contractstate["properties"]["storage"].Type != JTokenType.Null)
                            {
                                contractstate["storage"] = contractstate["properties"]["storage"];
                            }
                            contractstate["dynamic_invoke"] = null;
                            if (contractstate["properties"]["dynamic_invoke"].Type != JTokenType.Null)
                            {
                                contractstate["dynamic_invoke"] = contractstate["properties"]["dynamic_invoke"];
                            }
                            contractstate["properties"] = null;

                            if (databaseLayer.StoreContractAssetDB(contractstate, Database, debug))
                            {
                                success2 = true;
                            }
                            else
                            {
                                success2 = false;
                            }
                        }
                        else
                        {
                            precision = databaseLayer.GetNEPPrecision(contract, Database);
                            success2  = true;
                        }

                        //reverse value
                        if (blocknep5[j]["state"]["value"][3]["type"].ToString() == "ByteArray")
                        {
                            var valuehex = ReverseHex(blocknep5[j]["state"]["value"][3]["value"].ToString());
                            var value    = Convert.ToInt64(valuehex, 16) / (Decimal)Math.Pow(10, Convert.ToDouble(precision));
                            blocknep5[j]["value"] = value;
                        }
                        else
                        {
                            var value = (Decimal)long.Parse(blocknep5[j]["state"]["value"][3]["value"].ToString()) / (Decimal)Math.Pow(10, Convert.ToDouble(precision));
                            blocknep5[j]["value"] = value;
                        }
                        var from = "";
                        if (blocknep5[j]["state"]["value"][1]["value"].ToString() != "")
                        {
                            from = ToAddress(UInt160.Parse(ReverseHex(blocknep5[j]["state"]["value"][1]["value"].ToString())), AddressVersion);
                        }
                        var to = ToAddress(UInt160.Parse(ReverseHex(blocknep5[j]["state"]["value"][2]["value"].ToString())), AddressVersion);

                        blocknep5[j]["txid"]        = ConvertHash(blocknep5[j]["txid"].ToString(), 64);
                        blocknep5[j]["notifytype"]  = "transfer";
                        blocknep5[j]["contract"]    = contract;
                        blocknep5[j]["blockid"]     = BlockId;
                        blocknep5[j]["addressfrom"] = from;
                        blocknep5[j]["addressto"]   = to;
                        blocknep5[j]["state"]       = null;
                    }
                    else
                    {
                        blocknep5[j].Remove();
                        j = j - 1;
                    }
                }
                if (success2)
                {
                    //Store nep5 transfer in database
                    if (databaseLayer.StoreNEPTransferDB(blocknep5, Database, debug))
                    {
                        success = true;
                    }
                    else
                    {
                        success = false;
                    }
                }
                if (success && success2)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception e)
            {
                using (TextWriter errorWriter = Console.Error)
                {
                    errorWriter.WriteLine(e.Message);
                }
                return(false);
            }
        }