コード例 #1
0
ファイル: NonceTest.cs プロジェクト: planetarium/libplanet
        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
ファイル: Block.cs プロジェクト: hongminhee-prtest/libplanet
        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
ファイル: Block.cs プロジェクト: RozzaysRed/libplanet
        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
ファイル: TestUtils.cs プロジェクト: planetarium/libplanet
 public static void AssertBytesEqual(Nonce?expected, Nonce?actual) =>
 AssertBytesEqual(expected?.ToByteArray(), actual?.ToByteArray());
コード例 #10
0
ファイル: TestUtils.cs プロジェクト: planetarium/libplanet
 public static void AssertBytesEqual(Nonce expected, Nonce actual) =>
 AssertBytesEqual(expected.ToByteArray(), actual.ToByteArray());