Skip to content

Commit 6fa1f26

Browse files
committed
datastore: .ef v2 format
1 parent d67417a commit 6fa1f26

38 files changed

+465
-191
lines changed

silkworm/capi/cli/execute.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <silkworm/capi/silkworm.h>
2020
#include <silkworm/db/access_layer.hpp>
21+
#include <silkworm/db/blocks/step_block_num_converter.hpp>
2122
#include <silkworm/db/datastore/kvdb/mdbx.hpp>
2223
#include <silkworm/db/datastore/snapshots/snapshot_repository.hpp>
2324
#include <silkworm/infra/cli/common.hpp>
@@ -319,7 +320,7 @@ int build_indexes(SilkwormHandle handle, const BuildIndexesSettings& settings, c
319320
if (!snapshot_path.has_value())
320321
throw std::runtime_error("Invalid snapshot path");
321322

322-
segment::SegmentFileReader& segment = segments.emplace_back(*snapshot_path);
323+
segment::SegmentFileReader& segment = segments.emplace_back(*snapshot_path, db::blocks::kStepToBlockNumConverter);
323324

324325
auto mmf = new SilkwormMemoryMappedFile{
325326
.file_path = make_path(*snapshot_path),

silkworm/capi/silkworm_test.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <silkworm/db/datastore/kvdb/mdbx.hpp>
1818
#include <silkworm/db/datastore/snapshots/schema.hpp>
1919
#include <silkworm/db/datastore/snapshots/segment/segment_reader.hpp>
20+
#include <silkworm/db/state/step_txn_id_converter.hpp>
2021
#include <silkworm/db/test_util/temp_snapshots.hpp>
2122
#include <silkworm/db/test_util/test_database_context.hpp>
2223
#include <silkworm/infra/common/directories.hpp>
@@ -813,6 +814,8 @@ TEST_CASE_METHOD(CApiTest, "CAPI silkworm_execute_blocks_perpetual multiple bloc
813814
}
814815

815816
TEST_CASE_METHOD(CApiTest, "CAPI silkworm_add_blocks_snapshot_bundle", "[capi]") {
817+
static constexpr datastore::StepToTimestampConverter kStepConverter = db::blocks::kStepToBlockNumConverter;
818+
816819
snapshot_test::SampleHeaderSnapshotFile header_segment_file{tmp_dir.path()};
817820
auto& header_segment_path = header_segment_file.path();
818821
snapshot_test::SampleBodySnapshotFile body_segment_file{tmp_dir.path()};
@@ -823,20 +826,20 @@ TEST_CASE_METHOD(CApiTest, "CAPI silkworm_add_blocks_snapshot_bundle", "[capi]")
823826
auto header_index_builder = snapshots::HeaderIndex::make(header_segment_path);
824827
header_index_builder.set_base_data_id(header_segment_file.block_num_range().start);
825828
REQUIRE_NOTHROW(header_index_builder.build());
826-
snapshots::segment::SegmentFileReader header_segment{header_segment_path};
829+
snapshots::segment::SegmentFileReader header_segment{header_segment_path, kStepConverter};
827830
snapshots::rec_split::AccessorIndex idx_header_hash{header_segment_path.related_path_ext(db::blocks::kIdxExtension)};
828831

829832
auto body_index_builder = snapshots::BodyIndex::make(body_segment_path);
830833
body_index_builder.set_base_data_id(body_segment_file.block_num_range().start);
831834
REQUIRE_NOTHROW(body_index_builder.build());
832-
snapshots::segment::SegmentFileReader body_segment{body_segment_path};
835+
snapshots::segment::SegmentFileReader body_segment{body_segment_path, kStepConverter};
833836
snapshots::rec_split::AccessorIndex idx_body_number{body_segment_path.related_path_ext(db::blocks::kIdxExtension)};
834837

835838
auto tx_index_builder = snapshots::TransactionIndex::make(body_segment_path, txn_segment_path);
836839
tx_index_builder.build();
837840
auto tx_index_hash_to_block_builder = snapshots::TransactionToBlockIndex::make(body_segment_path, txn_segment_path, txn_segment_file.block_num_range().start);
838841
tx_index_hash_to_block_builder.build();
839-
snapshots::segment::SegmentFileReader txn_segment{txn_segment_path};
842+
snapshots::segment::SegmentFileReader txn_segment{txn_segment_path, kStepConverter};
840843
snapshots::rec_split::AccessorIndex idx_txn_hash{txn_segment_path.related_path_ext(db::blocks::kIdxExtension)};
841844
snapshots::rec_split::AccessorIndex idx_txn_hash_2_block{tx_index_hash_to_block_builder.path()};
842845

@@ -968,7 +971,7 @@ TEST_CASE_METHOD(CApiTest, "CAPI silkworm_add_state_snapshot", "[capi]") {
968971
constexpr uint32_t kZeroSalt{0};
969972

970973
const snapshot_test::SampleAccountsDomainSegmentFile kv_segment_file{tmp_dir.path()};
971-
segment::KVSegmentFileReader kv_segment{kv_segment_file.path(), seg::CompressionKind::kAll};
974+
segment::KVSegmentFileReader kv_segment{kv_segment_file.path(), db::state::kStepToTxnIdConverter, seg::CompressionKind::kAll};
972975
const auto kv_segment_path_string{kv_segment_file.path().path().string()};
973976

974977
const snapshot_test::SampleAccountsDomainExistenceIndexFile existence_index_file{tmp_dir.path()};

silkworm/capi/snapshots.cpp

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ static SnapshotPath parse_snapshot_path(const char* file_path) {
4141
static void build_inverted_index_bundle_data(
4242
const SilkwormInvertedIndexSnapshot& snapshot,
4343
const Schema::EntityDef& entity_def,
44+
const datastore::StepToTimestampConverter& step_converter,
4445
SnapshotBundleEntityData& data) {
4546
data.kv_segments.emplace(
4647
Schema::kInvIdxKVSegmentName,
4748
segment::KVSegmentFileReader{
4849
parse_snapshot_path(snapshot.segment.file_path),
50+
step_converter,
4951
entity_def.kv_segment(Schema::kInvIdxKVSegmentName).compression_kind(),
5052
make_region(snapshot.segment),
5153
});
@@ -59,21 +61,24 @@ static void build_inverted_index_bundle_data(
5961

6062
static snapshots::SnapshotBundleEntityData build_inverted_index_bundle_data(
6163
const SilkwormInvertedIndexSnapshot& snapshot,
62-
const Schema::EntityDef& entity_def) {
64+
const Schema::EntityDef& entity_def,
65+
const datastore::StepToTimestampConverter& step_converter) {
6366
SnapshotBundleEntityData data;
64-
build_inverted_index_bundle_data(snapshot, entity_def, data);
67+
build_inverted_index_bundle_data(snapshot, entity_def, step_converter, data);
6568
return data;
6669
}
6770

6871
static snapshots::SnapshotBundleEntityData build_domain_bundle_data(
6972
const SilkwormDomainSnapshot& snapshot,
7073
const Schema::EntityDef& entity_def,
74+
const datastore::StepToTimestampConverter& step_converter,
7175
uint32_t index_salt) {
7276
SnapshotBundleEntityData data;
7377
data.kv_segments.emplace(
7478
Schema::kDomainKVSegmentName,
7579
segment::KVSegmentFileReader{
7680
parse_snapshot_path(snapshot.segment.file_path),
81+
step_converter,
7782
entity_def.kv_segment(Schema::kDomainKVSegmentName).compression_kind(),
7883
make_region(snapshot.segment),
7984
});
@@ -102,12 +107,14 @@ static snapshots::SnapshotBundleEntityData build_domain_bundle_data(
102107

103108
static snapshots::SnapshotBundleEntityData build_history_bundle_data(
104109
const SilkwormHistorySnapshot& snapshot,
105-
const Schema::EntityDef& entity_def) {
110+
const Schema::EntityDef& entity_def,
111+
const datastore::StepToTimestampConverter& step_converter) {
106112
SnapshotBundleEntityData data;
107113
data.segments.emplace(
108114
Schema::kHistorySegmentName,
109115
segment::SegmentFileReader{
110116
parse_snapshot_path(snapshot.segment.file_path),
117+
step_converter,
111118
make_region(snapshot.segment),
112119
});
113120
data.accessor_indexes.emplace(
@@ -117,7 +124,7 @@ static snapshots::SnapshotBundleEntityData build_history_bundle_data(
117124
make_region(snapshot.accessor_index),
118125
});
119126

120-
build_inverted_index_bundle_data(snapshot.inverted_index, entity_def, data);
127+
build_inverted_index_bundle_data(snapshot.inverted_index, entity_def, step_converter, data);
121128
return data;
122129
}
123130

@@ -126,22 +133,24 @@ static snapshots::SnapshotBundle build_state_snapshot_bundle_latest(
126133
const Schema::RepositoryDef& schema,
127134
uint32_t salt) {
128135
SnapshotBundleData bundle_data;
136+
datastore::StepToTimestampConverter step_converter = schema.make_step_converter();
137+
129138
bundle_data.entities.emplace(
130139
db::state::kDomainNameAccounts,
131-
build_domain_bundle_data(bundle->accounts, schema.domain(db::state::kDomainNameAccounts), salt));
140+
build_domain_bundle_data(bundle->accounts, schema.domain(db::state::kDomainNameAccounts), step_converter, salt));
132141
bundle_data.entities.emplace(
133142
db::state::kDomainNameStorage,
134-
build_domain_bundle_data(bundle->storage, schema.domain(db::state::kDomainNameStorage), salt));
143+
build_domain_bundle_data(bundle->storage, schema.domain(db::state::kDomainNameStorage), step_converter, salt));
135144
bundle_data.entities.emplace(
136145
db::state::kDomainNameCode,
137-
build_domain_bundle_data(bundle->code, schema.domain(db::state::kDomainNameCode), salt));
146+
build_domain_bundle_data(bundle->code, schema.domain(db::state::kDomainNameCode), step_converter, salt));
138147
// TODO(canepat): enable after fixing .kvi configuration with IndexList-like implementation
139148
// bundle_data.entities.emplace(
140149
// db::state::kDomainNameCommitment,
141-
// build_domain_bundle_data(bundle->commitment, schema.domain(db::state::kDomainNameCommitment), salt));
150+
// build_domain_bundle_data(bundle->commitment, schema.domain(db::state::kDomainNameCommitment), step_converter, salt));
142151
bundle_data.entities.emplace(
143152
db::state::kDomainNameReceipts,
144-
build_domain_bundle_data(bundle->receipts, schema.domain(db::state::kDomainNameReceipts), salt));
153+
build_domain_bundle_data(bundle->receipts, schema.domain(db::state::kDomainNameReceipts), step_converter, salt));
145154

146155
return SnapshotBundle{
147156
parse_snapshot_path(bundle->accounts.segment.file_path).step_range(),
@@ -153,32 +162,33 @@ static snapshots::SnapshotBundle build_state_snapshot_bundle_historical(
153162
const SilkwormStateSnapshotBundleHistorical* bundle,
154163
const Schema::RepositoryDef& schema) {
155164
SnapshotBundleData bundle_data;
165+
datastore::StepToTimestampConverter step_converter = schema.make_step_converter();
156166

157167
bundle_data.entities.emplace(
158168
db::state::kDomainNameAccounts,
159-
build_history_bundle_data(bundle->accounts, schema.history(db::state::kDomainNameAccounts)));
169+
build_history_bundle_data(bundle->accounts, schema.history(db::state::kDomainNameAccounts), step_converter));
160170
bundle_data.entities.emplace(
161171
db::state::kDomainNameStorage,
162-
build_history_bundle_data(bundle->storage, schema.history(db::state::kDomainNameStorage)));
172+
build_history_bundle_data(bundle->storage, schema.history(db::state::kDomainNameStorage), step_converter));
163173
bundle_data.entities.emplace(
164174
db::state::kDomainNameCode,
165-
build_history_bundle_data(bundle->code, schema.history(db::state::kDomainNameCode)));
175+
build_history_bundle_data(bundle->code, schema.history(db::state::kDomainNameCode), step_converter));
166176
bundle_data.entities.emplace(
167177
db::state::kDomainNameReceipts,
168-
build_history_bundle_data(bundle->receipts, schema.history(db::state::kDomainNameReceipts)));
178+
build_history_bundle_data(bundle->receipts, schema.history(db::state::kDomainNameReceipts), step_converter));
169179

170180
bundle_data.entities.emplace(
171181
db::state::kInvIdxNameLogAddress,
172-
build_inverted_index_bundle_data(bundle->log_addresses, schema.inverted_index(db::state::kInvIdxNameLogAddress)));
182+
build_inverted_index_bundle_data(bundle->log_addresses, schema.inverted_index(db::state::kInvIdxNameLogAddress), step_converter));
173183
bundle_data.entities.emplace(
174184
db::state::kInvIdxNameLogTopics,
175-
build_inverted_index_bundle_data(bundle->log_topics, schema.inverted_index(db::state::kInvIdxNameLogTopics)));
185+
build_inverted_index_bundle_data(bundle->log_topics, schema.inverted_index(db::state::kInvIdxNameLogTopics), step_converter));
176186
bundle_data.entities.emplace(
177187
db::state::kInvIdxNameTracesFrom,
178-
build_inverted_index_bundle_data(bundle->traces_from, schema.inverted_index(db::state::kInvIdxNameTracesFrom)));
188+
build_inverted_index_bundle_data(bundle->traces_from, schema.inverted_index(db::state::kInvIdxNameTracesFrom), step_converter));
179189
bundle_data.entities.emplace(
180190
db::state::kInvIdxNameTracesTo,
181-
build_inverted_index_bundle_data(bundle->traces_to, schema.inverted_index(db::state::kInvIdxNameTracesTo)));
191+
build_inverted_index_bundle_data(bundle->traces_to, schema.inverted_index(db::state::kInvIdxNameTracesTo), step_converter));
182192

