Ejemplo n.º 1
0
        public async Task CheckIncomingTransactions()
        {
            var lastBlockId = await wallet.GetLastBlockId();

            int lastBlockHeight = 0;

            try
            {
                var block = await blockService.GetBlock(BlockLocator.ByBlockId(lastBlockId));

                lastBlockHeight = block.Height;
                Console.WriteLine($"Last known block id {lastBlockId} is on height {lastBlockHeight}");
            }
            catch (NxtException e)
            {
                if (e.Message == "Unknown block")
                {
                    Console.WriteLine($"Fork detected, unable to find block with id: {lastBlockId}. Manual rollback is needed!");
                    Environment.Exit(-1);
                }
                else
                {
                    throw;
                }
            }

            var blockchainStatus = await serverInfoService.GetBlockchainStatus();

            var currentBlockHeight = blockchainStatus.NumberOfBlocks - 1;
            var blocksToProcess    = Math.Max(0, currentBlockHeight - lastBlockHeight - confirmations);

            Console.WriteLine($"Current block height is: {currentBlockHeight} ({blocksToProcess} block(s) to process)");
            var depositAccounts = await wallet.GetAllDepositAccounts();

            var depositAddresses = new HashSet <string>(depositAccounts.Select(a => a.Address));

            while (currentBlockHeight > lastBlockHeight + confirmations)
            {
                lastBlockHeight++;
                Console.WriteLine($"Processing block @ height {lastBlockHeight}");

                var block = await blockService.GetBlockIncludeTransactions(BlockLocator.ByHeight(lastBlockHeight), true);

                var nxtTransactions = block.Transactions.Where(t => depositAddresses.Contains(t.RecipientRs) && !t.Phased)
                                      .Union(block.ExecutedPhasedTransactions.Where(t => depositAddresses.Contains(t.RecipientRs)))
                                      .Where(t => t.Amount.Nqt > 0);

                foreach (var transaction in nxtTransactions)
                {
                    Console.WriteLine($"Incoming {transaction.Amount.Nxt} NXT to {transaction.RecipientRs}");
                    var account = depositAccounts.Single(d => d.Address == transaction.RecipientRs);
                    account.BalanceNqt += transaction.Amount.Nqt;
                    await wallet.UpdateAccountBalance(account.Id, account.BalanceNqt);
                }
                await wallet.UpdateLastBlockId(block.BlockId);
            }
        }