|
4 | 4 | import { BigInt, ethereum } from '@graphprotocol/graph-ts'; |
5 | 5 | import { assert, describe, newTypedMockEventWithParams, test } from 'matchstick-as/assembly/index'; |
6 | 6 | import { OrdersMatched } from '../../../generated/Core/IexecInterfaceToken'; |
| 7 | +import { App, Workerpool } from '../../../generated/schema'; |
7 | 8 | import { handleOrdersMatched } from '../../../src/Modules'; |
8 | 9 | import { toRLC } from '../../../src/utils'; |
9 | 10 | import { EventParamBuilder } from '../utils/EventParamBuilder'; |
@@ -41,6 +42,29 @@ const sponsor = mockAddress('sponsor'); |
41 | 42 |
|
42 | 43 | describe('IexecPoco', () => { |
43 | 44 | test('Should handle OrdersMatched', () => { |
| 45 | + // Create app and workerpool entities first (they should exist from registry) |
| 46 | + let app = new App(appAddress.toHex()); |
| 47 | + app.owner = appOwner.toHex(); |
| 48 | + app.name = 'TestApp'; |
| 49 | + app.type = 'DOCKER'; |
| 50 | + app.multiaddr = mockBytes32('multiaddr'); |
| 51 | + app.checksum = mockBytes32('checksum'); |
| 52 | + app.mrenclave = mockBytes32('mrenclave'); |
| 53 | + app.timestamp = BigInt.zero(); |
| 54 | + app.usageCount = BigInt.zero(); |
| 55 | + app.lastUsageTimestamp = BigInt.zero(); |
| 56 | + app.save(); |
| 57 | + |
| 58 | + let workerpool = new Workerpool(workerpoolAddress.toHex()); |
| 59 | + workerpool.owner = workerpoolOwner.toHex(); |
| 60 | + workerpool.description = 'TestWorkerpool'; |
| 61 | + workerpool.workerStakeRatio = BigInt.fromI32(50); |
| 62 | + workerpool.schedulerRewardRatio = BigInt.fromI32(10); |
| 63 | + workerpool.timestamp = BigInt.zero(); |
| 64 | + workerpool.usageCount = BigInt.zero(); |
| 65 | + workerpool.lastUsageTimestamp = BigInt.zero(); |
| 66 | + workerpool.save(); |
| 67 | + |
44 | 68 | mockViewDeal(pocoAddress, dealId).returns([buildDefaultDeal()]); |
45 | 69 | // Create the mock event |
46 | 70 | let mockEvent = newTypedMockEventWithParams<OrdersMatched>( |
@@ -104,6 +128,173 @@ describe('IexecPoco', () => { |
104 | 128 | // Assert that a transaction was logged (if applicable) |
105 | 129 | const transactionId = mockEvent.transaction.hash.toHex(); |
106 | 130 | assert.fieldEquals('Transaction', transactionId, 'id', transactionId); |
| 131 | + |
| 132 | + // Assert that app usage statistics were updated |
| 133 | + assert.fieldEquals('App', appAddress.toHex(), 'usageCount', botSize.toString()); |
| 134 | + assert.fieldEquals('App', appAddress.toHex(), 'lastUsageTimestamp', timestamp.toString()); |
| 135 | + |
| 136 | + // Assert that workerpool usage statistics were updated |
| 137 | + assert.fieldEquals( |
| 138 | + 'Workerpool', |
| 139 | + workerpoolAddress.toHex(), |
| 140 | + 'usageCount', |
| 141 | + botSize.toString(), |
| 142 | + ); |
| 143 | + assert.fieldEquals( |
| 144 | + 'Workerpool', |
| 145 | + workerpoolAddress.toHex(), |
| 146 | + 'lastUsageTimestamp', |
| 147 | + timestamp.toString(), |
| 148 | + ); |
| 149 | + }); |
| 150 | + |
| 151 | + test('Should accumulate usage counts for multiple deals', () => { |
| 152 | + const dealId1 = mockBytes32('dealId1'); |
| 153 | + const dealId2 = mockBytes32('dealId2'); |
| 154 | + const timestamp1 = BigInt.fromI32(123456789); |
| 155 | + const timestamp2 = BigInt.fromI32(123456999); |
| 156 | + const botSize1 = BigInt.fromI32(5); |
| 157 | + const botSize2 = BigInt.fromI32(3); |
| 158 | + |
| 159 | + // Create app and workerpool entities first (they should exist from registry) |
| 160 | + let app = new App(appAddress.toHex()); |
| 161 | + app.owner = appOwner.toHex(); |
| 162 | + app.name = 'TestApp'; |
| 163 | + app.type = 'DOCKER'; |
| 164 | + app.multiaddr = mockBytes32('multiaddr'); |
| 165 | + app.checksum = mockBytes32('checksum'); |
| 166 | + app.mrenclave = mockBytes32('mrenclave'); |
| 167 | + app.timestamp = BigInt.zero(); |
| 168 | + app.usageCount = BigInt.zero(); |
| 169 | + app.lastUsageTimestamp = BigInt.zero(); |
| 170 | + app.save(); |
| 171 | + |
| 172 | + let workerpool = new Workerpool(workerpoolAddress.toHex()); |
| 173 | + workerpool.owner = workerpoolOwner.toHex(); |
| 174 | + workerpool.description = 'TestWorkerpool'; |
| 175 | + workerpool.workerStakeRatio = BigInt.fromI32(50); |
| 176 | + workerpool.schedulerRewardRatio = BigInt.fromI32(10); |
| 177 | + workerpool.timestamp = BigInt.zero(); |
| 178 | + workerpool.usageCount = BigInt.zero(); |
| 179 | + workerpool.lastUsageTimestamp = BigInt.zero(); |
| 180 | + workerpool.save(); |
| 181 | + |
| 182 | + // First deal |
| 183 | + mockViewDeal(pocoAddress, dealId1).returns([ |
| 184 | + buildDeal( |
| 185 | + appAddress, |
| 186 | + appOwner, |
| 187 | + appPrice, |
| 188 | + datasetAddress, |
| 189 | + datasetOwner, |
| 190 | + datasetPrice, |
| 191 | + workerpoolAddress, |
| 192 | + workerpoolOwner, |
| 193 | + workerpoolPrice, |
| 194 | + trust, |
| 195 | + category, |
| 196 | + tag, |
| 197 | + requester, |
| 198 | + beneficiary, |
| 199 | + callback, |
| 200 | + params, |
| 201 | + startTime, |
| 202 | + botFirst, |
| 203 | + botSize1, |
| 204 | + workerStake, |
| 205 | + schedulerRewardRatio, |
| 206 | + sponsor, |
| 207 | + ), |
| 208 | + ]); |
| 209 | + |
| 210 | + let mockEvent1 = newTypedMockEventWithParams<OrdersMatched>( |
| 211 | + EventParamBuilder.init() |
| 212 | + .bytes('dealid', dealId1) |
| 213 | + .bytes('appHash', appHash) |
| 214 | + .bytes('datasetHash', datasetHash) |
| 215 | + .bytes('workerpoolHash', workerpoolHash) |
| 216 | + .bytes('requestHash', requestHash) |
| 217 | + .build(), |
| 218 | + ); |
| 219 | + mockEvent1.block.timestamp = timestamp1; |
| 220 | + mockEvent1.address = pocoAddress; |
| 221 | + |
| 222 | + handleOrdersMatched(mockEvent1); |
| 223 | + |
| 224 | + // Verify first deal usage |
| 225 | + assert.fieldEquals('App', appAddress.toHex(), 'usageCount', botSize1.toString()); |
| 226 | + assert.fieldEquals('App', appAddress.toHex(), 'lastUsageTimestamp', timestamp1.toString()); |
| 227 | + assert.fieldEquals( |
| 228 | + 'Workerpool', |
| 229 | + workerpoolAddress.toHex(), |
| 230 | + 'usageCount', |
| 231 | + botSize1.toString(), |
| 232 | + ); |
| 233 | + assert.fieldEquals( |
| 234 | + 'Workerpool', |
| 235 | + workerpoolAddress.toHex(), |
| 236 | + 'lastUsageTimestamp', |
| 237 | + timestamp1.toString(), |
| 238 | + ); |
| 239 | + |
| 240 | + // Second deal |
| 241 | + mockViewDeal(pocoAddress, dealId2).returns([ |
| 242 | + buildDeal( |
| 243 | + appAddress, |
| 244 | + appOwner, |
| 245 | + appPrice, |
| 246 | + datasetAddress, |
| 247 | + datasetOwner, |
| 248 | + datasetPrice, |
| 249 | + workerpoolAddress, |
| 250 | + workerpoolOwner, |
| 251 | + workerpoolPrice, |
| 252 | + trust, |
| 253 | + category, |
| 254 | + tag, |
| 255 | + requester, |
| 256 | + beneficiary, |
| 257 | + callback, |
| 258 | + params, |
| 259 | + startTime, |
| 260 | + botFirst, |
| 261 | + botSize2, |
| 262 | + workerStake, |
| 263 | + schedulerRewardRatio, |
| 264 | + sponsor, |
| 265 | + ), |
| 266 | + ]); |
| 267 | + |
| 268 | + let mockEvent2 = newTypedMockEventWithParams<OrdersMatched>( |
| 269 | + EventParamBuilder.init() |
| 270 | + .bytes('dealid', dealId2) |
| 271 | + .bytes('appHash', mockBytes32('appHash2')) |
| 272 | + .bytes('datasetHash', mockBytes32('datasetHash2')) |
| 273 | + .bytes('workerpoolHash', mockBytes32('workerpoolHash2')) |
| 274 | + .bytes('requestHash', mockBytes32('requestHash2')) |
| 275 | + .build(), |
| 276 | + ); |
| 277 | + mockEvent2.block.timestamp = timestamp2; |
| 278 | + mockEvent2.address = pocoAddress; |
| 279 | + |
| 280 | + handleOrdersMatched(mockEvent2); |
| 281 | + |
| 282 | + // Verify accumulated usage |
| 283 | + const totalUsage = botSize1.plus(botSize2); |
| 284 | + assert.fieldEquals('App', appAddress.toHex(), 'usageCount', totalUsage.toString()); |
| 285 | + assert.fieldEquals('App', appAddress.toHex(), 'lastUsageTimestamp', timestamp2.toString()); |
| 286 | + assert.fieldEquals( |
| 287 | + 'Workerpool', |
| 288 | + workerpoolAddress.toHex(), |
| 289 | + 'usageCount', |
| 290 | + totalUsage.toString(), |
| 291 | + ); |
| 292 | + assert.fieldEquals( |
| 293 | + 'Workerpool', |
| 294 | + workerpoolAddress.toHex(), |
| 295 | + 'lastUsageTimestamp', |
| 296 | + timestamp2.toString(), |
| 297 | + ); |
107 | 298 | }); |
108 | 299 | }); |
109 | 300 |
|
|
0 commit comments