Exemplo n.º 1
0
        public static Block?FindParent(this IBlockFinder finder, Block block, BlockTreeLookupOptions options)
        {
            if (block.Header.ParentHash is null)
            {
                throw new InvalidOperationException(
                          $"Cannot find parent when parent hash is null on block with hash {block.Hash}.");
            }

            return(finder.FindBlock(block.Header.ParentHash, options));
        }
Exemplo n.º 2
0
        public BlockHeader FindHeader(Keccak blockHash, BlockTreeLookupOptions options)
        {
            if (blockHash == null || blockHash == Keccak.Zero)
            {
                // TODO: would be great to check why this is still needed (maybe it is something archaic)
                return(null);
            }

            BlockHeader header = _headerDb.Get(blockHash, _headerDecoder, _headerCache, false);

            if (header == null)
            {
                return(null);
            }

            bool totalDifficultyNeeded = (options & BlockTreeLookupOptions.TotalDifficultyNotNeeded) == BlockTreeLookupOptions.None;
            bool requiresCanonical     = (options & BlockTreeLookupOptions.RequireCanonical) == BlockTreeLookupOptions.RequireCanonical;

            if ((totalDifficultyNeeded && header.TotalDifficulty == null) || requiresCanonical)
            {
                (BlockInfo blockInfo, ChainLevelInfo level) = LoadInfo(header.Number, header.Hash);
                if (level == null || blockInfo == null)
                {
                    // TODO: this is here because storing block data is not transactional
                    // TODO: would be great to remove it, he?
                    SetTotalDifficulty(header);
                    blockInfo = new BlockInfo(header.Hash, header.TotalDifficulty.Value);
                    UpdateOrCreateLevel(header.Number, blockInfo);

                    (_, level) = LoadInfo(header.Number, header.Hash);
                }
                else
                {
                    header.TotalDifficulty = blockInfo.TotalDifficulty;
                }

                if (requiresCanonical)
                {
                    bool isMain = level.MainChainBlock?.BlockHash.Equals(blockHash) == true;
                    header = isMain ? header : null;
                }
            }

            if (header != null && ShouldCache(header.Number))
            {
                _headerCache.Set(blockHash, header);
            }

            return(header);
        }
Exemplo n.º 3
0
 public BlockHeader FindHeader(Keccak blockHash, BlockTreeLookupOptions options) => _blockTree.FindHeader(blockHash, options);
Exemplo n.º 4
0
 public Block FindBlock(long blockNumber, BlockTreeLookupOptions options) => _blockTree.FindBlock(blockNumber, options);
Exemplo n.º 5
0
 public Block FindBlock(Keccak blockHash, BlockTreeLookupOptions options) => _blockTree.FindBlock(blockHash, options);
Exemplo n.º 6
0
 public BlockHeader FindHeader(long blockNumber, BlockTreeLookupOptions options)
 {
     return(_blockTree.FindHeader(blockNumber, options));
 }
Exemplo n.º 7
0
        public static BlockHeader?FindParentHeader(this IBlockFinder finder, BlockHeader header, BlockTreeLookupOptions options)
        {
            if (header.MaybeParent is null)
            {
                if (header.ParentHash is null)
                {
                    throw new InvalidOperationException(
                              $"Cannot find parent when parent hash is null on block with hash {header.Hash}.");
                }

                BlockHeader parent = finder.FindHeader(header.ParentHash, options);
                header.MaybeParent = new WeakReference <BlockHeader>(parent);
                return(parent);
            }

            header.MaybeParent.TryGetTarget(out BlockHeader maybeParent);
            if (maybeParent is null)
            {
                if (header.ParentHash is null)
                {
                    throw new InvalidOperationException(
                              $"Cannot find parent when parent hash is null on block with hash {header.Hash}.");
                }

                BlockHeader parent = finder.FindHeader(header.ParentHash, options);
                header.MaybeParent.SetTarget(parent);
                return(parent);
            }

            if (maybeParent.TotalDifficulty is null && (options & BlockTreeLookupOptions.TotalDifficultyNotNeeded) == 0)
            {
                if (header.ParentHash is null)
                {
                    throw new InvalidOperationException(
                              $"Cannot find parent when parent hash is null on block with hash {header.Hash}.");
                }

                BlockHeader?fromDb = finder.FindHeader(header.ParentHash, options);
                maybeParent.TotalDifficulty = fromDb?.TotalDifficulty;
            }

            return(maybeParent);
        }
