Exemplo n.º 1
0
        /// <summary>
        /// Deserializes binary data to an object of specific type.
        /// </summary>
        /// <param name="bytes">Binary data representing a serialized object.</param>
        /// <param name="type">Type of the serialized object.</param>
        /// <returns>Deserialized object.</returns>
        public object Deserialize(byte[] bytes, Type type)
        {
            if (type == typeof(BlockHeader))
            {
                BlockHeader header = this.consensusFactory.CreateBlockHeader();
                header.ReadWrite(bytes, this.consensusFactory);
                return(header);
            }

            if (type == typeof(Transaction))
            {
                Transaction transaction = this.consensusFactory.CreateTransaction();
                transaction.ReadWrite(bytes, this.consensusFactory);
                return(transaction);
            }

            if (type == typeof(uint256))
            {
                return(new uint256(bytes));
            }

            if (type == typeof(Block))
            {
                return(Block.Load(bytes, this.consensusFactory));
            }

            if (type == typeof(BlockStake))
            {
                return(BlockStake.Load(bytes, this.consensusFactory));
            }

            if (type == typeof(ProvenBlockHeader))
            {
                ProvenBlockHeader provenBlockHeader =
                    ((PosConsensusFactory)this.consensusFactory).CreateProvenBlockHeader();

                provenBlockHeader.ReadWrite(bytes, this.consensusFactory);
                return(provenBlockHeader);
            }

            if (type == typeof(HashHeightPair))
            {
                return(HashHeightPair.Load(bytes, this.consensusFactory));
            }

            if (typeof(IBitcoinSerializable).IsAssignableFrom(type))
            {
                var result = (IBitcoinSerializable)Activator.CreateInstance(type);
                result.ReadWrite(bytes, this.consensusFactory);
                return(result);
            }

            throw new NotSupportedException();
        }
Exemplo n.º 2
0
        public void ProvenBlockHeaderShouldSerializeAndDeserializeCorrectly()
        {
            // Setup new header to serialize with some fake properties.
            ProvenBlockHeader provenHeaderToSerialize = CreateNewProvenBlockHeaderMock();

            provenHeaderToSerialize.BlockTime = new DateTimeOffset(new DateTime(2018, 1, 1));
            provenHeaderToSerialize.Bits      = 1;
            provenHeaderToSerialize.Nonce     = 2;

            // Attempt to serialize it.
            using (var ms = new MemoryStream())
            {
                provenHeaderToSerialize.ReadWrite(new BitcoinStream(ms, true));

                byte[] bytes = ms.ToArray();
                bytes.Should().HaveCountGreaterThan(0);

                // Setup another slightly different header and try to load it from
                // serialized data from original header.
                ProvenBlockHeader provenHeaderToDeserialize = CreateNewProvenBlockHeaderMock();
                provenHeaderToDeserialize.GetHash().Should().NotBe(provenHeaderToSerialize.GetHash());

                // Attempt to deserialize it.
                provenHeaderToDeserialize.ReadWrite(bytes, this.Network.Consensus.ConsensusFactory);

                provenHeaderToDeserialize.GetHash().Should().Be(provenHeaderToSerialize.GetHash());

                // Check if merke proofs are identical.
                provenHeaderToDeserialize.MerkleProof.Hashes.Should().BeEquivalentTo(provenHeaderToSerialize.MerkleProof.Hashes);
                provenHeaderToDeserialize.MerkleProof.TransactionCount.Should().Be(provenHeaderToSerialize.MerkleProof.TransactionCount);
                for (int i = 0; i < provenHeaderToSerialize.MerkleProof.Flags.Length; i++)
                {
                    provenHeaderToDeserialize.MerkleProof.Flags[i].Should().Be(provenHeaderToSerialize.MerkleProof.Flags[i]);
                }

                // Check if coinstake properties match.
                provenHeaderToDeserialize.Coinstake.Should().BeEquivalentTo(provenHeaderToSerialize.Coinstake);

                // Check if signature properties match.
                provenHeaderToDeserialize.Signature.Signature.Should().BeEquivalentTo(provenHeaderToSerialize.Signature.Signature);

                // Check base properties.
                provenHeaderToDeserialize.BlockTime.Should().Be(provenHeaderToSerialize.BlockTime);
                provenHeaderToDeserialize.CurrentVersion.Should().Be(provenHeaderToSerialize.CurrentVersion);
                provenHeaderToDeserialize.Nonce.Should().Be(provenHeaderToSerialize.Nonce);
                provenHeaderToDeserialize.Time.Should().Be(provenHeaderToSerialize.Time);
                provenHeaderToDeserialize.Version.Should().Be(provenHeaderToSerialize.Version);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Deserializes binary data to an object of specific type.
        /// </summary>
        /// <param name="bytes">Binary data representing a serialized object.</param>
        /// <param name="type">Type of the serialized object.</param>
        /// <returns>Deserialized object.</returns>
        internal object Deserializer(byte[] bytes, Type type)
        {
            if (type == typeof(Coins))
            {
                var coin = new Coins();
                coin.ReadWrite(bytes, this.Network.Consensus.ConsensusFactory);
                return(coin);
            }

            if (type == typeof(BlockHeader))
            {
                BlockHeader header = this.Network.Consensus.ConsensusFactory.CreateBlockHeader();
                header.ReadWrite(bytes, this.Network.Consensus.ConsensusFactory);
                return(header);
            }

            if (type == typeof(RewindData))
            {
                var rewind = new RewindData();
                rewind.ReadWrite(bytes, this.Network.Consensus.ConsensusFactory);
                return(rewind);
            }

            if (type == typeof(uint256))
            {
                return(new uint256(bytes));
            }

            if (type == typeof(Block))
            {
                return(Block.Load(bytes, this.Network));
            }

            if (type == typeof(BlockStake))
            {
                return(BlockStake.Load(bytes, this.Network));
            }

            if (type == typeof(HashHeightPair))
            {
                return(HashHeightPair.Load(bytes));
            }

            if (type == typeof(ProvenBlockHeader))
            {
                ProvenBlockHeader provenBlockHeader =
                    ((PosConsensusFactory)this.Network.Consensus.ConsensusFactory).CreateProvenBlockHeader();

                provenBlockHeader.ReadWrite(bytes, this.Network.Consensus.ConsensusFactory);
                return(provenBlockHeader);
            }

            if (typeof(IBitcoinSerializable).IsAssignableFrom(type))
            {
                var result = (IBitcoinSerializable)Activator.CreateInstance(type);
                result.ReadWrite(bytes);
                return(result);
            }

            throw new NotSupportedException();
        }