Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix blocknumber bug #72

Merged
merged 5 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions creator-node/src/apiHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports.handleResponse = (func) => {
sendResponse(req, res, resp)
next()
} catch (error) {
console.error('HandleResponse', error)
next(error)
}
}
Expand Down
2 changes: 2 additions & 0 deletions creator-node/src/fileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ async function saveFileToIPFSFromFS (req, srcPath, fileType, t) {

const ipfs = req.app.get('ipfsAPI')

req.logger.info(`beginning saveFileToIPFSFromFS for srcPath ${srcPath}`)

const multihash = (await ipfs.addFromFs(srcPath))[0].hash
const dstPath = path.join(req.app.get('storagePath'), multihash)

Expand Down
2 changes: 1 addition & 1 deletion creator-node/src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function authMiddleware (req, res, next) {
}

// Ensure cnodeUser exists for session
const cnodeUser = await models.CNodeUser.findOne({ where: { cnodeUserUUID: cnodeUserUUID } })
const cnodeUser = await models.CNodeUser.findOne({ where: { cnodeUserUUID } })
if (!cnodeUser) {
return sendResponse(req, res, errorResponseUnauthorized('No node user exists for provided authentication token'))
}
Expand Down
3 changes: 2 additions & 1 deletion creator-node/src/routes/nodeSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ async function _nodesync (req, walletPublicKeys, creatorNodeEndpoint) {
if ((fetchedLatestBlockNumber === -1 && latestBlockNumber !== -1) ||
(fetchedLatestBlockNumber !== -1 && fetchedLatestBlockNumber <= latestBlockNumber)
) {
throw new Error(`Imported data is outdated, will not sync.`)
throw new Error(`Imported data is outdated, will not sync. Imported latestBlockNumber \
${fetchedLatestBlockNumber} Self latestBlockNumber ${latestBlockNumber}`)
}

const cnodeUserUUID = cnodeUser.cnodeUserUUID
Expand Down
13 changes: 10 additions & 3 deletions creator-node/src/routes/tracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ module.exports = function (app) {

// Error on outdated blocknumber.
const cnodeUser = req.session.cnodeUser
if (!cnodeUser.latestBlockNumber || cnodeUser.latestBlockNumber >= blockNumber) {
if (!cnodeUser.latestBlockNumber || cnodeUser.latestBlockNumber > blockNumber) {
return errorResponseBadRequest(`Invalid blockNumber param. Must be higher than previously processed blocknumber.`)
}
const cnodeUserUUID = req.session.cnodeUserUUID
Expand Down Expand Up @@ -178,8 +178,15 @@ module.exports = function (app) {
}
}))

// Update cnodeUser's latestBlockNumber.
await cnodeUser.update({ latestBlockNumber: blockNumber }, { transaction: t })
// Update cnodeUser's latestBlockNumber if higher than previous latestBlockNumber.
// TODO - move to subquery to guarantee atomicity.
const updatedCNodeUser = await models.CNodeUser.findOne({ where: { cnodeUserUUID }, transaction: t })
if (!updatedCNodeUser || !updatedCNodeUser.latestBlockNumber) { return errorResponseServerError('no') }
SidSethi marked this conversation as resolved.
Show resolved Hide resolved
req.logger.info(`cnodeuser ${cnodeUserUUID} first latestBlockNumber ${cnodeUser.latestBlockNumber} || \
current latestBlockNumber ${updatedCNodeUser.latestBlockNumber} || given blockNumber ${blockNumber}`)
if (blockNumber > updatedCNodeUser.latestBlockNumber) {
await cnodeUser.update({ latestBlockNumber: blockNumber }, { transaction: t })
}

try {
await t.commit()
Expand Down