Esempio n. 1
0
        public void WhenGetUnspentTransactionsTwoBlocks()
        {
            RemoveBlockChain();
            var serviceProvider     = BuildServiceProvider();
            var blockChainFactory   = serviceProvider.GetService <IBlockChainFactory>();
            var blockChain          = blockChainFactory.Build(_network);
            var genesisBlock        = blockChain.GetCurrentBlock();
            var firstTransaction    = genesisBlock.Transactions.First() as BcBaseTransaction;
            var firstTransactionOut = firstTransaction.TransactionOut.First();

            var genesisKey = KeyStore.GetGenesisKey();
            var genesisAdr = new BlockChainAddress(_scriptTypes, _network, genesisKey); // Create block chain address.
            var destinationBlockChainAddress = GenerateBlockChainAddress();
            var minerBlockChainAddress       = GenerateBlockChainAddress();

            var signature     = genesisKey.GetSignature(); // Create the script.
            var scriptBuilder = new ScriptBuilder();
            var genesisScript = scriptBuilder
                                .New()
                                .AddToStack(signature)
                                .AddToStack(genesisKey.GetPublicKey())
                                .Build();
            var destinationScript = Script.CreateP2PKHScript(destinationBlockChainAddress.PublicKeyHash);
            var minerScript       = Script.CreateP2PKHScript(minerBlockChainAddress.PublicKeyHash);
            var genesisScriptDest = Script.CreateP2PKHScript(genesisKey.GetPublicKeyHashed());

            var transactionBuilder  = new TransactionBuilder();
            var coinBaseTransaction = transactionBuilder // Add COIN-BASE TRANSACTION.
                                      .NewCoinbaseTransaction()
                                      .SetBlockNumber(1)
                                      .AddOutput(1, minerScript)
                                      .Build();
            var noneCoinBaseTransaction = transactionBuilder // ADD GENESIS (10 BTC) => DESTINATION TRANSACTION.
                                          .NewNoneCoinbaseTransaction()
                                          .Spend(firstTransaction, 0, genesisScript.Serialize())
                                          .AddOutput(10, destinationScript)
                                          .Build();
            var otherCoinBaseTransaction = transactionBuilder
                                           .NewNoneCoinbaseTransaction()
                                           .Spend(firstTransaction, 0, genesisScript.Serialize())
                                           .AddOutput(39, genesisScriptDest)
                                           .Build();

            var nonce = NonceHelper.GetNonceUInt32(); // CREATE A BLOCK.
            var block = new Block(genesisBlock.GetHashHeader(), Constants.DEFAULT_NBITS, nonce);

            block.Transactions.Add(coinBaseTransaction);
            block.Transactions.Add(noneCoinBaseTransaction);
            block.Transactions.Add(otherCoinBaseTransaction);
            var a = noneCoinBaseTransaction.Serialize().ToArray();
            var b = BcBaseTransaction.Deserialize(a);

            block.UpdateMerkleRoot();
            blockChain.AddBlock(block);
            var unspentTransactions = blockChain.GetUnspentTransactions();

            Assert.IsNotNull(unspentTransactions);
            Assert.IsTrue(unspentTransactions.Count() == 3);
            Assert.IsTrue(unspentTransactions.Sum(t => t.Value) == 50);
        }
Esempio n. 2
0
        public void WhenSerializeBlockWithOneCoinbaseTransaction()
        {
            var ba          = BuildBlockChainAddress();
            var builder     = new TransactionBuilder();
            var nonce       = BitConverter.GetBytes(NonceHelper.GetNonceUInt64());
            var transaction = builder.NewCoinbaseTransaction()
                              .SetInput(4, nonce)
                              .AddOutput(20, Script.CreateP2PKHScript(ba.PublicKeyHash))
                              .Build();

            var block = new Block(null, NBits, NonceHelper.GetNonceUInt32());

            block.Transactions.Add(transaction);
            var serializedBlock = block.Serialize();

            var    des = Block.Deserialize(serializedBlock);
            string s   = "";
        }
Esempio n. 3
0
        public void WhenSerializeAndDeserializeBlockWithOneSContract()
        {
            var smartContractTransaction = new SmartContractTransaction
            {
                Data = new byte[4] {
                    9, 8, 7, 4
                },
                From = new byte[3] {
                    1, 2, 3
                },
                Nonce = 988
            };
            var block = new Block(null, NBits, NonceHelper.GetNonceUInt32());

            block.Transactions.Add(smartContractTransaction);
            var exceptedBlockHeader = block.GetHashHeader();
            var serializedBlock     = block.Serialize();
            var deserializedBlock   = Block.Deserialize(serializedBlock);
            var blockHeader         = deserializedBlock.GetHashHeader();

            Assert.IsTrue(blockHeader.SequenceEqual(exceptedBlockHeader));
        }