Skip to content

Commit

Permalink
#1748 fixing compatibiliti with geth prestate
Browse files Browse the repository at this point in the history
  • Loading branch information
kladkogex committed Dec 26, 2023
1 parent e120c45 commit 818c7fe
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
25 changes: 18 additions & 7 deletions libethereum/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,9 @@ Block Client::blockByNumber( BlockNumber _h ) const {

auto readState = m_state.createStateReadOnlyCopy();
readState.mutableHistoricState().setRootByBlockNumber( _h );
DEV_GUARDED( m_blockImportMutex ) { return Block( bc(), hash, readState ); }
DEV_GUARDED( m_blockImportMutex ) {
return Block( bc(), hash, readState );
}
assert( false );
return Block( bc() );
} catch ( Exception& ex ) {
Expand All @@ -1106,7 +1108,9 @@ Block Client::blockByNumber( BlockNumber _h ) const {
Block Client::latestBlock() const {
// TODO Why it returns not-filled block??! (see Block ctor)
try {
DEV_GUARDED( m_blockImportMutex ) { return Block( bc(), bc().currentHash(), m_state ); }
DEV_GUARDED( m_blockImportMutex ) {
return Block( bc(), bc().currentHash(), m_state );
}
assert( false );
return Block( bc() );
} catch ( Exception& ex ) {
Expand Down Expand Up @@ -1259,7 +1263,7 @@ ExecutionResult Client::call( Address const& _from, u256 _value, Address _dest,
// geth does a similar thing, we need to check whether it is fully compatible with
// geth
historicBlock.mutableState().mutableHistoricState().addBalance(
_from, ( u256 )( t.gas() * t.gasPrice() + t.value() ) );
_from, ( u256 ) ( t.gas() * t.gasPrice() + t.value() ) );
ret = historicBlock.executeHistoricCall( bc().lastBlockHashes(), t, nullptr, 0 );
} catch ( ... ) {
cwarn << boost::current_exception_diagnostic_information();
Expand All @@ -1283,7 +1287,8 @@ ExecutionResult Client::call( Address const& _from, u256 _value, Address _dest,
t.forceChainId( chainParams().chainID );
t.checkOutExternalGas( ~u256( 0 ) );
if ( _ff == FudgeFactor::Lenient )
temp.mutableState().addBalance( _from, ( u256 )( t.gas() * t.gasPrice() + t.value() ) );
temp.mutableState().addBalance(
_from, ( u256 ) ( t.gas() * t.gasPrice() + t.value() ) );
ret = temp.execute( bc().lastBlockHashes(), t, skale::Permanence::Reverted );
} catch ( InvalidNonce const& in ) {
LOG( m_logger ) << "exception in client call(1):"
Expand Down Expand Up @@ -1312,10 +1317,15 @@ Json::Value Client::traceCall( Address const& _from, u256 _value, Address _to, b

Transaction t = createTransactionForCallOrTraceCall(
_from, _value, _to, _data, gasLimit, _gasPrice, nonce );
// record original t.from balance for trace and then give
// lots of gas to it
auto originalFromBalance = historicBlock.mutableState().balance( _from );
historicBlock.mutableState().mutableHistoricState().addBalance(
_from, ( u256 )( t.gas() * t.gasPrice() + t.value() ) );
_from, ( u256 ) ( t.gas() * t.gasPrice() + t.value() ) );
auto traceOptions = TraceOptions::make( _jsonTraceConfig );
auto tracer = make_shared< AlethStandardTrace >( t, historicBlock.author(), traceOptions, true );
auto tracer =
make_shared< AlethStandardTrace >( t, historicBlock.author(), traceOptions, true );
tracer->setOriginalFromBalance( originalFromBalance );
auto er = historicBlock.executeHistoricCall( bc().lastBlockHashes(), t, tracer, 0 );
return tracer->getJSONResult();
} catch ( ... ) {
Expand Down Expand Up @@ -1365,7 +1375,8 @@ Json::Value Client::traceBlock( BlockNumber _blockNumber, Json::Value const& _js
auto hashString = toHexPrefixed( tx.sha3() );
transactionLog["txHash"] = hashString;
tx.checkOutExternalGas( chainParams().externalGasDifficulty );
auto tracer = std::make_shared< AlethStandardTrace >( tx, historicBlock.author(), traceOptions );
auto tracer =
std::make_shared< AlethStandardTrace >( tx, historicBlock.author(), traceOptions );
auto executionResult =
previousBlock.executeHistoricCall( bc().lastBlockHashes(), tx, tracer, k );
auto result = tracer->getJSONResult();
Expand Down
13 changes: 12 additions & 1 deletion libhistoric/AlethStandardTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,14 @@ AlethStandardTrace::AlethStandardTrace(
{ TraceType::REPLAY_TRACER, m_replayTracePrinter },
{ TraceType::FOUR_BYTE_TRACER, m_fourByteTracePrinter },
{ TraceType::NOOP_TRACER, m_noopTracePrinter } },
m_blockAuthor( _blockAuthor ) {
m_blockAuthor( _blockAuthor ), m_isCall(_isCall) {
// mark from and to accounts as accessed
m_accessedAccounts.insert( m_from );
m_accessedAccounts.insert( m_to );
}
void AlethStandardTrace::setOriginalFromBalance( const u256& _originalFromBalance ) {
m_originalFromBalance = _originalFromBalance;
}

/*
* This function is called by EVM on each instruction
Expand All @@ -254,7 +257,9 @@ void AlethStandardTrace::operator()( uint64_t _counter, uint64_t _pc, Instructio
if (_counter) {
recordMinerPayment(u256(_gasOpGas));
}

recordInstructionIsExecuted( _pc, _inst, _gasOpGas, _gasRemaining, _vm, _ext );

}

// this will be called each time before an instruction is executed by evm
Expand Down Expand Up @@ -440,6 +445,12 @@ void AlethStandardTrace::recordMinerPayment( u256 _minerGasPayment ) {
// transaction fee
this->m_accessedAccounts.insert(m_blockAuthor);
}
bool AlethStandardTrace::isCall() const {
return m_isCall;
}
const u256& AlethStandardTrace::getOriginalFromBalance() const {
return m_originalFromBalance;
}
} // namespace dev::eth

#endif
21 changes: 13 additions & 8 deletions libhistoric/AlethStandardTrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,16 @@ class AlethStandardTrace {
const;
void setTopFunctionCall( const std::shared_ptr< FunctionCallRecord >& _topFunctionCall );

static string toGethCompatibleCompactHexPrefixed(const u256 &_value);
[[nodiscard]] const Address& getBlockAuthor() const;
[[nodiscard]] const u256& getMinerPayment() const;
void setOriginalFromBalance( const u256& _originalFromBalance );

[[nodiscard]] const u256& getOriginalFromBalance() const;

[[nodiscard]] bool isCall() const;


static string toGethCompatibleCompactHexPrefixed( const u256& _value );

private:
// this operator will be executed by skaled on each EVM instruction
Expand Down Expand Up @@ -118,19 +127,14 @@ class AlethStandardTrace {
void processFunctionCallOrReturnIfHappened(
const AlethExtVM& _ext, const LegacyVM* _vm, std::uint64_t _gasRemaining );

public:
const Address& getBlockAuthor() const;
const u256& getMinerPayment() const;

private:
void appendOpToStandardOpTrace( std::uint64_t _pc, Instruction& _inst, const bigint& _gasCost,
const bigint& _gas, const ExtVMFace* _ext, AlethExtVM& _alethExt, const LegacyVM* _vm );

// print all supported traces. This can be used for QA
void printAllTraces( Json::Value& _jsonTrace, ExecutionResult& _er,
const HistoricState& _statePre, const HistoricState& _statePost );

void recordMinerPayment(u256 _minerGasPayment);
void recordMinerPayment( u256 _minerGasPayment );

std::shared_ptr< FunctionCallRecord > m_topFunctionCall;
std::shared_ptr< FunctionCallRecord > m_currentlyExecutingFunctionCall;
Expand Down Expand Up @@ -160,6 +164,7 @@ class AlethStandardTrace {

const Address m_blockAuthor;
u256 m_minerPayment;

u256 m_originalFromBalance;
bool m_isCall;
};
} // namespace dev::eth
8 changes: 8 additions & 0 deletions libhistoric/PrestateTracePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ void PrestateTracePrinter::printAllAccessedAccountPreValues(
// if this _address did not exist, we do not include it in the diff
if ( !_statePre.addressInUse( _address ) )
return;

auto balance = _statePre.balance( _address );

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

storagePreValues["balance"] =
AlethStandardTrace::toGethCompatibleCompactHexPrefixed( _statePre.balance( _address ) );
storagePreValues["nonce"] = ( uint64_t ) _statePre.getNonce( _address );
Expand Down
2 changes: 0 additions & 2 deletions test/historicstate/hardhat/scripts/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,6 @@ async function verifyPrestateTraceAgainstGethTrace(_fileName: string) {
if (address == ZERO_ADDRESS && key == "balance")
return;

if (address == "CALL.address" && key == "balance")
return;
}


Expand Down

0 comments on commit 818c7fe

Please sign in to comment.