public void TestReplayBlock()
        {
            var logger = LogManager.CreateNullLogger();

            using (var simulator = new MainnetSimulator())
            {
                simulator.AddBlockRange(0, 9999);
                simulator.WaitForUpdate();

                using (var chainState = simulator.CoreDaemon.GetChainState())
                {
                    Assert.AreEqual(9999, chainState.Chain.Height);

                    for (var blockHeight = 0; blockHeight <= chainState.Chain.Height; blockHeight++)
                    {
                        var blockHash = chainState.Chain.Blocks[blockHeight].Hash;

                        var expectedTransactions = simulator.BlockProvider.GetBlock(blockHeight).Transactions;
                        var actualTransactions = BlockReplayer.ReplayBlock(simulator.CoreDaemon.CoreStorage, chainState, blockHash, replayForward: true)
                            .ToEnumerable().ToList();

                        CollectionAssert.AreEqual(
                            expectedTransactions.Select(x => x.Hash).ToList(),
                            actualTransactions.Select(x => x.Transaction.Hash).ToList(),
                            $"Transactions differ at block {blockHeight:N0}");
                    }
                }
            }
        }
示例#2
0
        public void TestReplayBlock()
        {
            using (var simulator = new MainnetSimulator())
            {
                simulator.AddBlockRange(0, 9999);
                simulator.WaitForUpdate();

                using (var chainState = simulator.CoreDaemon.GetChainState())
                {
                    Assert.AreEqual(9999, chainState.Chain.Height);

                    for (var blockHeight = 0; blockHeight <= chainState.Chain.Height; blockHeight++)
                    {
                        var blockHash = chainState.Chain.Blocks[blockHeight].Hash;

                        var expectedTransactions = simulator.BlockProvider.GetBlock(blockHeight).Transactions;
                        var actualTransactions   = BlockReplayer.ReplayBlock(simulator.CoreDaemon.CoreStorage, chainState, blockHash, replayForward: true)
                                                   .ToEnumerable().ToList();

                        CollectionAssert.AreEqual(
                            expectedTransactions.Select(x => x.Hash).ToList(),
                            actualTransactions.Select(x => x.Transaction.Hash).ToList(),
                            $"Transactions differ at block {blockHeight:N0}");
                    }
                }
            }
        }
示例#3
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);
            }
        }
示例#4
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);
            }
        }