-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-collision-read.js
116 lines (111 loc) · 3.06 KB
/
test-collision-read.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';
const fs = require('fs');
const readline = require('readline');
const Contract = require('./index-collision');
const outputFile = process.argv[2];
const account = process.argv[3];
const accountPassphrase = process.argv[4];
const type = parseInt(process.argv[5]);
const serverUrl = process.argv[6];
let funcName;
let isByte = false;
let isIterable = false;
switch (type) {
case 0:
funcName = 'mapStrings';
break;
case 1:
funcName = 'mapBytes';
isByte = true;
break;
case 2:
funcName = 'arrayStrings';
break;
case 3:
funcName = 'arrayBytes';
isByte = true;
break;
case 4:
funcName = 'iterableMapping';
isIterable = 'getIterableMapping';
break;
default:
funcName = 'addressArray';
isIterable = 'getAddressArray';
break;
}
console.log('funcName:' + funcName);
Contract.setServer(serverUrl);
Contract.setAccount(account, accountPassphrase);
const rl = readline.createInterface({
input: fs.createReadStream(outputFile)
});
let errorIds = [];
let countError = 0;
let lines = [];
rl.on('line', (line) => {
lines.push(line);
}).on('close', () => {
checkFiles(0);
});
function checkFiles(index){
if(index < lines.length){
let elems = lines[index].split(',');
let docId = Contract.getWeb3().toBigNumber(elems[0].substring('docId:'.length));
Contract[funcName](docId, function(error, result) {
let expect = elems[2];
expect = expect.substring(1, expect.length - 1);
if (error) {
console.log(error);
} else {
let val = isByte ? new Buffer(result).toString() : result;
if (!val.startsWith(expect)) {
let res = 'ERROR';
errorIds.push([docId, expect]);
console.log(res + ' expect:' + expect + ';get:' + val.substring(0, expect.length));
}
}
checkFiles(index + 1);
});
} else {
console.log('countAll:' + lines.length);
console.log('countError:' + errorIds.length);
if(errorIds.length > 0){
console.log('findErrors');
Contract[funcName+"Length"](function(error, result) {
if (error) {
console.log(error);
process.exit();
} else {
console.log('filesCount:'+result);
findErrors(0, result);
}
});
} else {
console.log('end');
process.exit();
}
}
}
function findErrors(index, count){
if(index < count){
var funcNameCur = isIterable ? isIterable : funcName;
Contract[funcNameCur](index, function(error, result) {
if (error) {
console.log(error);
} else {
let val = isByte ? new Buffer(result).toString() : result;
for(let i = 0; i < errorIds.length; ++i){
if(val.startsWith(errorIds[i][1])){
var docId = isIterable ? '0x'+errorIds[i][0].toString(16) : errorIds[i][0].toString();
console.log('docId:'+docId+'; found at index:' + index);
}
}
}
findErrors(index + 1, count);
});
} else {
console.log('end');
process.exit();
}
}