@aztec/p2p 0.0.1-commit.e310a4c8 → 0.0.1-commit.e558bd1c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/client/factory.d.ts +3 -3
- package/dest/client/factory.d.ts.map +1 -1
- package/dest/client/factory.js +5 -3
- package/dest/client/interface.d.ts +9 -2
- package/dest/client/interface.d.ts.map +1 -1
- package/dest/client/p2p_client.d.ts +7 -4
- package/dest/client/p2p_client.d.ts.map +1 -1
- package/dest/client/p2p_client.js +17 -7
- package/dest/client/test/tx_proposal_collector/proposal_tx_collector_worker.js +1 -1
- package/dest/config.d.ts +9 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +3 -1
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts +94 -87
- package/dest/mem_pools/attestation_pool/attestation_pool.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/attestation_pool.js +411 -3
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts +2 -2
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/attestation_pool_test_suite.js +351 -85
- package/dest/mem_pools/attestation_pool/index.d.ts +2 -3
- package/dest/mem_pools/attestation_pool/index.d.ts.map +1 -1
- package/dest/mem_pools/attestation_pool/index.js +1 -2
- package/dest/mem_pools/index.d.ts +2 -2
- package/dest/mem_pools/index.d.ts.map +1 -1
- package/dest/mem_pools/index.js +1 -1
- package/dest/mem_pools/interface.d.ts +3 -3
- package/dest/mem_pools/interface.d.ts.map +1 -1
- package/dest/mem_pools/tx_pool_v2/archive/index.d.ts +2 -0
- package/dest/mem_pools/tx_pool_v2/archive/index.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/archive/index.js +1 -0
- package/dest/mem_pools/tx_pool_v2/archive/tx_archive.d.ts +43 -0
- package/dest/mem_pools/tx_pool_v2/archive/tx_archive.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/archive/tx_archive.js +103 -0
- package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.d.ts +47 -0
- package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/eviction/eviction_manager.js +119 -0
- package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts +17 -0
- package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.js +90 -0
- package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts +19 -0
- package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.js +89 -0
- package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts +10 -0
- package/dest/mem_pools/tx_pool_v2/eviction/index.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/eviction/index.js +11 -0
- package/dest/mem_pools/tx_pool_v2/eviction/interfaces.d.ts +131 -0
- package/dest/mem_pools/tx_pool_v2/eviction/interfaces.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/eviction/interfaces.js +17 -0
- package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.d.ts +15 -0
- package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.js +63 -0
- package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.d.ts +17 -0
- package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.js +91 -0
- package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts +16 -0
- package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.js +70 -0
- package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.d.ts +20 -0
- package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.js +63 -0
- package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.d.ts +15 -0
- package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.js +19 -0
- package/dest/mem_pools/tx_pool_v2/index.d.ts +5 -0
- package/dest/mem_pools/tx_pool_v2/index.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/index.js +4 -0
- package/dest/mem_pools/tx_pool_v2/interfaces.d.ts +197 -0
- package/dest/mem_pools/tx_pool_v2/interfaces.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/interfaces.js +6 -0
- package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts +71 -0
- package/dest/mem_pools/tx_pool_v2/tx_metadata.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/tx_metadata.js +95 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_bench_metrics.d.ts +26 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_bench_metrics.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_bench_metrics.js +70 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts +99 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_indices.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_indices.js +332 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts +55 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2.js +156 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts +69 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.d.ts.map +1 -0
- package/dest/mem_pools/tx_pool_v2/tx_pool_v2_impl.js +748 -0
- package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts +3 -3
- package/dest/msg_validators/attestation_validator/fisherman_attestation_validator.d.ts.map +1 -1
- package/dest/services/dummy_service.d.ts +6 -2
- package/dest/services/dummy_service.d.ts.map +1 -1
- package/dest/services/dummy_service.js +3 -0
- package/dest/services/index.d.ts +2 -1
- package/dest/services/index.d.ts.map +1 -1
- package/dest/services/index.js +1 -0
- package/dest/services/libp2p/libp2p_service.d.ts +74 -33
- package/dest/services/libp2p/libp2p_service.d.ts.map +1 -1
- package/dest/services/libp2p/libp2p_service.js +299 -228
- package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts +4 -4
- package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.d.ts.map +1 -1
- package/dest/services/reqresp/batch-tx-requester/batch_tx_requester.js +8 -8
- package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts +6 -4
- package/dest/services/reqresp/protocols/block_txs/block_txs_handler.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/block_txs/block_txs_handler.js +16 -11
- package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts +15 -10
- package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.d.ts.map +1 -1
- package/dest/services/reqresp/protocols/block_txs/block_txs_reqresp.js +12 -11
- package/dest/services/service.d.ts +18 -1
- package/dest/services/service.d.ts.map +1 -1
- package/dest/services/tx_collection/config.d.ts +3 -3
- package/dest/services/tx_collection/config.js +3 -3
- package/dest/services/tx_collection/fast_tx_collection.d.ts +4 -5
- package/dest/services/tx_collection/fast_tx_collection.d.ts.map +1 -1
- package/dest/services/tx_collection/fast_tx_collection.js +10 -14
- package/dest/services/tx_collection/index.d.ts +1 -1
- package/dest/services/tx_collection/proposal_tx_collector.d.ts +12 -12
- package/dest/services/tx_collection/proposal_tx_collector.d.ts.map +1 -1
- package/dest/services/tx_collection/proposal_tx_collector.js +4 -5
- package/dest/services/tx_file_store/config.d.ts +18 -0
- package/dest/services/tx_file_store/config.d.ts.map +1 -0
- package/dest/services/tx_file_store/config.js +26 -0
- package/dest/services/tx_file_store/index.d.ts +4 -0
- package/dest/services/tx_file_store/index.d.ts.map +1 -0
- package/dest/services/tx_file_store/index.js +3 -0
- package/dest/services/tx_file_store/instrumentation.d.ts +15 -0
- package/dest/services/tx_file_store/instrumentation.d.ts.map +1 -0
- package/dest/services/tx_file_store/instrumentation.js +29 -0
- package/dest/services/tx_file_store/tx_file_store.d.ts +47 -0
- package/dest/services/tx_file_store/tx_file_store.d.ts.map +1 -0
- package/dest/services/tx_file_store/tx_file_store.js +149 -0
- package/dest/test-helpers/testbench-utils.d.ts +10 -16
- package/dest/test-helpers/testbench-utils.d.ts.map +1 -1
- package/dest/test-helpers/testbench-utils.js +32 -30
- package/dest/testbench/p2p_client_testbench_worker.js +1 -1
- package/package.json +14 -14
- package/src/client/factory.ts +7 -4
- package/src/client/interface.ts +13 -1
- package/src/client/p2p_client.ts +25 -8
- package/src/client/test/tx_proposal_collector/proposal_tx_collector_worker.ts +1 -1
- package/src/config.ts +8 -1
- package/src/mem_pools/attestation_pool/attestation_pool.ts +444 -90
- package/src/mem_pools/attestation_pool/attestation_pool_test_suite.ts +436 -100
- package/src/mem_pools/attestation_pool/index.ts +9 -2
- package/src/mem_pools/index.ts +1 -1
- package/src/mem_pools/interface.ts +2 -2
- package/src/mem_pools/tx_pool_v2/README.md +209 -0
- package/src/mem_pools/tx_pool_v2/archive/index.ts +1 -0
- package/src/mem_pools/tx_pool_v2/archive/tx_archive.ts +120 -0
- package/src/mem_pools/tx_pool_v2/eviction/eviction_manager.ts +147 -0
- package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_eviction_rule.ts +118 -0
- package/src/mem_pools/tx_pool_v2/eviction/fee_payer_balance_pre_add_rule.ts +111 -0
- package/src/mem_pools/tx_pool_v2/eviction/index.ts +23 -0
- package/src/mem_pools/tx_pool_v2/eviction/interfaces.ts +164 -0
- package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_mining_rule.ts +74 -0
- package/src/mem_pools/tx_pool_v2/eviction/invalid_txs_after_reorg_rule.ts +101 -0
- package/src/mem_pools/tx_pool_v2/eviction/low_priority_eviction_rule.ts +86 -0
- package/src/mem_pools/tx_pool_v2/eviction/low_priority_pre_add_rule.ts +72 -0
- package/src/mem_pools/tx_pool_v2/eviction/nullifier_conflict_rule.ts +31 -0
- package/src/mem_pools/tx_pool_v2/index.ts +11 -0
- package/src/mem_pools/tx_pool_v2/interfaces.ts +227 -0
- package/src/mem_pools/tx_pool_v2/tx_metadata.ts +161 -0
- package/src/mem_pools/tx_pool_v2/tx_pool_bench_metrics.ts +77 -0
- package/src/mem_pools/tx_pool_v2/tx_pool_indices.ts +417 -0
- package/src/mem_pools/tx_pool_v2/tx_pool_v2.ts +212 -0
- package/src/mem_pools/tx_pool_v2/tx_pool_v2_impl.ts +882 -0
- package/src/msg_validators/attestation_validator/fisherman_attestation_validator.ts +2 -2
- package/src/services/dummy_service.ts +6 -0
- package/src/services/index.ts +1 -0
- package/src/services/libp2p/libp2p_service.ts +304 -230
- package/src/services/reqresp/batch-tx-requester/README.md +7 -7
- package/src/services/reqresp/batch-tx-requester/batch_tx_requester.ts +11 -11
- package/src/services/reqresp/protocols/block_txs/block_txs_handler.ts +22 -13
- package/src/services/reqresp/protocols/block_txs/block_txs_reqresp.ts +21 -15
- package/src/services/service.ts +20 -0
- package/src/services/tx_collection/config.ts +6 -6
- package/src/services/tx_collection/fast_tx_collection.ts +14 -24
- package/src/services/tx_collection/index.ts +1 -1
- package/src/services/tx_collection/proposal_tx_collector.ts +12 -14
- package/src/services/tx_file_store/config.ts +43 -0
- package/src/services/tx_file_store/index.ts +3 -0
- package/src/services/tx_file_store/instrumentation.ts +36 -0
- package/src/services/tx_file_store/tx_file_store.ts +173 -0
- package/src/test-helpers/testbench-utils.ts +18 -39
- package/src/testbench/p2p_client_testbench_worker.ts +1 -1
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts +0 -40
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.d.ts.map +0 -1
- package/dest/mem_pools/attestation_pool/kv_attestation_pool.js +0 -218
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts +0 -31
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.d.ts.map +0 -1
- package/dest/mem_pools/attestation_pool/memory_attestation_pool.js +0 -180
- package/src/mem_pools/attestation_pool/kv_attestation_pool.ts +0 -320
- package/src/mem_pools/attestation_pool/memory_attestation_pool.ts +0 -264
|
@@ -1,6 +1,414 @@
|
|
|
1
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
2
|
+
import { toArray } from '@aztec/foundation/iterable';
|
|
3
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
4
|
+
import { BlockProposal, CheckpointAttestation, CheckpointProposal } from '@aztec/stdlib/p2p';
|
|
5
|
+
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
6
|
+
import { PoolInstrumentation, PoolName } from '../instrumentation.js';
|
|
7
|
+
export const MAX_PROPOSALS_PER_SLOT = 5;
|
|
8
|
+
export const MAX_PROPOSALS_PER_POSITION = 3;
|
|
9
|
+
export const ATTESTATION_CAP_BUFFER = 10;
|
|
1
10
|
/**
|
|
2
|
-
*
|
|
11
|
+
* Pool for storing attestations and proposals collected by a validator.
|
|
3
12
|
*
|
|
4
|
-
* Attestations
|
|
13
|
+
* Attestations and proposals observed via the p2p network are stored for requests
|
|
5
14
|
* from the validator to produce a block, or to serve to other peers.
|
|
6
|
-
*/ export {
|
|
15
|
+
*/ export class AttestationPool {
|
|
16
|
+
store;
|
|
17
|
+
log;
|
|
18
|
+
metrics;
|
|
19
|
+
// Checkpoint attestations from attestation key (slot-proposalId-signer) to serialized CheckpointAttestation
|
|
20
|
+
// Keys are lexicographically sortable allowing range queries by slot or by (slot, proposalId)
|
|
21
|
+
checkpointAttestations;
|
|
22
|
+
// Checkpoint proposals from proposal archive to serialized CheckpointProposal
|
|
23
|
+
checkpointProposals;
|
|
24
|
+
// Checkpoint proposals indexed by slot for querying all proposals in a slot
|
|
25
|
+
// Key: slot number, Value: proposal archive strings
|
|
26
|
+
checkpointProposalsForSlot;
|
|
27
|
+
// Block proposals from proposal archive to serialized BlockProposal
|
|
28
|
+
blockProposals;
|
|
29
|
+
// Block proposals indexed by slot and index-within-checkpoint for duplicate detection
|
|
30
|
+
// Key: (slot << 10) | indexWithinCheckpoint, Value: archive string
|
|
31
|
+
blockProposalsForSlotAndIndex;
|
|
32
|
+
constructor(store, telemetry = getTelemetryClient(), log = createLogger('aztec:attestation_pool')){
|
|
33
|
+
this.store = store;
|
|
34
|
+
this.log = log;
|
|
35
|
+
this.poolStats = async ()=>{
|
|
36
|
+
return {
|
|
37
|
+
itemCount: await this.checkpointAttestations.sizeAsync()
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
// Initialize block proposal storage
|
|
41
|
+
this.blockProposals = store.openMap('proposals');
|
|
42
|
+
this.blockProposalsForSlotAndIndex = store.openMultiMap('block_proposals_for_slot_and_index');
|
|
43
|
+
// Initialize checkpoint attestations storage
|
|
44
|
+
this.checkpointAttestations = store.openMap('checkpoint_attestations');
|
|
45
|
+
// Initialize checkpoint proposal storage
|
|
46
|
+
this.checkpointProposals = store.openMap('checkpoint_proposals');
|
|
47
|
+
this.checkpointProposalsForSlot = store.openMultiMap('checkpoint_proposals_for_slot');
|
|
48
|
+
this.metrics = new PoolInstrumentation(telemetry, PoolName.ATTESTATION_POOL, this.poolStats);
|
|
49
|
+
}
|
|
50
|
+
poolStats;
|
|
51
|
+
/** Returns whether the pool is empty. */ async isEmpty() {
|
|
52
|
+
for await (const _ of this.checkpointAttestations.entriesAsync()){
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
for await (const _ of this.blockProposals.entriesAsync()){
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
getProposalKey(slot, proposalId) {
|
|
61
|
+
const slotStr = typeof slot === 'string' ? slot : new Fr(slot).toString();
|
|
62
|
+
const proposalIdStr = typeof proposalId === 'string' ? proposalId : Buffer.isBuffer(proposalId) ? Fr.fromBuffer(proposalId).toString() : proposalId.toString();
|
|
63
|
+
return `${slotStr}-${proposalIdStr}`;
|
|
64
|
+
}
|
|
65
|
+
getAttestationKey(slot, proposalId, address) {
|
|
66
|
+
return `${this.getProposalKey(slot, proposalId)}-${address}`;
|
|
67
|
+
}
|
|
68
|
+
/** Returns range bounds for querying all attestations for a given slot. */ getAttestationKeyRangeForSlot(slot) {
|
|
69
|
+
const slotStr = new Fr(slot).toString();
|
|
70
|
+
return {
|
|
71
|
+
start: `${slotStr}-`,
|
|
72
|
+
end: `${slotStr}-Z`
|
|
73
|
+
}; // 'Z' sorts after any hex character
|
|
74
|
+
}
|
|
75
|
+
/** Returns range bounds for querying all attestations for a given (slot, proposalId). */ getAttestationKeyRangeForProposal(slot, proposalId) {
|
|
76
|
+
const proposalKey = this.getProposalKey(slot, proposalId);
|
|
77
|
+
return {
|
|
78
|
+
start: `${proposalKey}-`,
|
|
79
|
+
end: `${proposalKey}-Z`
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/** Number of bits reserved for indexWithinCheckpoint in position keys. */ static INDEX_BITS = 10;
|
|
83
|
+
/** Maximum indexWithinCheckpoint value (2^10 - 1 = 1023). */ static MAX_INDEX = (1 << AttestationPool.INDEX_BITS) - 1;
|
|
84
|
+
/** Creates a position key for block proposals: (slot << 10) | indexWithinCheckpoint. */ getBlockPositionKey(slot, indexWithinCheckpoint) {
|
|
85
|
+
if (indexWithinCheckpoint > AttestationPool.MAX_INDEX) {
|
|
86
|
+
throw new Error(`Value for indexWithinCheckpoint ${indexWithinCheckpoint} exceeds maximum ${AttestationPool.MAX_INDEX}`);
|
|
87
|
+
}
|
|
88
|
+
return slot << AttestationPool.INDEX_BITS | indexWithinCheckpoint;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Attempts to add a block proposal to the pool.
|
|
92
|
+
*
|
|
93
|
+
* This method performs validation and addition in a single call:
|
|
94
|
+
* - Checks if the proposal already exists (returns alreadyExists: true if so)
|
|
95
|
+
* - Checks if the position has reached the proposal cap (returns added: false if so)
|
|
96
|
+
* - Adds the proposal if validation passes
|
|
97
|
+
*
|
|
98
|
+
* @param blockProposal - The block proposal to add
|
|
99
|
+
* @returns Result indicating whether the proposal was added and duplicate detection info
|
|
100
|
+
*/ async tryAddBlockProposal(blockProposal) {
|
|
101
|
+
return await this.store.transactionAsync(async ()=>{
|
|
102
|
+
const proposalId = blockProposal.archive.toString();
|
|
103
|
+
// Check if already exists
|
|
104
|
+
const alreadyExists = await this.blockProposals.hasAsync(proposalId);
|
|
105
|
+
if (alreadyExists) {
|
|
106
|
+
const totalForPosition = await this.getBlockProposalCountForPosition(blockProposal.slotNumber, blockProposal.indexWithinCheckpoint);
|
|
107
|
+
return {
|
|
108
|
+
added: false,
|
|
109
|
+
alreadyExists: true,
|
|
110
|
+
totalForPosition
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
// Get current count for position and check cap, do not add if exceeded
|
|
114
|
+
const totalForPosition = await this.getBlockProposalCountForPosition(blockProposal.slotNumber, blockProposal.indexWithinCheckpoint);
|
|
115
|
+
if (totalForPosition >= MAX_PROPOSALS_PER_POSITION) {
|
|
116
|
+
return {
|
|
117
|
+
added: false,
|
|
118
|
+
alreadyExists: false,
|
|
119
|
+
totalForPosition
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
// Add the proposal
|
|
123
|
+
await this.addBlockProposal(blockProposal);
|
|
124
|
+
this.log.debug(`Added block proposal for slot ${blockProposal.slotNumber} and index ${blockProposal.indexWithinCheckpoint}`, {
|
|
125
|
+
proposalId,
|
|
126
|
+
slotNumber: blockProposal.slotNumber,
|
|
127
|
+
indexWithinCheckpoint: blockProposal.indexWithinCheckpoint
|
|
128
|
+
});
|
|
129
|
+
return {
|
|
130
|
+
added: true,
|
|
131
|
+
alreadyExists: false,
|
|
132
|
+
totalForPosition: totalForPosition + 1
|
|
133
|
+
};
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/** Gets the count of block proposals for a given position (slot, indexWithinCheckpoint). */ getBlockProposalCountForPosition(slot, indexWithinCheckpoint) {
|
|
137
|
+
const positionKey = this.getBlockPositionKey(slot, indexWithinCheckpoint);
|
|
138
|
+
return this.blockProposalsForSlotAndIndex.getValueCountAsync(positionKey);
|
|
139
|
+
}
|
|
140
|
+
/** Internal method - must be called within a transaction. */ async addBlockProposal(blockProposal) {
|
|
141
|
+
const proposalId = blockProposal.archive.toString();
|
|
142
|
+
// Strip signedTxs before storing to avoid persisting full tx data
|
|
143
|
+
await this.blockProposals.set(proposalId, blockProposal.withoutSignedTxs().toBuffer());
|
|
144
|
+
// Index by slot and position for duplicate detection
|
|
145
|
+
const positionKey = this.getBlockPositionKey(blockProposal.slotNumber, blockProposal.indexWithinCheckpoint);
|
|
146
|
+
await this.blockProposalsForSlotAndIndex.set(positionKey, proposalId);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Get block proposal by its ID.
|
|
150
|
+
*
|
|
151
|
+
* @param id - The ID of the block proposal to retrieve. The ID is proposal.payload.archive
|
|
152
|
+
*
|
|
153
|
+
* @return The block proposal if it exists, otherwise undefined.
|
|
154
|
+
*/ async getBlockProposal(id) {
|
|
155
|
+
const buffer = await this.blockProposals.getAsync(id);
|
|
156
|
+
try {
|
|
157
|
+
if (buffer && buffer.length > 0) {
|
|
158
|
+
return BlockProposal.fromBuffer(buffer);
|
|
159
|
+
}
|
|
160
|
+
} catch {
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Attempts to add a checkpoint proposal to the pool.
|
|
167
|
+
*
|
|
168
|
+
* This method performs validation and addition in a single call:
|
|
169
|
+
* - Checks if the proposal already exists (returns alreadyExists: true if so)
|
|
170
|
+
* - Checks if the slot has reached the proposal cap (returns added: false if so)
|
|
171
|
+
* - Adds the proposal if validation passes
|
|
172
|
+
*
|
|
173
|
+
* Note: This method only handles the CheckpointProposalCore. If the original
|
|
174
|
+
* CheckpointProposal contains a lastBlock, the caller should extract it via
|
|
175
|
+
* getBlockProposal() and add it separately via tryAddBlockProposal().
|
|
176
|
+
*
|
|
177
|
+
* @param proposal - The checkpoint proposal core to add
|
|
178
|
+
* @returns Result indicating whether the proposal was added and duplicate detection info
|
|
179
|
+
*/ async tryAddCheckpointProposal(proposal) {
|
|
180
|
+
return await this.store.transactionAsync(async ()=>{
|
|
181
|
+
const proposalId = proposal.archive.toString();
|
|
182
|
+
// Check if already exists
|
|
183
|
+
const alreadyExists = await this.checkpointProposals.hasAsync(proposalId);
|
|
184
|
+
if (alreadyExists) {
|
|
185
|
+
const totalForPosition = await this.checkpointProposalsForSlot.getValueCountAsync(proposal.slotNumber);
|
|
186
|
+
return {
|
|
187
|
+
added: false,
|
|
188
|
+
alreadyExists: true,
|
|
189
|
+
totalForPosition
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
// Get current count for slot and check cap
|
|
193
|
+
const totalForPosition = await this.checkpointProposalsForSlot.getValueCountAsync(proposal.slotNumber);
|
|
194
|
+
if (totalForPosition >= MAX_PROPOSALS_PER_SLOT) {
|
|
195
|
+
return {
|
|
196
|
+
added: false,
|
|
197
|
+
alreadyExists: false,
|
|
198
|
+
totalForPosition
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
// Add the proposal if cap not exceeded
|
|
202
|
+
await this.addCheckpointProposal(proposal);
|
|
203
|
+
this.log.debug(`Added checkpoint proposal for slot ${proposal.slotNumber}`, {
|
|
204
|
+
proposalId,
|
|
205
|
+
slotNumber: proposal.slotNumber
|
|
206
|
+
});
|
|
207
|
+
return {
|
|
208
|
+
added: true,
|
|
209
|
+
alreadyExists: false,
|
|
210
|
+
totalForPosition: totalForPosition + 1
|
|
211
|
+
};
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/** Internal method - must be called within a transaction. */ async addCheckpointProposal(proposal) {
|
|
215
|
+
const slotKey = proposal.slotNumber;
|
|
216
|
+
const proposalId = proposal.archive.toString();
|
|
217
|
+
await this.checkpointProposalsForSlot.set(slotKey, proposalId);
|
|
218
|
+
await this.checkpointProposals.set(proposalId, proposal.toBuffer());
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Get checkpoint proposal by its ID.
|
|
222
|
+
*
|
|
223
|
+
* Returns a CheckpointProposalCore (without lastBlock info) since the lastBlock
|
|
224
|
+
* is extracted and stored separately as a BlockProposal when added.
|
|
225
|
+
*
|
|
226
|
+
* @param id - The ID of the checkpoint proposal to retrieve (proposal.archive)
|
|
227
|
+
* @return The checkpoint proposal core if it exists, otherwise undefined.
|
|
228
|
+
*/ async getCheckpointProposal(id) {
|
|
229
|
+
const buffer = await this.checkpointProposals.getAsync(id);
|
|
230
|
+
try {
|
|
231
|
+
if (buffer && buffer.length > 0) {
|
|
232
|
+
return CheckpointProposal.fromBuffer(buffer);
|
|
233
|
+
}
|
|
234
|
+
} catch {
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
return undefined;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Adds own checkpoint attestations to the pool.
|
|
241
|
+
* Skips validations on number of checkpoint attestations stored for the given slot.
|
|
242
|
+
*/ async addOwnCheckpointAttestations(attestations) {
|
|
243
|
+
await this.store.transactionAsync(async ()=>{
|
|
244
|
+
for (const attestation of attestations){
|
|
245
|
+
const slotNumber = attestation.payload.header.slotNumber;
|
|
246
|
+
const proposalId = attestation.archive;
|
|
247
|
+
const sender = attestation.getSender();
|
|
248
|
+
// Skip attestations with invalid signatures
|
|
249
|
+
if (!sender) {
|
|
250
|
+
this.log.warn(`Skipping own checkpoint attestation with invalid signature for slot ${slotNumber}`, {
|
|
251
|
+
signature: attestation.signature.toString(),
|
|
252
|
+
slotNumber,
|
|
253
|
+
proposalId
|
|
254
|
+
});
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
const address = sender.toString();
|
|
258
|
+
await this.checkpointAttestations.set(this.getAttestationKey(slotNumber, proposalId, address), attestation.toBuffer());
|
|
259
|
+
this.log.debug(`Added own checkpoint attestation for slot ${slotNumber} from ${address}`, {
|
|
260
|
+
signature: attestation.signature.toString(),
|
|
261
|
+
slotNumber,
|
|
262
|
+
address,
|
|
263
|
+
proposalId
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Get all checkpoint attestations for a given slot.
|
|
270
|
+
*
|
|
271
|
+
* @param slot - The slot to query
|
|
272
|
+
* @return CheckpointAttestations
|
|
273
|
+
*/ async getCheckpointAttestationsForSlot(slot) {
|
|
274
|
+
const range = this.getAttestationKeyRangeForSlot(slot);
|
|
275
|
+
const attestations = [];
|
|
276
|
+
for await (const [_, buf] of this.checkpointAttestations.entriesAsync(range)){
|
|
277
|
+
attestations.push(CheckpointAttestation.fromBuffer(buf));
|
|
278
|
+
}
|
|
279
|
+
return attestations;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Get checkpoint attestations for slot and given proposal.
|
|
283
|
+
*
|
|
284
|
+
* @param slot - The slot to query
|
|
285
|
+
* @param proposalId - The proposal to query
|
|
286
|
+
* @return CheckpointAttestations
|
|
287
|
+
*/ async getCheckpointAttestationsForSlotAndProposal(slot, proposalId) {
|
|
288
|
+
const range = this.getAttestationKeyRangeForProposal(slot, proposalId);
|
|
289
|
+
const attestations = [];
|
|
290
|
+
for await (const [_, buf] of this.checkpointAttestations.entriesAsync(range)){
|
|
291
|
+
attestations.push(CheckpointAttestation.fromBuffer(buf));
|
|
292
|
+
}
|
|
293
|
+
return attestations;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Delete all pool data (attestations, proposals) older than the given slot.
|
|
297
|
+
*
|
|
298
|
+
* @param oldestSlot - The oldest slot to keep.
|
|
299
|
+
*/ async deleteOlderThan(oldestSlot) {
|
|
300
|
+
let numberOfAttestations = 0;
|
|
301
|
+
let numberOfCheckpointProposals = 0;
|
|
302
|
+
let numberOfBlockProposals = 0;
|
|
303
|
+
await this.store.transactionAsync(async ()=>{
|
|
304
|
+
// Delete checkpoint attestations with slot < oldestSlot
|
|
305
|
+
// Attestation keys start with Fr(slot).toString(), so we use end bound of Fr(oldestSlot).toString()
|
|
306
|
+
const attestationEndKey = new Fr(oldestSlot).toString();
|
|
307
|
+
for await (const key of this.checkpointAttestations.keysAsync({
|
|
308
|
+
end: attestationEndKey
|
|
309
|
+
})){
|
|
310
|
+
await this.checkpointAttestations.delete(key);
|
|
311
|
+
numberOfAttestations++;
|
|
312
|
+
}
|
|
313
|
+
// Delete checkpoint proposals for slots < oldestSlot, using checkpointProposalsForSlot as index
|
|
314
|
+
for await (const slot of this.checkpointProposalsForSlot.keysAsync({
|
|
315
|
+
end: oldestSlot
|
|
316
|
+
})){
|
|
317
|
+
const proposalIds = await toArray(this.checkpointProposalsForSlot.getValuesAsync(slot));
|
|
318
|
+
for (const proposalId of proposalIds){
|
|
319
|
+
await this.checkpointProposals.delete(proposalId);
|
|
320
|
+
numberOfCheckpointProposals++;
|
|
321
|
+
}
|
|
322
|
+
await this.checkpointProposalsForSlot.delete(slot);
|
|
323
|
+
}
|
|
324
|
+
// Delete block proposals for slots < oldestSlot, using blockProposalsForSlotAndIndex as index
|
|
325
|
+
// Key format: (slot << INDEX_BITS) | indexWithinCheckpoint
|
|
326
|
+
const blockPositionEndKey = oldestSlot << AttestationPool.INDEX_BITS;
|
|
327
|
+
for await (const positionKey of this.blockProposalsForSlotAndIndex.keysAsync({
|
|
328
|
+
end: blockPositionEndKey
|
|
329
|
+
})){
|
|
330
|
+
const proposalIds = await toArray(this.blockProposalsForSlotAndIndex.getValuesAsync(positionKey));
|
|
331
|
+
for (const proposalId of proposalIds){
|
|
332
|
+
await this.blockProposals.delete(proposalId);
|
|
333
|
+
numberOfBlockProposals++;
|
|
334
|
+
}
|
|
335
|
+
await this.blockProposalsForSlotAndIndex.delete(positionKey);
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
this.log.verbose(`Deleted old pool data`, {
|
|
339
|
+
oldestSlot,
|
|
340
|
+
numberOfAttestations,
|
|
341
|
+
numberOfCheckpointProposals,
|
|
342
|
+
numberOfBlockProposals
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Attempts to add a checkpoint attestation to the pool.
|
|
347
|
+
*
|
|
348
|
+
* This method performs validation and addition in a single call:
|
|
349
|
+
* - Checks if the attestation already exists (returns alreadyExists: true if so)
|
|
350
|
+
* - Checks if the (slot, proposalId) has reached the attestation cap (returns added: false if so)
|
|
351
|
+
* - Adds the attestation if validation passes
|
|
352
|
+
*
|
|
353
|
+
* @param attestation - The checkpoint attestation to add
|
|
354
|
+
* @param committeeSize - Committee size for the attestation's slot
|
|
355
|
+
* @returns Result indicating whether the attestation was added and existence info
|
|
356
|
+
*/ async tryAddCheckpointAttestation(attestation, committeeSize) {
|
|
357
|
+
const slotNumber = attestation.payload.header.slotNumber;
|
|
358
|
+
const proposalId = attestation.archive.toString();
|
|
359
|
+
const sender = attestation.getSender();
|
|
360
|
+
if (!sender) {
|
|
361
|
+
return {
|
|
362
|
+
added: false,
|
|
363
|
+
alreadyExists: false,
|
|
364
|
+
totalForPosition: 0
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
return await this.store.transactionAsync(async ()=>{
|
|
368
|
+
const key = this.getAttestationKey(slotNumber, proposalId, sender.toString());
|
|
369
|
+
const alreadyExists = await this.checkpointAttestations.hasAsync(key);
|
|
370
|
+
if (alreadyExists) {
|
|
371
|
+
const total = await this.getAttestationCount(slotNumber, proposalId);
|
|
372
|
+
return {
|
|
373
|
+
added: false,
|
|
374
|
+
alreadyExists: true,
|
|
375
|
+
totalForPosition: total
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
const limit = committeeSize + ATTESTATION_CAP_BUFFER;
|
|
379
|
+
const currentCount = await this.getAttestationCount(slotNumber, proposalId);
|
|
380
|
+
if (currentCount >= limit) {
|
|
381
|
+
return {
|
|
382
|
+
added: false,
|
|
383
|
+
alreadyExists: false,
|
|
384
|
+
totalForPosition: currentCount
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
await this.checkpointAttestations.set(key, attestation.toBuffer());
|
|
388
|
+
this.log.debug(`Added checkpoint attestation for slot ${slotNumber} from ${sender.toString()}`, {
|
|
389
|
+
signature: attestation.signature.toString(),
|
|
390
|
+
slotNumber,
|
|
391
|
+
address: sender.toString(),
|
|
392
|
+
proposalId
|
|
393
|
+
});
|
|
394
|
+
return {
|
|
395
|
+
added: true,
|
|
396
|
+
alreadyExists: false,
|
|
397
|
+
totalForPosition: currentCount + 1
|
|
398
|
+
};
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
/** Gets the count of attestations for a given (slot, proposalId). */ async getAttestationCount(slot, proposalId) {
|
|
402
|
+
const range = this.getAttestationKeyRangeForProposal(slot, proposalId);
|
|
403
|
+
let count = 0;
|
|
404
|
+
for await (const _ of this.checkpointAttestations.keysAsync(range)){
|
|
405
|
+
count++;
|
|
406
|
+
}
|
|
407
|
+
return count;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
/** Creates an AttestationPool backed by a temporary store for testing. */ export async function createTestAttestationPool(telemetry) {
|
|
411
|
+
const { openTmpStore } = await import('@aztec/kv-store/lmdb-v2');
|
|
412
|
+
const store = await openTmpStore('test-attestation-pool');
|
|
413
|
+
return new AttestationPool(store, telemetry);
|
|
414
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AttestationPool } from './attestation_pool.js';
|
|
2
2
|
export declare function describeAttestationPool(getAttestationPool: () => AttestationPool): void;
|
|
3
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
3
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXR0ZXN0YXRpb25fcG9vbF90ZXN0X3N1aXRlLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbWVtX3Bvb2xzL2F0dGVzdGF0aW9uX3Bvb2wvYXR0ZXN0YXRpb25fcG9vbF90ZXN0X3N1aXRlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQVlBLE9BQU8sRUFBRSxLQUFLLGVBQWUsRUFBc0QsTUFBTSx1QkFBdUIsQ0FBQztBQUtqSCx3QkFBZ0IsdUJBQXVCLENBQUMsa0JBQWtCLEVBQUUsTUFBTSxlQUFlLFFBMnJCaEYifQ==
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"attestation_pool_test_suite.d.ts","sourceRoot":"","sources":["../../../src/mem_pools/attestation_pool/attestation_pool_test_suite.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"attestation_pool_test_suite.d.ts","sourceRoot":"","sources":["../../../src/mem_pools/attestation_pool/attestation_pool_test_suite.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,KAAK,eAAe,EAAsD,MAAM,uBAAuB,CAAC;AAKjH,wBAAgB,uBAAuB,CAAC,kBAAkB,EAAE,MAAM,eAAe,QA2rBhF"}
|