-
-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathsyncCommitteeMessagePool.ts
More file actions
163 lines (144 loc) · 5.71 KB
/
syncCommitteeMessagePool.ts
File metadata and controls
163 lines (144 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import {BitArray, toHexString} from "@chainsafe/ssz";
import {Signature, aggregateSignatures} from "@chainsafe/blst";
import {SYNC_COMMITTEE_SIZE, SYNC_COMMITTEE_SUBNET_COUNT} from "@lodestar/params";
import {altair, Root, Slot, SubcommitteeIndex} from "@lodestar/types";
import {MapDef} from "@lodestar/utils";
import {IClock} from "../../util/clock.js";
import {InsertOutcome, OpPoolError, OpPoolErrorCode} from "./types.js";
import {pruneBySlot, signatureFromBytesNoCheck} from "./utils.js";
/**
* SyncCommittee signatures are only useful during a single slot according to our peer's clocks
*/
const SLOTS_RETAINED = 3;
/**
* The maximum number of distinct `ContributionFast` that will be stored in each slot.
*
* This is a DoS protection measure.
*/
const MAX_ITEMS_PER_SLOT = 512;
type ContributionFast = Omit<altair.SyncCommitteeContribution, "aggregationBits" | "signature"> & {
aggregationBits: BitArray;
signature: Signature;
};
/** Hex string of `contribution.beaconBlockRoot` */
type BlockRootHex = string;
type Subnet = SubcommitteeIndex;
/**
* Preaggregate SyncCommitteeMessage into SyncCommitteeContribution
* and cache seen SyncCommitteeMessage by slot + validator index.
* This stays in-memory and should be pruned per slot.
*/
export class SyncCommitteeMessagePool {
/**
* Each array item is respective to a subcommitteeIndex.
* Preaggregate into SyncCommitteeContribution.
* */
private readonly contributionsByRootBySubnetBySlot = new MapDef<
Slot,
MapDef<Subnet, Map<BlockRootHex, ContributionFast>>
>(() => new MapDef<Subnet, Map<BlockRootHex, ContributionFast>>(() => new Map<BlockRootHex, ContributionFast>()));
private lowestPermissibleSlot = 0;
constructor(
private readonly clock: IClock,
private readonly cutOffSecFromSlot: number,
private readonly preaggregateSlotDistance = 0
) {}
/** Returns current count of unique ContributionFast by block root and subnet */
get size(): number {
let count = 0;
for (const contributionsByRootBySubnet of this.contributionsByRootBySubnetBySlot.values()) {
for (const contributionsByRoot of contributionsByRootBySubnet.values()) {
count += contributionsByRoot.size;
}
}
return count;
}
// TODO: indexInSubcommittee: number should be indicesInSyncCommittee
add(subnet: Subnet, signature: altair.SyncCommitteeMessage, indexInSubcommittee: number): InsertOutcome {
const {slot, beaconBlockRoot} = signature;
const rootHex = toHexString(beaconBlockRoot);
const lowestPermissibleSlot = this.lowestPermissibleSlot;
// Reject if too old.
if (slot < lowestPermissibleSlot) {
return InsertOutcome.Old;
}
// validator gets SyncCommitteeContribution at 2/3 of slot, it's no use to preaggregate later than that time
if (this.clock.secFromSlot(slot) > this.cutOffSecFromSlot) {
return InsertOutcome.Late;
}
// Limit object per slot
const contributionsByRoot = this.contributionsByRootBySubnetBySlot.getOrDefault(slot).getOrDefault(subnet);
if (contributionsByRoot.size >= MAX_ITEMS_PER_SLOT) {
throw new OpPoolError({code: OpPoolErrorCode.REACHED_MAX_PER_SLOT});
}
// Pre-aggregate the contribution with existing items
const contribution = contributionsByRoot.get(rootHex);
if (contribution) {
// Aggregate mutating
return aggregateSignatureInto(contribution, signature, indexInSubcommittee);
} else {
// Create new aggregate
contributionsByRoot.set(rootHex, signatureToAggregate(subnet, signature, indexInSubcommittee));
return InsertOutcome.NewData;
}
}
/**
* This is for the aggregator to produce ContributionAndProof.
*/
getContribution(subnet: SubcommitteeIndex, slot: Slot, prevBlockRoot: Root): altair.SyncCommitteeContribution | null {
const contribution = this.contributionsByRootBySubnetBySlot.get(slot)?.get(subnet)?.get(toHexString(prevBlockRoot));
if (!contribution) {
return null;
}
return {
...contribution,
aggregationBits: contribution.aggregationBits,
signature: contribution.signature.toBytes(),
};
}
/**
* Prune per clock slot.
* SyncCommittee signatures are only useful during a single slot according to our peer's clocks
*/
prune(clockSlot: Slot): void {
pruneBySlot(this.contributionsByRootBySubnetBySlot, clockSlot, SLOTS_RETAINED);
// by default preaggregateSlotDistance is 0, i.e only accept SyncCommitteeMessage in the same clock slot.
this.lowestPermissibleSlot = Math.max(clockSlot - this.preaggregateSlotDistance, 0);
}
}
/**
* Aggregate a new signature into `contribution` mutating it
*/
function aggregateSignatureInto(
contribution: ContributionFast,
signature: altair.SyncCommitteeMessage,
indexInSubcommittee: number
): InsertOutcome {
if (contribution.aggregationBits.get(indexInSubcommittee) === true) {
return InsertOutcome.AlreadyKnown;
}
contribution.aggregationBits.set(indexInSubcommittee, true);
contribution.signature = aggregateSignatures([
contribution.signature,
signatureFromBytesNoCheck(signature.signature),
]);
return InsertOutcome.Aggregated;
}
/**
* Format `signature` into an efficient `contribution` to add more signatures in with aggregateSignatureInto()
*/
function signatureToAggregate(
subnet: number,
signature: altair.SyncCommitteeMessage,
indexInSubcommittee: number
): ContributionFast {
const indexesPerSubnet = Math.floor(SYNC_COMMITTEE_SIZE / SYNC_COMMITTEE_SUBNET_COUNT);
const aggregationBits = BitArray.fromSingleBit(indexesPerSubnet, indexInSubcommittee);
return {
slot: signature.slot,
beaconBlockRoot: signature.beaconBlockRoot,
subcommitteeIndex: subnet,
aggregationBits,
signature: signatureFromBytesNoCheck(signature.signature),
};
}