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

[DO NOT MERGE!!!] [V8] Paranet sync rework (WIP) #3680

Open
wants to merge 6 commits into
base: v8/develop
Choose a base branch
from
Open
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
334 changes: 176 additions & 158 deletions src/commands/paranet/paranet-sync-command.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
export async function up({ context: { queryInterface, Sequelize } }) {
await queryInterface.createTable('paranet_asset', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
blockchain_id: {
type: Sequelize.STRING,
allowNull: false,
},
ual: {
type: Sequelize.STRING,
allowNull: false,
},
paranet_ual: {
type: Sequelize.STRING,
allowNull: false,
},
public_assertion_id: {
type: Sequelize.STRING,
allowNull: true,
},
private_assertion_id: {
type: Sequelize.STRING,
allowNull: true,
},
sender: {
type: Sequelize.STRING,
allowNull: true,
},
transaction_hash: {
type: Sequelize.STRING,
allowNull: true,
},
error_message: {
type: Sequelize.TEXT,
allowNull: true,
},
is_synced: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false,
},
retries: {
allowNull: false,
type: Sequelize.INTEGER,
defaultValue: 0,
},
created_at: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('NOW()'),
},
updated_at: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('NOW()'),
},
});
await queryInterface.addConstraint('paranet_asset', {
fields: ['ual', 'paranet_ual'],
type: 'unique',
});
}

