private Task PrefetchNew(IStateReader stateReader, Block block, Keccak stateRoot, Address miner)
        {
            if (block.TotalDifficulty == null)
            {
                throw new InvalidDataException(
                          $"Received a block with null {nameof(block.TotalDifficulty)} for beam processing");
            }

            CancellationTokenSource cancellationToken;

            lock (_tokens)
            {
                cancellationToken = _tokens.GetOrAdd(block.Number, t => new CancellationTokenSource());
                if (_isDisposed)
                {
                    return(Task.CompletedTask);
                }
            }

            string description = $"[miner {miner}]";
            Task   minerTask   = Task.Run(() =>
            {
                BeamSyncContext.MinimumDifficulty.Value = block.TotalDifficulty ?? 0;
                BeamSyncContext.Description.Value       = description;
                BeamSyncContext.LastFetchUtc.Value      = DateTime.UtcNow;
                BeamSyncContext.Cancelled.Value         = cancellationToken.Token;
                stateReader.GetAccount(stateRoot, miner);
                return(BeamSyncContext.ResolvedInContext.Value);
            }).ContinueWith(t =>
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug(
                        t.IsFaulted ? $"{description} prefetch failed {t.Exception?.Message}" : $"{description} prefetch complete - resolved {t.Result}");
                }
            });

            Task senderTask = Task.Run(() =>
            {
                BeamSyncContext.MinimumDifficulty.Value = block.TotalDifficulty ?? 0;
                BeamSyncContext.Cancelled.Value         = cancellationToken.Token;
                for (int i = 0; i < block.Transactions !.Length; i++)
                {
                    Transaction tx = block.Transactions[i];
                    BeamSyncContext.Description.Value  = $"[tx prefetch {i}]";
                    BeamSyncContext.LastFetchUtc.Value = DateTime.UtcNow;

                    // TODO: is SenderAddress for sure resolved here?
                    stateReader.GetAccount(stateRoot, tx.SenderAddress !);
                }

                return(BeamSyncContext.ResolvedInContext.Value);
            }).ContinueWith(t =>
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug(
                        t.IsFaulted ? $"tx prefetch failed {t.Exception?.Message}" : $"tx prefetch complete - resolved {t.Result}");
                }
            });

            Task storageTask = Task.Run(() =>
            {
                BeamSyncContext.MinimumDifficulty.Value = block.TotalDifficulty ?? 0;
                BeamSyncContext.Cancelled.Value         = cancellationToken.Token;
                for (int i = 0; i < block.Transactions !.Length; i++)
                {
                    Transaction tx = block.Transactions[i];
                    if (tx.To != null)
                    {
                        BeamSyncContext.Description.Value  = $"[storage prefetch {i}]";
                        BeamSyncContext.LastFetchUtc.Value = DateTime.UtcNow;
                        stateReader.GetStorageRoot(stateRoot, tx.To);
                    }
                }

                return(BeamSyncContext.ResolvedInContext.Value);
            }).ContinueWith(t =>
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug(t.IsFaulted ? $"storage prefetch failed {t.Exception?.Message}" : $"storage prefetch complete - resolved {t.Result}");
                }
            });


            Task codeTask = Task.Run(() =>
            {
                BeamSyncContext.MinimumDifficulty.Value = block.TotalDifficulty.Value;
                BeamSyncContext.Cancelled.Value         = cancellationToken.Token;
                for (int i = 0; i < block.Transactions !.Length; i++)
                {
                    Transaction tx = block.Transactions[i];
                    if (tx.To != null)
                    {
                        BeamSyncContext.Description.Value  = $"[code prefetch {i}]";
                        BeamSyncContext.LastFetchUtc.Value = DateTime.UtcNow;
                        stateReader.GetCode(stateRoot, tx.To);
                    }
                }

                return(BeamSyncContext.ResolvedInContext.Value);
            }).ContinueWith(t =>
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug(
                        t.IsFaulted ? $"code prefetch failed {t.Exception?.Message}" : $"code prefetch complete - resolved {t.Result}");
                }
            });

            return(Task.WhenAll(minerTask, senderTask, codeTask, storageTask));
        }
        private void PrefetchNew(IStateReader stateReader, Block block, Keccak stateRoot, Address miner)
        {
            CancellationTokenSource cancellationToken = _tokens.GetOrAdd(block.Number, t => new CancellationTokenSource());
            string description = $"[miner {miner}]";
            Task   minerTask   = Task <int> .Run(() =>
            {
                BeamSyncContext.MinimumDifficulty.Value = block.TotalDifficulty ?? 0;
                BeamSyncContext.Description.Value       = description;
                BeamSyncContext.LastFetchUtc.Value      = DateTime.UtcNow;
                stateReader.GetAccount(stateRoot, miner);
                BeamSyncContext.Cancelled.Value = cancellationToken.Token;
                return(BeamSyncContext.ResolvedInContext.Value);
            }).ContinueWith(t => { _logger.Info(t.IsFaulted ? $"{description} prefetch failed {t.Exception.Message}" : $"{description} prefetch complete - resolved {t.Result}"); });

            Task senderTask = Task <int> .Run(() =>
            {
                BeamSyncContext.MinimumDifficulty.Value = block.TotalDifficulty ?? 0;
                for (int i = 0; i < block.Transactions.Length; i++)
                {
                    Transaction tx = block.Transactions[i];
                    BeamSyncContext.Description.Value  = $"[tx prefetch {i}]";
                    BeamSyncContext.LastFetchUtc.Value = DateTime.UtcNow;
                    BeamSyncContext.Cancelled.Value    = cancellationToken.Token;
                    // _logger.Info($"Resolved sender of {block.Number}.{i}");
                    stateReader.GetAccount(stateRoot, tx.To);
                }

                return(BeamSyncContext.ResolvedInContext.Value);
            }).ContinueWith(t => { _logger.Info(t.IsFaulted ? $"tx prefetch failed {t.Exception.Message}" : $"tx prefetch complete - resolved {t.Result}"); });

            Task storageTask = Task <int> .Run(() =>
            {
                BeamSyncContext.MinimumDifficulty.Value = block.TotalDifficulty ?? 0;
                for (int i = 0; i < block.Transactions.Length; i++)
                {
                    Transaction tx = block.Transactions[i];
                    if (tx.To != null)
                    {
                        BeamSyncContext.Description.Value = $"[storage prefetch {i}]";
                        // _logger.Info($"Resolved storage of target of {block.Number}.{i}");
                        BeamSyncContext.LastFetchUtc.Value = DateTime.UtcNow;
                        BeamSyncContext.Cancelled.Value    = cancellationToken.Token;
                        stateReader.GetStorageRoot(stateRoot, tx.To);
                    }
                }

                return(BeamSyncContext.ResolvedInContext.Value);
            }).ContinueWith(t => { _logger.Info(t.IsFaulted ? $"storage prefetch failed {t.Exception.Message}" : $"storage prefetch complete - resolved {t.Result}"); });


            Task codeTask = Task <int> .Run(() =>
            {
                BeamSyncContext.MinimumDifficulty.Value = block.TotalDifficulty.Value;
                for (int i = 0; i < block.Transactions.Length; i++)
                {
                    Transaction tx = block.Transactions[i];
                    if (tx.To != null)
                    {
                        BeamSyncContext.Description.Value = $"[code prefetch {i}]";
                        // _logger.Info($"Resolved code of target of {block.Number}.{i}");
                        BeamSyncContext.LastFetchUtc.Value = DateTime.UtcNow;
                        BeamSyncContext.Cancelled.Value    = cancellationToken.Token;
                        stateReader.GetCode(stateRoot, tx.SenderAddress);
                        return(BeamSyncContext.ResolvedInContext.Value);
                    }
                }

                return(BeamSyncContext.ResolvedInContext.Value);
            }).ContinueWith(t =>
            {
                _logger.Info(t.IsFaulted ? $"code prefetch failed {t.Exception.Message}" : $"code prefetch complete - resolved {t.Result}");
            });
        }
Exemple #3
0
 public Keccak GetStorageRoot(Address address) => _stateReader.GetStorageRoot(StateRoot, address);
        public byte[] GetStorage(Address address, UInt256 index, Keccak stateRoot)
        {
            Keccak storageRoot = _stateReader.GetStorageRoot(stateRoot, address);

            return(_stateReader.GetStorage(storageRoot, index));
        }