Skip to content

Commit

Permalink
1664 adding call trace
Browse files Browse the repository at this point in the history
  • Loading branch information
kladkogex committed Oct 10, 2023
1 parent 298a168 commit 0c1b14c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
34 changes: 28 additions & 6 deletions libhistoric/AlethBaseTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ AlethBaseTrace::AlethBaseTrace( Transaction& _t, Json::Value const& _options )
}

void AlethBaseTrace::recordAccessesToAccountsAndStorageValues( uint64_t, Instruction& _inst,
const bigint& _gasUsed, const bigint& _gasLimit, const ExtVMFace* _face, AlethExtVM& _ext, const LegacyVM* _vm ) {
const bigint& _lastOpGas, const bigint& _gasRemaining, const ExtVMFace* _face, AlethExtVM& _ext, const LegacyVM* _vm ) {
// record the account access

STATE_CHECK( _face );
Expand All @@ -70,9 +70,26 @@ void AlethBaseTrace::recordAccessesToAccountsAndStorageValues( uint64_t, Instruc
auto currentDepth = _ext.depth;

if (currentDepth == lastDepth + 1) {
// we are beginning to execute a new function
auto data = _ext.data.toVector();
functionCalled(lastInstruction, _ext.caller, _ext.myAddress, (uint64_t ) _gasLimit,
functionCalled(lastInstruction, _ext.caller, _ext.myAddress, (uint64_t ) _gasRemaining,
data, _ext.value, 0, 0);
} else if (currentDepth == lastDepth - 1) {
// we just exited a function
auto returnedData = std::vector<uint8_t> ();
// the total gas remaining by the function is the gas remaining on function return
// if the return happened through selfdestruct we also need to addd
// self destruct cost
uint64_t gasRemainingOnReturn = lastGasRemaining - lastOpGas;
// this is not to double count if we immediately return from the upper function
// on out of gas
lastOpGas = 0;
std::string error;
std::string revertReason;
functionReturned(returnedData, gasRemainingOnReturn, error, revertReason);
} else {
// we should not have skipped frames
STATE_CHECK(currentDepth == lastDepth);
}

m_accessedAccounts.insert( _ext.myAddress );
Expand Down Expand Up @@ -115,6 +132,8 @@ void AlethBaseTrace::recordAccessesToAccountsAndStorageValues( uint64_t, Instruc
}
lastDepth = currentDepth;
lastInstruction = _inst;
lastGasRemaining = (uint64_t) _gasRemaining;
lastOpGas = (uint64_t ) _lastOpGas;
}


Expand All @@ -139,11 +158,11 @@ void AlethBaseTrace::functionCalled( Instruction _type, const Address& _from, co
lastFunctionCall = nestedCall;
}

void AlethBaseTrace::functionReturned( std::vector< uint8_t >& _outputData, uint64_t _gasUsed,
void AlethBaseTrace::functionReturned( std::vector< uint8_t >& _outputData, uint64_t _gasRemaingOnReturn,
std::string& _error, std::string& _revertReason ) {
lastFunctionCall->setOutputData( _outputData );
lastFunctionCall->setError( _error );
lastFunctionCall->setGasUsed( _gasUsed );
lastFunctionCall->setGasUsed(lastFunctionCall->getFunctionGasLimit() - _gasRemaingOnReturn);
lastFunctionCall->setRevertReason( _revertReason );

if ( lastFunctionCall == topFunctionCall ) {
Expand All @@ -165,6 +184,9 @@ void AlethBaseTrace::functionReturned( std::vector< uint8_t >& _outputData, uint
void AlethBaseTrace::FunctionCall::setGasUsed( uint64_t _gasUsed ) {
FunctionCall::gasUsed = _gasUsed;
}
uint64_t AlethBaseTrace::FunctionCall::getFunctionGasLimit() const {
return functionGasLimit;
}
void AlethBaseTrace::FunctionCall::setOutputData( const std::vector< uint8_t >& _outputData ) {
FunctionCall::outputData = _outputData;
}
Expand Down Expand Up @@ -194,13 +216,13 @@ int64_t AlethBaseTrace::FunctionCall::getDepth() const {


AlethBaseTrace::FunctionCall::FunctionCall( Instruction _type, const Address& _from,
const Address& _to, uint64_t _gas, const std::weak_ptr< FunctionCall >& _parentCall,
const Address& _to, uint64_t _functionGasLimit, const std::weak_ptr< FunctionCall >& _parentCall,
const std::vector< uint8_t >& _inputData, const u256& _value, int64_t _depth, uint64_t _retOffset,
uint64_t _retSize )
: type( _type ),
from( _from ),
to( _to ),
gas( _gas ),
functionGasLimit( _functionGasLimit ),
parentCall( _parentCall ),
inputData( _inputData ),
value( _value ),
Expand Down
11 changes: 7 additions & 4 deletions libhistoric/AlethBaseTrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AlethBaseTrace {

class FunctionCall {
public:
FunctionCall( Instruction _type, const Address& _from, const Address& _to, uint64_t _gas,
FunctionCall( Instruction _type, const Address& _from, const Address& _to, uint64_t _functionGasLimit,
const std::weak_ptr< FunctionCall >& _parentCall,
const std::vector< uint8_t >& _inputData, const u256& _value, int64_t _depth,
uint64_t _retOffset, uint64_t _retSize );
Expand All @@ -56,7 +56,7 @@ class AlethBaseTrace {
Instruction type;
Address from;
Address to;
uint64_t gas = 0;
uint64_t functionGasLimit = 0;
uint64_t gasUsed = 0;
std::vector< std::shared_ptr< FunctionCall > > nestedCalls;
std::weak_ptr< FunctionCall > parentCall;
Expand All @@ -69,6 +69,7 @@ class AlethBaseTrace {
public:
const Address& getFrom() const;
const Address& getTo() const;
uint64_t getFunctionGasLimit() const;

private:
int64_t depth = 0;
Expand All @@ -89,12 +90,12 @@ class AlethBaseTrace {
const std::vector< uint8_t >& _inputData, const u256& _value, uint64_t _retOffset,
uint64_t _retSize );

void functionReturned( std::vector< uint8_t >& _outputData, uint64_t _gasUsed,
void functionReturned( std::vector< uint8_t >& _outputData, uint64_t _gasRemaingOnReturn,
std::string& _error, std::string& _revertReason );


void recordAccessesToAccountsAndStorageValues( uint64_t _pc, Instruction& _inst,
const bigint& _gasCost, const bigint& _gas, const ExtVMFace* _voidExt, AlethExtVM& _ext,
const bigint& _lastOpGas, const bigint& _gasRemaining, const ExtVMFace* _voidExt, AlethExtVM& _ext,
const LegacyVM* _vm );

AlethBaseTrace::DebugOptions debugOptions( Json::Value const& _json );
Expand All @@ -115,6 +116,8 @@ class AlethBaseTrace {
// for each storage address the current value if recorded
std::map< Address, std::map< u256, u256 > > m_accessedStorageValues;

uint64_t lastOpGas = 0;
uint64_t lastGasRemaining = 0;
int64_t lastDepth = -1;
Instruction lastInstruction = Instruction::CALL;
};
Expand Down
6 changes: 3 additions & 3 deletions libhistoric/AlethStandardTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ AlethStandardTrace::AlethStandardTrace( Transaction& _t, Json::Value const& _opt
* This function is called on each EVM op
*/
void AlethStandardTrace::operator()( uint64_t, uint64_t _pc, Instruction _inst, bigint,
bigint _gasUsed, bigint _gasLimit, VMFace const* _vm, ExtVMFace const* _ext ) {
bigint _gasOpGas, bigint _gasRemaining, VMFace const* _vm, ExtVMFace const* _ext ) {


STATE_CHECK(_vm)
Expand All @@ -28,10 +28,10 @@ void AlethStandardTrace::operator()( uint64_t, uint64_t _pc, Instruction _inst,
BOOST_THROW_EXCEPTION( std::runtime_error( std::string( "Null _vm in" ) + __FUNCTION__ ) );
}

recordAccessesToAccountsAndStorageValues( _pc, _inst, _gasUsed, _gasLimit, _ext, ext, vm );
recordAccessesToAccountsAndStorageValues( _pc, _inst, _gasOpGas, _gasRemaining, _ext, ext, vm );

if ( m_options.tracerType == TraceType::DEFAULT_TRACER )
appendOpToDefaultOpTrace( _pc, _inst, _gasUsed, _gasLimit, _ext, ext, vm );
appendOpToDefaultOpTrace( _pc, _inst, _gasOpGas, _gasRemaining, _ext, ext, vm );
}

void AlethStandardTrace::appendOpToDefaultOpTrace( uint64_t _pc, Instruction& _inst,
Expand Down
2 changes: 1 addition & 1 deletion libhistoric/AlethStandardTrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AlethStandardTrace : public AlethBaseTrace {
explicit AlethStandardTrace( Transaction& _t, Json::Value const& _options );

void operator()( uint64_t _steps, uint64_t _pc, Instruction _inst, bigint _newMemSize,
bigint _gasCost, bigint _gas, VMFace const* _vm, ExtVMFace const* _voidExt );
bigint _gasOpGas, bigint _gasRemaining, VMFace const* _vm, ExtVMFace const* _voidExt );

OnOpFunc onOp() {
return [=]( uint64_t _steps, uint64_t _PC, Instruction _inst, bigint _newMemSize,
Expand Down

0 comments on commit 0c1b14c

Please sign in to comment.