-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactionsOperations.js
58 lines (52 loc) · 1.59 KB
/
transactionsOperations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function pairSmallerOrEqual(pair1, pair2) {
if (pair1.name === pair2.name && pair1.version <= pair2.version) {
return true;
}
return false;
}
function compareTransactions(transaction1, transaction2) {
if (transaction1.pulse < transaction2.pulse) {
return true;
}
if (transaction1.pulse > transaction2.pulse) {
return false;
}
var smallerThan=false;
for (var i in transaction1.output) {
for (var j in transaction2.input) {
if (pairSmallerOrEqual(transaction1.output[i], transaction2.input[j])) {
smallerThan=true;
return true;
}
}
}
for (var i in transaction1.input) {
for (var j in transaction2.output) {
if (pairSmallerOrEqual(transaction1.input[i], transaction2.output[j])) {
if(smallerThan) {
console.error('incompatible transactions');
return;
}
return false;s
}
}
}
if(smallerThan){
return true;
}
if (transaction1.digest < transaction2.digest) {
return true;
}
return false;
}
exports.sortTransactions = function (transactions) {
for (var i = 0; i < transactions.length - 1; i++) {
for (var j = i + 1; j < transactions.length; j++) {
if (compareTransactions(transactions[j], transactions[i])) {
var tempTransaction = transactions[i];
transactions[i] = transactions[j];
transactions[j] = tempTransaction;
}
}
}
}