示例#1
0
        public void ToByteArrayShouldNotExposeContents()
        {
            var nonce = new Nonce(new byte[5]);

            nonce.ToByteArray()[0] = 0x01;

            Assert.Equal(0x00, nonce.ToByteArray()[0]);
        }
示例#2
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));
        }
示例#3
0
        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);
        }
示例#4
0
文件: Block.cs 项目: x86chi/libplanet
        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
                       ));
        }
示例#5
0
        public void ToByteArray()
        {
            byte[] nonceBytes = TestUtils.GetRandomBytes(5);
            var    nonce      = new Nonce(nonceBytes);

            Assert.Equal(nonceBytes, nonce.ToByteArray());
        }
示例#6
0
文件: Block.cs 项目: lqez/libplanet
        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);
        }
示例#7
0
        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()
                       ));
        }
示例#8
0
        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()
                       ));
        }
示例#9
0
 public static void AssertBytesEqual(Nonce?expected, Nonce?actual) =>
 AssertBytesEqual(expected?.ToByteArray(), actual?.ToByteArray());
示例#10
0
 public static void AssertBytesEqual(Nonce expected, Nonce actual) =>
 AssertBytesEqual(expected.ToByteArray(), actual.ToByteArray());