示例#1
0
        public void TestMonitorAddress()
        {
            if (Debugger.IsAttached)
            {
                Assert.Inconclusive();
            }

            var sha256 = new SHA256Managed();

            //var publicKey =
            //    "04ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32f"
            //    .HexToByteArray();

            var publicKey =
                "04f9804cfb86fb17441a6562b07c4ee8f012bdb2da5be022032e4b87100350ccc7c0f4d47078b06c9d22b0ec10bdce4c590e0d01aed618987a6caa8c94d74ee6dc"
                .HexToByteArray().ToImmutableArray();

            var walletMonitor = new WalletMonitor(LogManager.CreateNullLogger());

            walletMonitor.AddAddress(new PublicKeyAddress(publicKey));

            using (var simulator = new MainnetSimulator())
            {
                simulator.CoreDaemon.SubscribeChainStateVisitor(walletMonitor);

                var block9999 = simulator.BlockProvider.GetBlock(9999);

                simulator.AddBlockRange(0, 9999);
                simulator.WaitForDaemon(expectedHeight: 9999);
                AssertMethods.AssertDaemonAtBlock(9999, block9999.Hash, simulator.CoreDaemon);

                var minedTxOutputs    = walletMonitor.Entries.Where(x => x.Type == EnumWalletEntryType.Mine).ToList();
                var receivedTxOutputs = walletMonitor.Entries.Where(x => x.Type == EnumWalletEntryType.Receive).ToList();
                var spentTxOutputs    = walletMonitor.Entries.Where(x => x.Type == EnumWalletEntryType.Spend).ToList();

                var actualMinedBtc    = minedTxOutputs.Sum(x => (decimal)x.Value) / 100.MILLION();
                var actualReceivedBtc = receivedTxOutputs.Sum(x => (decimal)x.Value) / 100.MILLION();
                var actualSpentBtc    = spentTxOutputs.Sum(x => (decimal)x.Value) / 100.MILLION();

                Assert.AreEqual(0, minedTxOutputs.Count);
                Assert.AreEqual(16, receivedTxOutputs.Count);
                Assert.AreEqual(14, spentTxOutputs.Count);
                Assert.AreEqual(0M, actualMinedBtc);
                Assert.AreEqual(569.44M, actualReceivedBtc);
                Assert.AreEqual(536.52M, actualSpentBtc);
            }
        }
示例#2
0
        public void TestRpcGetBlockCount()
        {
            using (var simulator = new MainnetSimulator())
            {
                var logger = LogManager.GetCurrentClassLogger();
                int port = FindFreePort();
                using (var rpcServer = new CoreRpcServer(simulator.CoreDaemon, port))
                {
                    rpcServer.StartListening();

                    var block9 = simulator.BlockProvider.GetBlock(9);

                    simulator.AddBlockRange(0, 9);
                    simulator.WaitForUpdate();
                    simulator.AssertAtBlock(9, block9.Hash);

                    var jsonRequestId = Guid.NewGuid().ToString();
                    var jsonRequest = JsonConvert.SerializeObject(
                        new JsonRpcRequest
                        {
                            method = "getblockcount",
                            @params = new string[0],
                            id = jsonRequestId
                        });
                    var jsonRequestBytes = Encoding.UTF8.GetBytes(jsonRequest);

                    var request = (HttpWebRequest)WebRequest.Create($"http://localhost:{port}");
                    request.Method = WebRequestMethods.Http.Post;
                    using (var requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(jsonRequestBytes, 0, jsonRequestBytes.Length);
                    }

                    using (var response = request.GetResponse())
                    using (var responseStream = response.GetResponseStream())
                    using (var responseStreamReader = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        var jsonResponseString = responseStreamReader.ReadToEnd();
                        var jsonResponse = JsonConvert.DeserializeObject<JsonRpcResponse>(jsonResponseString);

                        Assert.AreEqual("2.0", jsonResponse.jsonrpc);
                        Assert.AreEqual("9", jsonResponse.result);
                        Assert.AreEqual(jsonRequestId, jsonResponse.id);
                    }
                }
            }
        }
