added height to db
This commit is contained in:
parent
b4e6a6662e
commit
15322c3639
5 changed files with 34 additions and 22 deletions
|
@ -74,6 +74,7 @@ module.exports = {
|
||||||
name,
|
name,
|
||||||
claimId : result.claim_id,
|
claimId : result.claim_id,
|
||||||
outpoint: `${result.txid}:${result.nout}`,
|
outpoint: `${result.txid}:${result.nout}`,
|
||||||
|
height : 0,
|
||||||
fileName,
|
fileName,
|
||||||
filePath,
|
filePath,
|
||||||
fileType,
|
fileType,
|
||||||
|
|
|
@ -4,10 +4,10 @@ const logger = require('winston');
|
||||||
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js');
|
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js');
|
||||||
const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js');
|
const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js');
|
||||||
|
|
||||||
function getClaimAndHandleResponse (claimUri, resolve, reject) {
|
function getClaimAndHandleResponse (claimUri, height, resolve, reject) {
|
||||||
lbryApi
|
lbryApi
|
||||||
.getClaim(claimUri)
|
.getClaim(claimUri)
|
||||||
.then(({ name, outpoint, claim_id, file_name, download_path, mime_type, metadata }) => {
|
.then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => {
|
||||||
// create entry in the db
|
// create entry in the db
|
||||||
logger.debug('creating new record in db');
|
logger.debug('creating new record in db');
|
||||||
db.File
|
db.File
|
||||||
|
@ -15,6 +15,7 @@ function getClaimAndHandleResponse (claimUri, resolve, reject) {
|
||||||
name,
|
name,
|
||||||
claimId : claim_id,
|
claimId : claim_id,
|
||||||
outpoint,
|
outpoint,
|
||||||
|
height,
|
||||||
fileName: file_name,
|
fileName: file_name,
|
||||||
filePath: download_path,
|
filePath: download_path,
|
||||||
fileType: mime_type,
|
fileType: mime_type,
|
||||||
|
@ -35,7 +36,7 @@ function getClaimAndHandleResponse (claimUri, resolve, reject) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveAndUpdateIfNeeded (uri, claimName, claimId, outpoint) {
|
function resolveAndUpdateIfNeeded (uri, claimName, claimId, localOutpoint, localHeight) {
|
||||||
logger.debug('initiating resolve on local record to check outpoint');
|
logger.debug('initiating resolve on local record to check outpoint');
|
||||||
// 1. resolve claim
|
// 1. resolve claim
|
||||||
lbryApi
|
lbryApi
|
||||||
|
@ -43,19 +44,23 @@ function resolveAndUpdateIfNeeded (uri, claimName, claimId, outpoint) {
|
||||||
.then(result => {
|
.then(result => {
|
||||||
// logger.debug('resolved result:', result);
|
// logger.debug('resolved result:', result);
|
||||||
const resolvedOutpoint = `${result[uri].claim.txid}:${result[uri].claim.nout}`;
|
const resolvedOutpoint = `${result[uri].claim.txid}:${result[uri].claim.nout}`;
|
||||||
logger.debug('database outpoint:', outpoint);
|
const resolvedHeight = result[uri].claim.height;
|
||||||
|
logger.debug('database outpoint:', localOutpoint);
|
||||||
logger.debug('resolved outpoint:', resolvedOutpoint);
|
logger.debug('resolved outpoint:', resolvedOutpoint);
|
||||||
// 2. if the outpoint's match, no further work needed
|
// 2. if the outpoint's match, no further work needed
|
||||||
if (outpoint === resolvedOutpoint) {
|
if (localOutpoint === resolvedOutpoint) {
|
||||||
logger.debug(`local outpoint matched`);
|
logger.debug('local outpoint matched');
|
||||||
// 2. if the outpoints don't match, get claim and update records
|
// 2. if the outpoints don't match, check the height
|
||||||
|
} else if (localHeight > resolvedHeight) {
|
||||||
|
logger.debug('local height was greater than resolve height');
|
||||||
|
// 2. get the resolved claim
|
||||||
} else {
|
} else {
|
||||||
logger.debug(`local outpoint did not match for ${uri}. Initiating update.`);
|
logger.debug(`local outpoint did not match for ${uri}. Initiating update.`);
|
||||||
getClaimAndUpdate(uri, claimName, claimId);
|
getClaimAndUpdate(uri, claimName, claimId);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
logger.error(`error resolving ${uri}`, error);
|
logger.error(`error resolving "${uri}" >> `, error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,8 +103,9 @@ module.exports = {
|
||||||
.then(freePublicClaimList => {
|
.then(freePublicClaimList => {
|
||||||
const claimId = freePublicClaimList[0].claim_id;
|
const claimId = freePublicClaimList[0].claim_id;
|
||||||
const name = freePublicClaimList[0].name;
|
const name = freePublicClaimList[0].name;
|
||||||
const freePublicClaimOutpoint = `${freePublicClaimList[0].txid}:${freePublicClaimList[0].nout}`;
|
const outpoint = `${freePublicClaimList[0].txid}:${freePublicClaimList[0].nout}`;
|
||||||
const freePublicClaimUri = `${name}#${claimId}`;
|
const uri = `${name}#${claimId}`;
|
||||||
|
const height = freePublicClaimList[0].height;
|
||||||
// 2. check to see if the file is available locally
|
// 2. check to see if the file is available locally
|
||||||
db.File
|
db.File
|
||||||
.findOne({ where: { name: name, claimId: claimId } }) // note: consolidate for es6?
|
.findOne({ where: { name: name, claimId: claimId } }) // note: consolidate for es6?
|
||||||
|
@ -108,18 +114,18 @@ module.exports = {
|
||||||
if (claim) {
|
if (claim) {
|
||||||
logger.debug(`A mysql record was found for ${claimId}`);
|
logger.debug(`A mysql record was found for ${claimId}`);
|
||||||
// trigger update if needed
|
// trigger update if needed
|
||||||
if (claim.dataValues.outpoint === freePublicClaimOutpoint) {
|
if (claim.dataValues.outpoint === outpoint) {
|
||||||
logger.debug(`local outpoint matched for ${claimId}`);
|
logger.debug(`local outpoint matched for ${claimId}`);
|
||||||
} else {
|
} else {
|
||||||
logger.debug(`local outpoint did not match for ${claimId}`);
|
logger.debug(`local outpoint did not match for ${claimId}`);
|
||||||
getClaimAndUpdate(freePublicClaimUri, name, claimId);
|
getClaimAndUpdate(uri, name, claimId);
|
||||||
}
|
}
|
||||||
// return the claim
|
// return the claim
|
||||||
resolve(claim.dataValues);
|
resolve(claim.dataValues);
|
||||||
// 3. otherwise use daemon to retrieve it
|
// 3. otherwise use daemon to retrieve it
|
||||||
} else {
|
} else {
|
||||||
// 4. get the claim and serve it
|
// 4. get the claim and serve it
|
||||||
getClaimAndHandleResponse(freePublicClaimUri, resolve, reject);
|
getClaimAndHandleResponse(uri, height, resolve, reject);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
@ -144,7 +150,7 @@ module.exports = {
|
||||||
if (claim) {
|
if (claim) {
|
||||||
logger.debug(`A mysql record was found for ${claimId}`);
|
logger.debug(`A mysql record was found for ${claimId}`);
|
||||||
// trigger an update if needed
|
// trigger an update if needed
|
||||||
resolveAndUpdateIfNeeded(uri, claimName, claimId, claim.dataValues.outpoint); // ok to just start asynch function, or need to add to a task cue?
|
resolveAndUpdateIfNeeded(uri, claimName, claimId, claim.dataValues.outpoint, claim.dataValues.outpoint); // ok to just start asynch function, or need to add to a task cue?
|
||||||
// resolve and send
|
// resolve and send
|
||||||
resolve(claim.dataValues);
|
resolve(claim.dataValues);
|
||||||
// 2. otherwise use daemon to retrieve it
|
// 2. otherwise use daemon to retrieve it
|
||||||
|
@ -156,7 +162,7 @@ module.exports = {
|
||||||
// 4. check to see if the claim is free & public
|
// 4. check to see if the claim is free & public
|
||||||
if (isFreePublicClaim(result[uri].claim)) {
|
if (isFreePublicClaim(result[uri].claim)) {
|
||||||
// 5. get claim and serve
|
// 5. get claim and serve
|
||||||
getClaimAndHandleResponse(uri, resolve, reject);
|
getClaimAndHandleResponse(uri, result[uri].claim.height, resolve, reject);
|
||||||
} else {
|
} else {
|
||||||
reject('NO_FREE_PUBLIC_CLAIMS');
|
reject('NO_FREE_PUBLIC_CLAIMS');
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,8 @@ function filterForFreePublicClaims (claimsListArray) {
|
||||||
return freePublicClaims;
|
return freePublicClaims;
|
||||||
}
|
}
|
||||||
|
|
||||||
function orderTopClaims (claimsListArray) {
|
function orderClaims (claimsListArray) {
|
||||||
logger.debug('ordering the top claims');
|
logger.debug('ordering the free public claims');
|
||||||
claimsListArray.sort((a, b) => {
|
claimsListArray.sort((a, b) => {
|
||||||
if (a.amount === b.amount) {
|
if (a.amount === b.amount) {
|
||||||
return a.height < b.height;
|
return a.height < b.height;
|
||||||
|
@ -51,7 +51,7 @@ module.exports = claimName => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// order the claims
|
// order the claims
|
||||||
const orderedPublicClaims = orderTopClaims(freePublicClaims);
|
const orderedPublicClaims = orderClaims(freePublicClaims);
|
||||||
// resolve the promise
|
// resolve the promise
|
||||||
resolve(orderedPublicClaims);
|
resolve(orderedPublicClaims);
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,7 +2,7 @@ const logger = require('winston');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
handleRequestError (error, res) {
|
handleRequestError (error, res) {
|
||||||
logger.error('Request Error,', error);
|
logger.error('Request Error >>', error);
|
||||||
if (error === 'NO_CLAIMS' || error === 'NO_FREE_PUBLIC_CLAIMS') {
|
if (error === 'NO_CLAIMS' || error === 'NO_FREE_PUBLIC_CLAIMS') {
|
||||||
res.status(307).render('noClaims');
|
res.status(307).render('noClaims');
|
||||||
} else if (error.response) {
|
} else if (error.response) {
|
||||||
|
@ -10,11 +10,11 @@ module.exports = {
|
||||||
} else if (error.code === 'ECONNREFUSED') {
|
} else if (error.code === 'ECONNREFUSED') {
|
||||||
res.status(400).send('Connection refused. The daemon may not be running.');
|
res.status(400).send('Connection refused. The daemon may not be running.');
|
||||||
} else {
|
} else {
|
||||||
res.status(400).send(error.toString());
|
res.status(400).send(JSON.stringify(error));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
handlePublishError (error) {
|
handlePublishError (error) {
|
||||||
logger.error('Publish Error,', error);
|
logger.error('Publish Error >>', error);
|
||||||
if (error.code === 'ECONNREFUSED') {
|
if (error.code === 'ECONNREFUSED') {
|
||||||
return 'Connection refused. The daemon may not be running.';
|
return 'Connection refused. The daemon may not be running.';
|
||||||
} else if (error.response.data.error) {
|
} else if (error.response.data.error) {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
module.exports = (sequelize, { STRING, BOOLEAN }) => {
|
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER }) => {
|
||||||
const File = sequelize.define(
|
const File = sequelize.define(
|
||||||
'File',
|
'File',
|
||||||
{
|
{
|
||||||
|
@ -14,6 +14,11 @@ module.exports = (sequelize, { STRING, BOOLEAN }) => {
|
||||||
type : STRING,
|
type : STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
},
|
},
|
||||||
|
height: {
|
||||||
|
type : INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
default : 0,
|
||||||
|
},
|
||||||
fileName: {
|
fileName: {
|
||||||
type : STRING,
|
type : STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
|
|
Loading…
Reference in a new issue