Esempio n. 1
0
        public Block CreateBlock(BlockChain _Chain, Hash _blockHash, DateTime _dateTimeStampBlock)
        {
            int blockTimeDrift;

            //Gets the height of the blockChain when creating a new block
            blockHeight = _Chain.GetHeight();
            blockHeight--;  //It's the number of blocks away from the Genesis, so 1 becomes 0
            //Create a new block
            Block ParentBlock = _Chain.GetBlock(blockHeight - 1);

            blockDateTimeStamp = _dateTimeStampBlock;                           //Always use UTC
            parentHash         = null;
            difficulty         = BlockDifficulty.GetCurrentBlockDifficulty();   //Determine and write block difficulty
            blockHash          = _blockHash;

            if (ParentBlock != null)
            {
                parentHash     = ParentBlock.GetHash();
                blockTimeDrift = (blockDateTimeStamp - ParentBlock.blockDateTimeStamp).Seconds;

                if (blockTimeDrift < Parameters.GetParameter().AllowedMinimalBlocktime)
                {
                    //Possible timewarp attack, block
                    Console.WriteLine("Block too fast, prohibit time warping");
                    throw new Exception("Block too fast");
                }
            }

            ObjectSerializerBlock.WriteBlockToBlob(this, this.blockHeight);
            //TODO: Should be used when retrieving blocks loading the chain....ObjectSerializerBlock.ReadBlobToBlock(this.blockHeight);

            return(this);
        }
Esempio n. 2
0
        public Boolean AddTransactionToBlock(BlockChain _chain, BlockTransaction _transAction)
        {
            TargetBlock = _chain.GetBlock(_transAction.blockHeight);

            if (TargetBlock == null)
            {
                throw new InvalidOperationException("Block you want to add the transaction to does not exist on the chain");
            }

            if (!_transAction.isSigned)
            {
                throw new InvalidOperationException("Transaction is not signed, cannot be added to the chain");
            }

            TargetBlock.AddTransaction(this);

            return(true);
        }