spee.ch/server/chainquery/queries/claimQueries.js

252 lines
6.9 KiB
JavaScript
Raw Normal View History

2018-09-26 03:20:59 +02:00
const logger = require('winston');
const returnShortId = (claimsArray, longId) => {
let claimIndex;
let shortId = longId.substring(0, 1); // default short id is the first letter
let shortIdLength = 0;
// find the index of this claim id
claimIndex = claimsArray.findIndex(element => {
return element.claim_id === longId;
});
if (claimIndex < 0) {
throw new Error('claim id not found in claims list');
}
// get an array of all claims with lower height
let possibleMatches = claimsArray.slice(0, claimIndex);
// remove certificates with the same prefixes until none are left.
while (possibleMatches.length > 0) {
shortIdLength += 1;
shortId = longId.substring(0, shortIdLength);
possibleMatches = possibleMatches.filter(element => {
2019-01-26 01:37:07 +01:00
return element.claim_id && element.claim_id.substring(0, shortIdLength) === shortId;
2018-09-26 03:20:59 +02:00
});
}
return shortId;
};
2019-01-26 01:37:07 +01:00
const isLongClaimId = claimId => {
return claimId && claimId.length === 40;
};
2018-09-26 03:20:59 +02:00
2019-01-26 01:37:07 +01:00
const isShortClaimId = claimId => {
return claimId && claimId.length < 40;
};
2018-09-26 03:20:59 +02:00
export default (db, table, sequelize) => ({
2019-01-26 01:37:07 +01:00
getClaimChannelName: async publisher_id => {
return await table
.findAll({
where: { claim_id: publisher_id },
attributes: ['name'],
})
.then(result => {
if (result.length === 0) {
throw new Error(`no record found for ${claimId}`);
} else if (result.length !== 1) {
logger.warn(`more than one record matches ${claimId} in db.Claim`);
}
2018-09-26 03:20:59 +02:00
2019-01-26 01:37:07 +01:00
return result[0].name;
});
},
getShortClaimIdFromLongClaimId: async (claimId, claimName, pendingClaim) => {
2019-02-23 06:52:31 +01:00
logger.info(`claim.getShortClaimIdFromLongClaimId for ${claimName}#${claimId}`);
2019-01-26 01:37:07 +01:00
return await table
.findAll({
where: { name: claimName },
order: [['height', 'ASC']],
})
.then(result => {
if (result.length === 0) {
throw new Error('No claim(s) found with that claim name');
}
2019-01-26 01:37:07 +01:00
let list = result.map(claim => claim.dataValues);
if (pendingClaim) {
list = list.concat(pendingClaim);
}
2019-01-26 01:37:07 +01:00
return returnShortId(list, claimId);
});
},
2019-01-26 01:37:07 +01:00
getAllChannelClaims: async (channelClaimId, bidState) => {
logger.debug(`claim.getAllChannelClaims for ${channelClaimId}`);
const whereClause = bidState || {
[sequelize.Op.or]: [
{ bid_state: 'Controlling' },
{ bid_state: 'Active' },
{ bid_state: 'Accepted' },
],
};
const selectWhere = {
...whereClause,
publisher_id: channelClaimId,
};
2019-01-26 01:37:07 +01:00
return await table
.findAll({
where: selectWhere,
order: [['height', 'DESC'], ['claim_id', 'ASC']],
})
2018-10-19 17:36:42 +02:00
.then(channelClaimsArray => {
if (channelClaimsArray.length === 0) {
return null;
}
return channelClaimsArray;
});
2018-09-26 03:20:59 +02:00
},
getClaimIdByLongChannelId: async (channelClaimId, claimName) => {
logger.debug(`finding claim id for claim ${claimName} from channel ${channelClaimId}`);
2019-01-26 01:37:07 +01:00
return await table
.findAll({
where: {
name: claimName,
publisher_id: channelClaimId,
bid_state: { [sequelize.Op.or]: ['Controlling', 'Active', 'Accepted'] },
},
order: [['id', 'ASC']],
})
.then(result => {
switch (result.length) {
case 0:
return null;
case 1:
return result[0].claim_id;
default:
// Does this actually happen??? (from converted code)
logger.warn(
`${result.length} records found for "${claimName}" in channel "${channelClaimId}"`
);
return result[0].claim_id;
}
});
2018-09-26 03:20:59 +02:00
},
validateLongClaimId: async (name, claimId) => {
2019-01-26 01:37:07 +01:00
return await table
.findOne({
where: {
name,
claim_id: claimId,
},
})
.then(result => {
if (!result) {
return false;
}
return claimId;
});
2018-09-26 03:20:59 +02:00
},
getLongClaimIdFromShortClaimId: async (name, shortId) => {
2019-01-26 01:37:07 +01:00
return await table
.findAll({
where: {
name,
claim_id: {
[sequelize.Op.like]: `${shortId}%`,
},
},
order: [['height', 'ASC']],
})
.then(result => {
if (result.length === 0) {
return null;
}
return result[0].claim_id;
});
2018-09-26 03:20:59 +02:00
},
2019-01-26 01:37:07 +01:00
getTopFreeClaimIdByClaimName: async name => {
return await table
.findAll({
// TODO: Limit 1
where: { name, bid_state: { [sequelize.Op.or]: ['Controlling', 'Active', 'Accepted'] } },
order: [['effective_amount', 'DESC'], ['height', 'ASC']],
})
.then(result => {
if (result.length === 0) {
return null;
}
return result[0].claim_id;
});
2018-09-26 03:20:59 +02:00
},
getLongClaimId: async (claimName, claimId) => {
// TODO: Add failure case
logger.debug(`getLongClaimId(${claimName}, ${claimId})`);
if (isLongClaimId(claimId)) {
return table.queries.validateLongClaimId(claimName, claimId);
} else if (isShortClaimId(claimId)) {
return table.queries.getLongClaimIdFromShortClaimId(claimName, claimId);
} else {
return table.queries.getTopFreeClaimIdByClaimName(claimName);
}
},
resolveClaim: async (name, claimId) => {
logger.debug(`Claim.resolveClaim: ${name} ${claimId}`);
2019-01-26 01:37:07 +01:00
return table
.findAll({
where: { name, claim_id: claimId },
})
.then(claimArray => {
if (claimArray.length === 0) {
return null;
} else if (claimArray.length !== 1) {
logger.warn(`more than one record matches ${name}#${claimId} in db.Claim`);
}
return claimArray[0];
});
2018-09-26 03:20:59 +02:00
},
resolveClaimInChannel: async (claimName, channelId) => {
logger.debug(`Claim.resolveClaimByNames: ${claimName} in ${channelId}`);
2019-01-26 01:37:07 +01:00
return table
.findAll({
where: {
name: claimName,
publisher_id: channelId,
},
})
.then(claimArray => {
if (claimArray.length === 0) {
return null;
} else if (claimArray.length !== 1) {
logger.warn(`more than one record matches ${claimName} in ${channelId}`);
}
return claimArray[0];
});
},
2018-09-26 03:20:59 +02:00
getOutpoint: async (name, claimId) => {
logger.debug(`finding outpoint for ${name}#${claimId}`);
2019-01-26 01:37:07 +01:00
return await table
.findAll({
where: { name, claim_id: claimId },
attributes: ['transaction_hash_id'],
})
.then(result => {
if (result.length === 0) {
throw new Error(`no record found for ${name}#${claimId}`);
} else if (result.length !== 1) {
logger.warn(`more than one record matches ${name}#${claimId} in db.Claim`);
}
return result[0].transaction_hash_id;
});
2018-09-26 03:20:59 +02:00
},
getCurrentHeight: async () => {
2019-01-26 01:37:07 +01:00
return await table.max('height').then(result => {
return result || 100000;
2018-09-26 03:20:59 +02:00
});
},
2019-01-26 01:37:07 +01:00
});