示例#3
0
        public void TestRpcGetBlockCount()
        {
            using (var simulator = new MainnetSimulator())
            {
                var logger = LogManager.GetCurrentClassLogger();
                int port   = FindFreePort();
                using (var rpcServer = new CoreRpcServer(simulator.CoreDaemon, port))
                {
                    rpcServer.StartListening();

                    var block9 = simulator.BlockProvider.GetBlock(9);

                    simulator.AddBlockRange(0, 9);
                    simulator.WaitForUpdate();
                    simulator.AssertAtBlock(9, block9.Hash);

                    var jsonRequestId = Guid.NewGuid().ToString();
                    var jsonRequest   = JsonConvert.SerializeObject(
                        new JsonRpcRequest
                    {
                        method  = "getblockcount",
                        @params = new string[0],
                        id      = jsonRequestId
                    });
                    var jsonRequestBytes = Encoding.UTF8.GetBytes(jsonRequest);

                    var request = (HttpWebRequest)WebRequest.Create($"http://localhost:{port}");
                    request.Method = WebRequestMethods.Http.Post;
                    using (var requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(jsonRequestBytes, 0, jsonRequestBytes.Length);
                    }

                    using (var response = request.GetResponse())
                        using (var responseStream = response.GetResponseStream())
                            using (var responseStreamReader = new StreamReader(responseStream, Encoding.UTF8))
                            {
                                var jsonResponseString = responseStreamReader.ReadToEnd();
                                var jsonResponse       = JsonConvert.DeserializeObject <JsonRpcResponse>(jsonResponseString);

                                Assert.AreEqual("2.0", jsonResponse.jsonrpc);
                                Assert.AreEqual("9", jsonResponse.result);
                                Assert.AreEqual(jsonRequestId, jsonResponse.id);
                            }
                }
            }
        }
示例#4
0
        public void TestMonitorAddress()
        {
            var publicKey =
                "04f9804cfb86fb17441a6562b07c4ee8f012bdb2da5be022032e4b87100350ccc7c0f4d47078b06c9d22b0ec10bdce4c590e0d01aed618987a6caa8c94d74ee6dc"
                .HexToByteArray().ToImmutableArray();

            using (var simulator = new MainnetSimulator())
                using (var walletMonitor = new WalletMonitor(simulator.CoreDaemon))
                {
                    walletMonitor.AddAddress(new PublicKeyAddress(publicKey));
                    walletMonitor.Start();

                    var block9999 = simulator.BlockProvider.GetBlock(9999);

                    simulator.AddBlockRange(0, 9999);
                    simulator.WaitForUpdate();
                    simulator.AssertAtBlock(9999, block9999.Hash);

                    walletMonitor.WaitForUpdate();
                    Assert.AreEqual(9999, walletMonitor.WalletHeight);

                    var minedTxOutputs    = walletMonitor.Entries.Where(x => x.Type == EnumWalletEntryType.Mine).ToList();
                    var receivedTxOutputs = walletMonitor.Entries.Where(x => x.Type == EnumWalletEntryType.Receive).ToList();
                    var spentTxOutputs    = walletMonitor.Entries.Where(x => x.Type == EnumWalletEntryType.Spend).ToList();

                    var actualMinedBtc    = minedTxOutputs.Sum(x => (decimal)x.Value) / 100.MILLION();
                    var actualReceivedBtc = receivedTxOutputs.Sum(x => (decimal)x.Value) / 100.MILLION();
                    var actualSpentBtc    = spentTxOutputs.Sum(x => (decimal)x.Value) / 100.MILLION();

                    Assert.AreEqual(0, minedTxOutputs.Count);
                    Assert.AreEqual(16, receivedTxOutputs.Count);
                    Assert.AreEqual(14, spentTxOutputs.Count);
                    Assert.AreEqual(0M, actualMinedBtc);
                    Assert.AreEqual(569.44M, actualReceivedBtc);
                    Assert.AreEqual(536.52M, actualSpentBtc);
                }
        }