Exemplo n.º 8
0
 public static Block FindParent(this IBlockFinder finder, BlockHeader blockHeader, BlockTreeLookupOptions options)
 {
     return(finder.FindBlock(blockHeader.ParentHash, options));
 }
Exemplo n.º 9
0
 public BlockHeader FindHeader(long blockNumber, BlockTreeLookupOptions options) => _headBlock.Number == blockNumber ? _headBlock.Header : null;
Exemplo n.º 10
0
 public BlockHeader FindHeader(Keccak blockHash, BlockTreeLookupOptions options) => _headBlock.Hash == blockHash ? _headBlock.Header : null;
Exemplo n.º 11
0
 public Block FindBlock(long blockNumber, BlockTreeLookupOptions options) => _headBlock.Number == blockNumber ? _headBlock : null;
Exemplo n.º 12
0
 public Block FindBlock(Keccak blockHash, BlockTreeLookupOptions options) => _headBlock.Hash == blockHash ? _headBlock : null;
Exemplo n.º 13
0
 public static Block FindParent(this IBlockTree tree, Block block, BlockTreeLookupOptions options)
 {
     return(tree.FindBlock(block.Header.ParentHash, options));
 }
Exemplo n.º 14
0
 public static BlockHeader FindParentHeader(this IBlockTree tree, BlockHeader header, BlockTreeLookupOptions options)
 {
     return(tree.FindHeader(header.ParentHash, options));
 }
Exemplo n.º 15
0
 public BlockHeader FindHeader(long blockNumber, BlockTreeLookupOptions options) => _blockTree.FindHeader(blockNumber, options);
Exemplo n.º 16
0
 public Block FindBlock(Keccak blockHash, BlockTreeLookupOptions option)
 {
     return(_blockTree.FindBlock(blockHash, option));
 }
Exemplo n.º 17
0
        public static BlockHeader FindParentHeader(this IBlockFinder finder, BlockHeader header, BlockTreeLookupOptions options)
        {
            if (header.MaybeParent == null)
            {
                BlockHeader parent = finder.FindHeader(header.ParentHash, options);
                header.MaybeParent = new WeakReference <BlockHeader>(parent);
                return(parent);
            }

            header.MaybeParent.TryGetTarget(out BlockHeader maybeParent);
            if (maybeParent == null)
            {
                BlockHeader parent = finder.FindHeader(header.ParentHash, options);
                header.MaybeParent.SetTarget(parent);
                return(parent);
            }

            if (maybeParent.TotalDifficulty == null && (options & BlockTreeLookupOptions.TotalDifficultyNotNeeded) == 0)
            {
                BlockHeader fromDb = finder.FindHeader(header.ParentHash, options);
                maybeParent.TotalDifficulty = fromDb.TotalDifficulty;
            }

            return(maybeParent);
        }
Exemplo n.º 18
0
 public BlockHeader FindHeader(Keccak blockHash, BlockTreeLookupOptions options)
 {
     return(_blockTree.FindHeader(blockHash, options));
 }
Exemplo n.º 19
0
        public BlockHeader FindHeader(long number, BlockTreeLookupOptions options)
        {
            Keccak blockHash = GetBlockHashOnMainOrBestDifficultyHash(number);

            return(blockHash == null ? null : FindHeader(blockHash, options));
        }
Exemplo n.º 20
0
 public Block FindBlock(long blockNumber, BlockTreeLookupOptions options)
 {
     return(_blockTree.FindBlock(blockNumber, options));
 }
Exemplo n.º 21
0
        public Block FindBlock(long blockNumber, BlockTreeLookupOptions options)
        {
            Keccak hash = GetBlockHashOnMainOrBestDifficultyHash(blockNumber);

            return(FindBlock(hash, options));
        }
Exemplo n.º 22
0
 public Block FindBlock(Keccak blockHash, BlockTreeLookupOptions options)
 {
     return(_wrapped.FindBlock(blockHash, options));
 }