Exemplo n.º 1
0
        private void AddGenesisBlock()
        {
            if (_chain.Count > 0)
            {
                return;
            }

            _logger.LogInformation("Adding genesis block");

            var genesisData = new BlockData(Encoding.ASCII.GetBytes("This is the genesis block!!"));
            var sha512      = SHA512.Create();

            var blockParams = new BlockParams
            {
                Hash         = sha512.ComputeHash(genesisData.GetBytes().ToArray()),
                PreviousHash = new byte[0],
                Data         = genesisData,
                Index        = 0,
                Timestamp    = DateTime.Now
            };

            var genesisBlock = new Block(blockParams);

            _chain.AddLast(genesisBlock);
        }
Exemplo n.º 2
0
 public Block(BlockParams param)
 {
     this.Index        = param.Index;
     this.PreviousHash = param.PreviousHash;
     this.TimeStamp    = param.Timestamp;
     this.Hash         = param.Hash;
     this.BlockData    = param.Data;
     this.Signature    = param.Signature;
 }
Exemplo n.º 3
0
        private Block GenerateNextBlock(BlockData newData)
        {
            var previousBlock = _chain.Last.Value;

            var nextIndex     = previousBlock.Index + 1;
            var nextTimestamp = DateTime.Now;

            var blockParams = new BlockParams()
            {
                Data         = newData,
                Timestamp    = nextTimestamp,
                PreviousHash = previousBlock.Hash,
                Index        = nextIndex,
                Hash         = CalculateHash(previousBlock.Hash, nextIndex, nextTimestamp, newData.GetBytes())
            };

            return(new Block(blockParams));
        }