internal BlockHeader GetBlockHeader() { string timestampAsString = Timestamp.ToString( BlockHeader.TimestampFormat, CultureInfo.InvariantCulture ); ImmutableArray <byte> previousHashAsArray = PreviousHash?.ToByteArray().ToImmutableArray() ?? ImmutableArray <byte> .Empty; ImmutableArray <byte> actionsHashAsArray = EvaluationDigest?.ToByteArray().ToImmutableArray() ?? ImmutableArray <byte> .Empty; if (PreviousHash.Equals(EvaluationDigest)) { Console.WriteLine(); } // FIXME: When hash is not assigned, should throw an exception. return(new BlockHeader( index: Index, timestamp: timestampAsString, nonce: Nonce.ToByteArray().ToImmutableArray(), miner: Miner?.ToByteArray().ToImmutableArray() ?? ImmutableArray <byte> .Empty, difficulty: Difficulty, totalDifficulty: TotalDifficulty, previousHash: previousHashAsArray, txHash: TxHash?.ToByteArray().ToImmutableArray() ?? ImmutableArray <byte> .Empty, hash: Hash.ToByteArray().ToImmutableArray(), preEvaluationHash: PreEvaluationHash.ToByteArray().ToImmutableArray(), evaluationDigest: actionsHashAsArray )); }
public bool AddTransaction(Transaction transaction) { if (transaction == null) { return(false); } if (!PreviousHash.Equals(NoobChaiN.FirstBlockHash)) { if (!transaction.Process()) { Console.WriteLine("Wrong Transaction Aborted!"); return(false); } } Transactions.Add(transaction); return(true); }
public bool IsValid(bool verbose = false) { BlockRepository blockRepo = new BlockRepository(); List <Block> blocks = blockRepo.Get(); int index = blocks.FindIndex(x => x.Id == Id); if (index != 0) { if (!PreviousHash.Equals(blocks[index - 1].Hash)) { return(false); } } return(true); }