public void CanHandleReorgs() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode stratisNode = builder.CreateStratisPowNode(this.regTest, "cv-1-stratisNode").Start(); CoreNode coreNode1 = builder.CreateBitcoinCoreNode().Start(); CoreNode coreNode2 = builder.CreateBitcoinCoreNode().Start(); //Core1 discovers 10 blocks, sends to stratis coreNode1.FindBlock(10).Last(); TestHelper.ConnectAndSync(stratisNode, coreNode1); TestHelper.Disconnect(stratisNode, coreNode1); //Core2 discovers 20 blocks, sends to stratis coreNode2.FindBlock(20).Last(); TestHelper.ConnectAndSync(stratisNode, coreNode2); TestHelper.Disconnect(stratisNode, coreNode2); ((CachedCoinView)stratisNode.FullNode.CoinView()).Flush(); //Core1 discovers 30 blocks, sends to stratis coreNode1.FindBlock(30).Last(); TestHelper.ConnectAndSync(stratisNode, coreNode1); TestHelper.Disconnect(stratisNode, coreNode1); //Core2 discovers 50 blocks, sends to stratis coreNode2.FindBlock(50).Last(); TestHelper.ConnectAndSync(stratisNode, coreNode2); TestHelper.Disconnect(stratisNode, coreNode2); ((CachedCoinView)stratisNode.FullNode.CoinView()).Flush(); TestBase.WaitLoop(() => TestHelper.AreNodesSynced(stratisNode, coreNode2)); } }
public void CanBackupWallet() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15); builder.StartAll(); RPCClient rpcClient = node.CreateRPCClient(); string buildOutputDir = Path.GetDirectoryName("."); string filePath = Path.Combine(buildOutputDir, "wallet_backup.dat"); try { rpcClient.BackupWallet(filePath); Assert.True(File.Exists(filePath)); } finally { if (File.Exists(filePath)) { File.Delete(filePath); } } } }
public async Task CanGetPrivateKeysFromLockedAccountAsync() { string accountName = "account"; using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15); builder.StartAll(); RPCClient rpcClient = node.CreateRPCClient(); var key = new Key(); string passphrase = "password1234"; rpcClient.SendCommand(RPCOperations.encryptwallet, passphrase); // Wait for recepient to process the command. await Task.Delay(300); builder.Nodes[0].Restart(); rpcClient = node.CreateRPCClient(); rpcClient.ImportAddress(key.PubKey.GetAddress(Network.RegTest), accountName, false); BitcoinAddress address = rpcClient.GetAccountAddress(accountName); rpcClient.WalletPassphrase(passphrase, 60); BitcoinSecret secret = rpcClient.DumpPrivKey(address); BitcoinSecret secret2 = rpcClient.GetAccountSecret(accountName); Assert.Equal(secret.ToString(), secret2.ToString()); Assert.Equal(address.ToString(), secret.GetAddress().ToString()); } }
public void CanAuthWithCookieFile() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15); node.CookieAuth = true; builder.StartAll(); RPCClient rpcClient = node.CreateRPCClient(); rpcClient.GetBlockCount(); node.Restart(); rpcClient = node.CreateRPCClient(); rpcClient.GetBlockCount(); Assert.Throws <ArgumentException>(() => new RPCClient("cookiefile=Data\\invalid.cookie", new Uri("http://localhost/"), Network.RegTest)); Assert.Throws <FileNotFoundException>(() => new RPCClient("cookiefile=Data\\not_found.cookie", new Uri("http://localhost/"), Network.RegTest)); rpcClient = new RPCClient("bla:bla", null as Uri, Network.RegTest); Assert.Equal("http://127.0.0.1:" + Network.RegTest.RPCPort + "/", rpcClient.Address.AbsoluteUri); rpcClient = node.CreateRPCClient(); rpcClient = rpcClient.PrepareBatch(); Task <int> blockCountAsync = rpcClient.GetBlockCountAsync(); rpcClient.SendBatch(); int blockCount = blockCountAsync.GetAwaiter().GetResult(); node.Restart(); rpcClient = rpcClient.PrepareBatch(); blockCountAsync = rpcClient.GetBlockCountAsync(); rpcClient.SendBatch(); blockCount = blockCountAsync.GetAwaiter().GetResult(); rpcClient = new RPCClient("bla:bla", "http://toto/", Network.RegTest); } }
public void GetRawMemPoolWithValidTxThenReturnsSameTx() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode().Start(); RPCClient rpcClient = node.CreateRPCClient(); // generate 101 blocks node.GenerateAsync(101).GetAwaiter().GetResult(); CoreNode sfn = builder.CreateStratisPowNode(this.regTest).Start(); TestHelper.ConnectAndSync(node, sfn); uint256 txid = rpcClient.SendToAddress(new Key().PubKey.GetAddress(rpcClient.Network), Money.Coins(1.0m), "hello", "world"); uint256[] ids = rpcClient.GetRawMempool(); Assert.Single(ids); Assert.Equal(txid, ids[0]); RPCClient sfnRpc = sfn.CreateRPCClient(); // It seems to take a while for the transaction to actually propagate, so we have to wait for it before checking the txid is correct. TestBase.WaitLoop(() => sfnRpc.GetRawMempool().Length == 1); ids = sfnRpc.GetRawMempool(); Assert.Single(ids); Assert.Equal(txid, ids[0]); } }
public void CanGetRawTransaction() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: "0.18.0", useNewConfigStyle: true).Start(); RPCClient rpcClient = node.CreateRPCClient(); rpcClient.Generate(101); CoreNode sfn = builder.CreateStratisPowNode(this.regTest); sfn.Start(); TestHelper.ConnectAndSync(node, sfn); uint256 txid = rpcClient.SendToAddress(new Key().PubKey.GetAddress(rpcClient.Network), Money.Coins(1.0m)); TestBase.WaitLoop(() => node.CreateRPCClient().GetRawMempool().Length == 1); Transaction tx = rpcClient.GetRawTransaction(txid); RPCClient sfnRpc = sfn.CreateRPCClient(); TestBase.WaitLoop(() => sfnRpc.GetRawMempool().Length == 1); Transaction tx2 = sfnRpc.GetRawTransaction(txid); Assert.Equal(tx.ToHex(), tx2.ToHex()); } }
public void Pow_CanCoreSyncFromStratis() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode stratisNode = builder.CreateStratisPowNode(this.powNetwork).Start(); CoreNode coreNodeSync = builder.CreateBitcoinCoreNode().Start(); CoreNode coreCreateNode = builder.CreateBitcoinCoreNode().Start(); // first seed a core node with blocks and sync them to a stratis node // and wait till the stratis node is fully synced Block tip = coreCreateNode.FindBlock(5).Last(); TestHelper.ConnectAndSync(stratisNode, coreCreateNode); TestBase.WaitLoop(() => stratisNode.FullNode.ConsensusManager().Tip.Block.GetHash() == tip.GetHash()); // add a new stratis node which will download // the blocks using the GetData payload TestHelper.ConnectAndSync(coreNodeSync, stratisNode); } }
public void TryEstimateFeeRate() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start(); RPCClient rpcClient = node.CreateRPCClient(); Assert.Null(rpcClient.TryEstimateFeeRate(1)); } }
public void CanUseBatchedRequests() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15); builder.StartAll(); RPCClient rpcClient = node.CreateRPCClient(); uint256[] blocks = rpcClient.Generate(10); Assert.Throws <InvalidOperationException>(() => rpcClient.SendBatch()); rpcClient = rpcClient.PrepareBatch(); var requests = new List <Task <uint256> >(); for (int i = 1; i < 11; i++) { requests.Add(rpcClient.GetBlockHashAsync(i)); } Thread.Sleep(1000); foreach (Task <uint256> req in requests) { Assert.Equal(TaskStatus.WaitingForActivation, req.Status); } rpcClient.SendBatch(); rpcClient = rpcClient.PrepareBatch(); int blockIndex = 0; foreach (Task <uint256> req in requests) { Assert.Equal(blocks[blockIndex], req.Result); Assert.Equal(TaskStatus.RanToCompletion, req.Status); blockIndex++; } requests.Clear(); requests.Add(rpcClient.GetBlockHashAsync(10)); requests.Add(rpcClient.GetBlockHashAsync(11)); requests.Add(rpcClient.GetBlockHashAsync(9)); requests.Add(rpcClient.GetBlockHashAsync(8)); rpcClient.SendBatch(); rpcClient = rpcClient.PrepareBatch(); Assert.Equal(TaskStatus.RanToCompletion, requests[0].Status); Assert.Equal(TaskStatus.Faulted, requests[1].Status); Assert.Equal(TaskStatus.RanToCompletion, requests[2].Status); Assert.Equal(TaskStatus.RanToCompletion, requests[3].Status); requests.Clear(); requests.Add(rpcClient.GetBlockHashAsync(10)); requests.Add(rpcClient.GetBlockHashAsync(11)); rpcClient.CancelBatch(); rpcClient = rpcClient.PrepareBatch(); Thread.Sleep(100); Assert.Equal(TaskStatus.Canceled, requests[0].Status); Assert.Equal(TaskStatus.Canceled, requests[1].Status); } }
public void CanSendCommand() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start(); RPCClient rpcClient = node.CreateRPCClient(); RPCResponse response = rpcClient.SendCommand(RPCOperations.getinfo); Assert.NotNull(response.Result); } }
public void CanGetTxOutNoneFromRPC() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start(); RPCClient rpcClient = node.CreateRPCClient(); uint256 txid = rpcClient.Generate(1).Single(); UnspentTransaction resultTxOut = rpcClient.GetTxOut(txid, 0, true); Assert.Null(resultTxOut); } }
public void TryValidateAddress() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start(); RPCClient rpcClient = node.CreateRPCClient(); // RegTest BitcoinAddress pkh = rpcClient.GetNewAddress(); Assert.True(rpcClient.ValidateAddress(pkh).IsValid); } }
public void CanGetTransactionBlockFromRPC() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start(); RPCClient rpcClient = node.CreateRPCClient(); uint256 blockId = rpcClient.GetBestBlockHash(); Block block = rpcClient.GetBlock(blockId); Assert.True(block.CheckMerkleRoot()); } }
public void AddNodeWithValidNodeThenExecutesSuccessfully() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode nodeA = builder.CreateBitcoinCoreNode(); CoreNode nodeB = builder.CreateBitcoinCoreNode(); builder.StartAll(); RPCClient rpc = nodeA.CreateRPCClient(); rpc.RemoveNode(nodeA.Endpoint); rpc.AddNode(nodeB.Endpoint); AddedNodeInfo[] info = null; TestHelper.WaitLoop(() => { info = rpc.GetAddedNodeInfo(true); return(info != null && info.Length > 0); }); Assert.NotNull(info); Assert.NotEmpty(info); //For some reason this one does not pass anymore in 0.13.1 //Assert.Equal(nodeB.Endpoint, info.First().Addresses.First().Address); AddedNodeInfo oneInfo = rpc.GetAddedNodeInfo(true, nodeB.Endpoint); Assert.NotNull(oneInfo); Assert.Equal(nodeB.Endpoint.ToString(), oneInfo.AddedNode.ToString()); oneInfo = rpc.GetAddedNodeInfo(true, nodeA.Endpoint); Assert.Null(oneInfo); rpc.RemoveNode(nodeB.Endpoint); TestHelper.WaitLoop(() => { info = rpc.GetAddedNodeInfo(true); return(info.Length == 0); }); Assert.Empty(info); } }
public void CanGetGenesisFromRPC() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start(); RPCClient rpcClient = node.CreateRPCClient(); RPCResponse response = rpcClient.SendCommand(RPCOperations.getblockhash, 0); string actualGenesis = (string)response.Result; Assert.Equal(this.regTest.GetGenesis().GetHash().ToString(), actualGenesis); Assert.Equal(this.regTest.GetGenesis().GetHash(), rpcClient.GetBestBlockHash()); } }
public void RawTransactionIsConformsToRPC() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start(); RPCClient rpcClient = node.CreateRPCClient(); Transaction tx = this.testNet.GetGenesis().Transactions[0]; Transaction tx2 = rpcClient.DecodeRawTransaction(tx.ToBytes()); Assert.True(JToken.DeepEquals(tx.ToString(this.testNet, RawFormat.Satoshi), tx2.ToString(this.testNet, RawFormat.Satoshi))); } }
public void CanGetBlockFromRPC() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start(); RPCClient rpcClient = node.CreateRPCClient(); BlockHeader response = rpcClient.GetBlockHeader(0); Assert.Equal(this.regTest.GetGenesis().Header.ToBytes(), response.ToBytes()); response = rpcClient.GetBlockHeader(0); Assert.Equal(this.regTest.GenesisHash, response.GetHash()); } }
public void CanSignRawTransaction() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start(); RPCClient rpcClient = node.CreateRPCClient(); rpcClient.Generate(101); var tx = new Transaction(); tx.Outputs.Add(new TxOut(Money.Coins(1.0m), new Key())); FundRawTransactionResponse funded = node.CreateRPCClient().FundRawTransaction(tx); Transaction signed = node.CreateRPCClient().SignRawTransaction(funded.Transaction); node.CreateRPCClient().SendRawTransaction(signed); } }
public void CanGenerateToAddress() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start(); RPCClient rpcClient = node.CreateRPCClient(); var privateKey = new Key(); uint256[] blockHash = rpcClient.GenerateToAddress(1, privateKey.ScriptPubKey.GetDestinationAddress(rpcClient.Network)); Block block = rpcClient.GetBlock(blockHash[0]); Assert.Equal(privateKey.ScriptPubKey, block.Transactions[0].Outputs[0].ScriptPubKey); } }
public void Pow_CanStratisSyncFromCore() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode stratisNode = builder.CreateStratisPowNode(this.powNetwork).Start(); CoreNode coreNode = builder.CreateBitcoinCoreNode().Start(); Block tip = coreNode.FindBlock(10).Last(); TestHelper.ConnectAndSync(stratisNode, coreNode); TestHelper.Disconnect(stratisNode, coreNode); coreNode.FindBlock(10).Last(); TestHelper.ConnectAndSync(coreNode, stratisNode); } }
public void GetRawMemPoolWithValidTxThenReturnsSameTx() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode().Start(); RPCClient rpcClient = node.CreateRPCClient(); // generate 101 blocks node.GenerateAsync(101).GetAwaiter().GetResult(); uint256 txid = rpcClient.SendToAddress(new Key().PubKey.GetAddress(rpcClient.Network), Money.Coins(1.0m), "hello", "world"); uint256[] ids = rpcClient.GetRawMempool(); Assert.Single(ids); Assert.Equal(txid, ids[0]); } }
public void CanSignRawTransaction() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: "0.18.0", useNewConfigStyle: true).Start(); CoreNode sfn = builder.CreateStratisPowNode(this.regTest).WithWallet().Start(); TestHelper.ConnectAndSync(node, sfn); RPCClient rpcClient = node.CreateRPCClient(); RPCClient sfnRpc = sfn.CreateRPCClient(); // Need one block per node so they can each fund a transaction. rpcClient.Generate(1); TestHelper.ConnectAndSync(node, sfn); sfnRpc.Generate(1); TestHelper.ConnectAndSync(node, sfn); // And then enough blocks mined on top for the coinbases to mature. rpcClient.Generate(101); TestHelper.ConnectAndSync(node, sfn); var tx = new Transaction(); tx.Outputs.Add(new TxOut(Money.Coins(1.0m), new Key())); FundRawTransactionResponse funded = rpcClient.FundRawTransaction(tx); // signrawtransaction was removed in 0.18. So just use its equivalent so that we can test SFN's ability to call signrawtransaction. RPCResponse response = rpcClient.SendCommand("signrawtransactionwithwallet", tx.ToHex()); Assert.NotNull(response.Result["hex"]); sfnRpc.WalletPassphrase(sfn.WalletPassword, 60); tx = new Transaction(); tx.Outputs.Add(new TxOut(Money.Coins(1.0m), new Key())); funded = sfnRpc.FundRawTransaction(tx); Transaction signed = sfnRpc.SignRawTransaction(funded.Transaction); rpcClient.SendRawTransaction(signed); } }
public void CanGetPrivateKeysFromAccount() { string accountName = "account"; using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start(); RPCClient rpcClient = node.CreateRPCClient(); var key = new Key(); rpcClient.ImportAddress(key.PubKey.GetAddress(this.regTest), accountName, false); BitcoinAddress address = rpcClient.GetAccountAddress(accountName); BitcoinSecret secret = rpcClient.DumpPrivKey(address); BitcoinSecret secret2 = rpcClient.GetAccountSecret(accountName); Assert.Equal(secret.ToString(), secret2.ToString()); Assert.Equal(address.ToString(), secret.GetAddress().ToString()); } }
public void CanGetTxOutNoneFromRPC() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: "0.18.0", useNewConfigStyle: true).Start(); CoreNode sfn = builder.CreateStratisPowNode(this.regTest).Start(); TestHelper.ConnectAndSync(node, sfn); RPCClient rpcClient = node.CreateRPCClient(); RPCClient sfnRpc = sfn.CreateRPCClient(); uint256 txid = rpcClient.Generate(1).Single(); UnspentTransaction resultTxOut = rpcClient.GetTxOut(txid, 0, true); Assert.Null(resultTxOut); TestHelper.ConnectAndSync(node, sfn); resultTxOut = sfnRpc.GetTxOut(txid, 0, true); Assert.Null(resultTxOut); } }
public void CanAuthWithCookieFile() { using (NodeBuilder builder = NodeBuilder.Create(this)) { CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15, useCookieAuth: true).Start(); RPCClient rpcClient = node.CreateRPCClient(); rpcClient.GetBlockCount(); node.Restart(); rpcClient = node.CreateRPCClient(); rpcClient.GetBlockCount(); string invalidCookiePath = Path.Combine("Data", "invalid.cookie"); string notFoundCookiePath = Path.Combine("Data", "not_found.cookie"); Assert.Throws <ArgumentException>(() => new RPCClient($"cookiefile={invalidCookiePath}", new Uri("http://localhost/"), this.regTest)); Assert.Throws <FileNotFoundException>(() => new RPCClient($"cookiefile={notFoundCookiePath}", new Uri("http://localhost/"), this.regTest)); var uri = new Uri("http://127.0.0.1:" + this.regTest.DefaultRPCPort + "/"); rpcClient = new RPCClient("bla:bla", uri, this.regTest); Assert.Equal(uri.OriginalString, rpcClient.Address.AbsoluteUri); rpcClient = node.CreateRPCClient(); rpcClient = rpcClient.PrepareBatch(); Task <int> blockCountAsync = rpcClient.GetBlockCountAsync(); rpcClient.SendBatch(); int blockCount = blockCountAsync.GetAwaiter().GetResult(); node.Restart(); rpcClient = rpcClient.PrepareBatch(); blockCountAsync = rpcClient.GetBlockCountAsync(); rpcClient.SendBatch(); blockCount = blockCountAsync.GetAwaiter().GetResult(); rpcClient = new RPCClient("bla:bla", new Uri("http://toto/"), this.regTest); } }