diff --git a/libhistoric/AlethStandardTrace.cpp b/libhistoric/AlethStandardTrace.cpp index 5542cca05..14e8a270e 100644 --- a/libhistoric/AlethStandardTrace.cpp +++ b/libhistoric/AlethStandardTrace.cpp @@ -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 ); @@ -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 diff --git a/libhistoric/AlethStandardTrace.h b/libhistoric/AlethStandardTrace.h index 9b6a5a72b..ddd448fdb 100644 --- a/libhistoric/AlethStandardTrace.h +++ b/libhistoric/AlethStandardTrace.h @@ -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 ); diff --git a/libhistoric/PrestateTracePrinter.cpp b/libhistoric/PrestateTracePrinter.cpp index b5002da90..cf85a993d 100644 --- a/libhistoric/PrestateTracePrinter.cpp +++ b/libhistoric/PrestateTracePrinter.cpp @@ -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 ) { @@ -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 @@ -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 \ No newline at end of file diff --git a/libhistoric/PrestateTracePrinter.h b/libhistoric/PrestateTracePrinter.h index 92f646a19..f9e8d9fed 100644 --- a/libhistoric/PrestateTracePrinter.h +++ b/libhistoric/PrestateTracePrinter.h @@ -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, @@ -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 diff --git a/libhistoric/TracePrinter.h b/libhistoric/TracePrinter.h index eb29ddd00..61047a183 100644 --- a/libhistoric/TracePrinter.h +++ b/libhistoric/TracePrinter.h @@ -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;