-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathaccept-proposals.test.ts
More file actions
781 lines (665 loc) · 28.3 KB
/
Copy pathaccept-proposals.test.ts
File metadata and controls
781 lines (665 loc) · 28.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
import { createLogger, Logger, SubgraphDeploymentID } from '@graphprotocol/common-ts'
import { DipsManager } from '../dips'
import { PendingRcaConsumer } from '../pending-rca-consumer'
import { DecodedRcaProposal } from '../types'
import {
Allocation,
AllocationManager,
AllocationStatus,
IndexerManagementModels,
IndexingDecisionBasis,
Network,
SubgraphIdentifierType,
} from '@graphprotocol/indexer-common'
let logger: Logger
beforeAll(() => {
logger = createLogger({
name: 'AcceptProposals Test',
async: false,
level: 'error',
})
})
const TEST_DEPLOYMENT_BYTES32 =
'0x0100000000000000000000000000000000000000000000000000000000000000'
function createMockProposal(
overrides: Partial<DecodedRcaProposal> = {},
): DecodedRcaProposal {
const deployment = new SubgraphDeploymentID(
overrides.subgraphDeploymentId?.bytes32 ?? TEST_DEPLOYMENT_BYTES32,
)
return {
id: 'proposal-1',
status: 'pending',
createdAt: new Date(),
signedRca: {
rca: {
deadline: BigInt(Math.floor(Date.now() / 1000) + 3600),
endsAt: BigInt(Math.floor(Date.now() / 1000) + 86400),
payer: '0x1111111111111111111111111111111111111111',
dataService: '0x2222222222222222222222222222222222222222',
serviceProvider: '0x3333333333333333333333333333333333333333',
maxInitialTokens: 10000n,
maxOngoingTokensPerSecond: 100n,
minSecondsPerCollection: 3600n,
maxSecondsPerCollection: 86400n,
conditions: 0n,
nonce: 42n,
metadata: '0x',
},
signature: '0xaabbccdd',
},
signedPayload: new Uint8Array(),
payer: '0x1111111111111111111111111111111111111111',
serviceProvider: '0x3333333333333333333333333333333333333333',
dataService: '0x2222222222222222222222222222222222222222',
deadline: BigInt(Math.floor(Date.now() / 1000) + 3600),
endsAt: BigInt(Math.floor(Date.now() / 1000) + 86400),
maxInitialTokens: 10000n,
maxOngoingTokensPerSecond: 100n,
minSecondsPerCollection: 3600n,
maxSecondsPerCollection: 86400n,
nonce: 42n,
subgraphDeploymentId: deployment,
tokensPerSecond: 1000n,
tokensPerEntityPerSecond: 50n,
...overrides,
}
}
function createMockAllocation(
deploymentBytes32: string = TEST_DEPLOYMENT_BYTES32,
): Allocation {
return {
id: '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
status: AllocationStatus.ACTIVE,
isLegacy: false,
subgraphDeployment: {
id: new SubgraphDeploymentID(deploymentBytes32),
},
indexer: '0x5555555555555555555555555555555555555555',
allocatedTokens: 1000000000000000000n,
createdAt: 0,
createdAtEpoch: 100,
createdAtBlockHash: '0x',
closedAt: 0,
closedAtEpoch: 0,
closedAtEpochStartBlockHash: undefined,
previousEpochStartBlockHash: undefined,
closedAtBlockHash: '0x',
poi: undefined,
queryFeeRebates: 0n,
queryFeesCollected: 0n,
} as Allocation
}
function createMockConsumer(proposals: DecodedRcaProposal[] = []) {
return {
getPendingProposals: jest.fn().mockResolvedValue(proposals),
getPendingProposalsForDeployment: jest.fn().mockResolvedValue([]),
markAccepted: jest.fn().mockResolvedValue(undefined),
markRejected: jest.fn().mockResolvedValue(undefined),
} as unknown as PendingRcaConsumer
}
function createMockModels() {
return {
IndexingRule: {
findOne: jest.fn().mockResolvedValue(null),
findAll: jest.fn().mockResolvedValue([]),
destroy: jest.fn().mockResolvedValue(1),
upsert: jest.fn().mockResolvedValue([{ id: 1 }, true]),
},
} as unknown as IndexerManagementModels
}
function createMockParent() {
return {
matchingRuleExists: jest.fn().mockResolvedValue(false),
} as unknown as AllocationManager
}
function createMockNetwork() {
return {
contracts: {
SubgraphService: {
acceptIndexingAgreement: Object.assign(jest.fn(), {
estimateGas: jest.fn().mockResolvedValue(100000n),
populateTransaction: jest.fn().mockResolvedValue({ data: '0xaccept' }),
}),
startService: {
populateTransaction: jest.fn().mockResolvedValue({ data: '0xstart' }),
},
multicall: Object.assign(jest.fn(), {
estimateGas: jest.fn().mockResolvedValue(200000n),
}),
getAllocation: jest.fn().mockResolvedValue({ createdAt: 0n }),
getLegacyAllocation: jest
.fn()
.mockResolvedValue({ indexer: '0x0000000000000000000000000000000000000000' }),
target: '0x4444444444444444444444444444444444444444',
indexers: jest.fn().mockResolvedValue({ url: 'http://test' }),
},
EpochManager: {
currentEpoch: jest.fn().mockResolvedValue(100n),
},
RewardsManager: {
isDenied: jest.fn().mockResolvedValue(false),
},
},
transactionManager: {
executeTransaction: jest.fn(),
wallet: {
mnemonic: {
phrase: 'test test test test test test test test test test test junk',
},
},
},
networkMonitor: {
currentEpoch: jest.fn().mockResolvedValue(100n),
},
specification: {
indexerOptions: {
address: '0x5555555555555555555555555555555555555555',
enableDips: true,
dipsAllocationAmount: 0n,
defaultAllocationAmount: 10000000000000000000n, // 10 GRT
},
networkIdentifier: 'eip155:1337',
},
isHorizon: { value: jest.fn().mockResolvedValue(true) },
} as unknown as Network
}
function createDipsManager(
network: Network,
models: IndexerManagementModels,
consumer: PendingRcaConsumer,
parent: AllocationManager = createMockParent(),
offerMonitor?: { offerExists: jest.Mock },
): DipsManager {
const graphNode = { ensure: jest.fn().mockResolvedValue(undefined) }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const dm = new DipsManager(logger, models, network, graphNode as any, parent)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(dm as any).pendingRcaConsumer = consumer
if (offerMonitor !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(dm as any).offerMonitor = offerMonitor
}
return dm
}
describe('DipsManager.acceptPendingProposals', () => {
test('rejects proposals with expired deadlines', async () => {
const expiredProposal = createMockProposal({
deadline: BigInt(Math.floor(Date.now() / 1000) - 100), // expired
})
const consumer = createMockConsumer([expiredProposal])
const models = createMockModels()
const network = createMockNetwork()
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([])
expect(consumer.markRejected).toHaveBeenCalledWith(
expiredProposal.id,
'deadline_expired',
)
expect(consumer.markAccepted).not.toHaveBeenCalled()
})
test('cleans up DIPS rule when rejecting last proposal for a deployment', async () => {
const proposal = createMockProposal({
deadline: BigInt(Math.floor(Date.now() / 1000) - 100),
})
const consumer = createMockConsumer([proposal])
// After rejection, no other proposals for this deployment
;(consumer.getPendingProposalsForDeployment as jest.Mock).mockResolvedValue([])
const mockRule = { id: 42 }
const models = createMockModels()
;(models.IndexingRule.findOne as jest.Mock).mockResolvedValue(mockRule)
const network = createMockNetwork()
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([])
expect(models.IndexingRule.destroy).toHaveBeenCalledWith({ where: { id: 42 } })
})
test('does not clean up DIPS rule when other proposals exist for deployment', async () => {
const proposal = createMockProposal({
deadline: BigInt(Math.floor(Date.now() / 1000) - 100),
})
const otherProposal = createMockProposal({ id: 'proposal-2' })
const consumer = createMockConsumer([proposal])
// Another proposal exists for same deployment
;(consumer.getPendingProposalsForDeployment as jest.Mock).mockResolvedValue([
otherProposal,
])
const models = createMockModels()
const network = createMockNetwork()
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([])
expect(models.IndexingRule.destroy).not.toHaveBeenCalled()
})
test('returns early when no pending proposals', async () => {
const consumer = createMockConsumer([])
const models = createMockModels()
const network = createMockNetwork()
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([])
expect(consumer.markAccepted).not.toHaveBeenCalled()
expect(consumer.markRejected).not.toHaveBeenCalled()
})
test('returns early when pendingRcaConsumer is null', async () => {
const models = createMockModels()
const network = createMockNetwork()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const dm = new DipsManager(logger, models, network, {} as any, null)
// Should not throw
await dm.acceptPendingProposals([])
})
describe('with existing allocation', () => {
test('accepts proposal on-chain and marks accepted', async () => {
const proposal = createMockProposal()
const allocation = createMockAllocation()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
const mockReceipt = { hash: '0xtxhash', status: 1 }
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue(
mockReceipt,
)
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([allocation])
expect(network.transactionManager.executeTransaction).toHaveBeenCalled()
expect(consumer.markAccepted).toHaveBeenCalledWith(proposal.id)
})
test('skips acceptance when network is paused', async () => {
const proposal = createMockProposal()
const allocation = createMockAllocation()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue(
'paused',
)
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([allocation])
expect(consumer.markAccepted).not.toHaveBeenCalled()
expect(consumer.markRejected).not.toHaveBeenCalled()
})
test('skips acceptance when unauthorized', async () => {
const proposal = createMockProposal()
const allocation = createMockAllocation()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue(
'unauthorized',
)
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([allocation])
expect(consumer.markAccepted).not.toHaveBeenCalled()
expect(consumer.markRejected).not.toHaveBeenCalled()
})
test('uses multicall path when allocation is for different deployment', async () => {
const proposal = createMockProposal()
const differentDeployment =
'0x0200000000000000000000000000000000000000000000000000000000000000'
const allocation = createMockAllocation(differentDeployment)
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
const mockReceipt = { hash: '0xmulticall', status: 1 }
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue(
mockReceipt,
)
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([allocation])
// Should go to acceptWithNewAllocation (multicall) path, not existing allocation
expect(
network.contracts.SubgraphService.startService.populateTransaction,
).toHaveBeenCalled()
expect(consumer.markAccepted).toHaveBeenCalledWith(proposal.id)
})
})
describe('with new allocation (multicall)', () => {
test('creates allocation and accepts in single multicall', async () => {
const proposal = createMockProposal()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
const mockReceipt = { hash: '0xmulticallhash', status: 1 }
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue(
mockReceipt,
)
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([]) // no active allocations
expect(network.transactionManager.executeTransaction).toHaveBeenCalled()
expect(consumer.markAccepted).toHaveBeenCalledWith(proposal.id)
})
test('skips when allocation already exists on-chain', async () => {
const proposal = createMockProposal()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
// Allocation exists on-chain
;(
network.contracts.SubgraphService.getAllocation as unknown as jest.Mock
).mockResolvedValue({
createdAt: 100n,
})
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([])
expect(consumer.markAccepted).not.toHaveBeenCalled()
expect(network.transactionManager.executeTransaction).not.toHaveBeenCalled()
})
test('uses dipsAllocationAmount for token amount', async () => {
const proposal = createMockProposal()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
const mockReceipt = { hash: '0x', status: 1 }
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue(
mockReceipt,
)
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([])
// Verify startService was called with the configured allocation amount
expect(
network.contracts.SubgraphService.startService.populateTransaction,
).toHaveBeenCalled()
})
test('handles paused network during multicall', async () => {
const proposal = createMockProposal()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue(
'paused',
)
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([])
expect(consumer.markAccepted).not.toHaveBeenCalled()
expect(consumer.markRejected).not.toHaveBeenCalled()
})
})
describe('error handling', () => {
test('rejects proposal on deterministic CALL_EXCEPTION error', async () => {
const proposal = createMockProposal()
const allocation = createMockAllocation()
const consumer = createMockConsumer([proposal])
// After rejection, no remaining proposals for cleanup
;(consumer.getPendingProposalsForDeployment as jest.Mock).mockResolvedValue([])
const models = createMockModels()
const network = createMockNetwork()
;(network.transactionManager.executeTransaction as jest.Mock).mockRejectedValue({
code: 'CALL_EXCEPTION',
data: '0x',
})
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([allocation])
expect(consumer.markRejected).toHaveBeenCalledWith(proposal.id, expect.any(String))
})
test('leaves proposal pending on transient network error', async () => {
const proposal = createMockProposal()
const allocation = createMockAllocation()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
;(network.transactionManager.executeTransaction as jest.Mock).mockRejectedValue(
new Error('ECONNRESET'),
)
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([allocation])
expect(consumer.markRejected).not.toHaveBeenCalled()
expect(consumer.markAccepted).not.toHaveBeenCalled()
})
test('continues processing other proposals after error', async () => {
const failProposal = createMockProposal({ id: 'fail-1' })
const okDeployment =
'0x0200000000000000000000000000000000000000000000000000000000000000'
const okProposal = createMockProposal({
id: 'ok-1',
subgraphDeploymentId: new SubgraphDeploymentID(okDeployment),
})
const consumer = createMockConsumer([failProposal, okProposal])
const models = createMockModels()
const network = createMockNetwork()
const failAllocation = {
id: '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
status: AllocationStatus.ACTIVE,
isLegacy: false,
subgraphDeployment: {
id: new SubgraphDeploymentID(TEST_DEPLOYMENT_BYTES32),
},
} as Allocation
const okAllocation = {
id: '0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
status: AllocationStatus.ACTIVE,
isLegacy: false,
subgraphDeployment: {
id: new SubgraphDeploymentID(okDeployment),
},
} as Allocation
const mockReceipt = { hash: '0x', status: 1 }
;(network.transactionManager.executeTransaction as jest.Mock)
.mockRejectedValueOnce(new Error('first fails'))
.mockResolvedValueOnce(mockReceipt) // second succeeds
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([failAllocation, okAllocation])
// Second proposal should still be processed and accepted
expect(consumer.markAccepted).toHaveBeenCalledWith('ok-1')
})
})
describe('allocation amount selection', () => {
test('uses defaultAllocationAmount for rewarded (not denied) subgraph', async () => {
const proposal = createMockProposal()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
;(
network.contracts.RewardsManager.isDenied as unknown as jest.Mock
).mockResolvedValue(false)
const mockReceipt = { hash: '0x', status: 1 }
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue(
mockReceipt,
)
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([])
// Should check isDenied
expect(network.contracts.RewardsManager.isDenied).toHaveBeenCalledWith(
proposal.subgraphDeploymentId.bytes32,
)
// startService should be called (new allocation path)
expect(
network.contracts.SubgraphService.startService.populateTransaction,
).toHaveBeenCalled()
})
test('uses dipsAllocationAmount (0) for denied subgraph', async () => {
const proposal = createMockProposal()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
;(
network.contracts.RewardsManager.isDenied as unknown as jest.Mock
).mockResolvedValue(true)
const mockReceipt = { hash: '0x', status: 1 }
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue(
mockReceipt,
)
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([])
expect(network.contracts.RewardsManager.isDenied).toHaveBeenCalledWith(
proposal.subgraphDeploymentId.bytes32,
)
expect(consumer.markAccepted).toHaveBeenCalledWith(proposal.id)
})
test('uses per-deployment rule amount for rewarded subgraph with existing rule', async () => {
const proposal = createMockProposal()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const ruleAmount = 5000000000000000000n // 5 GRT
;(models.IndexingRule.findOne as jest.Mock).mockResolvedValue({
allocationAmount: ruleAmount.toString(),
})
const network = createMockNetwork()
;(
network.contracts.RewardsManager.isDenied as unknown as jest.Mock
).mockResolvedValue(false)
const mockReceipt = { hash: '0x', status: 1 }
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue(
mockReceipt,
)
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([])
expect(consumer.markAccepted).toHaveBeenCalledWith(proposal.id)
})
test('uses custom dipsAllocationAmount for denied subgraph with override', async () => {
const proposal = createMockProposal()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
// Override dipsAllocationAmount to non-zero
;(
network.specification.indexerOptions as { dipsAllocationAmount: bigint }
).dipsAllocationAmount = 1000000000000000000n // 1 GRT
;(
network.contracts.RewardsManager.isDenied as unknown as jest.Mock
).mockResolvedValue(true)
const mockReceipt = { hash: '0x', status: 1 }
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue(
mockReceipt,
)
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([])
expect(consumer.markAccepted).toHaveBeenCalledWith(proposal.id)
})
})
describe('rule creation ordering (race condition fix)', () => {
test('upserts the DIPS indexing rule before broadcasting acceptIndexingAgreement', async () => {
const proposal = createMockProposal()
const allocation = createMockAllocation()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue({
hash: '0xtx',
status: 1,
})
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([allocation])
const upsertOrder = (models.IndexingRule.upsert as jest.Mock).mock
.invocationCallOrder[0]
const executeOrder = (network.transactionManager.executeTransaction as jest.Mock)
.mock.invocationCallOrder[0]
expect(upsertOrder).toBeDefined()
expect(executeOrder).toBeDefined()
expect(upsertOrder).toBeLessThan(executeOrder)
})
test('skips rule upsert and rejects proposal when deployment is blocklisted', async () => {
const proposal = createMockProposal()
const allocation = createMockAllocation()
const consumer = createMockConsumer([proposal])
;(consumer.getPendingProposalsForDeployment as jest.Mock).mockResolvedValue([])
const models = createMockModels()
;(models.IndexingRule.findAll as jest.Mock).mockResolvedValue([
{
identifier: proposal.subgraphDeploymentId.ipfsHash,
identifierType: SubgraphIdentifierType.DEPLOYMENT,
decisionBasis: IndexingDecisionBasis.NEVER,
},
])
const network = createMockNetwork()
const dm = createDipsManager(network, models, consumer)
await dm.acceptPendingProposals([allocation])
expect(consumer.markRejected).toHaveBeenCalledWith(
proposal.id,
'deployment blocklisted',
)
expect(models.IndexingRule.upsert).not.toHaveBeenCalled()
expect(network.transactionManager.executeTransaction).not.toHaveBeenCalled()
})
test('skips rule upsert when parent reports a matching rule already exists', async () => {
const proposal = createMockProposal()
const allocation = createMockAllocation()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue({
hash: '0xtx',
status: 1,
})
const parent = {
matchingRuleExists: jest.fn().mockResolvedValue(true),
} as unknown as AllocationManager
const dm = createDipsManager(network, models, consumer, parent)
await dm.acceptPendingProposals([allocation])
expect(models.IndexingRule.upsert).not.toHaveBeenCalled()
expect(network.transactionManager.executeTransaction).toHaveBeenCalled()
expect(consumer.markAccepted).toHaveBeenCalledWith(proposal.id)
})
})
describe('offer-existence gate', () => {
test('stays pending when offer absent and deadline > now + safety margin', async () => {
const proposal = createMockProposal({
// 5 minutes from now — well beyond the 30s safety margin
deadline: BigInt(Math.floor(Date.now() / 1000) + 300),
})
const allocation = createMockAllocation()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
const offerMonitor = { offerExists: jest.fn().mockResolvedValue(false) }
const dm = createDipsManager(network, models, consumer, undefined, offerMonitor)
await dm.acceptPendingProposals([allocation])
expect(offerMonitor.offerExists).toHaveBeenCalledWith(proposal.id)
expect(network.transactionManager.executeTransaction).not.toHaveBeenCalled()
expect(consumer.markRejected).not.toHaveBeenCalled()
expect(consumer.markAccepted).not.toHaveBeenCalled()
})
test('marks rejected when offer absent and deadline within safety margin', async () => {
const proposal = createMockProposal({
// 10 seconds from now — inside the 30s safety margin
deadline: BigInt(Math.floor(Date.now() / 1000) + 10),
})
const allocation = createMockAllocation()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
const offerMonitor = { offerExists: jest.fn().mockResolvedValue(false) }
const dm = createDipsManager(network, models, consumer, undefined, offerMonitor)
await dm.acceptPendingProposals([allocation])
expect(offerMonitor.offerExists).toHaveBeenCalledWith(proposal.id)
expect(consumer.markRejected).toHaveBeenCalledWith(
proposal.id,
'offer_never_landed',
)
expect(network.transactionManager.executeTransaction).not.toHaveBeenCalled()
})
test('proceeds to acceptIndexingAgreement when offer is present', async () => {
const proposal = createMockProposal()
const allocation = createMockAllocation()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue({
hash: '0xtxhash',
status: 1,
})
const offerMonitor = { offerExists: jest.fn().mockResolvedValue(true) }
const dm = createDipsManager(network, models, consumer, undefined, offerMonitor)
await dm.acceptPendingProposals([allocation])
expect(offerMonitor.offerExists).toHaveBeenCalledWith(proposal.id)
expect(network.transactionManager.executeTransaction).toHaveBeenCalled()
expect(consumer.markAccepted).toHaveBeenCalledWith(proposal.id)
})
test('bypasses gate when indexingPaymentsSubgraph is not configured', async () => {
const proposal = createMockProposal()
const allocation = createMockAllocation()
const consumer = createMockConsumer([proposal])
const models = createMockModels()
const network = createMockNetwork()
;(network.transactionManager.executeTransaction as jest.Mock).mockResolvedValue({
hash: '0xtxhash',
status: 1,
})
// No offerMonitor passed — DipsManager constructor sets it to null
// because createMockNetwork() does not define indexingPaymentsSubgraph.
const dm = createDipsManager(network, models, consumer)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((dm as any).offerMonitor).toBeNull()
await dm.acceptPendingProposals([allocation])
expect(network.transactionManager.executeTransaction).toHaveBeenCalled()
expect(consumer.markAccepted).toHaveBeenCalledWith(proposal.id)
})
})
})