示例#1
0
        public static JSONBack ConvertToJSONBack(string jsonStr)
        {
            JSONBack JSONBack = new JSONBack();

            try
            {
                JSONBack = JsonConvert.DeserializeObject <JSONBack>(jsonStr);
            }
            catch (Exception e)
            {
                return(null);
            }
            return(JSONBack);
        }
示例#2
0
        //load a transaction from json line
        private static void loadTx(string readLine)
        {
            //string to object
            JSONBack jsonBack = JSONBack.ConvertToJSONBack(readLine);

            //paraper parameters
            List <In>     ins      = new List <In>();
            List <string> addrsIn  = new List <string>();
            List <string> addrsOut = new List <string>();

            foreach (Input _in in jsonBack.ins)
            {
                In __in = new In(_in.tx_index, _in.addr);
                ins.Add(__in);
                addrsIn.Add(_in.addr);
            }
            foreach (string _out in jsonBack.outs)
            {
                addrsOut.Add(_out);
            }

            ////Dequeue no use items
            //while (nearbyBegin != nearbyEnd && (jsonBack.time - txsNearby[nearbyBegin].time >= FREQ || jsonBack.time - txsNearby[nearbyBegin].time < 0))
            //    nearbyBegin = (nearbyBegin + 1) % nearbyLen;

            ////judge isFreq, only out
            //List<Int64> cellIDsFreq = new List<long>();
            //for (int nearbyIndex = nearbyBegin; nearbyIndex != nearbyEnd; nearbyIndex = (nearbyIndex + 1) % nearbyLen)
            //{
            //    foreach (string addr in txsNearby[nearbyIndex].addrsOut)
            //    {
            //        if (addrsOut.Contains(addr) && !cellIDsFreq.Contains(txsNearby[nearbyIndex].CellID))
            //        {
            //            cellIDsFreq.Add(txsNearby[nearbyIndex].CellID);
            //            //Console.WriteLine(addr);
            //            break;
            //        }
            //    }
            //}

            ////Enqueue next item
            //txsNearby[nearbyEnd].CellID = jsonBack.CellID;
            //txsNearby[nearbyEnd].addrsIn = addrsIn;
            //txsNearby[nearbyEnd].addrsOut = addrsOut;
            //txsNearby[nearbyEnd].time = jsonBack.time;
            //nearbyEnd = (nearbyEnd + 1) % nearbyLen;

            //Console.WriteLine(nearbyEnd-nearbyBegin);

            //save tx
            Tx tx = new Tx(jsonBack.CellID, jsonBack.time, jsonBack.hash, ins, jsonBack.outs, jsonBack.amount, (int)(jsonBack.amount / MAXAMOUNT), 0, false);

            Global.LocalStorage.SaveTx(tx);

            ////save TxCellID
            //TxCellID txCellID = new TxCellID(jsonBack.CellID);
            //Global.LocalStorage.SaveTxCellID(txCellID);

            //save addrCellIDs
            //foreach (string addr in addrsIn)
            //{
            //    Int64 addrHash = GetCellIDFromAddr(addr);
            //    using (var addrInfo = Global.LocalStorage.UseAddrInfo(addrHash, CellAccessOptions.CreateNewOnCellNotFound))
            //    {
            //        addrInfo.txs.Add(jsonBack.CellID);
            //    }
            //}
            foreach (string addr in jsonBack.outs)
            {
                Int64 addrHash = GetCellIDFromAddr(addr);
                using (var addrInfo = Global.LocalStorage.UseAddrInfo(addrHash, CellAccessOptions.CreateNewOnCellNotFound))
                {
                    addrInfo.txs.Add(jsonBack.CellID);
                }
            }

            //judege isCycle
            //List<Int64> isReading = new List<Int64>();
            //DFSFind(CYCLECOUNT, 0, jsonBack.CellID, jsonBack.ins, jsonBack.outs, circleTxs, isReading, MAXAMOUNT);
        }
