Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions yarn-project/foundation/src/curves/bn254/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,18 @@ abstract class BaseField {
}

cmp(rhs: BaseField): -1 | 0 | 1 {
const rhsBigInt = rhs.asBigInt;
return this.asBigInt === rhsBigInt ? 0 : this.asBigInt < rhsBigInt ? -1 : 1;
return BaseField.cmpAsBigInt(this.asBigInt, rhs.asBigInt);
}

static cmp(lhs: BaseField, rhs: BaseField): -1 | 0 | 1 {
return lhs.cmp(rhs);
}

// Actual bigint comparison. Arguments must have been validated previously.
static cmpAsBigInt(lhs: bigint, rhs: bigint): -1 | 0 | 1 {
return lhs === rhs ? 0 : lhs < rhs ? -1 : 1;
}

isZero(): boolean {
return this.asBigInt === 0n;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BlockHeader } from '@aztec/stdlib/tx';

import { type MockProxy, mock } from 'jest-mock-extended';

import { type TxMetaData, stubTxMetaValidationData } from '../tx_metadata.js';
import { type TxMetaData, stubTxMetaData } from '../tx_metadata.js';
import { EvictionManager } from './eviction_manager.js';
import {
EvictionEvent,
Expand Down Expand Up @@ -174,19 +174,7 @@ describe('EvictionManager', () => {
let preAddRule: MockProxy<PreAddRule>;
let poolAccess: MockProxy<PreAddPoolAccess>;

const createMeta = (txHash: string, priorityFee: bigint): TxMetaData => ({
txHash,
anchorBlockHeaderHash: '0x1234',
priorityFee,
feePayer: '0xfeepayer',
claimAmount: 0n,
feeLimit: 100n,
nullifiers: [`0x${txHash.slice(2)}null1`],
expirationTimestamp: 0n,
receivedAt: 0,
estimatedSizeBytes: 0,
data: stubTxMetaValidationData(),
});
const createMeta = (txHash: string, priorityFee: bigint): TxMetaData => stubTxMetaData(txHash, { priorityFee });

beforeEach(() => {
preAddRule = mock<PreAddRule>({ name: 'preAddRule' });
Expand Down Expand Up @@ -330,19 +318,7 @@ describe('EvictionManager', () => {
const preAddRule2 = mock<PreAddRule>({ name: 'secondRule' });
const poolAccess = mock<PreAddPoolAccess>();

const createMeta = (txHash: string, priorityFee: bigint): TxMetaData => ({
txHash,
anchorBlockHeaderHash: '0x1234',
priorityFee,
feePayer: '0xfeepayer',
claimAmount: 0n,
feeLimit: 100n,
nullifiers: [`0x${txHash.slice(2)}null1`],
expirationTimestamp: 0n,
receivedAt: 0,
estimatedSizeBytes: 0,
data: stubTxMetaValidationData(),
});
const createMeta = (txHash: string, priorityFee: bigint): TxMetaData => stubTxMetaData(txHash, { priorityFee });

preAddRule1.check.mockRejectedValue(new Error('Rule failed'));
preAddRule2.check.mockResolvedValue({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { BlockHeader, GlobalVariables } from '@aztec/stdlib/tx';
import { jest } from '@jest/globals';
import { type MockProxy, mock } from 'jest-mock-extended';

import { type TxMetaData, stubTxMetaValidationData } from '../tx_metadata.js';
import { type TxMetaData, stubTxMetaData } from '../tx_metadata.js';
import { FeePayerBalanceEvictionRule } from './fee_payer_balance_eviction_rule.js';
import type { EvictionContext, PoolOperations } from './interfaces.js';
import { EvictionEvent } from './interfaces.js';
Expand All @@ -33,19 +33,7 @@ describe('FeePayerBalanceEvictionRule', () => {
claimAmount?: bigint;
feePayer?: string;
} = {},
): TxMetaData => ({
txHash,
anchorBlockHeaderHash: '0x1234',
priorityFee: opts.priorityFee ?? 100n,
feePayer: opts.feePayer ?? feePayer1,
claimAmount: opts.claimAmount ?? 0n,
feeLimit: opts.feeLimit ?? 100n,
nullifiers: [`0x${txHash.slice(2)}null1`],
expirationTimestamp: 0n,
receivedAt: 0,
estimatedSizeBytes: 0,
data: stubTxMetaValidationData(),
});
) => stubTxMetaData(txHash, { feePayer: feePayer1, ...opts });

// Create mock pool operations
const createPoolOps = (txsByFeePayer: Map<string, TxMetaData[]>): PoolOperations => {
Expand Down Expand Up @@ -144,8 +132,8 @@ describe('FeePayerBalanceEvictionRule', () => {
const result = await rule.evict(context, pool);

expect(result.success).toBe(true);
expect(result.txsEvicted).toEqual(['0x1111']); // Low priority evicted
expect(deleteTxsMock).toHaveBeenCalledWith(['0x1111'], 'FeePayerBalanceEviction');
expect(result.txsEvicted).toEqual([lowPriorityMeta.txHash]); // Low priority evicted
expect(deleteTxsMock).toHaveBeenCalledWith([lowPriorityMeta.txHash], 'FeePayerBalanceEviction');
});

it('evicts multiple low-priority txs when balance is insufficient', async () => {
Expand All @@ -160,17 +148,17 @@ describe('FeePayerBalanceEvictionRule', () => {

const context: EvictionContext = {
event: EvictionEvent.TXS_ADDED,
newTxHashes: ['0x1111', '0x2222', '0x3333'],
newTxHashes: [lowMeta.txHash, medMeta.txHash, highMeta.txHash],
feePayers: [feePayer1],
};

const result = await rule.evict(context, pool);

expect(result.success).toBe(true);
// Both low and medium priority should be evicted
expect(result.txsEvicted).toContain('0x1111');
expect(result.txsEvicted).toContain('0x2222');
expect(result.txsEvicted).not.toContain('0x3333');
expect(result.txsEvicted).toContain(lowMeta.txHash);
expect(result.txsEvicted).toContain(medMeta.txHash);
expect(result.txsEvicted).not.toContain(highMeta.txHash);
});

it('priority ordering is correct - highest priority gets funded first', async () => {
Expand All @@ -186,15 +174,15 @@ describe('FeePayerBalanceEvictionRule', () => {

const context: EvictionContext = {
event: EvictionEvent.TXS_ADDED,
newTxHashes: ['0xaaaa', '0xbbbb', '0xcccc'],
newTxHashes: [tx10.txHash, tx50.txHash, tx100.txHash],
feePayers: [feePayer1],
};

const result = await rule.evict(context, pool);

expect(result.success).toBe(true);
expect(result.txsEvicted).toEqual(['0xaaaa']); // Only lowest priority evicted
expect(deleteTxsMock).toHaveBeenCalledWith(['0xaaaa'], 'FeePayerBalanceEviction');
expect(result.txsEvicted).toEqual([tx10.txHash]); // Only lowest priority evicted
expect(deleteTxsMock).toHaveBeenCalledWith([tx10.txHash], 'FeePayerBalanceEviction');
});

it('considers claim amount when calculating available balance', async () => {
Expand Down Expand Up @@ -249,7 +237,7 @@ describe('FeePayerBalanceEvictionRule', () => {
const result = await rule.evict(context, pool);

expect(result.success).toBe(true);
expect(result.txsEvicted).toEqual(['0x2222']); // Low priority evicted
expect(result.txsEvicted).toEqual([lowMeta.txHash]); // Low priority evicted
});

it('handles zero balance', async () => {
Expand All @@ -268,7 +256,7 @@ describe('FeePayerBalanceEvictionRule', () => {
const result = await rule.evict(context, pool);

expect(result.success).toBe(true);
expect(result.txsEvicted).toEqual(['0x1111']);
expect(result.txsEvicted).toEqual([meta.txHash]);
});

it('handles empty fee payers list', async () => {
Expand Down Expand Up @@ -347,7 +335,7 @@ describe('FeePayerBalanceEvictionRule', () => {
const result = await rule.evict(context, pool);

expect(result.success).toBe(true);
expect(result.txsEvicted).toEqual(['0x1111']);
expect(result.txsEvicted).toEqual([lowMeta.txHash]);
});
});

Expand Down Expand Up @@ -396,7 +384,7 @@ describe('FeePayerBalanceEvictionRule', () => {
const result = await rule.evict(context, pool);

expect(result.success).toBe(true);
expect(result.txsEvicted).toEqual(['0x1111']);
expect(result.txsEvicted).toEqual([meta.txHash]);
});
});

Expand Down Expand Up @@ -474,18 +462,18 @@ describe('FeePayerBalanceEvictionRule', () => {

const context: EvictionContext = {
event: EvictionEvent.TXS_ADDED,
newTxHashes: ['0xaaaa', '0xbbbb', '0xcccc'],
newTxHashes: [tx1.txHash, tx2.txHash, tx3.txHash],
feePayers: [feePayer1],
};

const result = await rule.evict(context, pool);

expect(result.success).toBe(true);
// tx1 (lowest priority) should be evicted
expect(result.txsEvicted).toEqual(['0xaaaa']);
expect(result.txsEvicted).toEqual([tx1.txHash]);
// tx2 and tx3 should be kept
expect(result.txsEvicted).not.toContain('0xbbbb');
expect(result.txsEvicted).not.toContain('0xcccc');
expect(result.txsEvicted).not.toContain(tx2.txHash);
expect(result.txsEvicted).not.toContain(tx3.txHash);
});

it('uses txHash as tiebreaker when priorities are equal', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type TxMetaData, stubTxMetaValidationData } from '../tx_metadata.js';
import { type TxMetaData, stubTxMetaData } from '../tx_metadata.js';
import { FeePayerBalancePreAddRule } from './fee_payer_balance_pre_add_rule.js';
import { type PreAddPoolAccess, TxPoolRejectionCode } from './interfaces.js';

Expand All @@ -14,19 +14,7 @@ describe('FeePayerBalancePreAddRule', () => {
claimAmount?: bigint;
feePayer?: string;
} = {},
): TxMetaData => ({
txHash,
anchorBlockHeaderHash: '0x1234',
priorityFee: opts.priorityFee ?? 100n,
feePayer: opts.feePayer ?? '0xfeepayer',
claimAmount: opts.claimAmount ?? 0n,
feeLimit: opts.feeLimit ?? 100n,
nullifiers: [`0x${txHash.slice(2)}null1`],
expirationTimestamp: 0n,
receivedAt: 0,
estimatedSizeBytes: 0,
data: stubTxMetaValidationData(),
});
) => stubTxMetaData(txHash, opts);

// Mock pool access with configurable behavior
const createPoolAccess = (balance: bigint, existingTxs: TxMetaData[] = []): PreAddPoolAccess => ({
Expand Down Expand Up @@ -127,7 +115,7 @@ describe('FeePayerBalancePreAddRule', () => {
const result = await rule.check(incomingMeta, poolAccess);

expect(result.shouldIgnore).toBe(false);
expect(result.txHashesToEvict).toContain('0x2222');
expect(result.txHashesToEvict).toContain(existingMeta.txHash);
});

it('evicts multiple lower-priority txs when high-priority tx is added', async () => {
Expand All @@ -141,8 +129,8 @@ describe('FeePayerBalancePreAddRule', () => {
const result = await rule.check(incomingMeta, poolAccess);

expect(result.shouldIgnore).toBe(false);
expect(result.txHashesToEvict).toContain('0x2222');
expect(result.txHashesToEvict).toContain('0x3333');
expect(result.txHashesToEvict).toContain(existingMeta1.txHash);
expect(result.txHashesToEvict).toContain(existingMeta2.txHash);
});

it('handles priority ordering correctly - highest priority gets funded first', async () => {
Expand All @@ -157,9 +145,9 @@ describe('FeePayerBalancePreAddRule', () => {
const result = await rule.check(incomingMeta, poolAccess);

expect(result.shouldIgnore).toBe(false);
expect(result.txHashesToEvict).toContain('0x2222'); // Low priority evicted
expect(result.txHashesToEvict).not.toContain('0x4444'); // High priority kept
expect(result.txHashesToEvict).not.toContain('0x3333'); // Med priority kept
expect(result.txHashesToEvict).toContain(lowPriorityMeta.txHash); // Low priority evicted
expect(result.txHashesToEvict).not.toContain(highPriorityMeta.txHash); // High priority kept
expect(result.txHashesToEvict).not.toContain(medPriorityMeta.txHash); // Med priority kept
});
});

Expand Down Expand Up @@ -245,7 +233,7 @@ describe('FeePayerBalancePreAddRule', () => {

expect(result.shouldIgnore).toBe(false);
expect(result.txHashesToEvict).toHaveLength(1);
expect(result.txHashesToEvict[0]).toEqual('0x2222');
expect(result.txHashesToEvict[0]).toEqual(existingMeta.txHash);
});

it('returns empty eviction list when no evictions needed', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,23 @@ export class FeePayerBalancePreAddRule implements PreAddRule {
// Create combined list with incoming tx
const allTxs: Array<{
txHash: string;
txHashBigInt: bigint;
priorityFee: bigint;
feeLimit: bigint;
claimAmount: bigint;
isIncoming: boolean;
}> = [
...existingTxs.map(t => ({
txHash: t.txHash,
txHashBigInt: t.txHashBigInt,
priorityFee: t.priorityFee,
feeLimit: t.feeLimit,
claimAmount: t.claimAmount,
isIncoming: false,
})),
{
txHash: incomingMeta.txHash,
txHashBigInt: incomingMeta.txHashBigInt,
priorityFee: incomingMeta.priorityFee,
feeLimit: incomingMeta.feeLimit,
claimAmount: incomingMeta.claimAmount,
Expand Down
Loading
Loading