Skip to content

Commit

Permalink
SKALED-1719 Use separate function to extract transaction bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
dimalit committed Apr 17, 2024
1 parent bc9cd39 commit 1815ebf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 49 deletions.
23 changes: 15 additions & 8 deletions libethcore/TransactionBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ TransactionBase::TransactionBase( TransactionSkeleton const& _ts, Secret const&
sign( _s );
}

void TransactionBase::fillFromRlpLegacy(
void TransactionBase::fillFromBytesLegacy(
bytesConstRef _rlpData, CheckTransaction _checkSig, bool _allowInvalid ) {
RLP const rlp( _rlpData );
try {
Expand Down Expand Up @@ -169,7 +169,7 @@ void TransactionBase::fillFromRlpLegacy(
}
}

void TransactionBase::fillFromRlpType1(
void TransactionBase::fillFromBytesType1(
bytesConstRef _rlpData, CheckTransaction _checkSig, bool _allowInvalid ) {
bytes croppedRlp( _rlpData.begin() + 1, _rlpData.end() );
RLP const rlp( croppedRlp );
Expand Down Expand Up @@ -230,7 +230,7 @@ void TransactionBase::fillFromRlpType1(
}
}

void TransactionBase::fillFromRlpType2(
void TransactionBase::fillFromBytesType2(
bytesConstRef _rlpData, CheckTransaction _checkSig, bool _allowInvalid ) {
bytes croppedRlp( _rlpData.begin() + 1, _rlpData.end() );
RLP const rlp( croppedRlp );
Expand Down Expand Up @@ -294,17 +294,17 @@ void TransactionBase::fillFromRlpType2(
}
}

void TransactionBase::fillFromRlpByType(
void TransactionBase::fillFromBytesByType(
bytesConstRef _rlpData, CheckTransaction _checkSig, bool _allowInvalid, TransactionType type ) {
switch ( type ) {
case TransactionType::Legacy:
fillFromRlpLegacy( _rlpData, _checkSig, _allowInvalid );
fillFromBytesLegacy( _rlpData, _checkSig, _allowInvalid );
break;
case TransactionType::Type1:
fillFromRlpType1( _rlpData, _checkSig, _allowInvalid );
fillFromBytesType1( _rlpData, _checkSig, _allowInvalid );
break;
case TransactionType::Type2:
fillFromRlpType2( _rlpData, _checkSig, _allowInvalid );
fillFromBytesType2( _rlpData, _checkSig, _allowInvalid );
break;
default:
BOOST_THROW_EXCEPTION(
Expand All @@ -326,7 +326,7 @@ TransactionBase::TransactionBase(
MICROPROFILE_SCOPEI( "TransactionBase", "ctor", MP_GOLD2 );
try {
TransactionType txnType = getTransactionType( _rlpData );
fillFromRlpByType( _rlpData, _checkSig, _allowInvalid, txnType );
fillFromBytesByType( _rlpData, _checkSig, _allowInvalid, txnType );
} catch ( ... ) {
m_type = Type::Invalid;
RLPStream s;
Expand Down Expand Up @@ -539,3 +539,10 @@ u256 TransactionBase::gas() const {
u256 TransactionBase::nonPowGas() const {
return m_gas;
}

bytesConstRef dev::eth::bytesRefFromTransactionRlp( const RLP& _rlp ) {
if ( _rlp.isList() )
return _rlp.data();
else
return _rlp.payload();
}
14 changes: 9 additions & 5 deletions libethcore/TransactionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,14 @@ class TransactionBase {
static TransactionType getTransactionType( bytesConstRef _rlp );

/// Constructs a transaction from the given RLP and transaction type.
void fillFromRlpByType( bytesConstRef _rlpData, CheckTransaction _checkSig, bool _allowInvalid,
TransactionType type );
void fillFromRlpLegacy(
void fillFromBytesByType( bytesConstRef _rlpData, CheckTransaction _checkSig,
bool _allowInvalid, TransactionType type );
void fillFromBytesLegacy(
bytesConstRef _rlpData, CheckTransaction _checkSig, bool _allowInvalid );
void fillFromBytesType1(
bytesConstRef _rlpData, CheckTransaction _checkSig, bool _allowInvalid );
void fillFromBytesType2(
bytesConstRef _rlpData, CheckTransaction _checkSig, bool _allowInvalid );
void fillFromRlpType1( bytesConstRef _rlpData, CheckTransaction _checkSig, bool _allowInvalid );
void fillFromRlpType2( bytesConstRef _rlpData, CheckTransaction _checkSig, bool _allowInvalid );

void streamLegacyTransaction( RLPStream& _s, IncludeSignature _sig, bool _forEip155hash ) const;
void streamType1Transaction( RLPStream& _s, IncludeSignature _sig ) const;
Expand Down Expand Up @@ -356,5 +358,7 @@ inline std::ostream& operator<<( std::ostream& _out, TransactionBase const& _t )
return _out;
}

extern bytesConstRef bytesRefFromTransactionRlp( const RLP& _rlp );

} // namespace eth
} // namespace dev
22 changes: 7 additions & 15 deletions libethereum/BlockChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ using skale::BaseState;
using skale::State;
using namespace skale::error;

extern bytesConstRef bytesRefFromTransactionRlp( const RLP& _rlp );

#define ETH_TIMED_IMPORTS 1

namespace {
Expand Down Expand Up @@ -777,15 +779,10 @@ size_t BlockChain::prepareDbDataAndReturnSize( VerifiedBlockRef const& _block,
for ( RLP::iterator it = txns_rlp.begin(); it != txns_rlp.end(); ++it ) {
MICROPROFILE_SCOPEI( "insertBlockAndExtras", "for2", MP_HONEYDEW );

if ( RLP( *it ).isList() )
// means Legacy transaction
extrasWriteBatch.insert( toSlice( sha3( ( *it ).data() ), ExtraTransactionAddress ),
( db::Slice ) dev::ref( ta.rlp() ) );
else {
auto payload = ( *it ).payload();
extrasWriteBatch.insert( toSlice( sha3( payload ), ExtraTransactionAddress ),
( db::Slice ) dev::ref( ta.rlp() ) );
}
auto txBytes = bytesRefFromTransactionRlp( *it );
extrasWriteBatch.insert( toSlice( sha3( txBytes ), ExtraTransactionAddress ),
( db::Slice ) dev::ref( ta.rlp() ) );

++ta.index;
}
}
Expand Down Expand Up @@ -1754,12 +1751,7 @@ VerifiedBlockRef BlockChain::verifyBlock( bytesConstRef _block,
( ImportRequirements::TransactionBasic | ImportRequirements::TransactionSignatures ) ) {
MICROPROFILE_SCOPEI( "BlockChain", "check txns", MP_ROSYBROWN );
for ( RLP const& tr : r[1] ) {
bytesConstRef d;
if ( tr.isList() )
// means Legacy transaction
d = tr.data();
else
d = tr.payload();
bytesConstRef d = bytesRefFromTransactionRlp( tr );
try {
Transaction t( d, ( _ir & ImportRequirements::TransactionSignatures ) ?
CheckTransaction::Everything :
Expand Down
20 changes: 4 additions & 16 deletions libethereum/BlockChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,8 @@ class BlockChain {
auto b = block( _hash );
RLP rlp( b );
h256s ret;
for ( auto d : rlp[1] ) {
if ( d.isList() )
// means Legacy transaction
ret.push_back( sha3( d.data() ) );
else
ret.push_back( sha3( d.payload() ) );
}
for ( auto d : rlp[1] )
ret.push_back( sha3( bytesRefFromTransactionRlp( d ) ) );
return ret;
}
TransactionHashes transactionHashes() const { return transactionHashes( currentHash() ); }
Expand Down Expand Up @@ -324,10 +319,7 @@ class BlockChain {
/// none given) & index. Thread-safe.
bytes transaction( h256 const& _blockHash, unsigned _i ) const {
bytes b = block( _blockHash );
if ( RLP( b )[1][_i].isList() )
// means Legacy transaction
return RLP( b )[1][_i].data().toBytes();
return RLP( b )[1][_i].payload().toBytes();
return bytesRefFromTransactionRlp( RLP( b )[1][_i] ).toBytes();
}
bytes transaction( unsigned _i ) const { return transaction( currentHash(), _i ); }

Expand All @@ -336,11 +328,7 @@ class BlockChain {
bytes b = block( _blockHash );
std::vector< bytes > ret;
for ( auto const& i : RLP( b )[1] )
if ( i.isList() )
// means Legacy transaction
ret.push_back( i.data().toBytes() );
else
ret.push_back( i.payload().toBytes() );
ret.push_back( bytesRefFromTransactionRlp( i ).toBytes() );
return ret;
}
std::vector< bytes > transactions() const { return transactions( currentHash() ); }
Expand Down
6 changes: 1 addition & 5 deletions libethereum/ClientBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,7 @@ Transactions ClientBase::transactions( h256 _blockHash ) const {
Transactions res;
for ( unsigned i = 0; i < b[1].itemCount(); i++ ) {
auto txRlp = b[1][i];
if ( txRlp.isList() )
// means Legacy transaction
res.emplace_back( txRlp.data(), CheckTransaction::Cheap, true );
else
res.emplace_back( txRlp.payload(), CheckTransaction::Cheap, true );
res.emplace_back( bytesRefFromTransactionRlp( txRlp ), CheckTransaction::Cheap, true );
}
return res;
}
Expand Down

0 comments on commit 1815ebf

Please sign in to comment.