示例#3
0
        //load block and update isAmount and isFreq
        public static void LoadBlocks(long amountMax, long timespan, int step)
        {
            Console.WriteLine("LoadBlocks begin...");

            //load cellIDs
            List <Int64> cellIDs = new List <Int64>();

            using (StreamReader cellIDsReader = new StreamReader(@"D:\Bit\cellIDs.txt"))
            {
                string cellIDLine;
                while (null != (cellIDLine = cellIDsReader.ReadLine()))
                {
                    cellIDs.Add(Int64.Parse(cellIDLine));
                }
            }

            //read json line by line
            using (StreamReader reader = new StreamReader(@"D:\Bit\block.txt"))
            {
                //statistics some characters
                int count = 0;

                Queue <freqPara> txsQueue    = new Queue <freqPara>();
                List <Int64>     cellIDsFreq = new List <long>();
                Int64            preCellID   = -1;
                Int64            preTime     = -1;

                //for circle
                List <Int64> circleTxs = new List <Int64>();

                string line;
                while (null != (line = reader.ReadLine()))
                {
                    //convert string to object
                    JSONBack jsonBack = ConvertToJSONBack(line);
                    if (jsonBack.amount > 0)
                    {
                        //convert List<Input> to List<In> for compatible
                        List <In>    ins   = new List <In>();
                        List <Int64> inIDs = new List <long>();
                        foreach (Input _in in jsonBack.ins)
                        {
                            In __in = new In(_in.tx_index, _in.addr);
                            ins.Add(__in);
                            inIDs.Add(_in.tx_index);
                        }

                        //high frequency from second
                        if (jsonBack.time - preTime > timespan)
                        {
                            if (txsQueue.Count > 0)
                            {
                                txsQueue.Clear();
                            }
                        }
                        else
                        {
                            while (txsQueue.Count > 0 && jsonBack.time - txsQueue.Peek().time > timespan)
                            {
                                txsQueue.Dequeue();
                            }

                            List <string> addrs = new List <string>();
                            foreach (Input _in in jsonBack.ins)
                            {
                                addrs.Add(_in.addr);
                            }
                            foreach (string _out in jsonBack.outs)
                            {
                                addrs.Add(_out);
                            }

                            foreach (freqPara pre in txsQueue)
                            {
                                foreach (string addr in pre.addrs)
                                {
                                    if (addrs.Contains(addr))
                                    {
                                        if (!cellIDsFreq.Contains(pre.CellID))
                                        {
                                            cellIDsFreq.Add(pre.CellID);
                                        }
                                        //count++;
                                    }
                                }
                            }
                            freqPara para;
                            para.CellID = jsonBack.CellID;
                            para.addrs  = addrs;
                            para.time   = jsonBack.time;
                            txsQueue.Enqueue(para);
                        }
                        preCellID = jsonBack.CellID;
                        preTime   = jsonBack.time;

                        try
                        {
                            Tx tx = new Tx(jsonBack.CellID, jsonBack.time, jsonBack.hash, ins, inIDs, jsonBack.outs, jsonBack.amount, (int)(jsonBack.amount / amountMax), cellIDsFreq.Count);
                            Global.LocalStorage.SaveTx(tx);
                            //if (jsonBack.amount > amountMax)
                            //{
                            //    Int64 cellID = jsonBack.CellID;
                            //    Thread t = new Thread(() => { DFSFind(step, 0, cellID, jsonBack.outs, cellID, circleTxs, cellIDs); });
                            //    t.IsBackground = true;
                            //    t.Start();
                            //    //DFSFind(step, 0, cellID, jsonBack.outs, cellID, circleTxs, cellIDs);
                            //}
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                            throw;
                        }

                        if (cellIDsFreq.Count > 0)
                        {
                            foreach (Int64 cellID in cellIDsFreq)
                            {
                                using (var tx = Global.LocalStorage.UseTx(cellID))
                                {
                                    if (tx.isFreq < cellIDsFreq.Count)
                                    {
                                        tx.isFreq = cellIDsFreq.Count;
                                    }
                                }
                            }
                            cellIDsFreq.Clear();
                        }
                    }
                }
                foreach (Int64 cellID in circleTxs)
                {
                    count++;
                    using (var tx = Global.LocalStorage.UseTx(cellID))
                    {
                        tx.isCycle = true;
                    }
                }
                Console.WriteLine(count);
            }
        }