public static void Main(string[] args) { var blockService = new BlockService(); var serverInfoService = new ServerInfoService(); var assetService = new AssetExchangeService(); var currentHeight = serverInfoService.GetBlockchainStatus().Result.NumberOfBlocks; var blockHeight = 335690; Block <Transaction> block = null; if (args.Length > 0 && args[0].Equals("-height", StringComparison.InvariantCultureIgnoreCase)) { int.TryParse(args[1], out blockHeight); } Console.WriteLine("Start scanning blockchain at height: {0}", blockHeight); for (; blockHeight < currentHeight; blockHeight++) { block = blockService.GetBlockIncludeTransactions(BlockLocator.ByHeight(blockHeight)).Result; foreach (var transaction in block.Transactions.Where(transaction => transaction.SubType == TransactionSubType.ColoredCoinsDividendPayment)) { var attachment = (ColoredCoinsDividendPaymentAttachment)transaction.Attachment; var asset = assetService.GetAsset(attachment.AssetId).Result; Console.WriteLine("{0} {1} {2} {3}", transaction.Timestamp.ToString("d"), transaction.TransactionId, asset.Name, attachment.AmountPerQnt.Nxt); } } if (block != null) { Console.WriteLine("Last checked block height: {0} ({1})", blockHeight, block.Timestamp.ToString(CultureInfo.InvariantCulture)); } Console.ReadLine(); }
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); } }
private void CheckTransactions() { var height = 483000; var service = _serviceProvider.GetService <IBlockService>(); while (height < TestSettings.MaxHeight) { var block = service.GetBlockIncludeTransactions(BlockLocator.ByHeight(height)).Result; height++; } }
private List <TimeSpan> GetBlockTimespans(int startHeight, int stopHeight, IBlockService service) { var previousTimestamp = DateTime.MinValue; var timeSpans = new List <TimeSpan>(); for (var i = startHeight; i <= stopHeight; i++) { var block = service.GetBlock(BlockLocator.ByHeight(i)).Result; if (i != startHeight) { timeSpans.Add(block.Timestamp.Subtract(previousTimestamp)); } previousTimestamp = block.Timestamp; } return(timeSpans); }
public async Task <Block <ulong> > GetBlockAsync(int height) { try { var blockService = _serviceFactory.CreateBlockService(); var blockReply = await blockService.GetBlock(BlockLocator.ByHeight(height), requireBlock : requireBlock); IsOnline = true; return(blockReply); } catch (HttpRequestException) { IsOnline = false; throw; } catch (JsonReaderException e) { IsOnline = false; throw new Exception("Error when parsing response", e); } }
public async Task <BlockchainStatus> GetBlockchainStatus() { var status = new BlockchainStatus(); var blockchainStatusReply = await _serverInfoService.GetBlockchainStatus(); var lastBlock = await _blockService.GetBlock(BlockLocator.ByBlockId(blockchainStatusReply.LastBlockId)); var confirmedBlock = await _blockService.GetBlock(BlockLocator.ByHeight(blockchainStatusReply.NumberOfBlocks - 11)); var secureBlock = await _blockService.GetBlock(BlockLocator.ByHeight(blockchainStatusReply.NumberOfBlocks - 721)); status.LastKnownBlockId = blockchainStatusReply.LastBlockId.ToSigned(); status.LastKnownBlockTimestamp = lastBlock.Timestamp; status.LastConfirmedBlockId = confirmedBlock.BlockId.ToSigned(); status.LastConfirmedBlockTimestamp = confirmedBlock.Timestamp; status.LastSecureBlockId = secureBlock.BlockId.ToSigned(); status.LastSecureBlockTimestamp = secureBlock.Timestamp; return(status); }