Skip to content

Commit

Permalink
fix: use explicit read transactions (#10911)
Browse files Browse the repository at this point in the history
Open and hold a read txn open until the entire iterator is consumed.

---------

Co-authored-by: ludamad <[email protected]>
Co-authored-by: ludamad <[email protected]>
  • Loading branch information
3 people authored Jan 3, 2025
1 parent 8debec2 commit 2a8e01c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,7 @@ describe('e2e_fees private_payment', () => {
});

// TODO(#7694): Remove this test once the lacking feature in TXE is implemented.
// TODO(#10775): Reenable, hit e.g. https://github.com/AztecProtocol/aztec-packages/actions/runs/12419409370/job/34675397831
it.skip('insufficient funded amount is correctly handled', async () => {
it('insufficient funded amount is correctly handled', async () => {
// We call arbitrary `private_get_name(...)` function just to check the correct error is triggered.
await expect(
bananaCoin.methods.private_get_name().prove({
Expand Down
22 changes: 14 additions & 8 deletions yarn-project/kv-store/src/lmdb/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,20 @@ export class LmdbAztecArray<T> implements AztecArray<T>, AztecAsyncArray<T> {
}

*entries(): IterableIterator<[number, T]> {
const values = this.#db.getRange({
start: this.#slot(0),
limit: this.length,
});

for (const { key, value } of values) {
const index = key[3];
yield [index, value];
const transaction = this.#db.useReadTransaction();
try {
const values = this.#db.getRange({
start: this.#slot(0),
limit: this.length,
transaction,
});

for (const { key, value } of values) {
const index = key[3];
yield [index, value];
}
} finally {
transaction.done();
}
}

Expand Down
80 changes: 47 additions & 33 deletions yarn-project/kv-store/src/lmdb/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
}

*getValues(key: K): IterableIterator<V> {
const values = this.db.getValues(this.slot(key));
for (const value of values) {
yield value?.[1];
const transaction = this.db.useReadTransaction();
try {
const values = this.db.getValues(this.slot(key), {
transaction,
});
for (const value of values) {
yield value?.[1];
}
} finally {
transaction.done();
}
}

Expand Down Expand Up @@ -88,38 +95,45 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
}

*entries(range: Range<K> = {}): IterableIterator<[K, V]> {
const { reverse = false, limit } = range;
// LMDB has a quirk where it expects start > end when reverse=true
// in that case, we need to swap the start and end sentinels
const start = reverse
? range.end
? this.slot(range.end)
: this.endSentinel
: range.start
? this.slot(range.start)
: this.startSentinel;

const end = reverse
? range.start
const transaction = this.db.useReadTransaction();

try {
const { reverse = false, limit } = range;
// LMDB has a quirk where it expects start > end when reverse=true
// in that case, we need to swap the start and end sentinels
const start = reverse
? range.end
? this.slot(range.end)
: this.endSentinel
: range.start
? this.slot(range.start)
: this.startSentinel
: range.end
? this.slot(range.end)
: this.endSentinel;
: this.startSentinel;

const lmdbRange: RangeOptions = {
start,
end,
reverse,
limit,
};

const iterator = this.db.getRange(lmdbRange);

for (const {
value: [key, value],
} of iterator) {
yield [key, value];
const end = reverse
? range.start
? this.slot(range.start)
: this.startSentinel
: range.end
? this.slot(range.end)
: this.endSentinel;

const lmdbRange: RangeOptions = {
start,
end,
reverse,
limit,
transaction,
};

const iterator = this.db.getRange(lmdbRange);

for (const {
value: [key, value],
} of iterator) {
yield [key, value];
}
} finally {
transaction.done();
}
}

Expand Down

0 comments on commit 2a8e01c

Please sign in to comment.