Skip to content

Commit

Permalink
#1751 case where no contract is called
Browse files Browse the repository at this point in the history
  • Loading branch information
kladkogex committed Jan 17, 2024
1 parent a557989 commit d620e7c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 15 deletions.
6 changes: 5 additions & 1 deletion libhistoric/AlethStandardTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ AlethStandardTrace::AlethStandardTrace(
m_isCall( _isCall ),
m_value(_t.value()),
m_gasLimit(_t.gas()),
m_inputData(_t.data())
m_inputData(_t.data()),
m_gasPrice(_t.gasPrice())
{
// mark from and to accounts as accessed
m_accessedAccounts.insert( m_from );
Expand Down Expand Up @@ -489,6 +490,9 @@ const u256& AlethStandardTrace::getValue() const {
const Address& AlethStandardTrace::getTo() const {
return m_to;
}
const u256& AlethStandardTrace::getGasPrice() const {
return m_gasPrice;
}
} // namespace dev::eth

#endif
6 changes: 6 additions & 0 deletions libhistoric/AlethStandardTrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,16 @@ class AlethStandardTrace {
u256 m_minerPayment;
u256 m_originalFromBalance;
bool m_isCall;

public:
const u256& getGasPrice() const;

private:
uint64_t m_totalGasUsed;
u256 m_value;
u256 m_gasLimit;
bytes m_inputData;
u256 m_gasPrice;

void printTrace(
ExecutionResult& _er, const HistoricState& _statePre, const HistoricState& _statePost );
Expand Down
51 changes: 38 additions & 13 deletions libhistoric/PrestateTracePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,10 @@ void PrestateTracePrinter::printPre(

Address minerAddress = m_trace.getBlockAuthor();
u256 minerBalance = getMinerBalancePre( _statePre );

_jsonTrace[toHexPrefixed( minerAddress )]["balance"] =
AlethStandardTrace::toGethCompatibleCompactHexPrefixed( minerBalance );
}

u256 PrestateTracePrinter::getMinerBalancePre( const HistoricState& _statePre ) const {
auto minerAddress = m_trace.getBlockAuthor();
auto minerBalance = _statePre.balance( minerAddress );

if ( m_trace.isCall() && minerAddress == m_trace.getFrom() ) {
// take into account that for calls balance is modified in the state before execution
minerBalance = m_trace.getOriginalFromBalance();
}

return minerBalance;
}


void PrestateTracePrinter::printDiff( Json::Value& _jsonTrace, const ExecutionResult&,
const HistoricState& _statePre, const HistoricState& _statePost ) {
Expand All @@ -75,9 +62,29 @@ void PrestateTracePrinter::printDiff( Json::Value& _jsonTrace, const ExecutionRe
printAccountPostDiff( postDiff, _statePre, _statePost, item );
};


// now deal with miner balance change as a result of transaction
// geth always prints miner balance change when NOT in call
if ( !m_trace.isCall() ) {
printMinerBalanceChange( _statePre, preDiff, postDiff );
}

// we are done, complete the trace JSON

_jsonTrace["pre"] = preDiff;
_jsonTrace["post"] = postDiff;
}
void PrestateTracePrinter::printMinerBalanceChange(
const HistoricState& _statePre, Json::Value& preDiff, Json::Value& postDiff ) const {
Address minerAddress = m_trace.getBlockAuthor();
u256 minerBalancePre = getMinerBalancePre( _statePre );
u256 minerBalancePost = getMinerBalancePost( _statePre );

preDiff[toHexPrefixed( minerAddress )]["balance"] =
AlethStandardTrace::toGethCompatibleCompactHexPrefixed( minerBalancePre );
postDiff[toHexPrefixed( minerAddress )]["balance"] =
AlethStandardTrace::toGethCompatibleCompactHexPrefixed( minerBalancePost );
}


// this function returns original values (pre) to result
Expand Down Expand Up @@ -290,6 +297,24 @@ PrestateTracePrinter::PrestateTracePrinter( AlethStandardTrace& standardTrace )
: TracePrinter( standardTrace, "prestateTrace" ) {}


u256 PrestateTracePrinter::getMinerBalancePre( const HistoricState& _statePre ) const {
auto minerAddress = m_trace.getBlockAuthor();
auto minerBalance = _statePre.balance( minerAddress );

if ( m_trace.isCall() && minerAddress == m_trace.getFrom() ) {
// take into account that for calls balance is modified in the state before execution
minerBalance = m_trace.getOriginalFromBalance();
}

return minerBalance;
}

u256 PrestateTracePrinter::getMinerBalancePost( const HistoricState& _statePre ) const {
auto minerBalance =
getMinerBalancePre( _statePre ) + m_trace.getTotalGasUsed() * m_trace.getGasPrice();
return minerBalance;
}

} // namespace dev::eth

#endif
3 changes: 3 additions & 0 deletions libhistoric/PrestateTracePrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class PrestateTracePrinter : public TracePrinter {
explicit PrestateTracePrinter( AlethStandardTrace& standardTrace );

[[nodiscard]] u256 getMinerBalancePre( const HistoricState& _statePre ) const;
[[nodiscard]] u256 getMinerBalancePost( const HistoricState& _statePre ) const;

private:
void printDiff( Json::Value& _jsonTrace, const ExecutionResult&, const HistoricState& _statePre,
Expand All @@ -59,5 +60,7 @@ class PrestateTracePrinter : public TracePrinter {
void printPre(
Json::Value& _jsonTrace, const HistoricState& _statePre, const HistoricState& _statePost );

void printMinerBalanceChange(
const HistoricState& _statePre, Json::Value& preDiff, Json::Value& postDiff ) const;
};
} // namespace dev::eth
2 changes: 1 addition & 1 deletion libhistoric/TracePrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AlethStandardTrace;

class TracePrinter {
public:
TracePrinter( AlethStandardTrace& mStandardTrace, const std::string jsonName );
TracePrinter( AlethStandardTrace& _standardTrace, const std::string jsonName );

virtual void print( Json::Value& _jsonTrace, const ExecutionResult&, const HistoricState&,
const HistoricState& ) = 0;
Expand Down

0 comments on commit d620e7c

Please sign in to comment.