Skip to content
Snippets Groups Projects
Commit 94e4eca3 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

[fix] Update to ECMA 2015

parent 98c816cf
No related branches found
No related tags found
No related merge requests found
Subproject commit be5b9ddbbb88a5a129ec47c0968b9061de3bfe29
Subproject commit 2667bc6efab8ae2393ab57f330c33f7dedf99fe2
Subproject commit 583a19c5d1733a606e68f72ecbb124ce2d1316cc
Subproject commit 197e3a8e9162548461251a20ba3b99e1fc89c314
......@@ -12,37 +12,38 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
BMA = BMA || defaultBMA;
var
api = new Api(this, "csTx-" + id),
api = new Api(this, "csTx-" + id);
_reduceTxAndPush = function(pubkey, txArray, result, processedTxMap, allowPendings) {
function reduceTxAndPush(pubkey, txArray, result, processedTxMap, allowPendings) {
if (!txArray || !txArray.length) return; // Skip if empty
_.forEach(txArray, function(tx) {
if (tx.block_number || allowPendings) {
var walletIsIssuer = false;
var otherIssuer = tx.issuers.reduce(function(issuer, res) {
if (tx.block_number !== null || allowPendings) {
let walletIsIssuer = false;
let otherIssuer = tx.issuers.reduce(function(issuer, res) {
walletIsIssuer = walletIsIssuer || (res === pubkey);
return issuer + ((res !== pubkey) ? ', ' + res : '');
}, '');
if (otherIssuer.length > 0) {
otherIssuer = otherIssuer.substring(2);
}
var otherReceiver;
var outputBase;
var sources = [];
var lockedOutputs;
var amount = tx.outputs.reduce(function(sum, output, noffset) {
let otherReceiver,
outputBase,
sources = [],
lockedOutputs;
const amount = tx.outputs.reduce(function(sum, output, noffset) {
// FIXME duniter v1.4.13
var outputArray = (typeof output == 'string') ? output.split(':',3) : [output.amount,output.base,output.conditions];
const outputArray = (typeof output == 'string') ? output.split(':',3) : [output.amount,output.base,output.conditions];
outputBase = parseInt(outputArray[1]);
var outputAmount = powBase(parseInt(outputArray[0]), outputBase);
var outputCondition = outputArray[2];
var sigMatches = BMA.regexp.TX_OUTPUT_SIG.exec(outputCondition);
const outputAmount = powBase(parseInt(outputArray[0]), outputBase);
const outputCondition = outputArray[2];
const sigMatches = BMA.regexp.TX_OUTPUT_SIG.exec(outputCondition);
// Simple unlock condition
if (sigMatches) {
var outputPubkey = sigMatches[1];
if (outputPubkey == pubkey) { // output is for the wallet
const outputPubkey = sigMatches[1];
if (outputPubkey === pubkey) { // output is for the wallet
if (!walletIsIssuer) {
return sum + outputAmount;
}
......@@ -54,7 +55,8 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
type: 'T',
identifier: tx.hash,
noffset: noffset,
consumed: false
consumed: false,
conditions: outputCondition
});
}
}
......@@ -69,20 +71,19 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
}
// Complex unlock condition, on the issuer pubkey
else if (outputCondition.indexOf('SIG('+pubkey+')') != -1) {
var lockedOutput = BMA.tx.parseUnlockCondition(outputCondition);
else if (outputCondition.indexOf('SIG('+pubkey+')') !== -1) {
const lockedOutput = BMA.tx.parseUnlockCondition(outputCondition);
if (lockedOutput) {
// Add a source
// FIXME: should be uncomment when filtering source on transfer()
/*sources.push(angular.merge({
sources.push(angular.merge({
amount: parseInt(outputArray[0]),
base: outputBase,
type: 'T',
identifier: tx.hash,
noffset: noffset,
conditions: outputCondition,
consumed: false
}, lockedOutput));
*/
lockedOutput.amount = outputAmount;
lockedOutputs = lockedOutputs || [];
lockedOutputs.push(lockedOutput);
......@@ -99,8 +100,8 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
// Avoid duplicated tx, or tx to him self
var txKey = amount + ':' + tx.hash + ':' + time;
if (!processedTxMap[txKey] && amount !== 0) {
processedTxMap[txKey] = true;
if (!processedTxMap[txKey]) {
processedTxMap[txKey] = true; // Mark as processed
var newTx = {
time: time,
amount: amount,
......@@ -123,9 +124,10 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
}
}
});
},
}
loadTx = function(pubkey, fromTime) {
function loadTx(pubkey, fromTime) {
return $q(function(resolve, reject) {
var nowInSec = moment().utc().unix();
......@@ -138,12 +140,6 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
};
var processedTxMap = {};
var _reduceTx = function (res) {
_reduceTxAndPush(pubkey, res.history.sent, tx.history, processedTxMap);
_reduceTxAndPush(pubkey, res.history.received, tx.history, processedTxMap);
_reduceTxAndPush(pubkey, res.history.sending, tx.pendings, processedTxMap, true /*allow pendings*/);
_reduceTxAndPush(pubkey, res.history.pending, tx.pendings, processedTxMap, true /*allow pendings*/);
};
var jobs = [
// get current block
......@@ -151,11 +147,18 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
// get pending tx
BMA.tx.history.pending({pubkey: pubkey})
.then(_reduceTx)
.then(function (res) {
reduceTxAndPush(pubkey, res.history.sending, tx.pendings, processedTxMap, true /*allow pendings*/);
reduceTxAndPush(pubkey, res.history.pending, tx.pendings, processedTxMap, true /*allow pendings*/);
})
];
// get TX history since
if (fromTime !== 'pending') {
var reduceTxFn = function (res) {
reduceTxAndPush(pubkey, res.history.sent, tx.history, processedTxMap, false);
reduceTxAndPush(pubkey, res.history.received, tx.history, processedTxMap, false);
};
// get TX from a given time
if (fromTime > 0) {
......@@ -163,19 +166,20 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
var sliceTime = csSettings.data.walletHistorySliceSecond;
fromTime = fromTime - (fromTime % sliceTime);
for(var i = fromTime; i - sliceTime < nowInSec; i += sliceTime) {
jobs.push(BMA.tx.history.times({pubkey: pubkey, from: i, to: i+sliceTime-1})
.then(_reduceTx)
jobs.push(BMA.tx.history.times({pubkey: pubkey, from: i, to: i+sliceTime-1}, true /*with cache*/)
.then(reduceTxFn)
);
}
jobs.push(BMA.tx.history.timesNoCache({pubkey: pubkey, from: nowInSec - (nowInSec % sliceTime), to: nowInSec+999999999})
.then(_reduceTx));
// Last slide: no cache
jobs.push(BMA.tx.history.times({pubkey: pubkey, from: nowInSec - (nowInSec % sliceTime), to: nowInSec+999999999}, false/*no cache*/)
.then(reduceTxFn));
}
// get all TX
else {
jobs.push(BMA.tx.history.all({pubkey: pubkey})
.then(_reduceTx)
.then(reduceTxFn)
);
}
......@@ -226,13 +230,11 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
tx.history.sort(function(tx1, tx2) {
return (tx2.time - tx1.time);
});
tx.validating = tx.history.filter(function(tx) {
return (tx.block_number > current.number - csSettings.data.blockValidityWindow);
var firstValidatedTxIndex = tx.history.findIndex(function(tx){
return (tx.block_number <= current.number - csSettings.data.blockValidityWindow);
});
// remove validating from history
if (tx.validating.length) {
tx.history.splice(0, tx.validating.length);
}
tx.validating = firstValidatedTxIndex > 0 ? tx.history.splice(0, firstValidatedTxIndex) : [];
tx.fromTime = fromTime !== 'pending' && fromTime || undefined;
tx.toTime = tx.history.length ? tx.history[0].time /*=max(tx.time)*/: tx.fromTime;
......@@ -241,27 +243,27 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
})
.catch(reject);
});
},
}
powBase = function(amount, base) {
function powBase(amount, base) {
return base <= 0 ? amount : amount * Math.pow(10, base);
},
}
addSource = function(src, sources, sourcesIndexByKey) {
function addSource(src, sources, sourcesIndexByKey) {
var srcKey = src.type+':'+src.identifier+':'+src.noffset;
if (angular.isUndefined(sourcesIndexByKey[srcKey])) {
sources.push(src);
sourcesIndexByKey[srcKey] = sources.length - 1;
}
},
}
addSources = function(result, sources) {
function addSources(result, sources) {
_(sources).forEach(function(src) {
addSource(src, result.sources, result.sourcesIndexByKey);
});
},
}
loadSourcesAndBalance = function(pubkey) {
function loadSourcesAndBalance(pubkey) {
return BMA.tx.sources({pubkey: pubkey})
.then(function(res){
var data = {
......@@ -282,9 +284,9 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
console.warn("[tx] Error while getting sources...", err);
throw err;
});
},
}
loadData = function(pubkey, fromTime) {
function loadData(pubkey, fromTime) {
var now = Date.now();
return $q.all([
......@@ -314,7 +316,7 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
// TODO get sources from the issuer ?
}
else {
_.forEach(tx.inputs, function(input) {
_.find(tx.inputs, function(input) {
var inputKey = input.split(':').slice(2).join(':');
var srcIndex = data.sourcesIndexByKey[inputKey];
if (angular.isDefined(srcIndex)) {
......@@ -322,7 +324,7 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
}
else {
valid = false;
return false; // break
return true; // break
}
});
if (tx.sources) { // add source output
......@@ -377,16 +379,20 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
console.debug('[tx] TX and sources loaded in '+ (Date.now()-now) +'ms');
return data;
});
})
.catch(function(err) {
console.warn("[tx] Error while getting sources and tx...", err);
throw err;
});
},
}
loadSources = function(pubkey) {
function loadSources(pubkey) {
console.debug("[tx] Loading sources for " + pubkey.substring(0,8));
return loadData(pubkey, 'pending');
};
}
// Download TX history file
downloadHistoryFile = function(pubkey, options) {
function downloadHistoryFile(pubkey, options) {
options = options || {};
options.fromTime = options.fromTime || -1;
......@@ -444,7 +450,7 @@ angular.module('cesium.tx.services', ['ngApi', 'cesium.bma.services',
FileSaver.saveAs(file, filename);
});
});
};
}
// Register extension points
api.registerEvent('data', 'loadUDs');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment