Exemple #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));
        }
Exemple #2
0
        public void SetStates()
        {
            // Check to set and to get.
            Assert.Throws <KeyNotFoundException>(() => _stateStore.GetRootHash(_fx.Block1.Hash));
            Assert.False(_stateStore.ContainsBlockStates(_fx.Block1.Hash));
            var states = ImmutableDictionary <string, IValue> .Empty
                         .Add("foo", (Text)"value");

            _stateStore.SetStates(_fx.Block1, states);
            Assert.Equal((Text)"value", _stateStore.GetState("foo", _fx.Block1.Hash));
            Assert.IsType <HashDigest <SHA256> >(_stateStore.GetRootHash(_fx.Block1.Hash));
            Assert.True(_stateStore.ContainsBlockStates(_fx.Block1.Hash));

            _stateStore.SetStates(_fx.Block2, _prestoredValues);

            // Check same states have same state hash.
            Assert.NotEqual(
                _stateStore.GetRootHash(_fx.GenesisBlock.Hash),
                _stateStore.GetRootHash(_fx.Block1.Hash));
            Assert.Equal(
                _stateStore.GetRootHash(_fx.GenesisBlock.Hash),
                _stateStore.GetRootHash(_fx.Block2.Hash));
        }