From 9e11c04b0b1b141169b290eeedbda19025857d17 Mon Sep 17 00:00:00 2001 From: Enzo Cioppettini <48031343+ecioppettini@users.noreply.github.com> Date: Wed, 17 Apr 2024 00:50:19 -0300 Subject: [PATCH] schedule presync stf events to the first sync block (#339) * schedule presync stf events to the first sync block * apply the change to the erc721 burn event too --- .../paima-sm/src/cde-cardano-mint-burn.ts | 5 +- .../engine/paima-sm/src/cde-cardano-pool.ts | 14 ++--- .../paima-sm/src/cde-cardano-projected-nft.ts | 62 +++++++++---------- .../paima-sm/src/cde-cardano-transfer.ts | 5 +- .../engine/paima-sm/src/cde-erc20-deposit.ts | 8 +-- .../engine/paima-sm/src/cde-erc721-mint.ts | 4 +- .../paima-sm/src/cde-erc721-transfer.ts | 9 ++- packages/engine/paima-sm/src/cde-generic.ts | 2 +- .../engine/paima-sm/src/cde-processing.ts | 4 +- 9 files changed, 57 insertions(+), 56 deletions(-) diff --git a/packages/engine/paima-sm/src/cde-cardano-mint-burn.ts b/packages/engine/paima-sm/src/cde-cardano-mint-burn.ts index ea6fb65f9..900bee069 100644 --- a/packages/engine/paima-sm/src/cde-cardano-mint-burn.ts +++ b/packages/engine/paima-sm/src/cde-cardano-mint-burn.ts @@ -4,7 +4,8 @@ import type { SQLUpdate } from '@paima/db'; import type { CdeCardanoMintBurnDatum } from './types.js'; export default async function processDatum( - cdeDatum: CdeCardanoMintBurnDatum + cdeDatum: CdeCardanoMintBurnDatum, + inPresync: boolean ): Promise { const cdeId = cdeDatum.cdeId; const prefix = cdeDatum.scheduledPrefix; @@ -14,7 +15,7 @@ export default async function processDatum( const inputAddresses = cdeDatum.payload.inputAddresses; const outputAddresses = cdeDatum.payload.outputAddresses; - const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1); + const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber; const scheduledInputData = `${prefix}|${txId}|${metadata}|${assets}|${JSON.stringify(inputAddresses)}|${JSON.stringify(outputAddresses)}`; const updateList: SQLUpdate[] = [ diff --git a/packages/engine/paima-sm/src/cde-cardano-pool.ts b/packages/engine/paima-sm/src/cde-cardano-pool.ts index 4595d8aad..b2962b909 100644 --- a/packages/engine/paima-sm/src/cde-cardano-pool.ts +++ b/packages/engine/paima-sm/src/cde-cardano-pool.ts @@ -12,14 +12,11 @@ export default async function processDatum( const address = cdeDatum.payload.address; const pool = cdeDatum.payload.pool; - const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1); + const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber; const scheduledInputData = `${prefix}|${address}|${pool}`; - const updateList: SQLUpdate[] = inPresync - ? [] - : [createScheduledData(scheduledInputData, scheduledBlockHeight)]; - - updateList.push( + const updateList: SQLUpdate[] = [ + createScheduledData(scheduledInputData, scheduledBlockHeight), [ cdeCardanoPoolInsertData, { @@ -34,7 +31,8 @@ export default async function processDatum( { address: cdeDatum.payload.address, }, - ] - ); + ], + ]; + return updateList; } diff --git a/packages/engine/paima-sm/src/cde-cardano-projected-nft.ts b/packages/engine/paima-sm/src/cde-cardano-projected-nft.ts index 629a8a0ff..3e116fb70 100644 --- a/packages/engine/paima-sm/src/cde-cardano-projected-nft.ts +++ b/packages/engine/paima-sm/src/cde-cardano-projected-nft.ts @@ -25,20 +25,41 @@ export default async function processDatum( const datum = cdeDatum.payload.plutusDatum; const forHowLong = cdeDatum.payload.forHowLong; - const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1); + const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber; const scheduledInputData = `${prefix}|${ownerAddress}|${previousTxHash}|${previousOutputIndex}|${currentTxHash}|${currentOutputIndex}|${policyId}|${assetName}|${status}`; if (previousTxHash === undefined || previousOutputIndex === undefined) { - const updateList: SQLUpdate[] = inPresync - ? [] - : [createScheduledData(scheduledInputData, scheduledBlockHeight)]; - updateList.push([ - cdeCardanoProjectedNftInsertData, + const updateList: SQLUpdate[] = [ + createScheduledData(scheduledInputData, scheduledBlockHeight), + [ + cdeCardanoProjectedNftInsertData, + { + cde_id: cdeId, + owner_address: ownerAddress, + current_tx_hash: currentTxHash, + current_tx_output_index: currentOutputIndex, + policy_id: policyId, + asset_name: assetName, + amount: amount, + status: status, + plutus_datum: datum, + for_how_long: forHowLong, + }, + ], + ]; + return updateList; + } + const updateList: SQLUpdate[] = [ + createScheduledData(scheduledInputData, scheduledBlockHeight), + [ + cdeCardanoProjectedNftUpdateData, { cde_id: cdeId, owner_address: ownerAddress, - current_tx_hash: currentTxHash, - current_tx_output_index: currentOutputIndex, + new_tx_hash: currentTxHash, + new_tx_output_index: currentOutputIndex, + previous_tx_hash: previousTxHash, + previous_tx_output_index: previousOutputIndex, policy_id: policyId, asset_name: assetName, amount: amount, @@ -46,29 +67,8 @@ export default async function processDatum( plutus_datum: datum, for_how_long: forHowLong, }, - ]); - return updateList; - } - const updateList: SQLUpdate[] = inPresync - ? [] - : [createScheduledData(scheduledInputData, scheduledBlockHeight)]; + ], + ]; - updateList.push([ - cdeCardanoProjectedNftUpdateData, - { - cde_id: cdeId, - owner_address: ownerAddress, - new_tx_hash: currentTxHash, - new_tx_output_index: currentOutputIndex, - previous_tx_hash: previousTxHash, - previous_tx_output_index: previousOutputIndex, - policy_id: policyId, - asset_name: assetName, - amount: amount, - status: status, - plutus_datum: datum, - for_how_long: forHowLong, - }, - ]); return updateList; } diff --git a/packages/engine/paima-sm/src/cde-cardano-transfer.ts b/packages/engine/paima-sm/src/cde-cardano-transfer.ts index 4c7e7f720..cd43d0784 100644 --- a/packages/engine/paima-sm/src/cde-cardano-transfer.ts +++ b/packages/engine/paima-sm/src/cde-cardano-transfer.ts @@ -4,7 +4,8 @@ import type { SQLUpdate } from '@paima/db'; import type { CdeCardanoTransferDatum } from './types.js'; export default async function processDatum( - cdeDatum: CdeCardanoTransferDatum + cdeDatum: CdeCardanoTransferDatum, + inPresync: boolean ): Promise { const cdeId = cdeDatum.cdeId; const prefix = cdeDatum.scheduledPrefix; @@ -14,7 +15,7 @@ export default async function processDatum( const outputs = JSON.stringify(cdeDatum.payload.outputs); const metadata = cdeDatum.payload.metadata || undefined; - const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1); + const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber; const scheduledInputData = `${prefix}|${txId}|${metadata}|${inputCredentials}|${outputs}`; const updateList: SQLUpdate[] = [ diff --git a/packages/engine/paima-sm/src/cde-erc20-deposit.ts b/packages/engine/paima-sm/src/cde-erc20-deposit.ts index 234eb339e..dff06e4a7 100644 --- a/packages/engine/paima-sm/src/cde-erc20-deposit.ts +++ b/packages/engine/paima-sm/src/cde-erc20-deposit.ts @@ -30,11 +30,9 @@ export default async function processErc20Datum( const updateList: SQLUpdate[] = []; try { - if (!inPresync) { - const scheduledInputData = `${prefix}|${fromAddr}|${value}`; - const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1); - updateList.push(createScheduledData(scheduledInputData, scheduledBlockHeight)); - } + const scheduledInputData = `${prefix}|${fromAddr}|${value}`; + const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber; + updateList.push(createScheduledData(scheduledInputData, scheduledBlockHeight)); if (fromRow.length > 0) { const oldTotal = BigInt(fromRow[0].total_deposited); diff --git a/packages/engine/paima-sm/src/cde-erc721-mint.ts b/packages/engine/paima-sm/src/cde-erc721-mint.ts index 02e7a59fc..113ed6548 100644 --- a/packages/engine/paima-sm/src/cde-erc721-mint.ts +++ b/packages/engine/paima-sm/src/cde-erc721-mint.ts @@ -8,11 +8,11 @@ export default async function processErc721Datum( inPresync: boolean ): Promise { const [address, prefix] = [cdeDatum.contractAddress, cdeDatum.scheduledPrefix]; - if (!prefix || inPresync) { + if (!prefix) { return []; } const { tokenId, mintData } = cdeDatum.payload; - const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1); + const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber; const scheduledInputData = `${prefix}|${address}|${tokenId}|${mintData}`; return [createScheduledData(scheduledInputData, scheduledBlockHeight)]; } diff --git a/packages/engine/paima-sm/src/cde-erc721-transfer.ts b/packages/engine/paima-sm/src/cde-erc721-transfer.ts index a923a0fb2..ef60ba7e4 100644 --- a/packages/engine/paima-sm/src/cde-erc721-transfer.ts +++ b/packages/engine/paima-sm/src/cde-erc721-transfer.ts @@ -15,7 +15,7 @@ import type { SQLUpdate } from '@paima/db'; export default async function processErc721Datum( readonlyDBConn: PoolClient, cdeDatum: CdeErc721TransferDatum, - isPresync: boolean + inPresync: boolean ): Promise { const cdeId = cdeDatum.cdeId; const { to, tokenId } = cdeDatum.payload; @@ -32,9 +32,12 @@ export default async function processErc721Datum( const newOwnerData = { cde_id: cdeId, token_id: tokenId, nft_owner: toAddr }; if (ownerRow.length > 0) { if (isBurn) { - if (cdeDatum.burnScheduledPrefix && !isPresync) { + if (cdeDatum.burnScheduledPrefix) { const scheduledInputData = `${cdeDatum.burnScheduledPrefix}|${ownerRow[0].nft_owner}|${tokenId}`; - const scheduledBlockHeight = cdeDatum.blockNumber; + + const scheduledBlockHeight = inPresync + ? ENV.SM_START_BLOCKHEIGHT + 1 + : cdeDatum.blockNumber; updateList.push(createScheduledData(scheduledInputData, scheduledBlockHeight)); } diff --git a/packages/engine/paima-sm/src/cde-generic.ts b/packages/engine/paima-sm/src/cde-generic.ts index 5694874c4..b0e29a877 100644 --- a/packages/engine/paima-sm/src/cde-generic.ts +++ b/packages/engine/paima-sm/src/cde-generic.ts @@ -12,7 +12,7 @@ export default async function processDatum( const payload = cdeDatum.payload; const prefix = cdeDatum.scheduledPrefix; - const scheduledBlockHeight = Math.max(cdeDatum.blockNumber, ENV.SM_START_BLOCKHEIGHT + 1); + const scheduledBlockHeight = inPresync ? ENV.SM_START_BLOCKHEIGHT + 1 : cdeDatum.blockNumber; const stringifiedPayload = JSON.stringify(payload); const scheduledInputData = `${prefix}|${stringifiedPayload}`; diff --git a/packages/engine/paima-sm/src/cde-processing.ts b/packages/engine/paima-sm/src/cde-processing.ts index 8ebdb5a41..49713ae77 100644 --- a/packages/engine/paima-sm/src/cde-processing.ts +++ b/packages/engine/paima-sm/src/cde-processing.ts @@ -42,9 +42,9 @@ export async function cdeTransitionFunction( case ChainDataExtensionDatumType.CardanoAssetUtxo: return await processCardanoAssetUtxoDatum(cdeDatum); case ChainDataExtensionDatumType.CardanoTransfer: - return await processCardanoTransferDatum(cdeDatum); + return await processCardanoTransferDatum(cdeDatum, inPresync); case ChainDataExtensionDatumType.CardanoMintBurn: - return await processCardanoMintBurnDatum(cdeDatum); + return await processCardanoMintBurnDatum(cdeDatum, inPresync); default: assertNever(cdeDatum); }