Пример #1
0
        private void ValidateNextBlockInvalidStateRootHash()
        {
            IKeyValueStore stateKeyValueStore     = new MemoryKeyValueStore(),
                           stateHashKeyValueStore = new MemoryKeyValueStore();
            var policy = new BlockPolicy <DumbAction>(
                null,
                TimeSpan.FromHours(3),
                1024,
                128);
            var stateStore = new TrieStateStore(stateKeyValueStore, stateHashKeyValueStore);
            // FIXME: It assumes that _fx.GenesisBlock doesn't update any states with transactions.
            //        Actually, it depends on BlockChain<T> to update states and it makes hard to
            //        calculate state root hash. To resolve this problem,
            //        it should be moved into StateStore.
            var genesisBlock = TestUtils.MineGenesis <DumbAction>(
                blockAction: policy.BlockAction, checkStateRootHash: true);
            var store = new DefaultStore(null);
            var chain = new BlockChain <DumbAction>(
                policy,
                store,
                stateStore,
                genesisBlock);

            var validNext = Block <DumbAction> .Mine(
                1,
                1024,
                genesisBlock.TotalDifficulty,
                genesisBlock.Miner.Value,
                genesisBlock.Hash,
                genesisBlock.Timestamp.AddSeconds(1),
                _emptyTransaction);

            chain.ExecuteActions(validNext);
            validNext =
                new Block <DumbAction>(validNext, stateStore.GetRootHash(validNext.Hash));
            chain.Append(validNext);

            var invalidStateRootHash = Block <DumbAction> .Mine(
                2,
                1032,
                validNext.TotalDifficulty,
                genesisBlock.Miner.Value,
                validNext.Hash,
                validNext.Timestamp.AddSeconds(1),
                _emptyTransaction);

            var actionEvaluations = _blockChain.BlockEvaluator.EvaluateActions(
                invalidStateRootHash,
                StateCompleterSet <DumbAction> .Recalculate);

            chain.SetStates(invalidStateRootHash, actionEvaluations, false);
            invalidStateRootHash = new Block <DumbAction>(
                invalidStateRootHash,
                new HashDigest <SHA256>(TestUtils.GetRandomBytes(HashDigest <SHA256> .Size)));
            Assert.Throws <InvalidBlockStateRootHashException>(() =>
                                                               chain.Append(invalidStateRootHash));
        }
Пример #2
0
        internal WebServiceRequestInfo(HttpContext context)
        {
            mContext = context;

            Headers = new MemoryKeyValueStore <string>();

            var requestHeaders = context?.Request?.Headers;

            if (requestHeaders != null)
            {
                foreach (var key in requestHeaders.Keys)
                {
                    Headers.Write(new StringKey(key), requestHeaders[key].Concat("; "));
                }
            }
        }
Пример #3
0
        private async Task <WebServiceClientResponse> FromResponseAsync(WebResponse response, IResult invokeResult, CancellationToken cancellationToken)
        {
            byte[] data;

            var responseStream = response.GetResponseStream();

            if (responseStream == null)
            {
                data = new byte[0];
            }
            else
            {
                using (responseStream)
                    using (var memoryStream = new MemoryStream())
                    {
                        await responseStream.CopyToAsync(memoryStream, 81920, cancellationToken);

                        if (cancellationToken.IsCancellationRequested)
                        {
                            return(new WebServiceClientResponse(new OperationCanceledException(), 900));
                        }

                        memoryStream.Seek(0, SeekOrigin.Begin);

                        data = memoryStream.ToArray();
                    }
            }

            var headers = new MemoryKeyValueStore <string>();

            foreach (var headerKey in response.Headers?.AllKeys ?? new string[0])
            {
                headers[headerKey] = response.Headers?[headerKey];
            }

            var http   = response as HttpWebResponse;
            var result = new WebServiceClientResponse(mConfiguration, mCommunicationStrategy, data, headers, invokeResult, (int)(http?.StatusCode ?? HttpStatusCode.OK));

            return(result);
        }
Пример #4
0
        public void CopyStates(bool secure)
        {
            var values = ImmutableDictionary <string, IValue> .Empty
                         .Add("foo", (Binary)GetRandomBytes(4096))
                         .Add("bar", (Text)ByteUtil.Hex(GetRandomBytes(2048)))
                         .Add("baz", (Bencodex.Types.Boolean)false)
                         .Add("qux", Bencodex.Types.Dictionary.Empty)
                         .Add(
                "zzz",
                Bencodex.Types.Dictionary.Empty
                .Add("binary", GetRandomBytes(4096))
                .Add("text", ByteUtil.Hex(GetRandomBytes(2048))));

            var stateStore = new TrieStateStore(_stateKeyValueStore, secure);

            IKeyValueStore targetStateKeyValueStore = new MemoryKeyValueStore();
            var            targetStateStore         = new TrieStateStore(targetStateKeyValueStore, secure);
            ITrie          trie            = stateStore.Commit(null, values);
            int            prevStatesCount = _stateKeyValueStore.ListKeys().Count();

            _stateKeyValueStore.Set(
                new KeyBytes("alpha", Encoding.UTF8),
                ByteUtil.ParseHex("00"));
            _stateKeyValueStore.Set(
                new KeyBytes("beta", Encoding.UTF8),
                ByteUtil.ParseHex("00"));

            Assert.Equal(prevStatesCount + 2, _stateKeyValueStore.ListKeys().Count());
            Assert.Empty(targetStateKeyValueStore.ListKeys());

            stateStore.CopyStates(
                ImmutableHashSet <HashDigest <SHA256> > .Empty.Add(trie.Hash),
                targetStateStore);

            // It will stay at the same count of nodes.
            // FIXME: Bencodex fingerprints also should be tracked.
            //        https://github.com/planetarium/libplanet/issues/1653
            Assert.Equal(prevStatesCount, targetStateKeyValueStore.ListKeys().Count());
        }
Пример #5
0
        private void ValidateNextBlockInvalidStateRootHash()
        {
            IKeyValueStore stateKeyValueStore = new MemoryKeyValueStore();
            var            policy             = new BlockPolicy <DumbAction>(
                blockInterval: TimeSpan.FromMilliseconds(3 * 60 * 60 * 1000)
                );
            var    stateStore   = new TrieStateStore(stateKeyValueStore);
            IStore store        = new MemoryStore();
            var    genesisBlock = TestUtils.MineGenesis <DumbAction>(
                policy.GetHashAlgorithm,
                TestUtils.GenesisMiner.PublicKey
                ).Evaluate(TestUtils.GenesisMiner, policy.BlockAction, stateStore);

            store.PutBlock(genesisBlock);
            Assert.NotNull(store.GetStateRootHash(genesisBlock.Hash));

            var chain1 = new BlockChain <DumbAction>(
                policy,
                new VolatileStagePolicy <DumbAction>(),
                store,
                stateStore,
                genesisBlock
                );

            Block <DumbAction> block1 = new BlockContent <DumbAction>
            {
                Index           = 1,
                Difficulty      = 1024L,
                TotalDifficulty = genesisBlock.TotalDifficulty + 1024,
                PublicKey       = TestUtils.GenesisMiner.PublicKey,
                PreviousHash    = genesisBlock.Hash,
                Timestamp       = genesisBlock.Timestamp.AddSeconds(1),
                Transactions    = _emptyTransaction,
            }.Mine(policy.GetHashAlgorithm(1)).Evaluate(TestUtils.GenesisMiner, chain1);

            var policyWithBlockAction = new BlockPolicy <DumbAction>(
                new SetStatesAtBlock(default, (Text)"foo", 1),