Skip to content

Commit b7a1621

Browse files
committed
datastore: .ef v2 format
1 parent f81ca64 commit b7a1621

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>
@@ -783,6 +784,8 @@ TEST_CASE_METHOD(CApiTest, "CAPI silkworm_execute_blocks_perpetual multiple bloc
783784
}
784785

785786
TEST_CASE_METHOD(CApiTest, "CAPI silkworm_add_blocks_snapshot_bundle", "[capi]") {
787+
static constexpr datastore::StepToTimestampConverter kStepConverter = db::blocks::kStepToBlockNumConverter;
788+
786789
snapshot_test::SampleHeaderSnapshotFile header_segment_file{tmp_dir.path()};
787790
auto& header_segment_path = header_segment_file.path();
788791
snapshot_test::SampleBodySnapshotFile body_segment_file{tmp_dir.path()};
@@ -793,20 +796,20 @@ TEST_CASE_METHOD(CApiTest, "CAPI silkworm_add_blocks_snapshot_bundle", "[capi]")
793796
auto header_index_builder = snapshots::HeaderIndex::make(header_segment_path);
794797
header_index_builder.set_base_data_id(header_segment_file.block_num_range().start);
795798
REQUIRE_NOTHROW(header_index_builder.build());
796-
snapshots::segment::SegmentFileReader header_segment{header_segment_path};
799+
snapshots::segment::SegmentFileReader header_segment{header_segment_path, kStepConverter};
797800
snapshots::rec_split::AccessorIndex idx_header_hash{header_segment_path.related_path_ext(db::blocks::kIdxExtension)};
798801

799802
auto body_index_builder = snapshots::BodyIndex::make(body_segment_path);
800803
body_index_builder.set_base_data_id(body_segment_file.block_num_range().start);
801804
REQUIRE_NOTHROW(body_index_builder.build());
802-
snapshots::segment::SegmentFileReader body_segment{body_segment_path};
805+
snapshots::segment::SegmentFileReader body_segment{body_segment_path, kStepConverter};
803806
snapshots::rec_split::AccessorIndex idx_body_number{body_segment_path.related_path_ext(db::blocks::kIdxExtension)};
804807

805808
auto tx_index_builder = snapshots::TransactionIndex::make(body_segment_path, txn_segment_path);
806809
tx_index_builder.build();
807810
auto tx_index_hash_to_block_builder = snapshots::TransactionToBlockIndex::make(body_segment_path, txn_segment_path, txn_segment_file.block_num_range().start);
808811
tx_index_hash_to_block_builder.build();
809-
snapshots::segment::SegmentFileReader txn_segment{txn_segment_path};
812+
snapshots::segment::SegmentFileReader txn_segment{txn_segment_path, kStepConverter};
810813
snapshots::rec_split::AccessorIndex idx_txn_hash{txn_segment_path.related_path_ext(db::blocks::kIdxExtension)};
811814
snapshots::rec_split::AccessorIndex idx_txn_hash_2_block{tx_index_hash_to_block_builder.path()};
812815

@@ -938,7 +941,7 @@ TEST_CASE_METHOD(CApiTest, "CAPI silkworm_add_state_snapshot", "[capi]") {
938941
constexpr uint32_t kZeroSalt{0};
939942

940943
const snapshot_test::SampleAccountsDomainSegmentFile kv_segment_file{tmp_dir.path()};
941-
segment::KVSegmentFileReader kv_segment{kv_segment_file.path(), seg::CompressionKind::kAll};
944+
segment::KVSegmentFileReader kv_segment{kv_segment_file.path(), db::state::kStepToTxnIdConverter, seg::CompressionKind::kAll};
942945
const auto kv_segment_path_string{kv_segment_file.path().path().string()};
943946

944947
const snapshot_test::SampleAccountsDomainExistenceIndexFile existence_index_file{tmp_dir.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
}

silkworm/db/capi/db.cpp

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ static SnapshotPath parse_snapshot_path(const char* file_path) {
4444
static void build_inverted_index_bundle_data(
4545
const SilkwormInvertedIndexSnapshot& snapshot,
4646
const Schema::EntityDef& entity_def,
47+
const datastore::StepToTimestampConverter& step_converter,
4748
SnapshotBundleEntityData& data) {
4849
data.kv_segments.emplace(
4950
Schema::kInvIdxKVSegmentName,
5051
segment::KVSegmentFileReader{
5152
parse_snapshot_path(snapshot.segment.file_path),
53+
step_converter,
5254
entity_def.kv_segment(Schema::kInvIdxKVSegmentName).compression_kind(),
5355
make_region(snapshot.segment),
5456
});
@@ -62,21 +64,24 @@ static void build_inverted_index_bundle_data(
6264

6365
static snapshots::SnapshotBundleEntityData build_inverted_index_bundle_data(
6466
const SilkwormInvertedIndexSnapshot& snapshot,
65-
const Schema::EntityDef& entity_def) {
67+
const Schema::EntityDef& entity_def,
68+
const datastore::StepToTimestampConverter& step_converter) {
6669
SnapshotBundleEntityData data;
67-
build_inverted_index_bundle_data(snapshot, entity_def, data);
70+
build_inverted_index_bundle_data(snapshot, entity_def, step_converter, data);
6871
return data;
6972
}
7073

7174
static snapshots::SnapshotBundleEntityData build_domain_bundle_data(
7275
const SilkwormDomainSnapshot& snapshot,
7376
const Schema::EntityDef& entity_def,
77+
const datastore::StepToTimestampConverter& step_converter,
7478
uint32_t index_salt) {
7579
SnapshotBundleEntityData data;
7680
data.kv_segments.emplace(
7781
Schema::kDomainKVSegmentName,
7882
segment::KVSegmentFileReader{
7983
parse_snapshot_path(snapshot.segment.file_path),
84+
step_converter,
8085
entity_def.kv_segment(Schema::kDomainKVSegmentName).compression_kind(),
8186
make_region(snapshot.segment),
8287
});
@@ -105,12 +110,14 @@ static snapshots::SnapshotBundleEntityData build_domain_bundle_data(
105110

106111
static snapshots::SnapshotBundleEntityData build_history_bundle_data(
107112
const SilkwormHistorySnapshot& snapshot,
108-
const Schema::EntityDef& entity_def) {
113+
const Schema::EntityDef& entity_def,
114+
const datastore::StepToTimestampConverter& step_converter) {
109115
SnapshotBundleEntityData data;
110116
data.segments.emplace(
111117
Schema::kHistorySegmentName,
112118
segment::SegmentFileReader{
113119
parse_snapshot_path(snapshot.segment.file_path),
120+
step_converter,
114121
make_region(snapshot.segment),
115122
});
116123
data.accessor_indexes.emplace(
@@ -120,7 +127,7 @@ static snapshots::SnapshotBundleEntityData build_history_bundle_data(
120127
make_region(snapshot.accessor_index),
121128
});
122129

123-
build_inverted_index_bundle_data(snapshot.inverted_index, entity_def, data);
130+
build_inverted_index_bundle_data(snapshot.inverted_index, entity_def, step_converter, data);
124131
return data;
125132
}
126133

@@ -129,22 +136,24 @@ static snapshots::SnapshotBundle build_state_snapshot_bundle_latest(
129136
const Schema::RepositoryDef& schema,
130137
uint32_t salt) {
131138
SnapshotBundleData bundle_data;
139+
datastore::StepToTimestampConverter step_converter = schema.make_step_converter();
140+
132141
bundle_data.entities.emplace(
133142
db::state::kDomainNameAccounts,
134-
build_domain_bundle_data(bundle->accounts, schema.domain(db::state::kDomainNameAccounts), salt));
143+
build_domain_bundle_data(bundle->accounts, schema.domain(db::state::kDomainNameAccounts), step_converter, salt));
135144
bundle_data.entities.emplace(
136145
db::state::kDomainNameStorage,
137-
build_domain_bundle_data(bundle->storage, schema.domain(db::state::kDomainNameStorage), salt));
146+
build_domain_bundle_data(bundle->storage, schema.domain(db::state::kDomainNameStorage), step_converter, salt));
138147
bundle_data.entities.emplace(
139148
db::state::kDomainNameCode,
140-
build_domain_bundle_data(bundle->code, schema.domain(db::state::kDomainNameCode), salt));
149+
build_domain_bundle_data(bundle->code, schema.domain(db::state::kDomainNameCode), step_converter, salt));
141150
// TODO(canepat): enable after fixing .kvi configuration with IndexList-like implementation
142151
// bundle_data.entities.emplace(
143152
// db::state::kDomainNameCommitment,
144-
// build_domain_bundle_data(bundle->commitment, schema.domain(db::state::kDomainNameCommitment), salt));
153+
// build_domain_bundle_data(bundle->commitment, schema.domain(db::state::kDomainNameCommitment), step_converter, salt));
145154
bundle_data.entities.emplace(
146155
db::state::kDomainNameReceipts,
147-
build_domain_bundle_data(bundle->receipts, schema.domain(db::state::kDomainNameReceipts), salt));
156+
build_domain_bundle_data(bundle->receipts, schema.domain(db::state::kDomainNameReceipts), step_converter, salt));
148157

149158
return SnapshotBundle{
150159
parse_snapshot_path(bundle->accounts.segment.file_path).step_range(),
@@ -156,32 +165,33 @@ static snapshots::SnapshotBundle build_state_snapshot_bundle_historical(
156165
const SilkwormStateSnapshotBundleHistorical* bundle,
157166
const Schema::RepositoryDef& schema) {
158167
SnapshotBundleData bundle_data;
168+
datastore::StepToTimestampConverter step_converter = schema.make_step_converter();
159169

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

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

186196
return SnapshotBundle{
187197
parse_snapshot_path(bundle->accounts.segment.file_path).step_range(),
@@ -281,13 +291,17 @@ SILKWORM_EXPORT int silkworm_build_recsplit_indexes(SilkwormHandle handle, struc
281291
return SILKWORM_OK;
282292
}
283293

284-
static snapshots::SnapshotBundle build_blocks_snapshot_bundle(const SilkwormBlocksSnapshotBundle* bundle) {
294+
static snapshots::SnapshotBundle build_blocks_snapshot_bundle(
295+
const SilkwormBlocksSnapshotBundle* bundle,
296+
const Schema::RepositoryDef& schema) {
285297
snapshots::SnapshotBundleEntityData data;
298+
datastore::StepToTimestampConverter step_converter = schema.make_step_converter();
286299

287300
data.segments.emplace(
288301
db::blocks::kHeaderSegmentName,
289302
snapshots::segment::SegmentFileReader{
290303
parse_snapshot_path(bundle->headers.segment.file_path),
304+
step_converter,
291305
make_region(bundle->headers.segment),
292306
});
293307
data.accessor_indexes.emplace(
@@ -301,6 +315,7 @@ static snapshots::SnapshotBundle build_blocks_snapshot_bundle(const SilkwormBloc
301315
db::blocks::kBodySegmentName,
302316
snapshots::segment::SegmentFileReader{
303317
parse_snapshot_path(bundle->bodies.segment.file_path),
318+
step_converter,
304319
make_region(bundle->bodies.segment),
305320
});
306321
data.accessor_indexes.emplace(
@@ -314,6 +329,7 @@ static snapshots::SnapshotBundle build_blocks_snapshot_bundle(const SilkwormBloc
314329
db::blocks::kTxnSegmentName,
315330
snapshots::segment::SegmentFileReader{
316331
parse_snapshot_path(bundle->transactions.segment.file_path),
332+
step_converter,
317333
make_region(bundle->transactions.segment),
318334
});
319335
data.accessor_indexes.emplace(
@@ -351,7 +367,7 @@ SILKWORM_EXPORT int silkworm_add_blocks_snapshot_bundle(
351367

352368
auto& repository = handle->db->blocks_repository;
353369

354-
repository.add_snapshot_bundle(build_blocks_snapshot_bundle(bundle));
370+
repository.add_snapshot_bundle(build_blocks_snapshot_bundle(bundle, repository.schema()));
355371
return SILKWORM_OK;
356372
} catch (const InvalidSnapshotPathException&) {
357373
return SILKWORM_INVALID_PATH;

0 commit comments

Comments
 (0)