Skip to content

Commit b7ea242

Browse files
committed
fix: fix tests
1 parent 5030c97 commit b7ea242

File tree

6 files changed

+49
-15
lines changed

6 files changed

+49
-15
lines changed

src/fixtures/pvm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ export const disableL1ValidatorTxBytes = () =>
389389
concatBytes(baseTxbytes(), idBytes(), bytesForInt(10), inputBytes());
390390

391391
export const feeState = (): FeeState => ({
392-
capacity: 1n,
392+
capacity: 1_000_000_000n,
393393
excess: 1n,
394394
price: 1n,
395395
timestamp: new Date().toISOString(),

src/vms/pvm/etna-builder/spend-reducers/verifyGasUsage.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ describe('verifyGasUsage', () => {
77
test('returns original state if gas is under the threshold', () => {
88
const initialState = getInitialReducerState();
99
const spendHelper = getSpendHelper();
10-
const spy = vi.spyOn(spendHelper, 'verifyAssetsConsumed');
10+
const spy = vi.spyOn(spendHelper, 'verifyGasUsage');
1111

12-
const state = verifyAssetsConsumed(initialState, spendHelper, testContext);
12+
const state = verifyGasUsage(initialState, spendHelper, testContext);
1313

1414
expect(state).toBe(initialState);
1515
expect(spy).toHaveBeenCalledTimes(1);
@@ -19,14 +19,14 @@ describe('verifyGasUsage', () => {
1919
const initialState = getInitialReducerState();
2020
const spendHelper = getSpendHelper();
2121

22-
// Mock the verifyAssetsConsumed method to throw an error
22+
// Mock the verifyGasUsage method to throw an error
2323
// Testing for this function can be found in the spendHelper.test.ts file
24-
spendHelper.verifyAssetsConsumed = vi.fn(() => {
24+
spendHelper.verifyGasUsage = vi.fn(() => {
2525
throw new Error('Test error');
2626
});
2727

2828
expect(() =>
29-
verifyAssetsConsumed(initialState, spendHelper, testContext),
29+
verifyGasUsage(initialState, spendHelper, testContext),
3030
).toThrow('Test error');
3131
});
3232
});

src/vms/pvm/etna-builder/spend-reducers/verifyGasUsage.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ import type { SpendReducerFunction } from './types';
55
*
66
* Calls the spendHelper's verifyGasUsage method.
77
*/
8-
export const verifyGasUsage: SpendReducerFunction = (
9-
state,
10-
spendHelper,
11-
) => {
8+
export const verifyGasUsage: SpendReducerFunction = (state, spendHelper) => {
129
const verifyError = spendHelper.verifyGasUsage();
1310

1411
if (verifyError) {
1512
throw verifyError;
1613
}
1714

1815
return state;
19-
};
16+
};

src/vms/pvm/etna-builder/spend.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import type { Dimensions } from '../../common/fees/dimensions';
99
import type { Context } from '../../context';
1010
import type { FeeState } from '../models';
1111
import type { SpendReducerFunction, SpendReducerState } from './spend-reducers';
12-
import { handleFeeAndChange, verifyAssetsConsumed, verifyGasUsage } from './spend-reducers';
12+
import {
13+
handleFeeAndChange,
14+
verifyAssetsConsumed,
15+
verifyGasUsage,
16+
} from './spend-reducers';
1317
import { SpendHelper } from './spendHelper';
1418

1519
type SpendResult = Readonly<{
@@ -145,7 +149,7 @@ export const spend = (
145149
...spendReducers,
146150
verifyAssetsConsumed,
147151
handleFeeAndChange,
148-
verifyGasUsage // This should happen after change is added
152+
verifyGasUsage, // This should happen after change is added
149153
// Consolidation and sorting happens in the SpendHelper.
150154
];
151155

src/vms/pvm/etna-builder/spendHelper.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,37 @@ describe('src/vms/pvm/etna-builder/spendHelper', () => {
374374
);
375375
});
376376
});
377+
describe('SpendHelper.verifyGasUsage', () => {
378+
test('returns null when gas is under capacity', () => {
379+
const spendHelper = new SpendHelper({
380+
...DEFAULT_PROPS,
381+
});
382+
383+
const changeOutput = transferableOutput();
384+
385+
spendHelper.addChangeOutput(changeOutput);
386+
387+
expect(spendHelper.verifyGasUsage()).toBe(null);
388+
});
389+
390+
test('returns an error when gas is over capacity', () => {
391+
const spendHelper = new SpendHelper({
392+
...DEFAULT_PROPS,
393+
feeState: {
394+
...DEFAULT_FEE_STATE,
395+
capacity: 0n,
396+
},
397+
});
398+
399+
const changeOutput = transferableOutput();
400+
401+
spendHelper.addChangeOutput(changeOutput);
402+
403+
expect(spendHelper.verifyGasUsage()).toEqual(
404+
new Error('Gas usage of transaction (113) exceeds capacity (0)'),
405+
);
406+
});
407+
});
377408

378409
test('no consolidated outputs when `shouldConsolidateOutputs` is `false`', () => {
379410
const spendHelper = new SpendHelper(DEFAULT_PROPS);

src/vms/pvm/etna-builder/spendHelper.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export class SpendHelper {
296296

297297
return null;
298298
}
299-
299+
300300
/**
301301
* Verifies that gas usage does not exceed the fee state maximum.
302302
*
@@ -305,7 +305,9 @@ export class SpendHelper {
305305
verifyGasUsage(): Error | null {
306306
const gas = this.calculateGas();
307307
if (this.feeState.capacity <= gas) {
308-
return new Error(`Gas usage of transaction (${gas.toString()}) exceeds capacity (${this.feeState.capacity.toString()})`)
308+
return new Error(
309+
`Gas usage of transaction (${gas.toString()}) exceeds capacity (${this.feeState.capacity.toString()})`,
310+
);
309311
}
310312

311313
return null;

0 commit comments

Comments
 (0)