Ejemplo n.º 1
0
        public static BlockChain <T> MakeBlockChain <T>(
            IBlockPolicy <T> policy,
            IStore store,
            IStateStore stateStore,
            IEnumerable <T> actions  = null,
            PrivateKey privateKey    = null,
            DateTimeOffset?timestamp = null,
            IEnumerable <IRenderer <T> > renderers = null,
            Block <T> genesisBlock = null,
            int protocolVersion    = Block <T> .CurrentProtocolVersion
            )
            where T : IAction, new()
        {
            actions    = actions ?? ImmutableArray <T> .Empty;
            privateKey = privateKey ?? new PrivateKey(
                new byte[]
            {
                0xcf, 0x36, 0xec, 0xf9, 0xe4, 0x7c, 0x87, 0x9a, 0x0d, 0xbf,
                0x46, 0xb2, 0xec, 0xd8, 0x3f, 0xd2, 0x76, 0x18, 0x2a, 0xde,
                0x02, 0x65, 0x82, 0x5e, 0x3b, 0x8c, 0x6b, 0xa2, 0x14, 0x46,
                0x7b, 0x76,
            }
                );

            var tx = Transaction <T> .Create(
                0,
                privateKey,
                null,
                actions,
                timestamp : timestamp ?? DateTimeOffset.MinValue);

            genesisBlock = genesisBlock ?? new Block <T>(
                0,
                0,
                0,
                new Nonce(new byte[] { 0x01, 0x00, 0x00, 0x00 }),
                GenesisMinerAddress,
                null,
                timestamp ?? DateTimeOffset.MinValue,
                new[] { tx },
                protocolVersion: protocolVersion
                );
            genesisBlock = genesisBlock.AttachStateRootHash(stateStore, policy.BlockAction);
            ValidatingActionRenderer <T> validator = null;

#pragma warning disable S1121
            var chain = new BlockChain <T>(
                policy,
                new VolatileStagePolicy <T>(),
                store,
                stateStore,
                genesisBlock,
                renderers: renderers ?? new[] { validator = new ValidatingActionRenderer <T>() }
                );
#pragma warning restore S1121

            if (validator != null)
            {
                validator.BlockChain = chain;
            }

            return(chain);
        }
Ejemplo n.º 2
0
        public static BlockChain <T> MakeBlockChain <T>(
            IBlockPolicy <T> policy,
            IStore store,
            IStateStore stateStore,
            IEnumerable <T> actions  = null,
            PrivateKey privateKey    = null,
            DateTimeOffset?timestamp = null,
            IEnumerable <IRenderer <T> > renderers = null,
            Block <T> genesisBlock = null,
            int protocolVersion    = Block <T> .CurrentProtocolVersion
            )
            where T : IAction, new()
        {
            actions    = actions ?? ImmutableArray <T> .Empty;
            privateKey = privateKey ?? ChainPrivateKey;

            var tx = Transaction <T> .Create(
                0,
                privateKey,
                null,
                actions,
                timestamp : timestamp ?? DateTimeOffset.MinValue);

            if (genesisBlock is null)
            {
                var content = new BlockContent <T>()
                {
                    Miner           = GenesisMiner.ToAddress(),
                    PublicKey       = protocolVersion < 2 ? null : GenesisMiner.PublicKey,
                    Timestamp       = timestamp ?? DateTimeOffset.MinValue,
                    Transactions    = new[] { tx },
                    ProtocolVersion = protocolVersion,
                };
                var preEval = new PreEvaluationBlock <T>(
                    content,
                    policy.GetHashAlgorithm(0),
                    new Nonce(new byte[] { 0x01, 0x00, 0x00, 0x00 })
                    );
                genesisBlock = protocolVersion < 2
                 ? new Block <T>(
                    preEval,
                    preEval.DetermineStateRootHash(policy.BlockAction, stateStore),
                    signature: null)
                 : preEval.Evaluate(GenesisMiner, policy.BlockAction, stateStore);
            }

            ValidatingActionRenderer <T> validator = null;

#pragma warning disable S1121
            var chain = new BlockChain <T>(
                policy,
                new VolatileStagePolicy <T>(),
                store,
                stateStore,
                genesisBlock,
                renderers: renderers ?? new[] { validator = new ValidatingActionRenderer <T>() }
                );
#pragma warning restore S1121

            if (validator != null)
            {
                validator.BlockChain = chain;
            }

            return(chain);
        }