예제 #1
0
        public void RealHistoryTest()
        {
            // load wallet
            Network      network  = Network.TestNet;
            string       path     = Path.Combine(Helpers.CommittedWalletsFolderPath, $"HiddenWallet.json");
            const string password = "";

            // I change it because I am using a very old wallet to test
            Safe.EarliestPossibleCreationTime = DateTimeOffset.ParseExact("2016-12-18", "yyyy-MM-dd", CultureInfo.InvariantCulture);
            Safe safe = Safe.Load(password, path);

            Assert.Equal(network, safe.Network);
            Debug.WriteLine($"Unique Safe ID: {safe.UniqueId}");

            // create walletjob
            WalletJob walletJob = new WalletJob(Helpers.SocksPortHandler, Helpers.ControlPortClient, safe)
            {
                MaxCleanAddressCount = 79
            };

            // note some event
            WalletJob.ConnectedNodeCountChanged += WalletJob_ConnectedNodeCountChanged;
            _syncedOnce             = false;
            walletJob.StateChanged += WalletJob_StateChanged;

            // start syncing
            var  cts           = new CancellationTokenSource();
            var  walletJobTask = walletJob.StartAsync(cts.Token);
            Task reportTask    = Helpers.ReportAsync(cts.Token, walletJob);

            try
            {
                // wait until fully synced
                while (!_syncedOnce)
                {
                    Task.Delay(1000).Wait();
                }

                Helpers.ReportFullHistory(walletJob);

                // 0. Query all operations, grouped our used safe addresses
                int MinUnusedKeyNum = 37;
                Dictionary <BitcoinAddress, List <BalanceOperation> > operationsPerAddresses = Helpers.QueryOperationsPerSafeAddressesAsync(new QBitNinjaClient(safe.Network), safe, MinUnusedKeyNum).Result;

                Dictionary <uint256, List <BalanceOperation> > operationsPerTransactions = QBitNinjaJutsus.GetOperationsPerTransactions(operationsPerAddresses);

                // 3. Create history records from the transactions
                // History records is arbitrary data we want to show to the user
                var txHistoryRecords = new List <Tuple <DateTimeOffset, Money, int, uint256> >();
                foreach (var elem in operationsPerTransactions)
                {
                    var amount = Money.Zero;
                    foreach (var op in elem.Value)
                    {
                        amount += op.Amount;
                    }

                    var firstOp = elem.Value.First();

                    txHistoryRecords
                    .Add(new Tuple <DateTimeOffset, Money, int, uint256>(
                             firstOp.FirstSeen,
                             amount,
                             firstOp.Confirmations,
                             elem.Key));
                }

                // 4. Order the records by confirmations and time (Simply time does not work, because of a QBitNinja issue)
                var qBitHistoryRecords = txHistoryRecords
                                         .OrderByDescending(x => x.Item3) // Confirmations
                                         .ThenBy(x => x.Item1);           // FirstSeen

                var fullSpvHistoryRecords = walletJob.GetSafeHistory();

                // This won't be equal QBit doesn't show us this transaction: 2017.01.04. 16:24:49	0.00000000	True		77b10ff78aab2e41764a05794c4c464922c73f0c23356190429833ce68fd7be9
                // Assert.Equal(qBitHistoryRecords.Count(), fullSpvHistoryRecords.Count());

                HashSet <WalletHistoryRecord> qBitFoundItToo = new HashSet <WalletHistoryRecord>();
                // Assert all record found by qbit also found by spv and they are identical
                foreach (var record in qBitHistoryRecords)
                {
                    // Item2 is the Amount
                    WalletHistoryRecord found = fullSpvHistoryRecords.FirstOrDefault(x => x.TransactionId == record.Item4);
                    Assert.True(found != default(WalletHistoryRecord));
                    Assert.True(found.TimeStamp.Equals(record.Item1));
                    Assert.True(found.Confirmed.Equals(record.Item3 > 0));
                    Assert.True(found.Amount.Equals(record.Item2));
                    qBitFoundItToo.Add(found);
                }

                foreach (var record in fullSpvHistoryRecords)
                {
                    if (!qBitFoundItToo.Contains(record))
                    {
                        Assert.True(null == qBitHistoryRecords.FirstOrDefault(x => x.Item4 == record.TransactionId));
                        Debug.WriteLine($@"QBitNinja failed to find, but SPV found it: {record.TimeStamp.DateTime}	{record.Amount}	{record.Confirmed}		{record.TransactionId}");
                    }
                }
            }
            finally
            {
                cts.Cancel();
                Task.WhenAll(reportTask, walletJobTask).Wait();

                WalletJob.ConnectedNodeCountChanged -= WalletJob_ConnectedNodeCountChanged;
                walletJob.StateChanged -= WalletJob_StateChanged;
            }
        }
