Exemplo n.º 1
0
        public static Block <T> MineNextBlock <T>(
            Block <T> previousBlock,
            HashAlgorithmGetter hashAlgorithmGetter,
            PrivateKey miner,
            IReadOnlyList <Transaction <T> > txs = null,
            byte[] nonce                      = null,
            long difficulty                   = 1,
            TimeSpan?blockInterval            = null,
            int protocolVersion               = Block <T> .CurrentProtocolVersion,
            HashDigest <SHA256> stateRootHash = default
            )
            where T : IAction, new()
        {
            PreEvaluationBlock <T> preEval = MineNext(
                previousBlock,
                hashAlgorithmGetter,
                txs,
                nonce,
                difficulty,
                miner?.PublicKey,
                blockInterval,
                protocolVersion
                );

            return(protocolVersion < 2
                ? new Block <T>(preEval, stateRootHash, null)
                : preEval.Sign(miner, stateRootHash));
        }
Exemplo n.º 2
0
        public void DetermineStateRootHash()
        {
            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())
            {
                HashDigest <SHA256> genesisStateRootHash =
                    preEvalGenesis.DetermineStateRootHash(blockAction, fx.StateStore);
                _output.WriteLine("#0 StateRootHash: {0}", genesisStateRootHash);
                Block <Arithmetic> genesis =
                    preEvalGenesis.Sign(_contents.GenesisKey, genesisStateRootHash);
                _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(genesisStateRootHash, 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));

                HashDigest <SHA256> b1StateRootHash = preEval1.DetermineStateRootHash(blockChain);
                _output.WriteLine("#1 StateRootHash: {0}", b1StateRootHash);
                Block <Arithmetic> block1 = preEval1.Sign(_contents.Block1Key, b1StateRootHash);
                _output.WriteLine("#1: {0}", block1);

                blockChain.Append(block1);
                AssertBencodexEqual((Bencodex.Types.Integer) 158, blockChain.GetState(address));
            }
        }
Exemplo n.º 3
0
        public static Block <T> MineGenesisBlock <T>(
            HashAlgorithmGetter hashAlgorithmGetter,
            PrivateKey miner,
            IReadOnlyList <Transaction <T> > transactions = null,
            DateTimeOffset?timestamp          = null,
            int protocolVersion               = Block <T> .CurrentProtocolVersion,
            HashDigest <SHA256> stateRootHash = default
            )
            where T : IAction, new()
        {
            PreEvaluationBlock <T> preEval = MineGenesis(
                hashAlgorithmGetter,
                miner?.PublicKey,
                transactions,
                timestamp,
                protocolVersion
                );

            return(protocolVersion < 2
                ? new Block <T>(preEval, stateRootHash, null)
                : preEval.Sign(miner, stateRootHash));
        }