Skip to content

Commit 50f7d33

Browse files
authored
Merge pull request #718 from evoskuil/master
Drop fees from validated_bk and change to array, DRY in tx/bk setters.
2 parents 8233f0f + 35bd0c5 commit 50f7d33

File tree

7 files changed

+89
-188
lines changed

7 files changed

+89
-188
lines changed

include/bitcoin/database/impl/query/validate.ipp

Lines changed: 34 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ bool CLASS::is_unconfirmable(const header_link& link) const NOEXCEPT
118118
TEMPLATE
119119
code CLASS::get_header_state(const header_link& link) const NOEXCEPT
120120
{
121-
table::validated_bk::slab_get_code valid{};
121+
table::validated_bk::record valid{};
122122
if (!store_.validated_bk.at(to_validated_bk(link), valid))
123123
return error::unvalidated;
124124

@@ -133,7 +133,7 @@ code CLASS::get_header_state(const header_link& link) const NOEXCEPT
133133
TEMPLATE
134134
code CLASS::get_block_state(const header_link& link) const NOEXCEPT
135135
{
136-
table::validated_bk::slab_get_code valid{};
136+
table::validated_bk::record valid{};
137137
if (!store_.validated_bk.at(to_validated_bk(link), valid))
138138
return is_associated(link) ? error::unvalidated : error::unassociated;
139139

@@ -305,171 +305,92 @@ bool CLASS::get_context(system::chain::context& ctx,
305305
return true;
306306
}
307307

308-
// Setters.
308+
// set_block_state
309309
// ----------------------------------------------------------------------------
310310

311311
TEMPLATE
312-
bool CLASS::set_block_valid(const header_link& link, uint64_t fees) NOEXCEPT
312+
bool CLASS::set_block_valid(const header_link& link) NOEXCEPT
313313
{
314-
// ========================================================================
315-
const auto scope = store_.get_transactor();
316-
317-
// Clean single allocation failure (e.g. disk full).
318-
return store_.validated_bk.put(to_validated_bk(link),
319-
table::validated_bk::slab
320-
{
321-
{},
322-
schema::block_state::valid,
323-
fees
324-
});
325-
// ========================================================================
314+
return set_block_state(link, schema::block_state::valid);
326315
}
327316

328317
TEMPLATE
329318
bool CLASS::set_block_confirmable(const header_link& link) NOEXCEPT
330319
{
331-
// ========================================================================
332-
const auto scope = store_.get_transactor();
333-
334-
// Clean single allocation failure (e.g. disk full).
335-
return store_.validated_bk.put(to_validated_bk(link),
336-
table::validated_bk::slab
337-
{
338-
{},
339-
schema::block_state::confirmable,
340-
0 // fees
341-
});
342-
// ========================================================================
320+
return set_block_state(link, schema::block_state::confirmable);
343321
}
344322

345323
TEMPLATE
346324
bool CLASS::set_block_unconfirmable(const header_link& link) NOEXCEPT
347325
{
348-
// ========================================================================
349-
const auto scope = store_.get_transactor();
350-
351-
// Clean single allocation failure (e.g. disk full).
352-
return store_.validated_bk.put(to_validated_bk(link),
353-
table::validated_bk::slab
354-
{
355-
{},
356-
schema::block_state::unconfirmable,
357-
0 // fees
358-
});
359-
// ========================================================================
326+
return set_block_state(link, schema::block_state::unconfirmable);
360327
}
361328

362329
TEMPLATE
363330
bool CLASS::set_block_unknown(const header_link& link) NOEXCEPT
364331
{
332+
return set_block_state(link, schema::block_state::block_unknown);
333+
}
334+
335+
// private
336+
TEMPLATE
337+
bool CLASS::set_block_state(const header_link& link,
338+
schema::block_state state) NOEXCEPT
339+
{
340+
const auto record = to_validated_bk(link);
341+
365342
// ========================================================================
366343
const auto scope = store_.get_transactor();
367344

368345
// Clean single allocation failure (e.g. disk full).
369-
return store_.validated_bk.put(to_validated_bk(link),
370-
table::validated_bk::slab
371-
{
372-
{},
373-
schema::block_state::block_unknown,
374-
0 // fees
375-
});
346+
return store_.validated_bk.put(record,
347+
table::validated_bk::record{ {}, state });
376348
// ========================================================================
377349
}
378350

351+
// set_tx_state
352+
// ----------------------------------------------------------------------------
353+
379354
TEMPLATE
380355
bool CLASS::set_tx_unknown(const tx_link& link) NOEXCEPT
381356
{
382-
// ========================================================================
383-
const auto scope = store_.get_transactor();
384-
385-
// Clean single allocation failure (e.g. disk full).
386-
return store_.validated_tx.put(link, table::validated_tx::slab
387-
{
388-
{},
389-
{},
390-
schema::tx_state::tx_unknown,
391-
0, // fee
392-
0 // sigops
393-
});
394-
// ========================================================================
357+
return set_tx_state(link, {}, {}, {}, schema::tx_state::tx_unknown);
395358
}
396359

397360
TEMPLATE
398361
bool CLASS::set_tx_disconnected(const tx_link& link,
399362
const context& ctx) NOEXCEPT
400363
{
401-
// ========================================================================
402-
const auto scope = store_.get_transactor();
403-
404-
// Clean single allocation failure (e.g. disk full).
405-
return store_.validated_tx.put(link, table::validated_tx::slab
406-
{
407-
{},
408-
ctx,
409-
schema::tx_state::disconnected,
410-
0, // fee
411-
0 // sigops
412-
});
413-
// ========================================================================
364+
return set_tx_state(link, ctx, {}, {}, schema::tx_state::disconnected);
414365
}
415366

416367
TEMPLATE
417368
bool CLASS::set_tx_connected(const tx_link& link, const context& ctx,
418369
uint64_t fee, size_t sigops) NOEXCEPT
370+
{
371+
return set_tx_state(link, ctx, fee, sigops, schema::tx_state::connected);
372+
}
373+
374+
// private
375+
TEMPLATE
376+
bool CLASS::set_tx_state(const tx_link& link, const context& ctx,
377+
uint64_t fee, size_t sigops, schema::tx_state state) NOEXCEPT
419378
{
420379
using sigs = linkage<schema::sigops>;
421380
BC_ASSERT(sigops<system::power2<sigs::integer>(to_bits(sigs::size)));
422381

423382
// ========================================================================
424383
const auto scope = store_.get_transactor();
384+
using namespace system;
425385

426386
// Clean single allocation failure (e.g. disk full).
427387
return store_.validated_tx.put(link, table::validated_tx::slab
428388
{
429-
{},
430-
ctx,
431-
schema::tx_state::connected,
432-
fee,
433-
system::possible_narrow_cast<sigs::integer>(sigops)
389+
{}, ctx, state, fee, possible_narrow_cast<sigs::integer>(sigops)
434390
});
435391
// ========================================================================
436392
}
437393

438-
////TEMPLATE
439-
////bool CLASS::set_txs_connected(const header_link& link) NOEXCEPT
440-
////{
441-
//// context ctx{};
442-
//// if (!get_context(ctx, link))
443-
//// return false;
444-
////
445-
//// const auto txs = to_transactions(link);
446-
//// if (txs.empty())
447-
//// return false;
448-
////
449-
//// // FOR PERFORMANCE EVALUATION ONLY.
450-
//// constexpr uint64_t fee = 99;
451-
//// constexpr size_t sigops = 42;
452-
//// using sigs = linkage<schema::sigops>;
453-
////
454-
//// // ========================================================================
455-
//// const auto scope = store_.get_transactor();
456-
////
457-
//// // Clean single allocation failure (e.g. disk full).
458-
//// return std_all_of(bc::seq, txs.begin(), txs.end(),
459-
//// [&](const tx_link& fk) NOEXCEPT
460-
//// {
461-
//// return store_.validated_tx.put(fk, table::validated_tx::slab
462-
//// {
463-
//// {},
464-
//// ctx,
465-
//// schema::tx_state::connected,
466-
//// fee,
467-
//// system::possible_narrow_cast<sigs::integer>(sigops)
468-
//// });
469-
//// });
470-
//// // ========================================================================
471-
////}
472-
473394
} // namespace database
474395
} // namespace libbitcoin
475396

