Skip to content

Commit

Permalink
IS-879 Remove unneeded checks after patch timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
dimalit committed Nov 2, 2023
1 parent eab517b commit 7ba33e3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 36 deletions.
62 changes: 62 additions & 0 deletions libethereum/BlockChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <libethcore/Exceptions.h>

#include <libskale/AmsterdamFixPatch.h>
#include <libskale/SkipInvalidTransactionsPatch.h>
#include <libskale/TotalStorageUsedPatch.h>

#include "Block.h"
Expand Down Expand Up @@ -172,6 +173,24 @@ unsigned c_maxCacheSize = 1024 * 1024 * 64;
/// Min size, below which we don't bother flushing it.
unsigned c_minCacheSize = 1024 * 1024 * 32;

bool hasPotentialInvalidTransactionsInBlock( BlockNumber _bn, const BlockChain& _bc ) {
if ( _bn == 0 )
return false;

Check warning on line 178 in libethereum/BlockChain.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/BlockChain.cpp#L178

Added line #L178 was not covered by tests

if ( SkipInvalidTransactionsPatch::getActivationTimestamp() == 0 )
return true;

if ( _bn == PendingBlock )
return !SkipInvalidTransactionsPatch::isEnabled();

Check warning on line 184 in libethereum/BlockChain.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/BlockChain.cpp#L184

Added line #L184 was not covered by tests

if ( _bn == LatestBlock )
_bn = _bc.number();

Check warning on line 187 in libethereum/BlockChain.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/BlockChain.cpp#L187

Added line #L187 was not covered by tests

time_t prev_ts = _bc.info( _bc.numberHash( _bn - 1 ) ).timestamp();

return prev_ts < SkipInvalidTransactionsPatch::getActivationTimestamp();
}

string BlockChain::getChainDirName( const ChainParams& _cp ) {
return toHex( BlockHeader( _cp.genesisBlock() ).hash().ref().cropped( 0, 4 ) );
}
Expand Down Expand Up @@ -334,6 +353,49 @@ void BlockChain::close() {
m_lastBlockHashes->clear();
}

std::pair< h256, unsigned > BlockChain::transactionLocation( h256 const& _transactionHash ) const {
// cached transactionAddresses for transactions with gasUsed==0 should be re-queried from DB
bool cached = false;
{
ReadGuard g( x_transactionAddresses );
cached = m_transactionAddresses.count( _transactionHash ) > 0;
}

// get transactionAddresses from DB or cache
TransactionAddress ta = queryExtras< TransactionAddress, ExtraTransactionAddress >(
_transactionHash, m_transactionAddresses, x_transactionAddresses, NullTransactionAddress );

if ( !ta )
return std::pair< h256, unsigned >( h256(), 0 );

Check warning on line 369 in libethereum/BlockChain.cpp

View check run for this annotation

Codecov / codecov/patch

libethereum/BlockChain.cpp#L369

Added line #L369 was not covered by tests

auto blockNumber = this->number( ta.blockHash );

if ( !hasPotentialInvalidTransactionsInBlock( blockNumber, *this ) )
return std::make_pair( ta.blockHash, ta.index );

// rest is for blocks with possibility of invalid transactions

// compute gas used
TransactionReceipt receipt = transactionReceipt( ta.blockHash, ta.index );
u256 cumulativeGasUsed = receipt.cumulativeGasUsed();
u256 prevGasUsed =
ta.index == 0 ? 0 : transactionReceipt( ta.blockHash, ta.index - 1 ).cumulativeGasUsed();
u256 gasUsed = cumulativeGasUsed - prevGasUsed;

// re-query receipt from DB if gasUsed==0 (and cache might have wrong value)
if ( gasUsed == 0 && cached ) {
// remove from cache
{
WriteGuard g( x_transactionAddresses );
m_transactionAddresses.erase( _transactionHash );
}
// re-read from DB
ta = queryExtras< TransactionAddress, ExtraTransactionAddress >( _transactionHash,
m_transactionAddresses, x_transactionAddresses, NullTransactionAddress );
}
return std::make_pair( ta.blockHash, ta.index );
}

string BlockChain::dumpDatabase() const {
ostringstream oss;
oss << m_lastBlockHash << '\n';
Expand Down
37 changes: 1 addition & 36 deletions libethereum/BlockChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,43 +312,8 @@ class BlockChain {
return bytes();
return transaction( ta.blockHash, ta.index );
}
std::pair< h256, unsigned > transactionLocation( h256 const& _transactionHash ) const {
// cached transactionAddresses for transactions with gasUsed==0 should be re-queried from DB
bool cached = false;
{
ReadGuard g( x_transactionAddresses );
cached = m_transactionAddresses.count( _transactionHash ) > 0;
}

// get transactionAddresses from DB or cache
TransactionAddress ta =
queryExtras< TransactionAddress, ExtraTransactionAddress >( _transactionHash,
m_transactionAddresses, x_transactionAddresses, NullTransactionAddress );

if ( !ta )
return std::pair< h256, unsigned >( h256(), 0 );

// compute gas used
TransactionReceipt receipt = transactionReceipt( ta.blockHash, ta.index );
u256 cumulativeGasUsed = receipt.cumulativeGasUsed();
u256 prevGasUsed = ta.index == 0 ?
0 :
transactionReceipt( ta.blockHash, ta.index - 1 ).cumulativeGasUsed();
u256 gasUsed = cumulativeGasUsed - prevGasUsed;

// re-query receipt from DB if gasUsed==0 (and cache might have wrong value)
if ( gasUsed == 0 && cached ) {
// remove from cache
{
WriteGuard g( x_transactionAddresses );
m_transactionAddresses.erase( _transactionHash );
}
// re-read from DB
ta = queryExtras< TransactionAddress, ExtraTransactionAddress >( _transactionHash,
m_transactionAddresses, x_transactionAddresses, NullTransactionAddress );
}
return std::make_pair( ta.blockHash, ta.index );
}
std::pair< h256, unsigned > transactionLocation( h256 const& _transactionHash ) const;

/// Get a block's transaction (RLP format) for the given block hash (or the most recent mined if
/// none given) & index. Thread-safe.
Expand Down

0 comments on commit 7ba33e3

Please sign in to comment.