Skip to content
24 changes: 24 additions & 0 deletions libethereum/Executive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,14 @@ bool Executive::finalize() {
feesEarned, currentBlockSchedule.shareOfTransactionFeeToRewardPromille );
#endif
m_s.addBalance( m_envInfo.author(), feesEarned );

#ifdef BITE
// no need to put it under Bite2Patch - if isCTX() is true, then Bite2Patch must have been
// enabled, since CTXs are only supported in Bite2Patch
if ( m_t.isCTX() && RefundCTXPatch::isEnabledWhen( m_envInfo.committedBlockTimestamp() ) ) {
refundCTXAndResetEphemeralSenderNonce();
}
#endif
}

// Suicides...
Expand Down Expand Up @@ -762,3 +770,19 @@ void Executive::revert() {
// Warmings added by opcodes inside this frame (SLOAD, CALL, etc.) are discarded.
*m_accessSets = m_accessSetsSnapshot;
}


#ifdef BITE
void Executive::refundCTXAndResetEphemeralSenderNonce() {
// Move leftover balance in ephemeral account back to target contract balance
// Reset ephemeral account nonce to 0 - makes the account empty so its pruned
// at comit.
Address ephemeral = m_t.sender();
Comment thread
PropzSaladaz marked this conversation as resolved.
Address targetContract = m_t.decryptedTo();
u256 ephemeralBalance = m_s.balance( ephemeral );
if ( ephemeralBalance > 0 ) {
m_s.transferBalance( ephemeral, targetContract, ephemeralBalance );
}
m_s.setNonce( ephemeral, 0 );
}
#endif
12 changes: 12 additions & 0 deletions libethereum/Executive.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,18 @@ class Executive {
Logger m_loggerDebug{ createLogger( VerbosityDebug, "Executive" ) };
Logger m_loggerTrace{ createLogger( VerbosityTrace, "Executive" ) };
Logger m_loggerWarning{ createLogger( VerbosityWarning, "Executive" ) };

#ifdef BITE
/**
* @brief Refund unused gas by CTX transactions to smart contract balance
* that originated it ( `.to` field of CTX). Also sets nonce of ephemeral
* CTX sender to 0.
* This is done to avoid locking user funds in ephemeral CTX accounts,
* as well as keeping storage of those accounts clean.
* Valid under Bite2Patch
*/
void refundCTXAndResetEphemeralSenderNonce();
#endif
};

} // namespace eth
Expand Down
4 changes: 4 additions & 0 deletions libethereum/SchainPatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ SchainPatchEnum getEnumForPatchName( const std::string& _patchName ) {
#ifdef BITE
else if ( _patchName == "BITE2Patch" || _patchName == "Bite2Patch" )
return SchainPatchEnum::Bite2Patch;
else if ( _patchName == "RefundCTXPatch" )
return SchainPatchEnum::RefundCTXPatch;
#endif // BITE
#ifdef FAIR
else if ( _patchName == "DisableSelfDestructPatch" )
Expand Down Expand Up @@ -113,6 +115,8 @@ std::string getPatchNameForEnum( SchainPatchEnum _enumValue ) {
#ifdef BITE
case SchainPatchEnum::Bite2Patch:
return "Bite2Patch";
case SchainPatchEnum::RefundCTXPatch:
return "RefundCTXPatch";
#endif // BITE
#ifdef FAIR
case SchainPatchEnum::DisableSelfDestructPatch:
Expand Down
9 changes: 9 additions & 0 deletions libethereum/SchainPatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ DEFINE_AMNESIC_PATCH( GroupIndexInitPatch );
* return a revert and CTX status is not set in TransactionBase constructor.
*/
DEFINE_SIMPLE_PATCH( Bite2Patch );

/*
* Purpose: Refund unused gas by CTX transactions to smart contract balance
* that originated it ( `.to` field of CTX).
* This is done to avoid locking user funds in ephemeral CTX accounts,
* as well as keeping storage of those accounts clean.
* Valid under Bite2Patch
*/
DEFINE_SIMPLE_PATCH( RefundCTXPatch );
#endif // BITE

/*
Expand Down
1 change: 1 addition & 0 deletions libethereum/SchainPatchEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enum class SchainPatchEnum {
ContractCreationReadOnlyPatch,
#ifdef BITE
Bite2Patch,
RefundCTXPatch,
#endif // BITE
#ifdef FAIR
DisableSelfDestructPatch,
Expand Down
12 changes: 12 additions & 0 deletions libethereum/SkaleHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,18 @@ std::vector< Transaction > SkaleHost::processRegularTransactions(
#endif // BITE
);
#ifdef BITE

// This check is a hardenning security added under refund CTX, since this patch introduces
// destructive actions for all `ctx.from` addresses that issued the transaction (ephemeral
// account) - ephemeral accounts are deleted. We must be sure no regular tx issued by a user
// can ever be interpreted as CTX
Comment thread
PropzSaladaz marked this conversation as resolved.
Outdated
if ( Bite2Patch::isEnabledInWorkingBlock() && RefundCTXPatch::isEnabledInWorkingBlock() &&
t.isCTX() ) {
BOOST_THROW_EXCEPTION(
IllegalCTXSubmission() << errinfo_comment(
"Illegal attempt to include CTX in regular transactions list" ) );
}

if ( regularTxnsIterator != _decryptedTransactions.regularTxsMap->end() &&
regularTxnsIterator->first == i ) {
std::optional< DecryptedRegularTxFields > txFields = regularTxnsIterator->second;
Expand Down
24 changes: 24 additions & 0 deletions libhistoric/AlethExecutive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "libethereum/Block.h"
#include "libethereum/BlockChain.h"
#include "libethereum/Interface.h"
#include "libethereum/SchainPatch.h"
#include "libevm/LegacyVM.h"
#include "libevm/VMFactory.h"
#include <libethashseal/Ethash.h>
Expand Down Expand Up @@ -424,6 +425,14 @@ bool AlethExecutive::finalize() {

u256 feesEarned = ( m_t.gas() - m_gas ) * m_t.gasPrice();
m_s.addBalance( m_envInfo.author(), feesEarned );

#ifdef BITE
// no need to put it under Bite2Patch - if isCTX() is true, then Bite2Patch must have been
// enabled, since CTXs are only supported in Bite2Patch
if ( m_t.isCTX() && RefundCTXPatch::isEnabledWhen( m_envInfo.committedBlockTimestamp() ) ) {
refundCTXAndResetEphemeralSenderNonce();
}
#endif
}

// Selfdestructs...
Expand Down Expand Up @@ -453,3 +462,18 @@ void AlethExecutive::revert() {
m_newAddress = {};
m_s.rollback( m_savepoint );
}

#ifdef BITE
void AlethExecutive::refundCTXAndResetEphemeralSenderNonce() {
// Move leftover balance in ephemeral account back to target contract balance
// Reset ephemeral account nonce to 0 - makes the account empty so its pruned
// at comit.
Comment thread
PropzSaladaz marked this conversation as resolved.
Outdated
Address ephemeral = m_t.sender();
Address targetContract = m_t.decryptedTo();
u256 ephemeralBalance = m_s.balance( ephemeral );
if ( ephemeralBalance > 0 ) {
m_s.transferBalance( ephemeral, targetContract, ephemeralBalance );
}
m_s.setNonce( ephemeral, 0 );
}
#endif
12 changes: 12 additions & 0 deletions libhistoric/AlethExecutive.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ class AlethExecutive {
Logger m_loggerTrace{ createLogger( VerbosityTrace, "AlethExecutive" ) };
Logger m_loggerError{ createLogger( VerbosityError, "AlethExecutive" ) };
Logger m_loggerInfo{ createLogger( VerbosityInfo, "AlethExecutive" ) };

#ifdef BITE
/**
* @brief Refund unused gas by CTX transactions to smart contract balance
* that originated it ( `.to` field of CTX). Also sets nonce of ephemeral
* CTX sender to 0.
* This is done to avoid locking user funds in ephemeral CTX accounts,
* as well as keeping storage of those accounts clean.
* Valid under Bite2Patch
*/
void refundCTXAndResetEphemeralSenderNonce();
#endif
};

} // namespace eth
Expand Down
Loading
Loading