예제 #1
0
        public JsonResult Get(int height = 0)
        {
            //TODO: Update this to split get Tx's from Split BC cache
            var sizeBlock   = 1000;
            var startHeight = height;
            var endHeight   = startHeight + 100;

            if (startHeight < 1)
            {
                startHeight = 1;
            }
            //todo, we don't have to query the chain height every time this is requested
            var chainHeight = RpcHelper.Request <GetHeightResp>("getheight").Height;

            if (chainHeight == 0)
            {
                chainHeight = endHeight + 100;
            }
            if (endHeight > chainHeight)
            {
                endHeight = chainHeight;
            }

            List <int> dbSegments   = new List <int>();
            var        segmentStart = Convert.ToInt32(Math.Floor((double)(startHeight / sizeBlock) * sizeBlock)) + 1;

            dbSegments.Add(segmentStart);
            if (startHeight + 100 > segmentStart + sizeBlock - 1)
            {
                dbSegments.Add(segmentStart + sizeBlock);
            }
            try
            {
                //TODO:... if we find a problem - ie: there are ANY tx's that don't return at least one Tx poer height, then we need to re-cache the DB file we're working with for this height...
                //we need to ensure that there is at least one Tx per block
                //should this be in a seperate "validation background job that's checking completed files - maybe to run once per day?
                List <LightTx> transactions = new List <LightTx>();
                foreach (var start in dbSegments)
                {
                    var end = start + sizeBlock - 1;

                    using (var db = new LiteDatabase(string.Concat(AppContext.BaseDirectory, @"App_Data\", "transactions_", start, "-", end, ".db")))
                    {
                        var cachedtxs = db.GetCollection <CachedTx>("cached_txs");
                        var txs       = cachedtxs.Find(x => x.height >= startHeight && x.height <= endHeight).Distinct().ToList();
                        transactions.AddRange(TransactionHelpers.MapTxs(txs));
                    }
                }
                return(new JsonResult(JsonConvert.SerializeObject(transactions)));
            }
            catch (Exception ex)
            {
                //todo: log and return client handlable exception
            }

            return(new JsonResult(""));
        }