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); }
public void WhenCalculateBalance48PTCSLeft() { RemoveBlockChain(); var serviceProvider = BuildServiceProvider(); var transactionHelper = serviceProvider.GetService <ITransactionHelper>(); var blockChainFactory = serviceProvider.GetService <IBlockChainFactory>(); var blockChainStore = serviceProvider.GetService <IBlockChainStore>(); blockChainStore.Switch(_network); var blockChain = blockChainStore.GetBlockChain(); // Get the genesis block. 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 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 destGenesisScript = Script.CreateP2PKHScript(genesisKey.GetPublicKeyHashed()); var transactionBuilder = new TransactionBuilder(); var noneCoinBaseTransaction = transactionBuilder // ADD GENESIS (10 BTC) => DESTINATION TRANSACTION. .NewNoneCoinbaseTransaction() .Spend(firstTransaction, 0, genesisScript.Serialize()) .AddOutput(2, destinationScript) .AddOutput(48, destGenesisScript) .Build(); var genesisBalance = transactionHelper.CalculateBalance(noneCoinBaseTransaction, new[] { genesisAdr }, _network); var receiverBalance = transactionHelper.CalculateBalance(noneCoinBaseTransaction, new[] { destinationBlockChainAddress }, _network); Assert.IsTrue(genesisBalance == 48); Assert.IsTrue(receiverBalance == 2); }