Esempio n. 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);
            }
        }
Esempio n. 2
0
        public void TestRpcGetBlockCount()
        {
            using (var simulator = new MainnetSimulator())
            {
                var logger = simulator.Kernel.Get <Logger>();
                using (var rpcServer = new CoreRpcServer(logger, simulator.CoreDaemon))
                {
                    rpcServer.StartListening();

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

                    simulator.AddBlockRange(0, 9);
                    simulator.WaitForDaemon(expectedHeight: 9);
                    AssertMethods.AssertDaemonAtBlock(9, block9.Hash, simulator.CoreDaemon);

                    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:8332");
                    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);
                            }
                }
            }
        }