Skip to content

Commit

Permalink
Merge pull request #2925 from Taraxa-project/intrinsic_gas_check
Browse files Browse the repository at this point in the history
feat: perform intrinsic gas check on trx insertion
  • Loading branch information
MatusKysel authored Jan 9, 2025
2 parents a6fd0c9 + 60155b8 commit 5be5b6d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,7 @@
},
"sortition": {
"changes_count_for_average": 10,
"dag_efficiency_targets": [
4900,
5100
],
"dag_efficiency_targets": [4900, 5100],
"changing_interval": 200,
"computation_interval": 50,
"vrf": {
Expand Down Expand Up @@ -286,10 +283,10 @@
"bridge_contract_address": "0xcAF2b453FE8382a4B8110356DF0508f6d71F22BF"
},
"cornus_hf": {
"block_num": 0,
"block_num": 1000,
"delegation_locking_period": 5,
"dag_gas_limit": "0x1908B100",
"pbft_gas_limit": "0x7d2b7500"
}
}
}
}
5 changes: 5 additions & 0 deletions libraries/common/include/common/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const size_t kV3NetworkVersion = 3;

const uint32_t kRecentlyFinalizedTransactionsFactor = 2;

constexpr uint64_t kTxGas = 21000;
constexpr uint64_t kTxGasContractCreation = 53000;
constexpr uint64_t kTxDataZeroGas = 4;
constexpr uint64_t kTxDataNonZeroGas = 68;

// The various denominations; here for ease of use where needed within code.
static const u256 kOneTara = dev::exp10<18>();
// static const u256 kFinney = exp10<15>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ std::pair<bool, std::string> TransactionManager::verifyTransaction(const std::sh
return {false, "invalid gas"};
}

if (kConf.genesis.state.hardforks.isOnCornusHardfork(this->final_chain_->lastBlockNumber())) {
if (!trx->intrinsicGasCovered()) {
return {false, "intrinsic gas too low"};
}
}

try {
trx->getSender();
} catch (Transaction::InvalidSignature const &) {
Expand All @@ -73,6 +79,7 @@ std::pair<bool, std::string> TransactionManager::verifyTransaction(const std::sh
return {false, "gas_price too low"};
}


return {true, ""};
}

Expand Down
2 changes: 2 additions & 0 deletions libraries/core_libs/network/rpc/jsonrpc_ws_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ std::string JsonRpcWsSession::handleSubscription(const Json::Value &req) {
if (params.size() == 2 && params[1].asString() == "includeSignatures") {
include_pillar_block_signatures = true;
}
} else {
throw std::runtime_error("Invalid subscription type");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct Transaction {
explicit Transaction(const bytes &_rlp, bool verify_strict = false, const h256 &hash = {});
virtual ~Transaction() = default;

bool intrinsicGasCovered() const;
auto isZero() const { return is_zero_; }
const trx_hash_t &getHash() const;
auto getNonce() const { return nonce_; }
Expand Down Expand Up @@ -85,6 +86,7 @@ using Transactions = std::vector<Transaction>;
using SharedTransactions = std::vector<SharedTransaction>;
using TransactionHashes = std::vector<trx_hash_t>;

uint64_t IntrinsicGas(const std::vector<uint8_t> &data, bool is_contract_creation);
TransactionHashes hashes_from_transactions(const SharedTransactions &transactions);

} // namespace taraxa
37 changes: 37 additions & 0 deletions libraries/types/transaction/src/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include <utility>

#include "common/constants.hpp"
#include "common/encoding_rlp.hpp"

namespace taraxa {
Expand Down Expand Up @@ -178,4 +179,40 @@ void Transaction::rlp(::taraxa::util::RLPDecoderRef encoding) { fromRLP(encoding

void Transaction::rlp(::taraxa::util::RLPEncoderRef encoding) const { encoding.appendRaw(rlp()); }

inline uint64_t IntrinsicGas(const std::vector<uint8_t> &data, bool is_contract_creation) {
uint64_t gas;
if (is_contract_creation) {
gas = kTxGasContractCreation;
} else {
gas = kTxGas;
}
// Bump the required gas by the amount of transactional data
if (!data.empty()) {
// Zero and non-zero bytes are priced differently
uint64_t nz = std::count_if(data.begin(), data.end(), [](uint8_t b) { return b != 0; });

// Make sure we don't exceed uint64 for all data combinations
if ((std::numeric_limits<uint64_t>::max() - gas) / kTxDataNonZeroGas < nz) {
throw std::runtime_error("Out of gas");
}
gas += nz * kTxDataNonZeroGas;

uint64_t z = static_cast<uint64_t>(data.size()) - nz;
if ((std::numeric_limits<uint64_t>::max() - gas) / kTxDataZeroGas < z) {
throw std::runtime_error("Out of gas");
}
gas += z * kTxDataZeroGas;
}
return gas;
}

bool Transaction::intrinsicGasCovered() const {
try {
uint64_t gas = IntrinsicGas(data_, !receiver_.has_value());
return gas <= gas_;
} catch (const std::runtime_error&) {
return false;
}
}

} // namespace taraxa
11 changes: 11 additions & 0 deletions tests/transaction_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,17 @@ TEST_F(TransactionTest, signature_performance) {
<< "ms" << std::endl;
}

TEST_F(TransactionTest, intrinsic_gas) {
EXPECT_EQ(IntrinsicGas(dev::bytes(), false), kTxGas);
EXPECT_EQ(IntrinsicGas(dev::bytes(), true), kTxGasContractCreation);
const auto data = dev::bytes(100000, 1);
EXPECT_EQ(IntrinsicGas(data, false), kTxGas + 100000 * kTxDataNonZeroGas);
EXPECT_EQ(IntrinsicGas(data, true), kTxGasContractCreation + 100000 * kTxDataNonZeroGas);
const auto data2 = dev::bytes(100000, 0);
EXPECT_EQ(IntrinsicGas(data2, false), kTxGas + 100000 * kTxDataZeroGas);
EXPECT_EQ(IntrinsicGas(data2, true), kTxGasContractCreation + 100000 * kTxDataZeroGas);
}

} // namespace taraxa::core_tests

using namespace taraxa;
Expand Down

0 comments on commit 5be5b6d

Please sign in to comment.