Beispiel #1
0
        public BlockWithTransactions Build()
        {
            if (_genesisBlock != null)
            {
                return(_genesisBlock);
            }

            var genesisConfig = _configManager.GetConfig <GenesisConfig>("genesis") ??
                                throw new InvalidOperationException("No genesis config found");

            var fromAddress = UInt160Utils.Zero; // mint initial tokens from zero address
            var balances    = genesisConfig !.Balances
                              .OrderBy(x => x.Key)
                              .ToArray();
            var genesisTransactions = balances.Select((t, i) => new Transaction
            {
                From     = fromAddress,
                Nonce    = (ulong)i,
                Value    = Money.Parse(t.Value).ToUInt256(),
                To       = t.Key.HexToUInt160(),
                GasPrice = 0,
            })
                                      .Select(tx => new TransactionReceipt
            {
                Transaction = tx,
                Hash        = HardforkHeights.IsHardfork_9Active(0) ? tx.FullHash(SignatureUtils.ZeroNew, true) : tx.FullHash(SignatureUtils.ZeroOld, false),
                Signature   = HardforkHeights.IsHardfork_9Active(0) ? SignatureUtils.ZeroNew : SignatureUtils.ZeroOld,
            })
                                      .ToList();

            var txHashes = genesisTransactions.Select(tx => tx.Hash).ToArray();

            var header = new BlockHeader
            {
                PrevBlockHash = UInt256Utils.Zero,
                MerkleRoot    = MerkleTree.ComputeRoot(txHashes) ?? UInt256Utils.Zero,
                Index         = 0,
                StateHash     = UInt256Utils.Zero,
                Nonce         = GenesisConsensusData
            };

            var result = new Block
            {
                Hash = header.Keccak(),
                TransactionHashes = { txHashes },
                Header            = header
            };

            _genesisBlock = new BlockWithTransactions(result, genesisTransactions.ToArray());
            return(_genesisBlock);
        }