Blocked timeout error #506

Merged
bones7242 merged 3 commits from blocked-timeout-error into master 2018-07-02 23:54:29 +02:00
6 changed files with 80 additions and 17 deletions
Showing only changes of commit 63b9f27d33 - Show all commits

View file

@ -1,8 +1,8 @@
const logger = require('winston');
const refreshBlockedList = require('../../../models/utils/refreshBlockedList.js');
const db = require('../../../models');
const updateBlockedList = (req, res) => {
refreshBlockedList()
db.Blocked.refreshTable()
.then(data => {
logger.info('finished updating blocked content list');
res.status(200).json({

View file

@ -17,8 +17,12 @@ const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId
getClaimId(channelName, channelClaimId, claimName, claimId)
.then(fullClaimId => {
claimId = fullClaimId;
logger.debug('FULL CLAIM ID:', fullClaimId);
return db.Blocked.isNotBlocked(fullClaimId, claimName);
logger.debug('Full claim id:', fullClaimId);
return db.Claim.getOutpoint(claimName, fullClaimId);
})
.then(outpoint => {
logger.debug('Outpoint:', outpoint);
return db.Blocked.isNotBlocked(outpoint);
})
.then(() => {
return getLocalFileRecord(claimId, claimName);

View file

@ -6,14 +6,6 @@ module.exports = (sequelize, { STRING }) => {
const Blocked = sequelize.define(
'Blocked',
{
claimId: {
type : STRING,
allowNull: false,
},
name: {
type : STRING,
allowNull: false,
},
outpoint: {
type : STRING,
allowNull: false,
@ -24,13 +16,12 @@ module.exports = (sequelize, { STRING }) => {
}
);
Blocked.isNotBlocked = function (claimId, name) {
logger.debug(`checking to see if ${name}#${claimId} is not blocked`);
Blocked.isNotBlocked = function (outpoint) {
logger.debug(`checking to see if ${outpoint} is not blocked`);
return new Promise((resolve, reject) => {
this.findOne({
where: {
claimId,
name,
outpoint,
},
})
.then(result => {
@ -46,5 +37,42 @@ module.exports = (sequelize, { STRING }) => {
});
};
Blocked.refreshTable = function () {
let blockedList = [];
return fetch('https://api.lbry.io/file/list_blocked')
.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');
}
return jsonResponse.data.outpoints;
})
.then(outpoints => {
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);
})
.catch(error => {
throw error;
});
};
return Blocked;
};

View file

@ -378,5 +378,27 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
});
};
Claim.getOutpoint = function (name, claimId) {
return this
.findAll({
where : { name, claimId },
attributes: ['outpoint'],
})
.then(result => {
logger.debug('outpoint result');
switch (result.length) {
case 0:
throw new Error(`no record found for ${name}#${claimId}`);
case 1:
return result[0].dataValues.outpoint;
default:
throw new Error(`more than one record found for ${name}#${claimId}`);
}
})
.catch(error => {
throw error;
});
};
return Claim;
};

View file

@ -1,3 +1,4 @@
const logger = require('winston');
const { details: { ipAddress } } = require('@config/siteConfig');
module.exports = (sequelize, { STRING }) => {
@ -25,6 +26,8 @@ module.exports = (sequelize, { STRING }) => {
return response.json();
})
.then(jsonResponse => {
logger.debug('total tor nodes:', jsonResponse.length);
// prep the records
for (let i = 0; i < jsonResponse.length; i++) {
torList.push({
address : jsonResponse[i].Address,

View file

@ -16,9 +16,15 @@ const refreshBlockedList = () => {
return jsonResponse.data.outpoints;
})
.then(outpoints => {
db.Claim.findAll({
where: {
outpoint: outpoints,
},
});
let updatePromises = [];
outpoints.forEach(outpoint => {
// logger.debug('outpoint:', outpoint);
logger.debug('blocked outpoint:', outpoint);
updatePromises.push(db.Claim
.findOne({
where: {