Ejemplo n.º 1
0
        private static void Part2()
        {
            var walletFilePath = @"Wallets\hiddenWallet.hid"; // extension can be anything

            try
            {
                // Monitor class monitors the blockchain


                var password = "******";
                var mnemonic = "teach track round spend push kangaroo quit volume defy want badge excuse";

                // Let's recover a safe I have created before and sent there testnet coins
                var safe = Safe.Recover(mnemonic, password, walletFilePath, Network.TestNet);

                // WalletMonitor class monitors a safe
                // Safe class helps you store and manage the seed
                // WalletMonitor does only need the public key of the seed
                var walletMonitor = new WalletMonitor(safe.SeedPublicKey);
            }
            finally
            {
                // Cleanup in order to be able to execute this chapter repeatedly
                File.Delete(walletFilePath);
            }
        }
Ejemplo n.º 2
0
        public MainWindowViewModel(IKernel kernel, WalletMonitor walletMonitor = null)
        {
            this.dispatcher = Dispatcher.CurrentDispatcher;

            this.kernel           = kernel;
            this.blockchainDaemon = kernel.Get <CoreDaemon>();
            this.localClient      = kernel.Get <LocalClient>();
            this.blockCache       = kernel.Get <BlockCache>();

            this.startTime         = DateTime.UtcNow;
            this.runningTimeTimer  = new DispatcherTimer();
            runningTimeTimer.Tick += (sender, e) =>
            {
                var runningTime = (DateTime.UtcNow - this.startTime);
                this.RunningTime = "{0:#,#00}:{1:mm':'ss}".Format2(Math.Floor(runningTime.TotalHours), runningTime);
            };
            runningTimeTimer.Interval = TimeSpan.FromMilliseconds(100);
            runningTimeTimer.Start();

            this.ratesTimer  = new DispatcherTimer();
            ratesTimer.Tick += (sender, e) =>
            {
                this.BlockRate                  = this.blockchainDaemon.GetBlockRate(TimeSpan.FromSeconds(1));
                this.TransactionRate            = this.blockchainDaemon.GetTxRate(TimeSpan.FromSeconds(1));
                this.InputRate                  = this.blockchainDaemon.GetInputRate(TimeSpan.FromSeconds(1));
                this.BlockDownloadRate          = this.localClient.GetBlockDownloadRate(TimeSpan.FromSeconds(1));
                this.DuplicateBlockDownloadRate = this.localClient.GetDuplicateBlockDownloadRate(TimeSpan.FromSeconds(1));
            };
            ratesTimer.Interval = TimeSpan.FromSeconds(1);
            ratesTimer.Start();

            this.viewChain = this.blockchainDaemon.CurrentChain;

            this.WinningBlockchainHeight = this.blockchainDaemon.TargetBlockHeight;
            this.CurrentBlockchainHeight = this.blockchainDaemon.CurrentChain.Height;
            this.DownloadedBlockCount    = this.blockCache.Count;

            this.blockCache.OnAddition +=
                (blockHash, block) =>
                DownloadedBlockCount = this.blockCache.Count;

            this.blockCache.OnRemoved +=
                (blockHash) =>
                DownloadedBlockCount = this.blockCache.Count;

            this.blockchainDaemon.OnTargetBlockChanged +=
                (sender, block) =>
                WinningBlockchainHeight = this.blockchainDaemon.TargetBlockHeight;

            this.blockchainDaemon.OnChainStateChanged +=
                (sender, chainState) =>
                CurrentBlockchainHeight = this.blockchainDaemon.CurrentChain.Height;

            if (walletMonitor != null)
            {
                this.walletMonitor = walletMonitor;
                this.WalletEntries = new ObservableCollection <WalletEntry>();
                this.walletMonitor.OnEntryAdded += HandleOnWalletEntryAdded;
            }
        }
Ejemplo n.º 3
0
        public MainWindowViewModel(IKernel kernel, WalletMonitor walletMonitor = null)
        {
            this.kernel        = kernel;
            coreDaemon         = kernel.Get <CoreDaemon>();
            coreStorage        = coreDaemon.CoreStorage;
            localClient        = kernel.Get <LocalClient>();
            this.walletMonitor = walletMonitor;

            startTime              = DateTimeOffset.Now;
            runningTimeTimer       = new DispatcherTimer();
            runningTimeTimer.Tick += (sender, e) =>
            {
                var runningTime = (DateTimeOffset.Now - startTime);
                RunningTime = $"{Math.Floor(runningTime.TotalHours):#,#00}:{runningTime:mm':'ss}";
            };
            runningTimeTimer.Interval = TimeSpan.FromMilliseconds(100);
            runningTimeTimer.Start();

            WinningBlockchainHeight = -1;
            CurrentBlockchainHeight = -1;
            DownloadedBlockCount    = -1;
            WalletHeight            = -1;

            updateWorker = new WorkerMethod("",
                                            _ =>
            {
                WinningBlockchainHeight = coreDaemon.TargetChainHeight;
                CurrentBlockchainHeight = coreDaemon.CurrentChain.Height;
                DownloadedBlockCount    = coreStorage.BlockWithTxesCount;

                BlockRate       = coreDaemon.GetBlockRate();
                TransactionRate = coreDaemon.GetTxRate();
                InputRate       = coreDaemon.GetInputRate();

                BlockDownloadRate           = localClient.GetBlockDownloadRate();
                DuplicateBlockDownloadCount = localClient.GetDuplicateBlockDownloadCount();
                BlockMissCount = localClient.GetBlockMissCount();

                if (walletMonitor != null)
                {
                    WalletHeight       = this.walletMonitor.WalletHeight;
                    WalletEntriesCount = this.walletMonitor.EntriesCount;
                    BitBalance         = this.walletMonitor.BitBalance;
                    BtcBalance         = this.walletMonitor.BtcBalance;
                }

                return(Task.CompletedTask);
            },
                                            initialNotify: true, minIdleTime: TimeSpan.FromSeconds(1), maxIdleTime: TimeSpan.FromSeconds(1));
            updateWorker.Start();
        }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 5
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);
            }
        }
Ejemplo n.º 6
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);
                }
        }
Ejemplo n.º 7
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);
            }
        }
Ejemplo n.º 8
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);
                }
        }