183193
return SnapshotBundle{
184194
parse_snapshot_path(bundle->accounts.segment.file_path).step_range(),
@@ -278,13 +288,17 @@ SILKWORM_EXPORT int silkworm_build_recsplit_indexes(SilkwormHandle handle, struc
278288
return SILKWORM_OK;
279289
}
280290

281-
static snapshots::SnapshotBundle build_blocks_snapshot_bundle(const SilkwormBlocksSnapshotBundle* bundle) {
291+
static snapshots::SnapshotBundle build_blocks_snapshot_bundle(
292+
const SilkwormBlocksSnapshotBundle* bundle,
293+
const Schema::RepositoryDef& schema) {
282294
snapshots::SnapshotBundleEntityData data;
295+
datastore::StepToTimestampConverter step_converter = schema.make_step_converter();
283296

284297
data.segments.emplace(
285298
db::blocks::kHeaderSegmentName,
286299
snapshots::segment::SegmentFileReader{
287300
parse_snapshot_path(bundle->headers.segment.file_path),
301+
step_converter,
288302
make_region(bundle->headers.segment),
289303
});
290304
data.accessor_indexes.emplace(
@@ -298,6 +312,7 @@ static snapshots::SnapshotBundle build_blocks_snapshot_bundle(const SilkwormBloc
298312
db::blocks::kBodySegmentName,
299313
snapshots::segment::SegmentFileReader{
300314
parse_snapshot_path(bundle->bodies.segment.file_path),
315+
step_converter,
301316
make_region(bundle->bodies.segment),
302317
});
303318
data.accessor_indexes.emplace(
@@ -311,6 +326,7 @@ static snapshots::SnapshotBundle build_blocks_snapshot_bundle(const SilkwormBloc
311326
db::blocks::kTxnSegmentName,
312327
snapshots::segment::SegmentFileReader{
313328
parse_snapshot_path(bundle->transactions.segment.file_path),
329+
step_converter,
314330
make_region(bundle->transactions.segment),
315331
});
316332
data.accessor_indexes.emplace(
@@ -348,7 +364,7 @@ SILKWORM_EXPORT int silkworm_add_blocks_snapshot_bundle(
348364

349365
auto& repository = *handle->blocks_repository;
350366

351-
repository.add_snapshot_bundle(build_blocks_snapshot_bundle(bundle));
367+
repository.add_snapshot_bundle(build_blocks_snapshot_bundle(bundle, repository.schema()));
352368
return SILKWORM_OK;
353369
} catch (const InvalidSnapshotPathException&) {
354370
return SILKWORM_INVALID_PATH;

silkworm/db/blocks/bodies/body_txs_amount_query_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <catch2/catch_test_macros.hpp>
77

8+
#include <silkworm/db/blocks/step_block_num_converter.hpp>
89
#include <silkworm/db/test_util/temp_snapshots.hpp>
910
#include <silkworm/infra/common/directories.hpp>
1011
#include <silkworm/infra/common/log.hpp>
@@ -15,7 +16,7 @@ namespace silkworm::snapshots {
1516
TEST_CASE("BodyTxsAmountSegmentQuery") {
1617
TemporaryDirectory tmp_dir;
1718
test_util::SampleBodySnapshotFile snapshot_file{tmp_dir.path()};
18-
segment::SegmentFileReader snapshot{snapshot_file.path()};
19+
segment::SegmentFileReader snapshot{snapshot_file.path(), db::blocks::kStepToBlockNumConverter};
1920

2021
BodyTxsAmountSegmentQuery query{snapshot};
2122
auto result = query.exec();

silkworm/db/blocks/headers/header_segment.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ void decode_word_into_header(ByteView word, BlockHeader& header) {
2828
success_or_throw(decode_result, "decode_word_into_header: rlp::decode error");
2929
}
3030

31-
void check_sanity_of_header_with_metadata(const BlockHeader& header, datastore::StepRange step_range) {
32-
auto block_num_range = db::blocks::kStepToBlockNumConverter.timestamp_range_from_step_range(step_range);
31+
void check_sanity_of_header_with_metadata(
32+
const BlockHeader& header,
33+
datastore::StepRange step_range,
34+
const datastore::StepToTimestampConverter& step_converter) {
35+
auto block_num_range = step_converter.timestamp_range_from_step_range(step_range);
3336
BlockNum block_from = block_num_range.start;
3437
BlockNum block_to = block_num_range.end;
3538
ensure((header.number >= block_from) && (header.number < block_to), [&]() {

silkworm/db/blocks/headers/header_segment.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ namespace silkworm::snapshots {
1313

1414
void encode_word_from_header(Bytes& word, const BlockHeader& header);
1515
void decode_word_into_header(ByteView word, BlockHeader& header);
16-
void check_sanity_of_header_with_metadata(const BlockHeader& header, datastore::StepRange step_range);
16+
void check_sanity_of_header_with_metadata(
17+
const BlockHeader& header,
18+
datastore::StepRange step_range,
19+
const datastore::StepToTimestampConverter& step_converter);
1720

1821
struct HeaderSegmentWordEncoder : public Encoder {
1922
BlockHeader value;
@@ -39,8 +42,8 @@ struct HeaderSegmentWordDecoder : public Decoder {
3942
decode_word_into_header(word, value);
4043
}
4144

42-
void check_sanity_with_metadata(const SnapshotPath& path) override {
43-
check_sanity_of_header_with_metadata(value, path.step_range());
45+
void check_sanity_with_metadata(const SnapshotPath& path, const datastore::StepToTimestampConverter& step_converter) override {
46+
check_sanity_of_header_with_metadata(value, path.step_range(), step_converter);
4447
}
4548
};
4649

silkworm/db/blocks/schema_config.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace silkworm::db::blocks {
1111
snapshots::Schema::RepositoryDef make_blocks_repository_schema() {
1212
snapshots::Schema::RepositoryDef repository_schema;
1313
repository_schema.index_salt_file_name("salt-blocks.txt");
14+
repository_schema.step_size(kStepSizeForBlockSnapshots);
1415
snapshots::Schema::EntityDef& schema = repository_schema.default_entity();
1516

1617
schema.segment(kHeaderSegmentName)
@@ -48,12 +49,12 @@ snapshots::SnapshotRepository make_blocks_repository(
4849
std::filesystem::path dir_path,
4950
bool open,
5051
std::optional<uint32_t> index_salt) {
52+
auto schema = make_blocks_repository_schema();
5153
return snapshots::SnapshotRepository{
5254
kBlocksRepositoryName,
5355
std::move(dir_path),
5456
open,
55-
make_blocks_repository_schema(),
56-
kStepToBlockNumConverter,
57+
schema,
5758
index_salt,
5859
make_blocks_index_builders_factory(),
5960
std::nullopt, // no domain caches

silkworm/db/blocks/transactions/txn_index.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <silkworm/db/blocks/bodies/body_txs_amount_query.hpp>
77
#include <silkworm/db/datastore/snapshots/segment/segment_reader.hpp>
88

9+
#include "silkworm/db/blocks/step_block_num_converter.hpp"
910
#include "txn_segment_word_codec.hpp"
1011

1112
namespace silkworm::snapshots {
@@ -17,7 +18,7 @@ Bytes TransactionKeyFactory::make(ByteView key_data, uint64_t i) {
1718
std::pair<uint64_t, uint64_t> TransactionIndex::compute_txs_amount(
1819
SnapshotPath bodies_segment_path,
1920
std::optional<MemoryMappedRegion> bodies_segment_region) {
20-
segment::SegmentFileReader body_segment{std::move(bodies_segment_path), bodies_segment_region};
21+
segment::SegmentFileReader body_segment{std::move(bodies_segment_path), db::blocks::kStepToBlockNumConverter, bodies_segment_region};
2122
auto result = BodyTxsAmountSegmentQuery{body_segment}.exec();
2223
return {result.first_tx_id, result.count};
2324
}

0 commit comments

Comments
 (0)