Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        private Block(RawBlock rb)
            : this(
#pragma warning disable SA1118
                rb.Header.ProtocolVersion,
                new HashDigest <SHA256>(rb.Header.Hash),
                rb.Header.Index,
                rb.Header.Difficulty,
                rb.Header.TotalDifficulty,
                new Nonce(rb.Header.Nonce.ToArray()),
                rb.Header.Miner.Any() ? new Address(rb.Header.Miner) : (Address?)null,
                rb.Header.PreviousHash.Any()
                    ? new HashDigest <SHA256>(rb.Header.PreviousHash)
                : (HashDigest <SHA256>?)null,
                DateTimeOffset.ParseExact(
                    rb.Header.Timestamp,
                    BlockHeader.TimestampFormat,
                    CultureInfo.InvariantCulture).ToUniversalTime(),
                rb.Header.TxHash.Any()
                    ? new HashDigest <SHA256>(rb.Header.TxHash)
                : (HashDigest <SHA256>?)null,
                rb.Transactions
                .Select(tx => Transaction <T> .Deserialize(tx.ToArray(), false))
                .ToList(),
                rb.Header.PreEvaluationHash.Any()
                    ? new HashDigest <SHA256>(rb.Header.PreEvaluationHash)
                : (HashDigest <SHA256>?)null,
                rb.Header.StateRootHash.Any()
                    ? new HashDigest <SHA256>(rb.Header.StateRootHash)
                : (HashDigest <SHA256>?)null)
#pragma warning restore SA1118
        {
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            bool includeHash            = false;
            bool includeTransactionData = false;

            if (context.Context is BlockSerializationContext blockSerializationContext)
            {
                includeHash            = blockSerializationContext.IncludeHash;
                includeTransactionData = blockSerializationContext.IncludeTransactionData;
            }

            RawBlock rawBlock = ToRawBlock(includeHash, includeTransactionData);

            rawBlock.GetObjectData(info, context);
        }
Exemplo n.º 5
0
        private Block(RawBlock rb)
            : this(
                rb.Header.Index,
                rb.Header.Difficulty,
                new Nonce(rb.Header.Nonce.ToArray()),
                rb.Header.Miner.Any() ? new Address(rb.Header.Miner) : (Address?)null,
#pragma warning disable MEN002 // Line is too long
                rb.Header.PreviousHash.Any() ? new HashDigest <SHA256>(rb.Header.PreviousHash.ToArray()) : (HashDigest <SHA256>?)null,
#pragma warning restore MEN002 // Line is too long
                DateTimeOffset.ParseExact(
                    rb.Header.Timestamp,
                    BlockHeader.TimestampFormat,
                    CultureInfo.InvariantCulture).ToUniversalTime(),
                rb.Transactions
                .Select(tx => Transaction <T> .Deserialize(tx.ToArray()))
                .ToList()
                )
        {
        }
Exemplo n.º 6
0
        private Block(RawBlock rb)
            : this(
                rb.Index,
                rb.Difficulty,
                new Nonce(rb.Nonce),
                rb.Miner is null ? (Address?)null : new Address(rb.Miner),
#pragma warning disable MEN002 // Line is too long
                rb.PreviousHash is null ? (HashDigest <SHA256>?)null : new HashDigest <SHA256>(rb.PreviousHash),
#pragma warning restore MEN002 // Line is too long
                DateTimeOffset.ParseExact(
                    rb.Timestamp,
                    TimestampFormat,
                    CultureInfo.InvariantCulture).ToUniversalTime(),
                rb.Transactions
                .Select(Transaction <T> .Deserialize)
                .ToList()
                )
        {
        }
Exemplo n.º 7
0
 private Block(RawBlock rawBlock)
 {
     Index      = rawBlock.Index;
     Difficulty = rawBlock.Difficulty;
     Nonce      = new Nonce(rawBlock.Nonce);
     Miner      = (rawBlock.Miner != null)
         ? new Address(rawBlock.Miner)
         : default(Address?);
     PreviousHash = (rawBlock.PreviousHash != null)
         ? new HashDigest <SHA256>(rawBlock.PreviousHash)
         : default(HashDigest <SHA256>?);
     Timestamp = DateTimeOffset.ParseExact(
         rawBlock.Timestamp,
         TimestampFormat,
         CultureInfo.InvariantCulture).ToUniversalTime();
     Transactions = rawBlock.Transactions
                    .Cast <Dictionary <string, object> >()
                    .Select(d => new Transaction <T>(new RawTransaction(d)))
                    .ToList();
 }