Exemple #1
0
        public void GenesisHashTest()
        {
            BinarySerializer.RegisterTypes(typeof(Block).Assembly);

            var crypto                      = Crypto.Default;
            var binarySerialier             = BinarySerializer.Default;
            var witnessOperationManager     = new WitnessOperationsManager(crypto);
            var transactionOperationManager = new TransactionOperationManager(crypto, binarySerialier, witnessOperationManager, new Mock <ITransactionRepository>().Object, new Mock <IAssetRepository>().Object, new TransactionContext());
            var blockOperationManager       = new BlockOperationManager(crypto, binarySerialier, transactionOperationManager, witnessOperationManager, new Mock <IBlockRepository>().Object);

            this.AutoMockContainer.Register(crypto);
            this.AutoMockContainer.Register(binarySerialier);
            this.AutoMockContainer.Register <IWitnessOperationsManager>(witnessOperationManager);
            this.AutoMockContainer.Register <ITransactionOperationsManager>(transactionOperationManager);
            this.AutoMockContainer.Register <ISigner <Block> >(blockOperationManager);
            this.AutoMockContainer.Register <ISigner <Transaction> >(transactionOperationManager);

            var genesisAssets = this.AutoMockContainer.Create <GenesisAssetsBuilder>();

            this.AutoMockContainer.Register <IGenesisAssetsBuilder>(genesisAssets);

            // TODO: this test need to be refactor.
            var genesis = this.AutoMockContainer.Create <GenesisBuilder>();

            Assert.AreEqual(genesis.Build().Hash.ToString(true), "0xd42561e3d30e15be6400b6df2f328e02d2bf6354c41dce433bc57687c82144bf");

            Assert.AreEqual(genesisAssets.BuildGoverningTokenRegisterTransaction().Hash.ToString(true), "0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b");
            Assert.AreEqual(genesisAssets.BuildUtilityTokenRegisterTransaction().Hash.ToString(true), "0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7");
        }
        private void FillRandomTx(Transaction tx)
        {
            var witnessOperationsManager     = new WitnessOperationsManager(Crypto.Default);
            var transactionOperationsManager = new TransactionOperationManager(Crypto.Default, this._serializer, witnessOperationsManager, new Mock <ITransactionRepository>().Object, new Mock <IAssetRepository>().Object, new TransactionContext());

            tx.Attributes = RandomTransactionAtrributes().ToArray();
            tx.Inputs     = RandomCoinReferences(_random.Next(1, 255)).ToArray();
            tx.Outputs    = RandomTransactionOutputs().ToArray();
            tx.Witness    = RandomWitness().ToArray();

            transactionOperationsManager.Sign(tx);
        }
        public void BlockSerialize()
        {
            var witnessOperationsManager     = new WitnessOperationsManager(Crypto.Default);
            var blockHeaderOperationsManager = new BlockHeaderOperationsManager(Crypto.Default, BinarySerializer.Default, witnessOperationsManager);

            var blockHeader = new Block
            {
                ConsensusData     = 100_000_000,
                Hash              = UInt256.Zero,
                Index             = 0,
                MerkleRoot        = UInt256.Zero,
                NextConsensus     = UInt160.Zero,
                PreviousBlockHash = UInt256.Zero,
                Timestamp         = 3,
                Version           = 4,
                Witness           = new Witness
                {
                    InvocationScript   = new byte[0],
                    VerificationScript = new byte[0],
                },
                Transactions = new Transaction[] { new InvocationTransaction()
                                                   {
                                                       Attributes = new TransactionAttribute[] { },
                                                       Inputs     = new CoinReference[] { },
                                                       Outputs    = new TransactionOutput[] {},
                                                       Witness    = new Witness[] { },
                                                       Script     = new byte[] { 0x01 },
                                                       Version    = 0
                                                   } }
            };

            blockHeaderOperationsManager.Sign(blockHeader);

            var blockHeaderCopy = _serializer.Deserialize <Block>(_serializer.Serialize(blockHeader));

            blockHeaderOperationsManager.Sign(blockHeaderCopy);

            Assert.AreEqual(blockHeader.ConsensusData, blockHeaderCopy.ConsensusData);
            Assert.AreEqual(blockHeader.Hash, blockHeaderCopy.Hash);
            Assert.AreEqual(blockHeader.Index, blockHeaderCopy.Index);
            Assert.AreEqual(blockHeader.MerkleRoot, blockHeaderCopy.MerkleRoot);
            Assert.AreEqual(blockHeader.NextConsensus, blockHeaderCopy.NextConsensus);
            Assert.AreEqual(blockHeader.PreviousBlockHash, blockHeaderCopy.PreviousBlockHash);
            Assert.AreEqual(blockHeader.Timestamp, blockHeaderCopy.Timestamp);
            Assert.AreEqual(blockHeader.Version, blockHeaderCopy.Version);

            Assert.IsTrue(blockHeader.Witness.InvocationScript.SequenceEqual(blockHeaderCopy.Witness.InvocationScript));
            Assert.IsTrue(blockHeader.Witness.VerificationScript.SequenceEqual(blockHeaderCopy.Witness.VerificationScript));
        }
    }
        void EqualTx(Transaction original, params Transaction[] copies)
        {
            var witnessOperationsManager     = new WitnessOperationsManager(Crypto.Default);
            var transactionOperationsManager = new TransactionOperationManager(Crypto.Default, this._serializer, witnessOperationsManager, new Mock <ITransactionRepository>().Object, new Mock <IAssetRepository>().Object, new TransactionContext());

            foreach (var copy in copies)
            {
                Assert.AreEqual(original.GetType(), copy.GetType());

                Assert.AreEqual(original.Type, copy.Type);
                Assert.AreEqual(original.Version, copy.Version);

                CollectionAssert.AreEqual(original.Attributes, copy.Attributes);
                CollectionAssert.AreEqual(original.Inputs, copy.Inputs);
                CollectionAssert.AreEqual(original.Outputs, copy.Outputs);
                CollectionAssert.AreEqual(original.Witness, copy.Witness);

                // Recompute hash

                transactionOperationsManager.Sign(copy);

                Assert.AreEqual(original.Hash, copy.Hash);
            }
        }