private void IncludeNextBlock()
        {
            BlockSelector selector = new BlockSelector
            {
                IsInBestHeaderChain = true,
                Heights = new int[] {node.Blockchain.State.BestChain.Height + 1},
                Order = BlockSelector.SortOrder.Height,
                Direction = BlockSelector.SortDirection.Asc
            };

            StoredBlock nextBlock = node.Blockchain.FindFirst(selector);
            if (nextBlock != null && nextBlock.HasContent)
            {
                node.Blockchain.Include(nextBlock.Hash);
                // this block can be not the last one available for inclusion
                resumeEvent.Set();
            }
        }
 public StoredBlock FindFirst(BlockSelector selector)
 {
     return storage.FindFirst(selector);
 }
 public List<StoredBlock> Find(BlockSelector selector, int count)
 {
     return storage.Find(selector, count);
 }
Esempio n. 4
0
 /// <summary>
 /// Finds first block that matches a given selector.
 /// </summary>
 /// <param name="selector">The selector.</param>
 /// <returns>The first block that matches selector; or null if there is no such block.</returns>
 /// <exception cref="ArgumentException">If the sort order is not defined.</exception>
 public StoredBlock FindFirst(BlockSelector selector)
 {
     //todo: use transaction?
     lock (blockchainLock)
     {
         return blockchain.FindFirst(selector);
     }
 }
        private void RevertBlockchain()
        {
            BlockSelector selector = new BlockSelector();
            selector.IsInBestHeaderChain = true;
            selector.IsInBestBlockChain = true;
            selector.Order = BlockSelector.SortOrder.Height;
            selector.Direction = BlockSelector.SortDirection.Desc;
            StoredBlock lastBlockToKeep = node.Blockchain.FindFirst(selector);

            //todo: revert block, restore mempool
            bool reverted;
            try
            {
                reverted = node.Blockchain.TruncateTo(lastBlockToKeep.Hash);
            }
            catch (InvalidOperationException e)
            {
                // this exception should not happen, since only one thread updates blockchain
                logger.Error(e, $"Failed to revert blockchain to block '{BitConverter.ToString(lastBlockToKeep.Hash)}'.");
                return;
            }
            if (!reverted)
            {
                node.Blockchain.Truncate();
            }
        }