Example #1
0
        public override BlockDigest?GetBlockDigest(BlockHash blockHash)
        {
            if (_blockCache.TryGetValue(blockHash, out BlockDigest cachedDigest))
            {
                return(cachedDigest);
            }

            UPath path = BlockPath(blockHash);

            if (!_blocks.FileExists(path))
            {
                return(null);
            }

            BlockDigest blockDigest;

            try
            {
                blockDigest = BlockDigest.Deserialize(_blocks.ReadAllBytes(path));
            }
            catch (FileNotFoundException)
            {
                return(null);
            }

            _blockCache.AddOrUpdate(blockHash, blockDigest);
            return(blockDigest);
        }
Example #2
0
        public IValue GetState(
            string stateKey,
            HashDigest <SHA256>?blockHash = null,
            Guid?chainId = null)
        {
            if (chainId is null)
            {
                throw new ArgumentNullException(nameof(chainId));
            }

            blockHash ??= IndexBlockHash(chainId.Value, -1);

            if (blockHash is null)
            {
                return(null);
            }

            BlockDigest block = GetBlockDigest(blockHash.Value).Value;
            Tuple <HashDigest <SHA256>, long> stateReference;

            stateReference = LookupStateReference(chainId.Value, stateKey, block.Header.Index);

            if (stateReference is null)
            {
                return(null);
            }

            HashDigest <SHA256> hashValue = stateReference.Item1;
            IImmutableDictionary <string, IValue> blockStates = GetBlockStates(hashValue);

            if (blockStates is null)
            {
                return(null);
            }

            return(blockStates.TryGetValue(stateKey, out IValue state) ? state : null);
        }
Example #3
0
        internal override BlockDigest?GetBlockDigest(HashDigest <SHA256> blockHash)
        {
            if (_blockCache.TryGetValue(blockHash, out BlockDigest cahcedDigest))
            {
                return(cahcedDigest);
            }

            UPath path = BlockPath(blockHash);

            if (!_blocks.FileExists(path))
            {
                return(null);
            }

            BlockDigest blockDigest;

            try
            {
                IValue value = new Codec().Decode(_blocks.ReadAllBytes(path));
                if (!(value is Bencodex.Types.Dictionary dict))
                {
                    throw new DecodingException(
                              $"Expected {typeof(Bencodex.Types.Dictionary)} but " +
                              $"{value.GetType()}");
                }

                blockDigest = new BlockDigest(dict);
            }
            catch (FileNotFoundException)
            {
                return(null);
            }

            _blockCache.AddOrUpdate(blockHash, blockDigest);
            return(blockDigest);
        }
Example #4
0
        /// <inheritdoc/>
        public override void PutBlock <T>(Block <T> block)
        {
            if (_blockCache.ContainsKey(block.Hash))
            {
                return;
            }

            UPath path = BlockPath(block.Hash);

            if (_blocks.FileExists(path))
            {
                return;
            }

            foreach (Transaction <T> tx in block.Transactions)
            {
                PutTransaction(tx);
            }

            BlockDigest digest = BlockDigest.FromBlock(block);

            WriteContentAddressableFile(_blocks, path, digest.Serialize());
            _blockCache.AddOrUpdate(block.Hash, digest);
        }