include/bitcoin/database/query.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ class query
502502
const header_link& link) const NOEXCEPT;
503503

504504
/// Setters.
505-
bool set_block_valid(const header_link& link, uint64_t fees) NOEXCEPT;
505+
bool set_block_valid(const header_link& link) NOEXCEPT;
506506
bool set_block_unconfirmable(const header_link& link) NOEXCEPT;
507507
bool set_block_confirmable(const header_link& link) NOEXCEPT;
508508
bool set_block_unknown(const header_link& link) NOEXCEPT;
@@ -654,6 +654,13 @@ class query
654654
bool is_block_validated(code& state, const header_link& link,
655655
size_t height, size_t checkpoint) const NOEXCEPT;
656656

657+
/// Setters.
658+
/// -----------------------------------------------------------------------
659+
bool set_block_state(const header_link& link,
660+
schema::block_state state) NOEXCEPT;
661+
bool set_tx_state(const tx_link& link, const context& ctx,
662+
uint64_t fee, size_t sigops, schema::tx_state state) NOEXCEPT;
663+
657664
/// Confirm.
658665
/// -----------------------------------------------------------------------
659666
bool is_confirmed_unspent(const output_link& link) const NOEXCEPT;

include/bitcoin/database/tables/caches/validated_bk.hpp

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,57 +34,28 @@ struct validated_bk
3434
using coding = linkage<schema::code>;
3535
using array_map<schema::validated_bk>::arraymap;
3636

