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(); }
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>(); }
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")); }
public Sha256Hash Hash() { var properties = new { Index, Timestamp, Proof, PreviousHash, Transactions }; return(Sha256Hash.Of(properties.AsJson())); }
public bool IsGenesisBlock() { return(Index == 1 && PreviousHash == Sha256Hash.Of("Genesis") && Proof == new ProofOfWork(1)); }