This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
Add RPC & client call to replay a transaction. #1734
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ use util::kvdb::*; | |
|
||
// other | ||
use views::BlockView; | ||
use error::{ImportError, ExecutionError, BlockError, ImportResult}; | ||
use error::{ImportError, ExecutionError, ReplayError, BlockError, ImportResult}; | ||
use header::BlockNumber; | ||
use state::State; | ||
use spec::Spec; | ||
|
@@ -473,7 +473,7 @@ impl Client { | |
results.len() | ||
} | ||
|
||
/// Attempt to get a copy of a specific block's state. | ||
/// Attempt to get a copy of a specific block's final state. | ||
/// | ||
/// This will not fail if given BlockID::Latest. | ||
/// Otherwise, this can fail (but may not) if the DB prunes state. | ||
|
@@ -504,6 +504,18 @@ impl Client { | |
}) | ||
} | ||
|
||
/// Attempt to get a copy of a specific block's beginning state. | ||
/// | ||
/// This will not fail if given BlockID::Latest. | ||
/// Otherwise, this can fail (but may not) if the DB prunes state. | ||
pub fn state_at_beginning(&self, id: BlockID) -> Option<State> { | ||
// fast path for latest state. | ||
match id { | ||
BlockID::Pending => self.state_at(BlockID::Latest), | ||
id => self.block_number(id).and_then(|n| self.state_at(BlockID::Number(n - 1))), | ||
} | ||
} | ||
|
||
/// Get a copy of the best block's state. | ||
pub fn state(&self) -> State { | ||
State::from_existing( | ||
|
@@ -660,6 +672,51 @@ impl BlockChainClient for Client { | |
ret | ||
} | ||
|
||
fn replay(&self, id: TransactionID, analytics: CallAnalytics) -> Result<Executed, ReplayError> { | ||
if let Some(address) = self.transaction_address(id) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about this: let address = try!(self.transaction_address(id).ok_or(ReplayError::TransactionNotFound));
let block_data = try!(self.block(BlockID::Hash(address.block_hash)).ok_or(ReplayError::StatePruned));
let mut state = try!(self.state_at_beginning(BlockID::Hash(address.block_hash).ok_or(ReplayError::StatePruned));
let block = ...
if address.index >= txs.len() {
return Err(ReplayError::TransactionNotFound);
}
... to avoid nesting? |
||
if let (Some(block_data), Some(mut state)) = (self.block(BlockID::Hash(address.block_hash)), self.state_at_beginning(BlockID::Hash(address.block_hash))) { | ||
let block = BlockView::new(&block_data); | ||
let txs = block.transactions(); | ||
if txs.len() > address.index { | ||
let options = TransactOptions { tracing: analytics.transaction_tracing, vm_tracing: analytics.vm_tracing, check_nonce: false }; | ||
let view = block.header_view(); | ||
let last_hashes = self.build_last_hashes(view.hash()); | ||
let mut env_info = EnvInfo { | ||
number: view.number(), | ||
author: view.author(), | ||
timestamp: view.timestamp(), | ||
difficulty: view.difficulty(), | ||
last_hashes: last_hashes, | ||
gas_used: U256::zero(), | ||
gas_limit: view.gas_limit(), | ||
}; | ||
for t in txs.iter().take(address.index) { | ||
match Executive::new(&mut state, &env_info, self.engine.deref().deref(), &self.vm_factory).transact(t, Default::default()) { | ||
Ok(x) => { env_info.gas_used = env_info.gas_used + x.gas_used; } | ||
Err(ee) => { return Err(ReplayError::Execution(ee)) } | ||
} | ||
} | ||
let t = &txs[address.index]; | ||
let orig = state.clone(); | ||
let mut ret = Executive::new(&mut state, &env_info, self.engine.deref().deref(), &self.vm_factory).transact(t, options); | ||
if analytics.state_diffing { | ||
if let Ok(ref mut x) = ret { | ||
x.state_diff = Some(state.diff_from(orig)); | ||
} | ||
} | ||
ret.map_err(|ee| ReplayError::Execution(ee)) | ||
} else { | ||
// really weird. shouldn't ever actually happen. | ||
Err(ReplayError::TransactionNotFound) | ||
} | ||
} else { | ||
Err(ReplayError::StatePruned) | ||
} | ||
} else { | ||
Err(ReplayError::TransactionNotFound) | ||
} | ||
} | ||
|
||
fn keep_alive(&self) { | ||
if self.mode != Mode::Active { | ||
self.wake_up(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can overflow for genesis?