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

better hooks #391

Merged
merged 3 commits into from
Nov 26, 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
19 changes: 13 additions & 6 deletions run/models/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ module.exports = (sequelize, DataTypes) => {
async transaction => this.safeDestroy(transaction)
);
}

async afterCreate(options) {
const afterCreateFn = () => enqueue('processBlock', `processBlock-${this.id}`, { blockId: this.id });
if (options.transaction)
return options.transaction.afterCommit(afterCreateFn);
else
return afterCreateFn();
}
}
Block.init({
baseFeePerGas: DataTypes.STRING,
Expand Down Expand Up @@ -72,12 +80,11 @@ module.exports = (sequelize, DataTypes) => {
},
}, {
hooks: {
async afterCreate(block, options) {
const afterCreateFn = () => enqueue('processBlock', `processBlock-${block.id}`, { blockId: block.id });
if (options.transaction)
return options.transaction.afterCommit(afterCreateFn);
else
return afterCreateFn();
afterBulkCreate(blocks, options) {
return Promise.all(blocks.map(b => b.afterCreate(options)));
},
afterCreate(block, options) {
return block.afterCreate(options);
},
async afterSave(block, options) {
const afterSaveFn = async () => {
Expand Down
93 changes: 50 additions & 43 deletions run/models/tokentransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,51 @@ module.exports = (sequelize, DataTypes) => {
await tokenBalanceChange.insertAnalyticEvent(transaction);
});
}

async afterCreate(options) {
const transaction = await this.getTransaction({
attributes: ['blockNumber', 'hash'],
include: [
{
model: sequelize.models.TokenTransfer,
attributes: ['id', 'src', 'dst', 'token'],
as: 'tokenTransfers',
},
{
model: sequelize.models.Workspace,
attributes: ['id', 'public', 'rpcServer', 'name'],
as: 'workspace',
include: {
model: sequelize.models.User,
attributes: ['firebaseUserId'],
as: 'user'
}
}
]
});

if (transaction.workspace.public) {
options.transaction.afterCommit(() => {
return enqueue('processTokenTransfer',
`processTokenTransfer-${this.workspaceId}-${this.token}-${this.id}`, {
tokenTransferId: this.id
}
);
});

if (this.tokenId)
await enqueue('reloadErc721Token',
`reloadErc721Token-${this.workspaceId}-${this.token}-${this.tokenId}`, {
workspaceId: this.workspaceId,
address: this.token,
tokenId: this.tokenId
}
);
}

if (!transaction.workspace.public)
trigger(`private-processableTransactions;workspace=${transaction.workspace.id}`, 'new', transaction.toJSON());
}
}
TokenTransfer.init({
amount: DataTypes.STRING,
Expand Down Expand Up @@ -129,49 +174,11 @@ module.exports = (sequelize, DataTypes) => {
processed: DataTypes.BOOLEAN
}, {
hooks: {
async afterCreate(tokenTransfer, options) {
const transaction = await tokenTransfer.getTransaction({
attributes: ['blockNumber', 'hash'],
include: [
{
model: sequelize.models.TokenTransfer,
attributes: ['id', 'src', 'dst', 'token'],
as: 'tokenTransfers',
},
{
model: sequelize.models.Workspace,
attributes: ['id', 'public', 'rpcServer', 'name'],
as: 'workspace',
include: {
model: sequelize.models.User,
attributes: ['firebaseUserId'],
as: 'user'
}
}
]
});

if (transaction.workspace.public) {
options.transaction.afterCommit(() => {
return enqueue('processTokenTransfer',
`processTokenTransfer-${tokenTransfer.workspaceId}-${tokenTransfer.token}-${tokenTransfer.id}`, {
tokenTransferId: tokenTransfer.id
}
);
});

if (tokenTransfer.tokenId)
await enqueue('reloadErc721Token',
`reloadErc721Token-${tokenTransfer.workspaceId}-${tokenTransfer.token}-${tokenTransfer.tokenId}`, {
workspaceId: tokenTransfer.workspaceId,
address: tokenTransfer.token,
tokenId: tokenTransfer.tokenId
}
);
}

if (!transaction.workspace.public)
trigger(`private-processableTransactions;workspace=${transaction.workspace.id}`, 'new', transaction.toJSON());
afterBulkCreate(tokenTransfers, options) {
return Promise.all(tokenTransfers.map(t => t.afterCreate(options)));
},
afterCreate(tokenTransfer, options) {
return tokenTransfer.afterCreate(options);
}
},
sequelize,
Expand Down
38 changes: 18 additions & 20 deletions run/models/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ module.exports = (sequelize, DataTypes) => {
],
{
ignoreDuplicates: true,
individualHooks: true,
returning: true,
transaction
}
Expand Down Expand Up @@ -174,7 +173,6 @@ module.exports = (sequelize, DataTypes) => {
const storedTokenTransfers = await sequelize.models.TokenTransfer.bulkCreate(tokenTransfers, {
ignoreDuplicates: true,
returning: true,
individualHooks: true,
transaction
});
for (let i = 0; i < storedTokenTransfers.length; i++)
Expand Down Expand Up @@ -331,6 +329,17 @@ module.exports = (sequelize, DataTypes) => {
});
});
}

async afterCreate(options) {
const afterCommitFn = () => {
return this.triggerEvents();
};

if (options.transaction)
return options.transaction.afterCommit(afterCommitFn);
else
return afterCommitFn();
}
}
Transaction.init({
blockHash: DataTypes.STRING,
Expand Down Expand Up @@ -424,25 +433,14 @@ module.exports = (sequelize, DataTypes) => {
}
}, {
hooks: {
async afterCreate(transaction, options) {
const afterCommitFn = () => {
return transaction.triggerEvents();
};

if (options.transaction)
return options.transaction.afterCommit(afterCommitFn);
else
return afterCommitFn();
afterBulkCreate(transactions, options) {
return Promise.all(transactions.map(t => t.afterCreate(options)));
},
async afterSave(transaction, options) {
const afterCommitFn = () => {
return transaction.triggerEvents();
};

if (options.transaction)
return options.transaction.afterCommit(afterCommitFn);
else
return afterCommitFn();
afterCreate(transaction, options) {
return transaction.afterCreate(options);
},
afterSave(transaction, options) {
return transaction.afterCreate(options);
}
},
sequelize,
Expand Down
61 changes: 34 additions & 27 deletions run/models/transactionreceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ module.exports = (sequelize, DataTypes) => {
}
return this.destroy({ transaction });
}

async afterCreate(options) {
const transaction = await sequelize.models.Transaction.findByPk(this.transactionId, { include: 'workspace' });

// Here is the stuff that we only want to do once everything has been created (typically notifications & jobs queuing)
const afterCommitFn = async () => {
if (this.status == 0) {
await enqueue('processTransactionError', `processTransactionError-${this.workspaceId}-${this.transactionHash}`, { transactionId: this.transactionId }, 1);
trigger(`private-failedTransactions;workspace=${this.workspaceId}`, 'new', this.toJSON());
}

return transaction.triggerEvents();
}

// We finish creating stuff here to make sure we can put it in the transaction if applicable
if (this.contractAddress) {
const canCreateContract = await transaction.workspace.canCreateContract();
if (canCreateContract)
await transaction.workspace.safeCreateOrUpdateContract({
address: this.contractAddress,
transactionId: this.transactionId
}, options.transaction);
}

if (options.transaction) {
return options.transaction.afterCommit(afterCommitFn);
} else
return afterCommitFn();
}
}
TransactionReceipt.init({
blockHash: DataTypes.STRING,
Expand Down Expand Up @@ -78,33 +107,11 @@ module.exports = (sequelize, DataTypes) => {
raw: DataTypes.JSON
}, {
hooks: {
async afterCreate(receipt, options) {
const transaction = await sequelize.models.Transaction.findByPk(receipt.transactionId, { include: 'workspace' });

// Here is the stuff that we only want to do once everything has been created (typically notifications & jobs queuing)
const afterCommitFn = async () => {
if (receipt.status == 0) {
await enqueue('processTransactionError', `processTransactionError-${receipt.workspaceId}-${receipt.transactionHash}`, { transactionId: receipt.transactionId }, 1);
trigger(`private-failedTransactions;workspace=${receipt.workspaceId}`, 'new', receipt.toJSON());
}

return transaction.triggerEvents();
}

// We finish creating stuff here to make sure we can put it in the transaction if applicable
if (receipt.contractAddress) {
const canCreateContract = await transaction.workspace.canCreateContract();
if (canCreateContract)
await transaction.workspace.safeCreateOrUpdateContract({
address: receipt.contractAddress,
transactionId: receipt.transactionId
}, options.transaction);
}

if (options.transaction) {
return options.transaction.afterCommit(afterCommitFn);
} else
return afterCommitFn();
afterBulkCreate(receipts, options) {
return Promise.all(receipts.map(r => r.afterCreate(options)));
},
afterCreate(receipt, options) {
return receipt.afterCreate(options);
}
},
sequelize,
Expand Down
2 changes: 0 additions & 2 deletions run/models/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,6 @@ module.exports = (sequelize, DataTypes) => {
],
{
ignoreDuplicates: true,
individualHooks: true,
returning: true,
transaction: sequelizeTransaction
}
Expand All @@ -972,7 +971,6 @@ module.exports = (sequelize, DataTypes) => {
const storedTransactions = await sequelize.models.Transaction.bulkCreate(transactionsToInsert, {
ignoreDuplicates: true,
returning: true,
individualHooks: true,
transaction: sequelizeTransaction
});

Expand Down