Skip to content

Commit d67b419

Browse files
committed
trial hack
1 parent a5ee4dc commit d67b419

File tree

10 files changed

+80
-33
lines changed

10 files changed

+80
-33
lines changed

memory_database/user_account.h

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class UserAccount {
3535
static_assert(
3636
__builtin_popcount(MAX_OPS_PER_TX) == 1, "should be power of two");
3737

38+
static_assert(
39+
MAX_SEQ_NUMS_PER_BLOCK <= 64, "rework sequence num reservations");
40+
3841
using amount_t = typename RevertableAsset::amount_t;
3942

4043
mutable std::mutex uncommitted_assets_mtx;

modlog/log_entry_fns.cc

+30-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ struct NewSelfTransactionCompareFn {
1414
}
1515
};
1616

17-
template<typename Value, typename CompareFn>
18-
void dedup(std::vector<Value>& values, CompareFn comparator) {
19-
for (std::size_t i = 1; i < values.size(); i++) {
17+
template<typename value_list, typename CompareFn>
18+
void dedup(value_list& values, CompareFn comparator) {
19+
for (size_t i = 1u; i < values.size(); i++) {
2020
if (comparator(values[i], values[i-1])) {
2121
values.erase(values.begin() + i);
2222
} else {
@@ -25,6 +25,33 @@ void dedup(std::vector<Value>& values, CompareFn comparator) {
2525
}
2626
}
2727

28+
void
29+
LogMergeFn::value_merge(
30+
AccountModificationTxList& original_value,
31+
const AccountModificationTxList& merge_in_value)
32+
{
33+
for (const auto& new_tx : merge_in_value.new_transactions_self) {
34+
original_value.new_transactions_self.push_back(new_tx);
35+
}
36+
//original_value.new_transactions_self.insert(
37+
// original_value.new_transactions_self.end(),
38+
// merge_in_value.new_transactions_self.begin(),
39+
// merge_in_value.new_transactions_self.end());
40+
original_value.identifiers_self.insert(
41+
original_value.identifiers_self.end(),
42+
merge_in_value.identifiers_self.begin(),
43+
merge_in_value.identifiers_self.end());
44+
original_value.identifiers_others.insert(
45+
original_value.identifiers_others.end(),
46+
merge_in_value.identifiers_others.begin(),
47+
merge_in_value.identifiers_others.end());
48+
49+
if (original_value.owner != merge_in_value.owner) {
50+
throw std::runtime_error("owner mismatch when merging logs!!!");
51+
}
52+
}
53+
54+
2855
void
2956
LogNormalizeFn::apply_to_value (AccountModificationTxListWrapper& log) {
3057
std::sort(log.identifiers_self.begin(), log.identifiers_self.end());

modlog/log_entry_fns.h

+8-23
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ struct LogInsertFn : public GenericInsertFn {
2424

2525
static void value_insert(AccountModificationTxList& main_value, const SignedTransaction& self_transaction) {
2626
main_value.new_transactions_self.push_back(self_transaction);
27-
if (main_value.new_transactions_self.size() > main_value.new_transactions_self.capacity() || main_value.new_transactions_self.size() > 500000) {
28-
std::printf("%lu %lu %lu\n", main_value.new_transactions_self.size(), main_value.new_transactions_self.capacity(),
29-
main_value.new_transactions_self.size());
30-
throw std::runtime_error("invalid main_value!!!");
31-
}
27+
//if (main_value.new_transactions_self.size() > main_value.new_transactions_self.capacity() || main_value.new_transactions_self.size() > 500000) {
28+
// std::printf("%lu %lu %lu\n", main_value.new_transactions_self.size(), main_value.new_transactions_self.capacity(),
29+
// main_value.new_transactions_self.size());
30+
// throw std::runtime_error("invalid main_value!!!");
31+
//}
3232
}
3333
/*
3434
template<typename AtomicMetadataType, typename ValueType>
@@ -49,24 +49,9 @@ struct LogInsertFn : public GenericInsertFn {
4949

5050
//might like to make these one struct, reduce extra code/etc, but type signatures on metadata merge are slightly diff
5151
struct LogMergeFn {
52-
static void value_merge(AccountModificationTxList& original_value, const AccountModificationTxList& merge_in_value) {
53-
original_value.new_transactions_self.insert(
54-
original_value.new_transactions_self.end(),
55-
merge_in_value.new_transactions_self.begin(),
56-
merge_in_value.new_transactions_self.end());
57-
original_value.identifiers_self.insert(
58-
original_value.identifiers_self.end(),
59-
merge_in_value.identifiers_self.begin(),
60-
merge_in_value.identifiers_self.end());
61-
original_value.identifiers_others.insert(
62-
original_value.identifiers_others.end(),
63-
merge_in_value.identifiers_others.begin(),
64-
merge_in_value.identifiers_others.end());
65-
66-
if (original_value.owner != merge_in_value.owner) {
67-
throw std::runtime_error("owner mismatch when merging logs!!!");
68-
}
69-
}
52+
static void value_merge(
53+
AccountModificationTxList& original_value,
54+
const AccountModificationTxList& merge_in_value);
7055
};
7156

7257
struct LogNormalizeFn {

speedex/speedex_persistence.h

+20-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Phase 3: Everything else (orderbooks, header hash)
2424

2525
namespace speedex {
2626

27+
/*! Call before sending transaction block to a validator.
28+
Persists account block + header, and prepares memory database with a persistence
29+
thunk.
30+
*/
2731
std::unique_ptr<AccountModificationBlock>
2832
persist_critical_round_data(
2933
SpeedexManagementStructures& management_structures,
@@ -32,24 +36,28 @@ persist_critical_round_data(
3236
bool get_block = false,
3337
uint64_t log_offset = 0);
3438

39+
//! Memory database loads persistence thunk into lmdb
3540
void
3641
persist_async_phase1(
3742
SpeedexManagementStructures& management_structures,
3843
const uint64_t current_block_number,
3944
BlockDataPersistenceMeasurements& measurements);
4045

46+
//! Msync the memory database lmdb
4147
void
4248
persist_async_phase2(
4349
SpeedexManagementStructures& management_structures,
4450
uint64_t current_block_number,
4551
BlockDataPersistenceMeasurements& measurements);
4652

53+
//! Finish persistence (orderbooks, header hash map).
4754
void
4855
persist_async_phase3(
4956
SpeedexManagementStructures& management_structures,
5057
uint64_t current_block_number,
5158
BlockDataPersistenceMeasurements& measurements);
5259

60+
//! Operates a background thread for phase 3 persistence.
5361
class AsyncPersisterPhase3 : public AsyncWorker {
5462
using AsyncWorker::mtx;
5563
using AsyncWorker::cv;
@@ -90,7 +98,8 @@ class AsyncPersisterPhase3 : public AsyncWorker {
9098
}
9199
};
92100

93-
101+
//! Operates a background thread for phase 2 persistence.
102+
//! Automatically calls phase 3 when done.
94103
class AsyncPersisterPhase2 : public AsyncWorker {
95104
using AsyncWorker::mtx;
96105
using AsyncWorker::cv;
@@ -137,6 +146,8 @@ class AsyncPersisterPhase2 : public AsyncWorker {
137146
}
138147
};
139148

149+
//! Operates a background thread for phase 1 persistence.
150+
//! Automatically calls phase 2 when done.
140151
struct AsyncPersister : public AsyncWorker {
141152
using AsyncWorker::mtx;
142153
using AsyncWorker::cv;
@@ -157,7 +168,6 @@ struct AsyncPersister : public AsyncWorker {
157168
return latest_measurements != nullptr;
158169
}
159170

160-
161171
void run();
162172

163173
public:
@@ -169,13 +179,15 @@ struct AsyncPersister : public AsyncWorker {
169179
start_async_thread([this] {run();});
170180
}
171181

172-
173-
174182
uint64_t get_highest_persisted_block() {
175183
std::lock_guard lock(mtx);
176184
return highest_persisted_block;
177185
}
178186

187+
188+
//! Begin persisting a block to disk
189+
//! (all blocks up to persist_block_number).
190+
//! When phase 1 finishes, phase 2 is automatically called.
179191
void do_async_persist(
180192
const uint64_t persist_block_number,
181193
BlockDataPersistenceMeasurements& measurements) {
@@ -197,6 +209,10 @@ struct AsyncPersister : public AsyncWorker {
197209
cv.notify_one();
198210
}
199211

212+
//! Wait for all async persistence phases to complete.
213+
//! Clears up all uses of measurements object reference.
214+
//! Should be called before shutdown or before invalidating measurements
215+
//! object reference.
200216
void wait_for_async_persist() {
201217
//clears up all uses of measurements reference
202218
wait_for_async_task();

trie/prefix.h

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#pragma once
22

3+
4+
/*! \file prefix.h
5+
6+
Two implementations of a trie prefix. One is an arbitrary-length
7+
byte array, and one is specialized for accountid keys.
8+
*/
39
#include <atomic>
410
#include <compare>
511
#include <concepts>
@@ -367,6 +373,7 @@ class AccountIDPrefix {
367373
bool
368374
operator==(const AccountIDPrefix& other) const = default;
369375

376+
//! Get the bits of the prefix just beyond branch_point
370377
unsigned char get_branch_bits(const PrefixLenBits branch_point) const {
371378
if (branch_point.len >= MAX_LEN_BITS) {
372379
std::printf("Bad branch bits was %u\n", branch_point.len);
@@ -375,6 +382,8 @@ class AccountIDPrefix {
375382
return (prefix >> (60 - branch_point.len)) & BRANCH_MASK;
376383
}
377384

385+
//! Compute the length of the longest matching initial subsequence
386+
//! of this prefix and the other prefix.
378387
PrefixLenBits get_prefix_match_len(
379388
const PrefixLenBits& self_len,
380389
const AccountIDPrefix& other,
@@ -392,10 +401,12 @@ class AccountIDPrefix {
392401
return std::min({computed, self_len, other_len});
393402
}
394403

404+
//! Truncate the prefix to a defined length
395405
void truncate(const PrefixLenBits truncate_point) {
396406
prefix &= (UINT64_MAX << (64 - truncate_point.len));
397407
}
398408

409+
//! Convert prefix to an array of bytes.
399410
xdr::opaque_array<MAX_LEN_BYTES> get_bytes_array() const {
400411
xdr::opaque_array<MAX_LEN_BYTES> out;
401412
write_unsigned_big_endian(out, prefix);
@@ -419,6 +430,7 @@ class AccountIDPrefix {
419430
return debug::array_to_str(bytes.data(), len.num_prefix_bytes());
420431
}
421432

433+
//! Modify the prefix by setting the bits after fixed_len to bb
422434
void set_next_branch_bits(PrefixLenBits fixed_len, const uint8_t bb) {
423435
uint8_t offset = (60-fixed_len.len);
424436
uint64_t mask = ((uint64_t) BRANCH_MASK) << offset;

trie/recycling_impl/trie.h

+3
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,9 @@ class SerialAccountTrie {
446446

447447
friend class AccountTrie<ValueType>;
448448

449+
static_assert(sizeof(node_t) <= 64, "account trie node should be at most 1 cache line");
450+
451+
449452
public:
450453

451454
SerialAccountTrie(AccountTrieNodeAllocator<node_t>& allocator)

utils/save_load_xdr.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ save_account_block_fast(
273273

274274
size_t total_written_bytes = 0;
275275

276-
const xdr::xvector<SignedTransaction>* tx_buffer = nullptr;
276+
const xdr::xvector<SignedTransaction, MAX_SEQ_NUMS_PER_BLOCK>* tx_buffer = nullptr;
277277
size_t buffer_idx = 0;
278278
size_t num_written = 0;
279279

xdr/database_commitments.x

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct TxIdentifier {
4242

4343
struct AccountModificationTxList {
4444
AccountID owner;
45-
SignedTransaction new_transactions_self<>; //transactions
45+
SignedTransaction new_transactions_self<MAX_SEQ_NUMS_PER_BLOCK>; //transactions
4646
uint64 identifiers_self<>; // sequence numbers, in addition
4747
TxIdentifier identifiers_others<>;
4848
};

xdr/transaction.x

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ struct Operation {
106106

107107
const MAX_OPS_PER_TX = 256;
108108
const RESERVED_SEQUENCE_NUM_LOWBITS = 255;
109+
const MAX_SEQ_NUMS_PER_BLOCK = 64;
109110

110111
struct TransactionMetadata {
111112
AccountID sourceAccount;

xdrpp

0 commit comments

Comments
 (0)