Skip to content

Commit

Permalink
TxnId
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr committed Oct 14, 2024
1 parent 39d7bb1 commit adcb8bd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
16 changes: 15 additions & 1 deletion silkworm/core/common/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,20 @@ template <class T>
concept UnsignedIntegral = std::unsigned_integral<T> || std::same_as<T, intx::uint128> ||
std::same_as<T, intx::uint256> || std::same_as<T, intx::uint512>;

using TxnId = uint64_t;

struct TxnIdRange {
TxnId start;
TxnId end;
TxnIdRange(TxnId start1, TxnId end1) : start(start1), end(end1) {}
friend bool operator==(const TxnIdRange&, const TxnIdRange&) = default;
bool contains(TxnId num) const { return (start <= num) && (num < end); }
bool contains_range(TxnIdRange range) const { return (start <= range.start) && (range.end <= end); }
TxnId size() const { return end - start; }
std::string to_string() const { return std::string("[") + std::to_string(start) + ", " + std::to_string(end) + ")"; }
};

using BlockNum = uint64_t;
using BlockTime = uint64_t;

struct BlockNumRange {
BlockNum start;
Expand All @@ -56,6 +68,8 @@ struct BlockNumRange {
std::string to_string() const { return std::string("[") + std::to_string(start) + ", " + std::to_string(end) + ")"; }
};

using BlockTime = uint64_t;

inline constexpr BlockNum kEarliestBlockNumber{0ul};

inline constexpr size_t kAddressLength{20};
Expand Down
10 changes: 4 additions & 6 deletions silkworm/db/datastore/snapshots/step.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ struct Step {
return Step{static_cast<size_t>(block_num / kStepSizeForBlockSnapshots)};
}

uint64_t to_txn_id() const { return value * kStepSizeForTemporalSnapshots; }
static Step from_txn_id(uint64_t txn_id) {
TxnId to_txn_id() const { return value * kStepSizeForTemporalSnapshots; }
static Step from_txn_id(TxnId txn_id) {
return Step{static_cast<size_t>(txn_id / kStepSizeForTemporalSnapshots)};
}
};
Expand All @@ -65,10 +65,8 @@ struct StepRange {
return {Step::from_block_num(range.start), Step::from_block_num(range.end + kStepSizeForBlockSnapshots - 1)};
}

// TODO: fix range type to txn range
BlockNumRange to_txn_id_range() const { return {start.to_txn_id(), end.to_txn_id()}; }
// TODO: fix range type to txn range
static StepRange from_txn_id_range(BlockNumRange range) {
TxnIdRange to_txn_id_range() const { return {start.to_txn_id(), end.to_txn_id()}; }
static StepRange from_txn_id_range(TxnIdRange range) {
return {Step::from_txn_id(range.start), Step::from_txn_id(range.end + kStepSizeForTemporalSnapshots - 1)};
}
};
Expand Down
2 changes: 2 additions & 0 deletions silkworm/db/kv/api/endpoint/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

namespace silkworm::db::kv::api {

//! Database meta-transaction ID (a transaction over mdbx and snapshots)
using TxId = uint64_t;

using Timestamp = int64_t;
using TimestampRange = std::pair<Timestamp, Timestamp>;

Expand Down
4 changes: 3 additions & 1 deletion silkworm/db/kv/txn_num.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@

#include <silkworm/infra/concurrency/task.hpp>

#include <silkworm/core/common/base.hpp>

#include "../kv/api/transaction.hpp"

namespace silkworm::db::txn {

//! TxNum represents the monotonically increasing unique numbering of blockchain transactions in range [0, inf)
//! TxNum is contiguous (no holes) and canonical, i.e. universal among all client nodes
//! \see txnum.go in Erigon
using TxNum = uint64_t;
using TxNum = TxnId;

//! Return the maximum TxNum in specified \code block_number
Task<TxNum> max_tx_num(kv::api::Transaction& tx, BlockNum block_number);
Expand Down

0 comments on commit adcb8bd

Please sign in to comment.