Ejemplo n.º 1
0
        public async Task GlobalCleanup()
        {
            foreach (var block in _blocks)
            {
                await _txHub.HandleBlockAcceptedAsync(new BlockAcceptedEvent
                {
                    BlockHeader = block.Header
                });

                foreach (var tx in block.Body.TransactionIds)
                {
                    await _transactionManager.RemoveTransactionAsync(tx);

                    await _transactionResultManager.RemoveTransactionResultAsync(tx, block.GetHash());

                    await _transactionResultManager.RemoveTransactionResultAsync(tx, block.Header.GetPreMiningHash());
                }
                await _chainManager.RemoveChainBlockLinkAsync(block.GetHash());

                await _blockManager.RemoveBlockAsync(block.GetHash());
            }

            await _txHub.HandleBestChainFoundAsync(new BestChainFoundEventData
            {
                BlockHash   = _chain.BestChainHash,
                BlockHeight = _chain.BestChainHeight
            });

            await _chains.SetAsync(_chain.Id.ToStorageKey(), _chain);
        }
Ejemplo n.º 2
0
        public async Task IterationCleanup()
        {
            await _blockStateSets.RemoveAsync(_block.GetHash().ToStorageKey());

            foreach (var transaction in _transactions)
            {
                _transactionResultManager.RemoveTransactionResultAsync(transaction.GetHash(), _block.GetHash());
                _transactionResultManager.RemoveTransactionResultAsync(transaction.GetHash(),
                                                                       _block.Header.GetPreMiningHash());
            }
        }
Ejemplo n.º 3
0
        public async Task IterationCleanup()
        {
            await _blockStateSets.RemoveAsync(_block.GetHash().ToStorageKey());

            foreach (var tx in _block.Body.Transactions)
            {
                _transactionManager.RemoveTransaction(tx);
                _transactionResultManager.RemoveTransactionResultAsync(tx, _block.GetHash());
                _transactionResultManager.RemoveTransactionResultAsync(tx, _block.Header.GetPreMiningHash());
            }
            await _chainManager.RemoveChainBlockLinkAsync(_block.GetHash());

            await _blockManager.RemoveBlockAsync(_block.GetHash());

            await _chains.SetAsync(_chain.Id.ToStorageKey(), _chain);
        }
Ejemplo n.º 4
0
        public async Task HandleEventAsync(BestChainFoundEventData eventData)
        {
            foreach (var blockHash in eventData.ExecutedBlocks)
            {
                var block = await _blockchainService.GetBlockByHashAsync(blockHash);

                Logger.LogTrace($"Handle lib for transactions of block {block.Height}");

                var preMiningHash         = block.Header.GetPreMiningHash();
                var transactionBlockIndex = new TransactionBlockIndex()
                {
                    BlockHash   = blockHash,
                    BlockHeight = block.Height
                };
                if (block.Body.TransactionIds.Count == 0)
                {
                    // This will only happen during test environment
                    return;
                }

                var firstTransaction = block.Body.TransactionIds.First();
                var withBlockHash    = await _transactionResultManager.GetTransactionResultAsync(
                    firstTransaction, blockHash);

                var withPreMiningHash = await _transactionResultManager.GetTransactionResultAsync(
                    firstTransaction, preMiningHash);

                if (withBlockHash == null)
                {
                    // TransactionResult is not saved with real BlockHash
                    // Save results with real (post mining) Hash, so that it can be queried with TransactionBlockIndex
                    foreach (var txId in block.Body.TransactionIds)
                    {
                        var result = await _transactionResultManager.GetTransactionResultAsync(txId, preMiningHash);

                        await _transactionResultManager.AddTransactionResultAsync(result, transactionBlockIndex.BlockHash);
                    }
                }

                if (withPreMiningHash != null)
                {
                    // TransactionResult is saved with PreMiningHash
                    // Remove results saved with PreMiningHash, as it will never be queried
                    foreach (var txId in block.Body.TransactionIds)
                    {
                        await _transactionResultManager.RemoveTransactionResultAsync(txId, preMiningHash);
                    }
                }

                // Add TransactionBlockIndex
                foreach (var txId in block.Body.TransactionIds)
                {
                    await _transactionBlockIndexManager.SetTransactionBlockIndexAsync(txId, transactionBlockIndex);
                }
            }
        }
Ejemplo n.º 5
0
        public async Task IterationCleanup()
        {
            await _blockStateSets.RemoveAsync(_block.GetHash().ToStorageKey());

            foreach (var transaction in _transactions)
            {
                _transactionManager.RemoveTransaction(transaction.GetHash());
                _transactionResultManager.RemoveTransactionResultAsync(transaction.GetHash(), _block.GetHash());
                _transactionResultManager.RemoveTransactionResultAsync(transaction.GetHash(),
                                                                       _block.Header.GetPreMiningHash());
            }

            await _txHub.HandleUnexecutableTransactionsFoundAsync(new UnexecutableTransactionsFoundEvent
                                                                  (null, _transactions.Select(t => t.GetHash()).ToList()));

            await _txHub.HandleBestChainFoundAsync(new BestChainFoundEventData
            {
                BlockHash   = _chain.BestChainHash,
                BlockHeight = _chain.BestChainHeight
            });
        }
Ejemplo n.º 6
0
        public async Task HandleEventAsync(NewIrreversibleBlockFoundEvent eventData)
        {
            var blockHash = eventData.BlockHash;

            while (true)
            {
                var block = await _blockchainService.GetBlockByHashAsync(blockHash);

                var preMiningHash         = block.Header.GetPreMiningHash();
                var transactionBlockIndex = new TransactionBlockIndex()
                {
                    BlockHash = blockHash
                };
                if (block.Body.Transactions.Count == 0)
                {
                    // This will only happen during test environment
                    return;
                }

                var firstTransaction = block.Body.Transactions.First();
                var withBlockHash    = await _transactionResultManager.GetTransactionResultAsync(
                    firstTransaction, blockHash);

                var withPreMiningHash = await _transactionResultManager.GetTransactionResultAsync(
                    firstTransaction, preMiningHash);

                if (withBlockHash == null)
                {
                    // TransactionResult is not saved with real BlockHash
                    // Save results with real (post mining) Hash, so that it can be queried with TransactionBlockIndex
                    foreach (var txId in block.Body.Transactions)
                    {
                        var result = await _transactionResultManager.GetTransactionResultAsync(txId, preMiningHash);

                        await _transactionResultManager.AddTransactionResultAsync(result, transactionBlockIndex.BlockHash);
                    }
                }

                if (withPreMiningHash != null)
                {
                    // TransactionResult is saved with PreMiningHash
                    // Remove results saved with PreMiningHash, as it will never be queried
                    foreach (var txId in block.Body.Transactions)
                    {
                        await _transactionResultManager.RemoveTransactionResultAsync(txId, preMiningHash);
                    }
                }

                // Add TransactionBlockIndex
                foreach (var txId in block.Body.Transactions)
                {
                    await _transactionBlockIndexManager.SetTransactionBlockIndexAsync(txId, transactionBlockIndex);
                }

                if (block.Height <= eventData.PreviousIrreversibleBlockHeight)
                {
                    break;
                }

                blockHash = block.Header.PreviousBlockHash;
            }
        }