Skip to content
Draft
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
12 changes: 11 additions & 1 deletion src/components/c2d/compute_engine_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,23 @@ export abstract class C2DEngine {
token: string,
maxJobDuration: number
): number | null {
CORE_LOGGER.logMessage(`minJobDuration ${env?.minJobDuration}`)

if (maxJobDuration < env.minJobDuration) maxJobDuration = env.minJobDuration
CORE_LOGGER.logMessage(`Env maxJobDuration ${maxJobDuration}`)

const prices = this.getEnvPricesForToken(env, chainId, token)
if (!prices) return null
let cost: number = 0
for (const request of resourcesRequest) {
CORE_LOGGER.logMessage(`Resource ${request.id} with amount ${request.amount}`)

const resourcePrice = this.getResourcePrice(prices, request.id)
cost += resourcePrice * request.amount * Math.ceil(maxJobDuration / 60)
CORE_LOGGER.logMessage(`Resource price ${resourcePrice}`)
const resourceCost = resourcePrice * request.amount * Math.ceil(maxJobDuration / 60)
CORE_LOGGER.logMessage(`Resource cost ${resourceCost}`)

cost += resourceCost
}
return cost
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/core/compute/startCompute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class PaidComputeStartHandler extends CommandHandler {
if (!task.queueMaxWaitTime) {
task.queueMaxWaitTime = 0
}

const authValidationResponse = await this.validateTokenOrSignature(
task.authorization,
task.consumerAddress,
Expand Down Expand Up @@ -522,6 +523,7 @@ export class PaidComputeStartHandler extends CommandHandler {
// job ID unicity
const jobId = generateUniqueID(s)
// let's calculate payment needed based on resources request and maxJobDuration
CORE_LOGGER.logMessage(`MaxJobDuration received ${task.maxJobDuration}`)
const cost = engine.calculateResourcesCost(
task.resources,
env,
Expand Down
22 changes: 22 additions & 0 deletions src/components/core/utils/escrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export class Escrow {
}

getMinLockTime(maxJobDuration: number) {
console.log(
`maxJobDuration ${maxJobDuration} and claimDurationTimeout ${this.claimDurationTimeout}`
)
return maxJobDuration + this.claimDurationTimeout
}

Expand Down Expand Up @@ -150,13 +153,21 @@ export class Escrow {
amount: number,
expiry: BigNumberish
): Promise<string | null> {
console.log(`--> createLock expiry ${expiry}`)
const jobId = create256Hash(job)
const blockchain = this.getBlockchain(chain)
const signer = await blockchain.getSigner()
const contract = this.getContract(chain, signer)
if (!contract) throw new Error(`Failed to initialize escrow contract`)
const wei = await this.getPaymentAmountInWei(amount, chain, token)
const userBalance = await this.getUserAvailableFunds(chain, payer, token)
console.log(`--> createLock userBalance ${userBalance}`)
console.log(`--> createLock userBalance ${BigInt(userBalance.toString())}`)
CORE_LOGGER.logMessage(
`User balance ${BigInt(userBalance.toString())} and payment amount in wei ${BigInt(wei)}`
)
console.log(`--> createLock wei ${wei}`)
console.log(`--> createLock wei ${BigInt(wei)}`)
if (BigInt(userBalance.toString()) < BigInt(wei)) {
// not enough funds
throw new Error(`User ${payer} does not have enough funds`)
Expand Down Expand Up @@ -189,15 +200,26 @@ export class Escrow {
} authorizations.`
)
}
console.log(
`--> createLock auths[0].currentLockedAmount ${auths[0].currentLockedAmount}`
)
console.log(`--> createLock auths[0].maxLockedAmount ${auths[0].maxLockedAmount}`)

console.log(
`--> createLock BigInt(auths[0].currentLockedAmount.toString()) + BigInt(wei) ${BigInt(auths[0].currentLockedAmount.toString()) + BigInt(wei)}`
)
if (
BigInt(auths[0].currentLockedAmount.toString()) + BigInt(wei) >
BigInt(auths[0].maxLockedAmount.toString())
) {
throw new Error(`No valid escrow auths found(will go over limit)`)
}
console.log('--> Before maxLockSeconds check')

if (BigInt(auths[0].maxLockSeconds.toString()) < BigInt(expiry)) {
throw new Error(`No valid escrow auths found(maxLockSeconds too low)`)
}
console.log('--> After maxLockSeconds check')
if (
BigInt(auths[0].currentLocks.toString()) + BigInt(1) >
BigInt(auths[0].maxLockCounts.toString())
Expand Down
4 changes: 4 additions & 0 deletions src/components/core/utils/nonceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,19 @@ async function validateNonceAndSignature(
try {
const addressFromHashSignature = ethers.verifyMessage(consumerMessage, signature)
const addressFromBytesSignature = ethers.verifyMessage(messageHashBytes, signature)

if (
ethers.getAddress(addressFromHashSignature)?.toLowerCase() ===
ethers.getAddress(consumer)?.toLowerCase() ||
ethers.getAddress(addressFromBytesSignature)?.toLowerCase() ===
ethers.getAddress(consumer)?.toLowerCase()
) {
CORE_LOGGER.logMessage('[DEBUG] ✅ EOA signature validation PASSED', true)
return { valid: true }
}
CORE_LOGGER.logMessage('[DEBUG] ❌ EOA signature validation FAILED', true)
} catch (error) {
CORE_LOGGER.logMessage(`[DEBUG] EOA validation exception: ${error.message}`, true)
// Continue to smart account check
}

Expand Down
Loading