Skip to content

Commit 5fce355

Browse files
committed
fix requireNothing and add tests
1 parent 0dd9f7f commit 5fce355

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/lib/mina/precondition.test.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,50 @@ describe('preconditions', () => {
208208
await expect(tx.sign([feePayer.key]).send()).rejects.toThrow(/unsatisfied/);
209209
});
210210

211+
it('unsatisfied requireEquals should be overridden by requireNothing', async () => {
212+
let nonce = contract.account.nonce.get();
213+
let publicKey = PublicKey.from({ x: Field(-1), isOdd: Bool(false) });
214+
215+
await Mina.transaction(feePayer, async () => {
216+
for (let precondition of implementedNumber) {
217+
let p = precondition().get();
218+
precondition().requireEquals(p.add(1) as any);
219+
precondition().requireNothing();
220+
}
221+
for (let precondition of implementedBool) {
222+
let p = precondition().get();
223+
precondition().requireEquals(p.not());
224+
precondition().requireNothing();
225+
}
226+
contract.account.delegate.requireEquals(publicKey);
227+
contract.account.delegate.requireNothing();
228+
contract.requireSignature();
229+
AccountUpdate.attachToTransaction(contract.self);
230+
})
231+
.sign([feePayer.key, contractAccount.key])
232+
.send();
233+
// check that tx was applied, by checking nonce was incremented
234+
expect(contract.account.nonce.get()).toEqual(nonce.add(1));
235+
});
236+
237+
it('unsatisfied requireBetween should be overridden by requireNothing', async () => {
238+
let nonce = contract.account.nonce.get();
239+
240+
await Mina.transaction(feePayer, async () => {
241+
for (let precondition of implementedWithRange) {
242+
let p: any = precondition().get();
243+
precondition().requireBetween(p.add(20), p.add(30));
244+
precondition().requireNothing();
245+
}
246+
contract.requireSignature();
247+
AccountUpdate.attachToTransaction(contract.self);
248+
})
249+
.sign([feePayer.key, contractAccount.key])
250+
.send();
251+
// check that tx was applied, by checking nonce was incremented
252+
expect(contract.account.nonce.get()).toEqual(nonce.add(1));
253+
});
254+
211255
// TODO: is this a gotcha that should be addressed?
212256
// the test below fails, so it seems that nonce is applied successfully with a WRONG precondition..
213257
// however, this is just because `zkapp.requireSignature()` overwrites the nonce precondition with one that is satisfied
@@ -227,7 +271,6 @@ let implementedNumber = [
227271
() => contract.account.receiptChainHash,
228272
() => contract.network.blockchainLength,
229273
() => contract.network.globalSlotSinceGenesis,
230-
() => contract.network.timestamp,
231274
() => contract.network.minWindowDensity,
232275
() => contract.network.totalCurrency,
233276
() => contract.network.stakingEpochData.epochLength,
@@ -251,6 +294,7 @@ let implementedBool = [
251294
let implemented = [
252295
...implementedNumber,
253296
...implementedBool,
297+
() => contract.network.timestamp,
254298
() => contract.account.delegate,
255299
];
256300
let implementedWithRange = [

src/lib/mina/precondition.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,17 @@ function preconditionSubclass<
381381
) as AnyCondition<U>;
382382
if ('isSome' in property) {
383383
property.isSome = Bool(false);
384-
property.value = fieldType.empty();
384+
if ('lower' in property.value && 'upper' in property.value) {
385+
if (fieldType === UInt64) {
386+
property.value.lower = UInt64.zero as U;
387+
property.value.upper = UInt64.MAXINT() as U;
388+
} else if (fieldType === UInt32) {
389+
property.value.lower = UInt32.zero as U;
390+
property.value.upper = UInt32.MAXINT() as U;
391+
}
392+
} else {
393+
property.value = fieldType.empty();
394+
}
385395
}
386396
context.constrained.add(longKey);
387397
},

0 commit comments

Comments
 (0)