Esempio n. 1
0
        public override IEnumerable <PendingSwap> Update()
        {
            lock (String.Intern("PendingSetCurrentHeight_" + "neo"))
            {
                var result = new List <PendingSwap>();
                try
                {
                    var _interopBlockHeight = BigInteger.Parse(OracleReader.GetCurrentHeight("neo", "neo"));

                    // initial start, we have to verify all processed swaps
                    if (initialStart)
                    {
                        Logger.Debug($"Read all neo blocks now.");
                        // TODO check if quick sync nodes are configured, if so use quick sync
                        // we need to find a better solution for that though
                        var allInteropBlocks = OracleReader.ReadAllBlocks("neo", "neo");

                        Logger.Debug($"Found {allInteropBlocks.Count} blocks");

                        foreach (var block in allInteropBlocks)
                        {
                            try
                            {
                                ProcessBlock(block, result);
                            }
                            catch (Exception e)
                            {
                                Logger.Debug($"Block {block.Hash} was not processed correctly: " + e);
                            }
                        }

                        initialStart = false;

                        Logger.Debug($"QuickSync: " + quickSync);
                        // quick sync is only done once after startup
                        if (quickSync)
                        {
                            // if quick sync is active, we can use a specific plugin installed on the nodes (EventTracker)
                            try
                            {
                                var blockIds = neoAPI.GetSwapBlocks("ed07cffad18f1308db51920d99a2af60ac66a7b3", LocalAddress, _interopBlockHeight.ToString());
                                Logger.Debug($"Found {blockIds.Count} blocks to process ");
                                List <InteropBlock> blockList = new List <InteropBlock>();
                                foreach (var entry in blockIds)
                                {
                                    //logger.Debug($"read block {entry.Value}");
                                    var url = DomainExtensions.GetOracleBlockURL("neo", "neo", PBigInteger.Parse(entry.Value.ToString()));
                                    blockList.Add(OracleReader.Read <InteropBlock>(DateTime.Now, url));
                                }

                                // get blocks and order them for processing
                                var blocksToProcess = blockList.Where(x => blockIds.ContainsKey(x.Hash.ToString()))
                                                      .Select(x => new { block = x, id = blockIds[x.Hash.ToString()] })
                                                      .OrderBy(x => x.id);

                                Logger.Debug($"blocks to process: {blocksToProcess.Count()}");

                                foreach (var entry in blocksToProcess.OrderBy(x => x.id))
                                {
                                    Logger.Debug($"process block {entry.id}");
                                    ProcessBlock(entry.block, result);
                                    OracleReader.SetCurrentHeight("neo", "neo", _interopBlockHeight.ToString());
                                    _interopBlockHeight = BigInteger.Parse(entry.id.ToString());
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Inital start failed: " + e.ToString());
                            }
                        }

                        // return after the initial start to be able to process all swaps that happend in the mean time.
                        return(result);
                    }

                    var blockIterator   = new BlockIterator(neoAPI);
                    var blockDifference = blockIterator.currentBlock - _interopBlockHeight;
                    var batchCount      = (blockDifference > 8) ? 8 : blockDifference; //TODO make it a constant, should be no more than 8

                    while (blockIterator.currentBlock > _interopBlockHeight)
                    {
                        if (_resyncBlockIds.Any())
                        {
                            for (var i = 0; i < _resyncBlockIds.Count; i++)
                            {
                                var blockId = _resyncBlockIds.ElementAt(i);
                                if (blockId > _interopBlockHeight)
                                {
                                    Logger.Debug($"NeoInterop: Update() resync block {blockId} higher than current interop height, can't resync.");
                                    _resyncBlockIds.RemoveAt(i);
                                    continue;
                                }

                                try
                                {
                                    Logger.Debug($"NeoInterop: Update() resync block {blockId} now.");
                                    var interopBlock = GetInteropBlock(blockId);
                                    ProcessBlock(interopBlock, result);
                                }
                                catch (Exception e)
                                {
                                    Logger.Error($"NeoInterop: Update() resync block {blockId} failed: " + e);
                                }
                                _resyncBlockIds.RemoveAt(i);
                            }
                        }

                        Logger.Debug($"Swaps: Current Neo chain height: {blockIterator.currentBlock}, interop: {_interopBlockHeight}, delta: {blockIterator.currentBlock - _interopBlockHeight}");
                        blockDifference = blockIterator.currentBlock - _interopBlockHeight;
                        batchCount      = (blockDifference > 8) ? 8 : blockDifference;

                        if (batchCount > 1)
                        {
                            List <Task <InteropBlock> > taskList = CreateTaskList(batchCount);

                            foreach (var task in taskList)
                            {
                                task.Start();
                            }

                            Task.WaitAll(taskList.ToArray());

                            foreach (var task in taskList)
                            {
                                var block = task.Result;

                                ProcessBlock(block, result);
                            }

                            OracleReader.SetCurrentHeight("neo", "neo", _interopBlockHeight.ToString());
                            _interopBlockHeight += batchCount;
                        }
                        else
                        {
                            var interopBlock = GetInteropBlock(_interopBlockHeight);

                            ProcessBlock(interopBlock, result);

                            OracleReader.SetCurrentHeight("neo", "neo", _interopBlockHeight.ToString());
                            _interopBlockHeight++;
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.Error("Neo block sync failed: " + e);
                }
                return(result);
            }
        }