Blocked timeout error #506

Merged
bones7242 merged 3 commits from blocked-timeout-error into master 2018-07-02 23:54:29 +02:00
7 changed files with 81 additions and 18 deletions

View file

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

View file

@ -22,7 +22,10 @@ const claimLongId = ({ ip, originalUrl, body, params }, res) => {
getClaimId(channelName, channelClaimId, claimName, claimId) getClaimId(channelName, channelClaimId, claimName, claimId)
.then(fullClaimId => { .then(fullClaimId => {
claimId = fullClaimId; claimId = fullClaimId;
return db.Blocked.isNotBlocked(fullClaimId, claimName); return db.Claim.getOutpoint(claimName, fullClaimId);
})
.then(outpoint => {
return db.Blocked.isNotBlocked(outpoint);
}) })
.then(() => { .then(() => {
res.status(200).json({success: true, data: claimId}); res.status(200).json({success: true, data: claimId});

View file

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

View file

@ -6,14 +6,6 @@ module.exports = (sequelize, { STRING }) => {
const Blocked = sequelize.define( const Blocked = sequelize.define(
'Blocked', 'Blocked',
{ {
claimId: {
type : STRING,
allowNull: false,
},
name: {
type : STRING,
allowNull: false,
},
outpoint: { outpoint: {
type : STRING, type : STRING,
allowNull: false, allowNull: false,
@ -24,13 +16,12 @@ module.exports = (sequelize, { STRING }) => {
} }
); );
Blocked.isNotBlocked = function (claimId, name) { Blocked.isNotBlocked = function (outpoint) {
logger.debug(`checking to see if ${name}#${claimId} is not blocked`); logger.debug(`checking to see if ${outpoint} is not blocked`);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.findOne({ this.findOne({
where: { where: {
claimId, outpoint,
name,
}, },
}) })
.then(result => { .then(result => {
@ -46,5 +37,39 @@ 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);
});
};
return Blocked; 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; return Claim;
}; };

View file

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

View file

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