export async function down({ context: { queryInterface } }) {
await queryInterface.dropTable('paranet_asset');
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// NOT USED ANYMORE

export default (sequelize, DataTypes) => {
const blockchain = sequelize.define(
'missed_paranet_asset',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
export default (sequelize, DataTypes) => {
const paranetAsset = sequelize.define(
'paranet_asset',
{
id: {
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
blockchainId: {
allowNull: false,
type: DataTypes.STRING,
},
ual: {
allowNull: false,
type: DataTypes.STRING,
},
paranetUal: {
allowNull: false,
type: DataTypes.STRING,
},
publicAssertionId: {
allowNull: true,
type: DataTypes.STRING,
},
privateAssertionId: {
allowNull: true,
type: DataTypes.STRING,
},
sender: {
allowNull: true,
type: DataTypes.STRING,
},
transactionHash: {
allowNull: true,
type: DataTypes.STRING,
},
errorMessage: {
allowNull: true,
type: DataTypes.TEXT,
},
isSynced: {
allowNull: false,
type: DataTypes.BOOLEAN,
defaultValue: false,
},
retries: {
allowNull: false,
type: DataTypes.INTEGER,
defaultValue: 0,
},
createdAt: {
type: DataTypes.DATE,
},
updatedAt: {
type: DataTypes.DATE,
},
},
{
underscored: true,
indexes: [
{
unique: true,
fields: ['ual', 'paranetUal'], // Composite unique constraint on `ual` and `paranetUal`
},
{
fields: ['updatedAt', 'retries', 'isSynced'],
},
{
fields: ['ual', 'paranetUal'],
},
{
fields: ['isSynced', 'paranetUal'],
},
],
},
);

paranetAsset.associate = () => {
// Define associations here if needed
};

return paranetAsset;
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PARANET_SYNC_SOURCES } from '../../../../../constants/constants.js';

// NOT USED ANYMORE
export default (sequelize, DataTypes) => {
const blockchain = sequelize.define(
'paranet_synced_asset',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import Sequelize from 'sequelize';

class ParanetAssetRepository {
constructor(models) {
this.sequelize = models.sequelize;
this.model = models.paranet_asset;
}

async createParanetAssetRecord(missedParanetAsset, options) {
return this.model.create({ ...missedParanetAsset, isSynced: false }, options);
}

async getCountOfMissedAssetsOfParanet(paranetUal, options = {}) {
return this.model.count({
where: {
paranetUal,
isSynced: false,
},
...options,
});
}

async getParanetSyncedAssetRecordsCount(paranetUal, options = {}) {
return this.model.count({
where: {
paranet_ual: paranetUal,
isSynced: true,
},
...options,
});
}

// TODO: remove
// async getFilteredCountOfMissedAssetsOfParanet(
// paranetUal,
// retryCountLimit,
// retryDelayInMs,
// options = {},
// ) {
// const now = new Date();
// const delayDate = new Date(now.getTime() - retryDelayInMs);

// const records = await this.model.findAll({
// where: {
// paranetUal,
// isSynced: false, // Only unsynced assets
// retries: {
// [Sequelize.Op.lt]: retryCountLimit, // Filter by retries count
// },
// created_at: {
// [Sequelize.Op.lte]: delayDate, // Filter by created_at date
// },
// },
// ...options,
// });

// return records.length; // Return the count of matching records
// }

async getMissedParanetAssetsRecordsWithRetryCount(
paranetUal,
retryCountLimit,
retryDelayInMs,
limit = null,
options = {},
) {
const now = new Date();
const delayDate = new Date(now.getTime() - retryDelayInMs);

const queryOptions = {
where: {
paranetUal,
isSynced: false,
retries: {
[Sequelize.Op.lt]: retryCountLimit,
},
updated_at: {
[Sequelize.Op.lte]: delayDate,
},
},
...options,
};

if (limit !== null) {
queryOptions.limit = limit;
}

return this.model.findAll(queryOptions);
}

async missedParanetAssetRecordExists(ual, paranetUal, options = {}) {
const missedParanetAssetRecord = await this.model.findOne({
where: { ual, isSynced: false },
...options,
});

return !!missedParanetAssetRecord;
}

async getParanetSyncedAssetRecordByUAL(ual, paranetUal, options = {}) {
return this.model.findOne({
where: { ual, paranetUal, isSynced: true },
...options,
});
}

async paranetSyncedAssetRecordExists(ual, paranetUal, options = {}) {
const paranetSyncedAssetRecord = await this.getParanetSyncedAssetRecordByUAL(
ual,
paranetUal,
options,
);

return !!paranetSyncedAssetRecord;
}

async updateAssetToBeSynced(ual, paranetUal, options = {}) {
const [affectedRows] = await this.model.update(
{ isSynced: true },
{
where: {
ual,
paranetUal,
},
...options,
},
);

return affectedRows;
}

async incrementRetriesForUalAndParanetUal(ual, paranetUal, options = {}) {
const [affectedRows] = await this.model.increment('retries', {
by: 1,
where: {
ual,
paranetUal,
},
...options,
});

return affectedRows;
}
}

export default ParanetAssetRepository;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// DEPRECATED
class ParanetSyncedAssetRepository {
constructor(models) {
this.sequelize = models.sequelize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import OperationResponseRepository from './repositories/operation-response.js';
import ShardRepository from './repositories/shard-repository.js';
import TokenRepository from './repositories/token-repository.js';
import UserRepository from './repositories/user-repository.js';
import MissedParanetAssetRepository from './repositories/missed-paranet-asset-repository.js';
import ParanetSyncedAssetRepository from './repositories/paranet-synced-asset-repository.js';
// import MissedParanetAssetRepository from './repositories/missed-paranet-asset-repository.js';
// import ParanetSyncedAssetRepository from './repositories/paranet-synced-asset-repository.js';
import FinalityStatusRepository from './repositories/finality-status-repository.js';
import ParanetAssetRepository from './repositories/paranet-asset-repository.js';

const __dirname = fileURLToPath(new URL('.', import.meta.url));

Expand All @@ -38,8 +39,9 @@ class SequelizeRepository {
command: new CommandRepository(this.models),
event: new EventRepository(this.models),
paranet: new ParanetRepository(this.models),
paranet_synced_asset: new ParanetSyncedAssetRepository(this.models),
missed_paranet_asset: new MissedParanetAssetRepository(this.models),
// paranet_synced_asset: new ParanetSyncedAssetRepository(this.models),
// missed_paranet_asset: new MissedParanetAssetRepository(this.models),
paranet_asset: new ParanetAssetRepository(this.models),
operation_id: new OperationIdRepository(this.models),
operation: new OperationRepository(this.models),
operation_response: new OperationResponseRepository(this.models),
Expand Down
Loading
Loading