Exemple #1
0
        public Block(long index, ProofOfWork proof, Sha256Hash previousHash, IEnumerable <Transaction> transactions, long?timestamp = null)
        {
            if (index <= 0)
            {
                throw new ArgumentException("Index must be greater than zero");
            }
            if (proof == null)
            {
                throw new ArgumentNullException("Proof must not be null");
            }
            if (previousHash == null)
            {
                throw new ArgumentNullException("PreviousHash must not be null");
            }
            if (timestamp.HasValue && timestamp.Value <= 0)
            {
                throw new ArgumentException("Timestamp must be greater than zero");
            }

            // Blocks without transactions are adding new layers of difficulty to ensure the
            // practical immutability of past blocks, i.e. the list of transactions may be empty

            this.index        = index;
            this.timestamp    = timestamp ?? DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            this.proof        = proof;
            this.previousHash = previousHash;
            this.transactions = transactions.ToList();
        }
Exemple #2
0
 private Block()
 {
     this.index        = 1;
     this.timestamp    = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
     this.proof        = new ProofOfWork(1);
     this.previousHash = Sha256Hash.Of("Genesis");
     this.transactions = new List <Transaction>();
 }
Exemple #3
0
        public virtual bool Verify(ProofOfWork lastProof)
        {
            if (lastProof == null)
            {
                throw new InvalidOperationException("Last proof must not be null");
            }

            return(Sha256Hash
                   .Of($"{lastProof.Value}{Value}")
                   .StartsWith("0000"));
        }
Exemple #4
0
        /// <summary>
        /// Create a new block in the blockchain
        /// </summary>
        private Block NewBlock(ProofOfWork proof)
        {
            var block = new Block(
                index: chain.Count + 1,
                proof: proof,
                previousHash: LastBlock.Hash(),
                transactions: currentTransactions);

            currentTransactions.Clear();
            chain.Add(block);

            DomainEvents.Raise(new NewBlockForgedEvent());

            return(block);
        }