Beispiel #1
0
        public void Evaluate()
        {
            Address address     = _contents.Tx0InBlock1.Signer;
            var     blockAction = new SetStatesAtBlock(address, (Bencodex.Types.Integer) 123, 0);
            var     policy      = new BlockPolicy <Arithmetic>(
                blockAction: blockAction,
                blockInterval: TimeSpan.FromMilliseconds(3 * 60 * 60 * 1000),
                minimumDifficulty: 2,
                difficultyStability: 1
                );
            var stagePolicy = new VolatileStagePolicy <Arithmetic>();

            PreEvaluationBlock <Arithmetic> preEvalGenesis =
                _contents.Genesis.Mine(policy.GetHashAlgorithm(0));

            using (var fx = new DefaultStoreFixture())
            {
                Block <Arithmetic> genesis =
                    preEvalGenesis.Evaluate(_contents.GenesisKey, blockAction, fx.StateStore);
                AssertPreEvaluationBlocksEqual(preEvalGenesis, genesis);
                _output.WriteLine("#1: {0}", genesis);

                var blockChain = new BlockChain <Arithmetic>(
                    policy,
                    stagePolicy,
                    fx.Store,
                    fx.StateStore,
                    genesis
                    );
                AssertBencodexEqual((Bencodex.Types.Integer) 123, blockChain.GetState(address));

                HashDigest <SHA256> identicalGenesisStateRootHash =
                    preEvalGenesis.DetermineStateRootHash(blockChain);
                AssertBytesEqual(genesis.StateRootHash, identicalGenesisStateRootHash);

                BlockContent <Arithmetic> content1 = _contents.Block1;
                content1.PreviousHash = genesis.Hash;
                content1.Difficulty   = 2;
                content1.Transactions = new[] { _contents.Tx0InBlock1 };
                PreEvaluationBlock <Arithmetic> preEval1 = content1.Mine(policy.GetHashAlgorithm(1));

                Block <Arithmetic> block1 = preEval1.Evaluate(_contents.Block1Key, blockChain);
                AssertPreEvaluationBlocksEqual(preEval1, block1);
                _output.WriteLine("#1: {0}", block1);

                HashDigest <SHA256> identicalBlock1StateRootHash =
                    preEval1.DetermineStateRootHash(blockChain);
                AssertBytesEqual(block1.StateRootHash, identicalBlock1StateRootHash);

                blockChain.Append(block1);
                AssertBencodexEqual((Bencodex.Types.Integer) 158, blockChain.GetState(address));
            }
        }
Beispiel #2
0
        public void GetHashAlgorithm()
        {
            Assert.Equal(HashAlgorithmType.Of <SHA256>(), _policy.GetHashAlgorithm(0));
            Assert.Equal(HashAlgorithmType.Of <SHA256>(), _policy.GetHashAlgorithm(1));
            Assert.Equal(HashAlgorithmType.Of <SHA256>(), _policy.GetHashAlgorithm(2));
            Assert.Equal(HashAlgorithmType.Of <SHA256>(), _policy.GetHashAlgorithm(10));
            Assert.Equal(HashAlgorithmType.Of <SHA256>(), _policy.GetHashAlgorithm(15));

            var p = new BlockPolicy <DumbAction>(hashAlgorithmGetter: i =>
                                                 i % 2 == 0 ? HashAlgorithmType.Of <MD5>() : HashAlgorithmType.Of <SHA1>()
                                                 );

            Assert.Equal(HashAlgorithmType.Of <MD5>(), p.GetHashAlgorithm(0));
            Assert.Equal(HashAlgorithmType.Of <SHA1>(), p.GetHashAlgorithm(1));
            Assert.Equal(HashAlgorithmType.Of <MD5>(), p.GetHashAlgorithm(2));
            Assert.Equal(HashAlgorithmType.Of <MD5>(), p.GetHashAlgorithm(10));
            Assert.Equal(HashAlgorithmType.Of <SHA1>(), p.GetHashAlgorithm(15));
        }
Beispiel #3
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),