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

Fix failed tx cleanup #370

Merged
merged 1 commit into from
May 25, 2024
Merged
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
143 changes: 75 additions & 68 deletions packages/engine/paima-sm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,38 +367,42 @@ async function processScheduledData(
DBConn
);
for (const data of scheduledData) {
const inputData: STFSubmittedData = {
userId: SCHEDULED_DATA_ID,
realAddress: SCHEDULED_DATA_ADDRESS,
userAddress: SCHEDULED_DATA_ADDRESS,
inputData: data.input_data,
inputNonce: '',
suppliedValue: '0',
scheduled: true,
scheduledTxHash: data.tx_hash,
};
// Trigger STF
let sqlQueries: SQLUpdate[] = [];
try {
sqlQueries = await gameStateTransition(
inputData,
data.block_height,
randomnessGenerator,
DBConn
);
} catch (err) {
// skip scheduled data where the STF fails
doLog(`[paima-sm] Error on scheduled data STF call. Skipping`, err);
continue;
}
if (sqlQueries.length === 0) continue;

await tryOrRollback(DBConn, async () => {
for (const [query, params] of sqlQueries) {
await query.run(params, DBConn);
const inputData: STFSubmittedData = {
userId: SCHEDULED_DATA_ID,
realAddress: SCHEDULED_DATA_ADDRESS,
userAddress: SCHEDULED_DATA_ADDRESS,
inputData: data.input_data,
inputNonce: '',
suppliedValue: '0',
scheduled: true,
scheduledTxHash: data.tx_hash,
};
// Trigger STF
let sqlQueries: SQLUpdate[] = [];
try {
sqlQueries = await gameStateTransition(
inputData,
data.block_height,
randomnessGenerator,
DBConn
);
} catch (err) {
// skip scheduled data where the STF fails
doLog(`[paima-sm] Error on scheduled data STF call. Skipping`, err);
continue;
}
if (sqlQueries.length !== 0) {
await tryOrRollback(DBConn, async () => {
for (const [query, params] of sqlQueries) {
await query.run(params, DBConn);
}
});
}
} finally {
// guarantee we run this no matter if there is an error or a continue
await deleteScheduled.run({ id: data.id }, DBConn);
});
}
}
return scheduledData.length;
}
Expand Down Expand Up @@ -430,47 +434,50 @@ async function processUserInputs(
userAddress: address.address,
userId: address.id,
};

// Check if internal Concise Command
// Internal Concise Commands are prefixed with an ampersand (&)
//
// delegate = &wd|from?|to?|from_signature|to_signature
// migrate = &wm|from?|to?|from_signature|to_signature
// cancelDelegate = &wc|to?
const delegateWallet = new DelegateWallet(DBConn);
if (inputData.inputData.startsWith(DelegateWallet.INTERNAL_COMMAND_PREFIX)) {
const status = await delegateWallet.process(
inputData.realAddress,
inputData.userAddress,
inputData.inputData
);
if (!status) continue;
} else if (inputData.userId === NO_USER_ID) {
// If wallet does not exist in address table: create it.
const newAddress = await delegateWallet.createAddress(inputData.userAddress);
inputData.userId = newAddress.id;
}

// Trigger STF
let sqlQueries: SQLUpdate[] = [];
try {
sqlQueries = await gameStateTransition(
inputData,
latestChainData.blockNumber,
randomnessGenerator,
DBConn
);
} catch (err) {
// skip inputs where the STF fails
doLog(`[paima-sm] Error on user input STF call. Skipping`, err);
continue;
}
if (sqlQueries.length === 0) continue;
// Check if internal Concise Command
// Internal Concise Commands are prefixed with an ampersand (&)
//
// delegate = &wd|from?|to?|from_signature|to_signature
// migrate = &wm|from?|to?|from_signature|to_signature
// cancelDelegate = &wc|to?
const delegateWallet = new DelegateWallet(DBConn);
if (inputData.inputData.startsWith(DelegateWallet.INTERNAL_COMMAND_PREFIX)) {
const status = await delegateWallet.process(
inputData.realAddress,
inputData.userAddress,
inputData.inputData
);
if (!status) continue;
} else if (inputData.userId === NO_USER_ID) {
// If wallet does not exist in address table: create it.
const newAddress = await delegateWallet.createAddress(inputData.userAddress);
inputData.userId = newAddress.id;
}

await tryOrRollback(DBConn, async () => {
for (const [query, params] of sqlQueries) {
await query.run(params, DBConn);
// Trigger STF
let sqlQueries: SQLUpdate[] = [];
try {
sqlQueries = await gameStateTransition(
inputData,
latestChainData.blockNumber,
randomnessGenerator,
DBConn
);
} catch (err) {
// skip inputs where the STF fails
doLog(`[paima-sm] Error on user input STF call. Skipping`, err);
continue;
}
if (sqlQueries.length !== 0) {
await tryOrRollback(DBConn, async () => {
for (const [query, params] of sqlQueries) {
await query.run(params, DBConn);
}
});
}
} finally {
// guarantee we run this no matter if there is an error or a continue
await insertNonce.run(
{
nonce: inputData.inputNonce,
Expand All @@ -488,7 +495,7 @@ async function processUserInputs(
DBConn
);
}
});
}
}
return latestChainData.submittedData.length;
}
Expand Down