public Task <ResultWrapper <UInt256?> > eth_getBalance(Address address, BlockParameter blockParameter = null) { SearchResult <BlockHeader> searchResult = _blockFinder.SearchForHeader(blockParameter); if (searchResult.IsError) { return(Task.FromResult(ResultWrapper <UInt256?> .Fail(searchResult))); } BlockHeader header = searchResult.Object; if (!HasStateForBlock(header)) { return(Task.FromResult(ResultWrapper <UInt256?> .Fail($"No state available for block {header.Hash}", ErrorCodes.ResourceUnavailable))); } Account account = _stateReader.GetAccount(header.StateRoot, address); return(Task.FromResult(ResultWrapper <UInt256?> .Success(account?.Balance ?? UInt256.Zero))); }
public async Task get_nonce_should_invoke_blockchain_bridge_get_nonce() { UInt256 nonce = 1; Address address = TestItem.AddressA; _blockchainBridge.HeadBlock.Returns(_anyBlock); _stateReader.GetAccount(_anyBlock.StateRoot, address).Returns(Account.TotallyEmpty.WithChangedNonce(nonce)); UInt256 result = await _ndmBridge.GetNonceAsync(address); _stateReader.Received().GetNonce(_anyBlock.StateRoot, address); result.Should().Be(nonce); }
public void transactions_are_addable_to_block_after_sealing() { int chainId = 5; BlockHeader blockHeader = Build.A.BlockHeader.TestObject; GeneratedTransaction tx1 = Build.A.GeneratedTransaction.WithSenderAddress(TestItem.AddressA).TestObject; GeneratedTransaction tx2 = Build.A.GeneratedTransaction.WithSenderAddress(TestItem.AddressA).TestObject; ITimestamper timestamper = Substitute.For <ITimestamper>(); IStateReader stateReader = Substitute.For <IStateReader>(); Address nodeAddress = TestItem.AddressA; UInt256 expectedNonce = 10; stateReader.GetAccount(blockHeader.StateRoot, nodeAddress).Returns(Account.TotallyEmpty.WithChangedNonce(expectedNonce)); ulong expectedTimeStamp = 100; timestamper.UnixTime.Returns(UnixTime.FromSeconds(expectedTimeStamp)); int gasLimit = 200; ITxSource innerTxSource = Substitute.For <ITxSource>(); innerTxSource.GetTransactions(blockHeader, gasLimit).Returns(new[] { tx1, tx2 }); TxSealer txSealer = new(new Signer((ulong)chainId, Build.A.PrivateKey.TestObject, LimboLogs.Instance), timestamper); GeneratedTxSource transactionFiller = new(innerTxSource, txSealer, stateReader, LimboLogs.Instance); Transaction[] sealedTxs = transactionFiller.GetTransactions(blockHeader, gasLimit).ToArray(); Transaction sealedTx1 = sealedTxs.First(); Transaction sealedTx2 = sealedTxs.Skip(1).First(); sealedTx1.IsSigned.Should().BeTrue(); sealedTx1.Nonce.Should().Be(expectedNonce); sealedTx1.Hash.Should().Be(tx1.CalculateHash()); sealedTx1.Timestamp.Should().Be(expectedTimeStamp); sealedTx2.IsSigned.Should().BeTrue(); sealedTx2.Nonce.Should().Be(expectedNonce + 1); sealedTx2.Hash.Should().NotBe(tx1.CalculateHash()); sealedTx2.Timestamp.Should().Be(expectedTimeStamp); }
public static Keccak GetCodeHash(this IStateReader stateReader, Keccak stateRoot, Address address) { return(stateReader.GetAccount(stateRoot, address)?.CodeHash ?? Keccak.OfAnEmptyString); }
public static Keccak GetStorageRoot(this IStateReader stateReader, Keccak stateRoot, Address address) { return(stateReader.GetAccount(stateRoot, address)?.StorageRoot ?? Keccak.EmptyTreeHash); }
public static UInt256 GetBalance(this IStateReader stateReader, Keccak stateRoot, Address address) { return(stateReader.GetAccount(stateRoot, address)?.Balance ?? UInt256.Zero); }
public Account GetAccount(Address address, Keccak stateRoot) { return(_stateReader.GetAccount(stateRoot, address)); }
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)); }
public Account GetAccount(Address address) => _stateReader.GetAccount(StateRoot, address);
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}"); }); }
public Account GetAccount(Address address) => _stateReader.GetAccount(StateRoot, address) ?? Account.TotallyEmpty;