Skip to content

Commit

Permalink
Use async.parallel for cleaner flow control in colorTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewfhart committed Nov 15, 2014
1 parent 4368faf commit f355b43
Showing 1 changed file with 40 additions and 25 deletions.
65 changes: 40 additions & 25 deletions lib/protocol/ColoringEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,37 +133,52 @@ ColoringEngine.prototype.colorTransaction = function (transaction, cb) {
if (markerOutputPayload) {
// Extract the marker output (asset quantity and metadata) information
markerOutput = MarkerOutput.prototype.deserializePayload(markerOutputPayload);
// If valid marker output information was extracted, do not process further outputs
// If valid marker output information was extracted, we have all the
// information necessary to compute the colored outputs for this tx
if (markerOutput) {
foundMarkerOutput = true;

// Fetch the colored outputs for the previous transaction
// Build a recursive backtracking function for each of this
// transactions inputs. Looking at the colored outputs from
// transactions linked to this transaction's inputs will allow
// us to dertermine which assets flow into the current transaction.
var prevouts = [];
transaction.inputs().forEach(function (i, idx) {
// 'i' is a 2 element array [(Buffer) outHash, (int) outIndex]
var outHash = buffertools.reverse(i[0]).toString('hex');
coloringEngine.getOutput(outHash, idx, function (err, inTx) {
if (err) return cb(err);

// Add this transaction to the list of inputs
inputs[idx] = (inTx);

// If all inputs have been processed, compute asset ids
if (inputs.length == transaction.ins.length) {
outputsWithAssetIds = ColoringEngine.prototype._computeAssetIds(
inputs,
outIdx,
transaction.outs,
markerOutput.assetQuantities);

// If valid asset ids were computed, return them
if (outputsWithAssetIds) {
return cb(null, outputsWithAssetIds);
} else {
return cb(null, makeUncoloredResponse(transaction));
}
}

prevouts.push(function (fcb) {
var outHash = buffertools.reverse(i[0]).toString('hex');
coloringEngine.getOutput(outHash, idx, fcb);
});
}, coloringEngine);

// Fetch the colored outputs for each previous transaction
async.parallel(prevouts, function (err, inTxs){
if (err) return cb(err);

// Store results of all recursive backtracking
inputs = inTxs;

// Ensure all inputs were processed
if (inputs.length !== transaction.ins.length) {
return ("Error processing inputs: expected "
+ transaction.ins.length + " results, got " + inputs.length);
}

// Compute the asset ids of the colored outputs
outputsWithAssetIds = ColoringEngine.prototype._computeAssetIds(
inputs,
outIdx,
transaction.outs,
markerOutput.assetQuantities);

if (outputsWithAssetIds) {
// If successful, return the colored outputs
return cb(null, outputsWithAssetIds);
} else {
// Otherwise, the transaction should be considered uncolored
return cb(null, makeUncoloredResponse(transaction));
}
});
}
}
}
Expand Down

0 comments on commit f355b43

Please sign in to comment.