예제 #2
0
        public async Task RealHistoryTestAsync()
        {
            // load wallet
            Network      network  = Network.TestNet;
            string       path     = Path.Combine(Helpers.CommittedWalletsFolderPath, $"HiddenWallet.json");
            const string password = "";

            // I change it because I am using a very old wallet to test
            Safe.EarliestPossibleCreationTime = DateTimeOffset.ParseExact("2016-12-18", "yyyy-MM-dd", CultureInfo.InvariantCulture);
            Safe safe = await Safe.LoadAsync(password, path);

            Assert.Equal(network, safe.Network);
            Debug.WriteLine($"Unique Safe ID: {safe.UniqueId}");

            // create walletjob
            var walletJob = new WalletJob
            {
                MaxCleanAddressCount = 79
            };
            await walletJob.InitializeAsync(Helpers.SocksPortHandler, Helpers.ControlPortClient, safe);

            // note some event
            walletJob.ConnectedNodeCountChanged += WalletJob_ConnectedNodeCountChanged;
            _syncedOnce             = false;
            walletJob.StateChanged += WalletJob_StateChanged;

            Assert.Empty(walletJob.SafeAccounts);
            Assert.Equal(0, walletJob.ConnectedNodeCount);
            Assert.Equal(WalletState.NotStarted, walletJob.State);
            Assert.True(walletJob.TracksDefaultSafe);

            // start syncing
            var cts           = new CancellationTokenSource();
            var walletJobTask = walletJob.StartAsync(cts.Token);

            Assert.NotEqual(WalletState.NotStarted, walletJob.State);
            Task reportTask = Helpers.ReportAsync(cts.Token, walletJob);


            try
            {
                // wait until fully synced
                while (!_syncedOnce)
                {
                    await Task.Delay(1000);
                }

                Assert.Equal(WalletState.Synced, walletJob.State);
                Assert.NotEqual(Height.Unknown, await walletJob.GetCreationHeightAsync());
                var headerHeightResult = await walletJob.TryGetHeaderHeightAsync();

                Assert.True(headerHeightResult.Success);
                Assert.NotEmpty((await walletJob.GetTrackerAsync()).TrackedScriptPubKeys);

                await Helpers.ReportFullHistoryAsync(walletJob);

                // 0. Query all operations, grouped our used safe addresses
                int MinUnusedKeyNum = 74;
                Dictionary <BitcoinAddress, List <BalanceOperation> > operationsPerAddresses = await Helpers.QueryOperationsPerSafeAddressesAsync(new QBitNinjaClient(safe.Network), safe, MinUnusedKeyNum);

                Dictionary <uint256, List <BalanceOperation> > operationsPerTransactions = QBitNinjaJutsus.GetOperationsPerTransactions(operationsPerAddresses);

                // 3. Create history records from the transactions
                // History records is arbitrary data we want to show to the user
                var txHistoryRecords = new List <(DateTimeOffset FirstSeen, Money Amount, int Confirmations, uint256 TxId)>();
                foreach (var elem in operationsPerTransactions)
                {
                    var amount = Money.Zero;
                    foreach (var op in elem.Value)
                    {
                        amount += op.Amount;
                    }

                    var firstOp = elem.Value.First();

                    txHistoryRecords
                    .Add((
                             firstOp.FirstSeen,
                             amount,
                             firstOp.Confirmations,
                             elem.Key));
                }

                // 4. Order the records by confirmations and time (Simply time does not work, because of a QBitNinja issue)
                var qBitHistoryRecords = txHistoryRecords
                                         .OrderByDescending(x => x.Confirmations) // Confirmations
                                         .ThenBy(x => x.FirstSeen);               // FirstSeen

                var fullSpvHistoryRecords = await walletJob.GetSafeHistoryAsync();

                // This won't be equal QBit doesn't show us this transaction: 2017.01.04. 16:24:49	0.00000000	True		77b10ff78aab2e41764a05794c4c464922c73f0c23356190429833ce68fd7be9
                // Assert.Equal(qBitHistoryRecords.Count(), fullSpvHistoryRecords.Count());

                HashSet <WalletHistoryRecord> qBitFoundItToo = new HashSet <WalletHistoryRecord>();
                // Assert all record found by qbit also found by spv and they are identical
                foreach (var record in qBitHistoryRecords)
                {
                    WalletHistoryRecord found = fullSpvHistoryRecords.FirstOrDefault(x => x.TransactionId == record.TxId);

                    Assert.NotEqual(default, found);