37-
struct slab
37+
struct record
3838
: public schema::validated_bk
3939
{
40-
inline link count() const NOEXCEPT
41-
{
42-
using namespace system;
43-
return possible_narrow_cast<link::integer>(coding::size +
44-
variable_size(fees));
45-
}
46-
4740
inline bool from_data(reader& source) NOEXCEPT
4841
{
4942
code = source.read_little_endian<coding::integer, coding::size>();
50-
fees = source.read_variable();
51-
BC_ASSERT(!source || source.get_read_position() == count());
43+
BC_ASSERT(!source || source.get_read_position() == count() * minrow);
5244
return source;
5345
}
5446

5547
inline bool to_data(finalizer& sink) const NOEXCEPT
5648
{
5749
sink.write_little_endian<coding::integer, coding::size>(code);
58-
sink.write_variable(fees);
59-
BC_ASSERT(!sink || sink.get_write_position() == count());
50+
BC_ASSERT(!sink || sink.get_write_position() == count() * minrow);
6051
return sink;
6152
}
6253

63-
inline bool operator==(const slab& other) const NOEXCEPT
64-
{
65-
return code == other.code
66-
&& fees == other.fees;
67-
}
68-
69-
coding::integer code{};
70-
uint64_t fees{};
71-
};
72-
73-
struct slab_get_code
74-
: public schema::validated_bk
75-
{
76-
inline link count() const NOEXCEPT
54+
inline bool operator==(const record& other) const NOEXCEPT
7755
{
78-
BC_ASSERT(false);
79-
return {};
56+
return code == other.code;
8057
}
8158

82-
inline bool from_data(reader& source) NOEXCEPT
83-
{
84-
code = source.read_little_endian<coding::integer, coding::size>();
85-
return source;
86-
}
87-
8859
coding::integer code{};
8960
};
9061
};

include/bitcoin/database/tables/optionals/filter_bk.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct filter_bk
4040
{
4141
hash = source.read_hash();
4242
head = source.read_hash();
43+
BC_ASSERT(!source || source.get_read_position() == count() * minrow);
4344
return source;
4445
}
4546

include/bitcoin/database/tables/schema.hpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,17 @@ constexpr size_t prevout_ = 5; // ->prevout slab.
4848
constexpr size_t txs_ = 5; // ->txs slab.
4949
constexpr size_t tx = 4; // ->tx record.
5050
constexpr size_t block = 3; // ->header record.
51-
constexpr size_t bk_slab = 4; // ->validated_bk record.
5251
constexpr size_t tx_slab = 5; // ->validated_tx record.
5352
constexpr size_t filter_ = 5; // ->filter record.
5453
constexpr size_t doubles_ = 4; // doubles bucket (no actual keys).
5554

5655
/// Archive tables.
5756
/// -----------------------------------------------------------------------
5857

58+
// size_t `cell` sets the hashmap bucket size (minimum size of link type).
59+
// bool `align` causes arraymap bucket size to be expanded to nearest word.
60+
// Memory fencing used (vs. mutex) when array/hashmap bucket is word sized.
61+
5962
// record hashmap
6063
struct header
6164
{
@@ -295,20 +298,20 @@ struct prevout
295298
static_assert(link::size == 5u);
296299
};
297300

298-
// slab arraymap
301+
// record arraymap
299302
struct validated_bk
300303
{
301304
static constexpr size_t align = false;
302-
static constexpr size_t pk = schema::bk_slab;
305+
static constexpr size_t pk = schema::block;
303306
using link = linkage<pk, to_bits(pk)>;
304307
static constexpr size_t minsize =
305-
schema::code + // TODO: change code to variable.
306-
one;
308+
schema::code;
307309
static constexpr size_t minrow = minsize;
308-
static constexpr size_t size = max_size_t;
309-
static_assert(minsize == 2u);
310-
static_assert(minrow == 2u);
311-
static_assert(link::size == 4u);
310+
static constexpr size_t size = minsize;
311+
static constexpr link count() NOEXCEPT { return 1; }
312+
static_assert(minsize == 1u);
313+
static_assert(minrow == 1u);
314+
static_assert(link::size == 3u);
312315
};
313316

314317
// slab modest (sk:4) multimap, with low multiple rate.
@@ -331,6 +334,7 @@ struct validated_tx
331334
static inline link count() NOEXCEPT;
332335
static_assert(minsize == 14u);
333336
static_assert(minrow == 23u);
337+
static_assert(link::size == 5u);
334338
};
335339

336340
/// Optional tables.

test/query/validate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ BOOST_AUTO_TEST_CASE(query_validate__get_block_state__valid__block_valid)
234234
BOOST_REQUIRE(query.initialize(test::genesis));
235235
BOOST_REQUIRE(query.set(test::block1, context{}, false, false));
236236

237-
BOOST_REQUIRE(query.set_block_valid(1, 42));
237+
BOOST_REQUIRE(query.set_block_valid(1));
238238
BOOST_REQUIRE_EQUAL(query.get_header_state(1), error::block_valid);
239239
BOOST_REQUIRE_EQUAL(query.get_block_state(1), error::block_valid);
240240
}

0 commit comments

Comments
 (0)