public void ToByteArrayShouldNotExposeContents() { var nonce = new Nonce(new byte[5]); nonce.ToByteArray()[0] = 0x01; Assert.Equal(0x00, nonce.ToByteArray()[0]); }
private byte[] SerializeForHash() { var dict = Bencodex.Types.Dictionary.Empty .Add("index", Index) .Add( "timestamp", Timestamp.ToString(BlockHeader.TimestampFormat, CultureInfo.InvariantCulture)) .Add("difficulty", Difficulty) .Add("nonce", Nonce.ToByteArray()); if (!(Miner is null)) { dict = dict.Add("reward_beneficiary", Miner.Value.ToByteArray()); } if (!(PreviousHash is null)) { dict = dict.Add("previous_hash", PreviousHash.Value.ToByteArray()); } if (!(TxHash is null)) { dict = dict.Add("transaction_fingerprint", TxHash.Value.ToByteArray()); } return(new Codec().Encode(dict)); }
internal RawBlock ToRawBlock( bool includeHash, bool includeTransactionData ) { IEnumerable transactions = Transactions.Select( tx => includeTransactionData ? tx.ToRawTransaction(true) as object : tx.Id.ToByteArray() as object ); var rawBlock = new RawBlock( index: Index, timestamp: Timestamp.ToString(TimestampFormat), nonce: Nonce.ToByteArray(), miner: Miner?.ToByteArray(), difficulty: Difficulty, transactions: transactions, previousHash: PreviousHash?.ToByteArray() ); if (includeHash) { rawBlock = rawBlock.AddHash(Hash.ToByteArray()); } return(rawBlock); }
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 void ToByteArray() { byte[] nonceBytes = TestUtils.GetRandomBytes(5); var nonce = new Nonce(nonceBytes); Assert.Equal(nonceBytes, nonce.ToByteArray()); }
internal RawBlock ToRawBlock( bool includeHash, bool includeTransactionData ) { IEnumerable <byte[]> transactions = Transactions.OrderBy(tx => tx.Id).Select( tx => includeTransactionData ? tx.Serialize(true) : tx.Id.ToByteArray() ); var rawBlock = new RawBlock( index: Index, timestamp: Timestamp.ToString(TimestampFormat, CultureInfo.InvariantCulture), nonce: Nonce.ToByteArray(), miner: Miner?.ToByteArray(), difficulty: Difficulty, transactions: transactions, previousHash: PreviousHash?.ToByteArray() ); if (includeHash) { rawBlock = rawBlock.AddHash(Hash.ToByteArray()); } return(rawBlock); }
internal BlockHeader GetBlockHeader() { // FIXME: When hash is not assigned, should throw an exception. return(new BlockHeader( index: Index, timestamp: Timestamp.ToString(TimestampFormat, CultureInfo.InvariantCulture), nonce: Nonce.ToByteArray().ToImmutableArray(), miner: Miner?.ToByteArray().ToImmutableArray() ?? ImmutableArray <byte> .Empty, difficulty: Difficulty, #pragma warning disable MEN002 // line is too long previousHash: PreviousHash?.ToByteArray().ToImmutableArray() ?? ImmutableArray <byte> .Empty, #pragma warning restore MEN002 // line is too long txHash: TxHash?.ToByteArray().ToImmutableArray() ?? ImmutableArray <byte> .Empty, hash: Hash.ToByteArray().ToImmutableArray() )); }
internal BlockHeader GetBlockHeader() { string timestampAsString = Timestamp.ToString( BlockHeader.TimestampFormat, CultureInfo.InvariantCulture ); ImmutableArray <byte> previousHashAsArray = PreviousHash?.ToByteArray().ToImmutableArray() ?? ImmutableArray <byte> .Empty; // 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, previousHash: previousHashAsArray, txHash: TxHash?.ToByteArray().ToImmutableArray() ?? ImmutableArray <byte> .Empty, hash: Hash.ToByteArray().ToImmutableArray() )); }
public static void AssertBytesEqual(Nonce?expected, Nonce?actual) => AssertBytesEqual(expected?.ToByteArray(), actual?.ToByteArray());
public static void AssertBytesEqual(Nonce expected, Nonce actual) => AssertBytesEqual(expected.ToByteArray(), actual.ToByteArray());