Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schedule presync stf events to the first sync block #339

Merged
merged 2 commits into from
Apr 17, 2024
Merged
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
5 changes: 3 additions & 2 deletions packages/engine/paima-sm/src/cde-cardano-mint-burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SQLUpdate[]> {
const cdeId = cdeDatum.cdeId;
const prefix = cdeDatum.scheduledPrefix;
Expand All @@ -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[] = [
Expand Down
14 changes: 6 additions & 8 deletions packages/engine/paima-sm/src/cde-cardano-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand All @@ -34,7 +31,8 @@ export default async function processDatum(
{
address: cdeDatum.payload.address,
},
]
);
],
];

return updateList;
}
62 changes: 31 additions & 31 deletions packages/engine/paima-sm/src/cde-cardano-projected-nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,50 @@ 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,
status: status,
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;
}
5 changes: 3 additions & 2 deletions packages/engine/paima-sm/src/cde-cardano-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SQLUpdate[]> {
const cdeId = cdeDatum.cdeId;
const prefix = cdeDatum.scheduledPrefix;
Expand All @@ -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[] = [
Expand Down
8 changes: 3 additions & 5 deletions packages/engine/paima-sm/src/cde-erc20-deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/engine/paima-sm/src/cde-erc721-mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export default async function processErc721Datum(
inPresync: boolean
): Promise<SQLUpdate[]> {
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)];
}
9 changes: 6 additions & 3 deletions packages/engine/paima-sm/src/cde-erc721-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { SQLUpdate } from '@paima/db';
export default async function processErc721Datum(
readonlyDBConn: PoolClient,
cdeDatum: CdeErc721TransferDatum,
isPresync: boolean
inPresync: boolean
): Promise<SQLUpdate[]> {
const cdeId = cdeDatum.cdeId;
const { to, tokenId } = cdeDatum.payload;
Expand All @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/paima-sm/src/cde-generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

Expand Down
4 changes: 2 additions & 2 deletions packages/engine/paima-sm/src/cde-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down