more checks

This commit is contained in:
Christien Rioux 2023-07-30 01:32:01 -04:00
parent 8952751cb0
commit 1907a15b0b

View file

@ -32,11 +32,11 @@ class DHTShortArray {
case DHTSchemaSMPL(): case DHTSchemaSMPL():
throw StateError('Wrote kind of DHT record for DHTShortArray'); throw StateError('Wrote kind of DHT record for DHTShortArray');
} }
assert(stride <= MAX_ELEMENTS, 'stride too long'); assert(stride <= maxElements, 'stride too long');
_stride = stride; _stride = stride;
} }
static const MAX_ELEMENTS = 256; static const maxElements = 256;
// Head DHT record // Head DHT record
final DHTRecord _headRecord; final DHTRecord _headRecord;
@ -47,7 +47,7 @@ class DHTShortArray {
static Future<DHTShortArray> create(VeilidRoutingContext dhtctx, int stride, static Future<DHTShortArray> create(VeilidRoutingContext dhtctx, int stride,
{DHTRecordCrypto? crypto}) async { {DHTRecordCrypto? crypto}) async {
assert(stride <= MAX_ELEMENTS, 'stride too long'); assert(stride <= maxElements, 'stride too long');
final dhtRecord = await DHTRecord.create(dhtctx, final dhtRecord = await DHTRecord.create(dhtctx,
schema: DHTSchema.dflt(oCnt: stride + 1), crypto: crypto); schema: DHTSchema.dflt(oCnt: stride + 1), crypto: crypto);
try { try {
@ -116,14 +116,17 @@ class DHTShortArray {
List<Typed<FixedEncodedString43>> linkedKeys, List<int> index) { List<Typed<FixedEncodedString43>> linkedKeys, List<int> index) {
// Ensure nothing is duplicated in the linked keys set // Ensure nothing is duplicated in the linked keys set
final newKeys = linkedKeys.toSet(); final newKeys = linkedKeys.toSet();
assert(newKeys.length <= (maxElements + (_stride - 1)) ~/ _stride,
'too many keys');
assert(newKeys.length == linkedKeys.length, 'duplicated linked keys'); assert(newKeys.length == linkedKeys.length, 'duplicated linked keys');
final newIndex = index.toSet(); final newIndex = index.toSet();
assert(newIndex.length == newIndex.length, 'duplicated index locations'); assert(newIndex.length <= maxElements, 'too many indexes');
assert(newIndex.length == index.length, 'duplicated index locations');
// Ensure all the index keys fit into the existing records // Ensure all the index keys fit into the existing records
final indexCount = (linkedKeys.length + 1) * _stride; final indexCapacity = (linkedKeys.length + 1) * _stride;
int? maxIndex; int? maxIndex;
for (final idx in newIndex) { for (final idx in newIndex) {
assert(idx >= 0 || idx < indexCount, 'index out of range'); assert(idx >= 0 || idx < indexCapacity, 'index out of range');
if (maxIndex == null || idx > maxIndex) { if (maxIndex == null || idx > maxIndex) {
maxIndex = idx; maxIndex = idx;
} }