2018-04-29 19:31:26 +02:00
|
|
|
const logger = require('winston');
|
2018-04-29 21:17:23 +02:00
|
|
|
const BLOCKED_CLAIM = 'BLOCKED_CLAIM';
|
|
|
|
|
2018-04-28 00:17:09 +02:00
|
|
|
module.exports = (sequelize, { STRING }) => {
|
2018-04-29 19:31:26 +02:00
|
|
|
const Blocked = sequelize.define(
|
2018-04-28 00:17:09 +02:00
|
|
|
'Blocked',
|
|
|
|
{
|
|
|
|
outpoint: {
|
|
|
|
type : STRING,
|
|
|
|
allowNull: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
freezeTableName: true,
|
|
|
|
}
|
|
|
|
);
|
2018-04-29 19:31:26 +02:00
|
|
|
|
2019-01-07 23:44:34 +01:00
|
|
|
Blocked.getBlockList = function () {
|
|
|
|
logger.debug('returning full block list');
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.findAll()
|
|
|
|
.then(list => { return resolve(list) });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-06-30 05:34:48 +02:00
|
|
|
Blocked.isNotBlocked = function (outpoint) {
|
|
|
|
logger.debug(`checking to see if ${outpoint} is not blocked`);
|
2018-04-29 19:31:26 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.findOne({
|
|
|
|
where: {
|
2018-06-30 05:34:48 +02:00
|
|
|
outpoint,
|
2018-04-29 19:31:26 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(result => {
|
2018-04-29 21:17:23 +02:00
|
|
|
if (result) {
|
|
|
|
return reject(BLOCKED_CLAIM);
|
2018-04-29 19:31:26 +02:00
|
|
|
}
|
|
|
|
resolve(true);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-04-29 21:17:23 +02:00
|
|
|
logger.error(error);
|
|
|
|
reject(BLOCKED_CLAIM);
|
2018-04-29 19:31:26 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-01-07 23:44:34 +01:00
|
|
|
Blocked.refreshTable = function (blockEndpoint) {
|
2018-06-30 05:34:48 +02:00
|
|
|
let blockedList = [];
|
2019-01-07 23:44:34 +01:00
|
|
|
|
|
|
|
return fetch(blockEndpoint)
|
2018-06-30 05:34:48 +02:00
|
|
|
.then(response => {
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then(jsonResponse => {
|
|
|
|
if (!jsonResponse.data) {
|
|
|
|
throw new Error('no data in list_blocked response');
|
|
|
|
}
|
|
|
|
if (!jsonResponse.data.outpoints) {
|
|
|
|
throw new Error('no outpoints in list_blocked response');
|
|
|
|
}
|
2019-01-07 23:44:34 +01:00
|
|
|
let outpoints = jsonResponse.data.outpoints;
|
2018-06-30 05:34:48 +02:00
|
|
|
logger.debug('total outpoints:', outpoints.length);
|
|
|
|
// prep the records
|
|
|
|
for (let i = 0; i < outpoints.length; i++) {
|
|
|
|
blockedList.push({
|
|
|
|
outpoint: outpoints[i],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// clear the table
|
|
|
|
return this.destroy({
|
|
|
|
truncate: true,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
// fill the table
|
|
|
|
return this.bulkCreate(blockedList);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-04-29 19:31:26 +02:00
|
|
|
return Blocked;
|
2018-04-28 00:17:09 +02:00
|
|
|
};
|