示例#5
0
        public void TestMonitorAddressRollback()
        {
            var publicKey =
                "04f9804cfb86fb17441a6562b07c4ee8f012bdb2da5be022032e4b87100350ccc7c0f4d47078b06c9d22b0ec10bdce4c590e0d01aed618987a6caa8c94d74ee6dc"
                .HexToByteArray().ToImmutableArray();

            using (var simulator = new MainnetSimulator())
                using (var walletMonitor = new WalletMonitor(simulator.CoreDaemon))
                {
                    walletMonitor.AddAddress(new PublicKeyAddress(publicKey));
                    walletMonitor.Start();

                    var block0    = simulator.BlockProvider.GetBlock(0);
                    var block9999 = simulator.BlockProvider.GetBlock(9999);

                    simulator.AddBlockRange(0, 9999);
                    simulator.WaitForUpdate();
                    simulator.AssertAtBlock(9999, block9999.Hash);

                    // verify initial wallet state
                    walletMonitor.WaitForUpdate();
                    Assert.AreEqual(9999, walletMonitor.WalletHeight);

                    var minedTxOutputs    = walletMonitor.Entries.Where(x => x.Type == EnumWalletEntryType.Mine).ToList();
                    var receivedTxOutputs = walletMonitor.Entries.Where(x => x.Type == EnumWalletEntryType.Receive).ToList();
                    var spentTxOutputs    = walletMonitor.Entries.Where(x => x.Type == EnumWalletEntryType.Spend).ToList();

                    var actualMinedBtc    = minedTxOutputs.Sum(x => (decimal)x.Value) / 100.MILLION();
                    var actualReceivedBtc = receivedTxOutputs.Sum(x => (decimal)x.Value) / 100.MILLION();
                    var actualSpentBtc    = spentTxOutputs.Sum(x => (decimal)x.Value) / 100.MILLION();

                    Assert.AreEqual(0, minedTxOutputs.Count);
                    Assert.AreEqual(16, receivedTxOutputs.Count);
                    Assert.AreEqual(14, spentTxOutputs.Count);
                    Assert.AreEqual(0M, actualMinedBtc);
                    Assert.AreEqual(569.44M, actualReceivedBtc);
                    Assert.AreEqual(536.52M, actualSpentBtc);

                    // mark chain as invalid back to genesis
                    simulator.CoreDaemon.CoreStorage.MarkBlockInvalid(simulator.BlockProvider.GetBlock(1).Hash, simulator.CoreDaemon.TargetChain);

                    // verify chain state reset to genesis
                    simulator.WaitForUpdate();
                    simulator.AssertAtBlock(0, block0.Hash);

                    // verify wallet state rolled back to genesis
                    walletMonitor.WaitForUpdate();
                    Assert.AreEqual(0, walletMonitor.WalletHeight);

                    var unminedTxOutputs    = walletMonitor.Entries.Where(x => x.Type == EnumWalletEntryType.UnMine).ToList();
                    var unreceivedTxOutputs = walletMonitor.Entries.Where(x => x.Type == EnumWalletEntryType.Receive).ToList();
                    var unspentTxOutputs    = walletMonitor.Entries.Where(x => x.Type == EnumWalletEntryType.Spend).ToList();

                    var actualUnminedBtc    = unminedTxOutputs.Sum(x => (decimal)x.Value) / 100.MILLION();
                    var actualUnreceivedBtc = unreceivedTxOutputs.Sum(x => (decimal)x.Value) / 100.MILLION();
                    var actualUnspentBtc    = unspentTxOutputs.Sum(x => (decimal)x.Value) / 100.MILLION();

                    Assert.AreEqual(minedTxOutputs.Count, unminedTxOutputs.Count);
                    Assert.AreEqual(receivedTxOutputs.Count, unreceivedTxOutputs.Count);
                    Assert.AreEqual(spentTxOutputs.Count, unspentTxOutputs.Count);
                    Assert.AreEqual(actualMinedBtc, actualUnminedBtc);
                    Assert.AreEqual(actualReceivedBtc, actualUnreceivedBtc);
                    Assert.AreEqual(actualSpentBtc, actualUnspentBtc);
                }
        }