protected GethLikeTxTrace ExecuteAndTrace(params byte[] code) { GethLikeTxTracer tracer = new GethLikeTxTracer(GethTraceOptions.Default); (var block, var transaction) = PrepareTx(BlockNumber, 100000, code); _processor.Execute(transaction, block.Header, tracer); return(tracer.BuildResult()); }
private TransactionReceipt Execute(params byte[] code) { _stateProvider.CreateAccount(A, 100.Ether()); _stateProvider.CreateAccount(B, 100.Ether()); Keccak codeHash = _stateProvider.UpdateCode(code); _stateProvider.UpdateCodeHash(TestObject.AddressB, codeHash, _spec.GenesisSpec); _stateProvider.Commit(_spec.GenesisSpec); Transaction transaction = Build.A.Transaction .WithGasLimit(100000) .WithGasPrice(1) .WithTo(TestObject.AddressB) .SignedAndResolved(_ethereumSigner, TestObject.PrivateKeyA, 100000) .TestObject; Assert.AreEqual(A, _ethereumSigner.RecoverAddress(transaction, 100000)); Block block = Build.A.Block.WithNumber(10000).TestObject; TransactionReceipt receipt = _processor.Execute(transaction, block.Header); return(receipt); }
private void ProcessTransactions(Block block, Transaction[] transactions) { TransactionReceipt[] receipts = new TransactionReceipt[transactions.Length]; for (int i = 0; i < transactions.Length; i++) { var transaction = transactions[i]; if (_logger.IsDebugEnabled) { _logger.Debug($"Processing transaction {i}"); } // TODO: setup a DB for this // _transactionStore.AddTransaction(transaction); TransactionReceipt receipt = _transactionProcessor.Execute(transaction, block.Header); if (transaction.Hash == null) { throw new InvalidOperationException("Transaction's hash is null when processing"); } // TODO: setup a DB for this // _transactionStore.AddTransactionReceipt(transaction.Hash, receipt, block.Hash); receipts[i] = receipt; } SetReceipts(block, receipts); }
/// <summary> /// Helper method that actually does the actual call to <see cref="ITransactionProcessor"/>. /// </summary> /// <param name="transactionProcessor">Actual transaction processor to be called upon.</param> /// <param name="header">Header in which context the call is done.</param> /// <param name="transaction">Transaction to be executed.</param> /// <param name="callAndRestore">Is it restore call.</param> /// <returns>Bytes with result.</returns> /// <exception cref="AbiException">Thrown when there is an exception during execution or <see cref="CallOutputTracer.StatusCode"/> is <see cref="StatusCode.Failure"/>.</exception> protected static byte[] CallCore(ITransactionProcessor transactionProcessor, BlockHeader header, Transaction transaction, bool callAndRestore = false) { bool failure; CallOutputTracer tracer = new CallOutputTracer(); try { if (callAndRestore) { transactionProcessor.CallAndRestore(transaction, header, tracer); } else { transactionProcessor.Execute(transaction, header, tracer); } failure = tracer.StatusCode != StatusCode.Success; } catch (Exception e) { throw new AbiException($"System call returned an exception '{e.Message}' at block {header.Number}.", e); } if (failure) { throw new AbiException($"System call returned error '{tracer.Error}' at block {header.Number}."); } else { return(tracer.ReturnValue); } }
public Block Load() { Block genesis = _chainSpec.Genesis; foreach ((Address address, ChainSpecAllocation allocation) in _chainSpec.Allocations.OrderBy(a => a.Key)) { _stateProvider.CreateAccount(address, allocation.Balance); if (allocation.Code != null) { Keccak codeHash = _stateProvider.UpdateCode(allocation.Code); _stateProvider.UpdateCodeHash(address, codeHash, _specProvider.GenesisSpec); } if (allocation.Storage != null) { foreach (KeyValuePair <UInt256, byte[]> storage in allocation.Storage) { _storageProvider.Set(new StorageCell(address, storage.Key), storage.Value.WithoutLeadingZeros().ToArray()); } } if (allocation.Constructor != null) { Transaction constructorTransaction = new SystemTransaction() { SenderAddress = address, Init = allocation.Constructor, GasLimit = genesis.GasLimit }; CallOutputTracer outputTracer = new CallOutputTracer(); _transactionProcessor.Execute(constructorTransaction, genesis.Header, outputTracer); if (outputTracer.StatusCode != StatusCode.Success) { throw new InvalidOperationException($"Failed to initialize constructor for address {address}. Error: {outputTracer.Error}"); } } } // we no longer need the allocations - 0.5MB RAM, 9000 objects for mainnet _chainSpec.Allocations = null; _storageProvider.Commit(); _stateProvider.Commit(_specProvider.GenesisSpec); _storageProvider.CommitTrees(); _stateProvider.CommitTree(); _dbProvider.StateDb.Commit(); _dbProvider.CodeDb.Commit(); genesis.Header.StateRoot = _stateProvider.StateRoot; genesis.Header.Hash = genesis.Header.CalculateHash(); return(genesis); }
public bool TryInvokeTransaction(BlockHeader header, ITransactionProcessor transactionProcessor, Transaction transaction, CallOutputTracer tracer) { if (transaction != null) { transactionProcessor.Execute(transaction, header, tracer); return(tracer.StatusCode == StatusCode.Success); } return(false); }
public void InvokeTransaction(BlockHeader header, ITransactionProcessor transactionProcessor, Transaction transaction, CallOutputTracer tracer) { if (transaction != null) { transactionProcessor.Execute(transaction, header, tracer); if (tracer.StatusCode != StatusCode.Success) { throw new AuRaException($"System call returned error '{tracer.Error}' at block {header.Number}."); } } }
private bool TryCall(BlockHeader header, Transaction transaction, out byte[] result) { CallOutputTracer tracer = new(); try { _transactionProcessor.Execute(transaction, header, tracer); result = tracer.ReturnValue; return(tracer.StatusCode == StatusCode.Success); } catch (Exception) { result = null; return(false); } }
public Block Load() { Block genesis = _chainSpec.Genesis; foreach ((Address address, ChainSpecAllocation allocation) in _chainSpec.Allocations) { _stateProvider.CreateAccount(address, allocation.Balance); if (allocation.Code != null) { Keccak codeHash = _stateProvider.UpdateCode(allocation.Code); _stateProvider.UpdateCodeHash(address, codeHash, _specProvider.GenesisSpec); } if (allocation.Constructor != null) { Transaction constructorTransaction = new SystemTransaction() { SenderAddress = address, Init = allocation.Constructor, GasLimit = genesis.GasLimit }; _transactionProcessor.Execute(constructorTransaction, genesis.Header, NullTxTracer.Instance); } } // we no longer need the allocations - 0.5MB RAM, 9000 objects for mainnet _chainSpec.Allocations = null; _storageProvider.Commit(); _stateProvider.Commit(_specProvider.GenesisSpec); _storageProvider.CommitTrees(); _stateProvider.CommitTree(); _dbProvider.StateDb.Commit(); _dbProvider.CodeDb.Commit(); genesis.Header.StateRoot = _stateProvider.StateRoot; genesis.Header.Hash = genesis.Header.CalculateHash(); return(genesis); }
public void InvokeTransaction(BlockHeader header, ITransactionProcessor transactionProcessor, Transaction transaction, CallOutputTracer tracer) { bool failure; try { transactionProcessor.Execute(transaction, header, tracer); failure = tracer.StatusCode != StatusCode.Success; } catch (Exception e) { throw new AuRaException($"System call returned an exception '{e.Message}' at block {header.Number}.", e); } if (failure) { throw new AuRaException($"System call returned error '{tracer.Error}' at block {header.Number}."); } }
private void Preallocate(Block genesis) { foreach ((Address address, ChainSpecAllocation allocation) in _chainSpec.Allocations.OrderBy(a => a.Key)) { _stateProvider.CreateAccount(address, allocation.Balance, allocation.Nonce); if (allocation.Code != null) { Keccak codeHash = _stateProvider.UpdateCode(allocation.Code); _stateProvider.UpdateCodeHash(address, codeHash, _specProvider.GenesisSpec); } if (allocation.Storage != null) { foreach (KeyValuePair <UInt256, byte[]> storage in allocation.Storage) { _storageProvider.Set(new StorageCell(address, storage.Key), storage.Value.WithoutLeadingZeros().ToArray()); } } if (allocation.Constructor != null) { Transaction constructorTransaction = new SystemTransaction() { SenderAddress = address, Data = allocation.Constructor, GasLimit = genesis.GasLimit }; CallOutputTracer outputTracer = new(); _transactionProcessor.Execute(constructorTransaction, genesis.Header, outputTracer); if (outputTracer.StatusCode != StatusCode.Success) { throw new InvalidOperationException( $"Failed to initialize constructor for address {address}. Error: {outputTracer.Error}"); } } } }
private TxReceipt[] ProcessTransactions(Block block, ProcessingOptions processingOptions, IBlockTracer blockTracer) { _receiptsTracer.SetOtherTracer(blockTracer); _receiptsTracer.StartNewBlockTrace(block); for (int i = 0; i < block.Transactions.Length; i++) { Transaction currentTx = block.Transactions[i]; if ((processingOptions & ProcessingOptions.DoNotVerifyNonce) != 0) { currentTx.Nonce = _stateProvider.GetNonce(currentTx.SenderAddress); } _receiptsTracer.StartNewTxTrace(currentTx.Hash); _transactionProcessor.Execute(currentTx, block.Header, _receiptsTracer); _receiptsTracer.EndTxTrace(); TransactionProcessed?.Invoke(this, new TxProcessedEventArgs(i, currentTx, _receiptsTracer.TxReceipts[i])); } return(_receiptsTracer.TxReceipts); }
private TransactionReceipt[] ProcessTransactions(Block block, ProcessingOptions processingOptions, IBlockTracer blockTracer) { _receiptsTracer.SetOtherTracer(blockTracer); _receiptsTracer.StartNewBlockTrace(block); for (int i = 0; i < block.Transactions.Length; i++) { if (_logger.IsTrace) { _logger.Trace($"Processing transaction {i}"); } Transaction currentTx = block.Transactions[i]; _receiptsTracer.StartNewTxTrace(currentTx.Hash); _transactionProcessor.Execute(currentTx, block.Header, _receiptsTracer); _receiptsTracer.EndTxTrace(); if ((processingOptions & ProcessingOptions.ReadOnlyChain) == 0) { TransactionProcessed?.Invoke(this, new TransactionProcessedEventArgs(_receiptsTracer.TransactionReceipts[i])); } } return(_receiptsTracer.TransactionReceipts); }
public void Execute(Transaction transaction, BlockHeader block, ITxTracer txTracer) { _transactionProcessor.Execute(transaction, block, txTracer); }
private void LoadGenesisBlock(Keccak expectedGenesisHash) { // if we already have a database with blocks then we do not need to load genesis from spec if (_blockTree.Genesis != null) { ValidateGenesisHash(expectedGenesisHash); return; } Block genesis = _chainSpec.Genesis; CreateSystemAccounts(); foreach ((Address address, ChainSpecAllocation allocation) in _chainSpec.Allocations) { _stateProvider.CreateAccount(address, allocation.Balance); if (allocation.Code != null) { Keccak codeHash = _stateProvider.UpdateCode(allocation.Code); _stateProvider.UpdateCodeHash(address, codeHash, _specProvider.GenesisSpec); } if (allocation.Constructor != null) { Transaction constructorTransaction = new Transaction(true) { SenderAddress = address, Init = allocation.Constructor, GasLimit = genesis.GasLimit }; _transactionProcessor.Execute(constructorTransaction, genesis.Header, NullTxTracer.Instance); } } _storageProvider.Commit(); _stateProvider.Commit(_specProvider.GenesisSpec); _storageProvider.CommitTrees(); _stateProvider.CommitTree(); _dbProvider.StateDb.Commit(); _dbProvider.CodeDb.Commit(); genesis.StateRoot = _stateProvider.StateRoot; genesis.Hash = BlockHeader.CalculateHash(genesis.Header); ManualResetEventSlim genesisProcessedEvent = new ManualResetEventSlim(false); bool genesisLoaded = false; void GenesisProcessed(object sender, BlockEventArgs args) { genesisLoaded = true; _blockTree.NewHeadBlock -= GenesisProcessed; genesisProcessedEvent.Set(); } _blockTree.NewHeadBlock += GenesisProcessed; _blockTree.SuggestBlock(genesis); genesisProcessedEvent.Wait(TimeSpan.FromSeconds(5)); if (!genesisLoaded) { throw new BlockchainException("Genesis block processing failure"); } ValidateGenesisHash(expectedGenesisHash); }
public void Validate(ValidationContext context) { _transactions.Execute <ISession>(session => ValidateAgainstSession(session, context)); }