forked from trufflesuite/truffle-compile
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
360 lines (291 loc) · 11.2 KB
/
index.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
var Profiler = require("./profiler");
var OS = require("os");
var path = require("path");
var fs = require("fs");
var async = require("async");
var Profiler = require("./profiler");
var CompileError = require("./compileerror");
var expect = require("truffle-expect");
var find_contracts = require("truffle-contract-sources");
var Config = require("truffle-config");
var debug = require("debug")("compile");
// Most basic of the compile commands. Takes a hash of sources, where
// the keys are file or module paths and the values are the bodies of
// the contracts. Does not evaulate dependencies that aren't already given.
//
// Default options:
// {
// strict: false,
// quiet: false,
// logger: console
// }
var compile = function(sources, options, callback) {
if (typeof options == "function") {
callback = options;
options = {};
}
if (options.logger == null) {
options.logger = console;
}
expect.options(options, [
"contracts_directory",
"solc"
]);
// Load solc module only when compilation is actually required.
var solc = require("solc");
// Clean up after solc.
var listeners = process.listeners("uncaughtException");
var solc_listener = listeners[listeners.length - 1];
if (solc_listener) {
process.removeListener("uncaughtException", solc_listener);
}
// Ensure sources have operating system independent paths
// i.e., convert backslashes to forward slashes; things like C: are left intact.
var operatingSystemIndependentSources = {};
var originalPathMappings = {};
Object.keys(sources).forEach(function(source) {
// Turn all backslashes into forward slashes
var replacement = source.replace(/\\/g, "/");
// Turn G:/.../ into /G/.../ for Windows
if (replacement.length >= 2 && replacement[1] == ":") {
replacement = "/" + replacement;
replacement = replacement.replace(":", "");
}
// Save the result
operatingSystemIndependentSources[replacement] = sources[source];
// Map the replacement back to the original source path.
originalPathMappings[replacement] = source;
});
var solcStandardInput = {
language: "Solidity",
sources: {},
settings: {
optimizer: options.solc.optimizer,
outputSelection: {
"*": {
"": [
"legacyAST",
"ast"
],
"*": [
"abi",
"evm.bytecode.object",
"evm.bytecode.sourceMap",
"evm.deployedBytecode.object",
"evm.deployedBytecode.sourceMap"
]
},
}
}
};
// Nothing to compile? Bail.
if (Object.keys(sources).length == 0) {
return callback(null, [], []);
}
Object.keys(operatingSystemIndependentSources).forEach(function(file_path) {
solcStandardInput.sources[file_path] = {
content: operatingSystemIndependentSources[file_path]
}
});
var result = solc.compileStandard(JSON.stringify(solcStandardInput));
var standardOutput = JSON.parse(result);
var errors = standardOutput.errors || [];
var warnings = [];
if (options.strict !== true) {
warnings = errors.filter(function(error) {
return error.severity == "warning";
});
errors = errors.filter(function(error) {
return error.severity != "warning";
});
if (options.quiet !== true && warnings.length > 0) {
options.logger.log(OS.EOL + "Compilation warnings encountered:" + OS.EOL);
options.logger.log(warnings.map(function(warning) {
return warning.formattedMessage;
}).join());
}
}
if (errors.length > 0) {
options.logger.log("");
return callback(new CompileError(standardOutput.errors.map(function(error) {
return error.formattedMessage;
}).join()));
}
var contracts = standardOutput.contracts;
var files = [];
Object.keys(standardOutput.sources).forEach(function(filename) {
var source = standardOutput.sources[filename];
files[source.id] = originalPathMappings[filename];
});
var returnVal = {};
// This block has comments in it as it's being prepared for solc > 0.4.10
Object.keys(contracts).forEach(function(source_path) {
var files_contracts = contracts[source_path];
Object.keys(files_contracts).forEach(function(contract_name) {
var contract = files_contracts[contract_name];
var contract_definition = {
contract_name: contract_name,
sourcePath: originalPathMappings[source_path], // Save original source path, not modified ones
source: operatingSystemIndependentSources[source_path],
sourceMap: contract.evm.bytecode.sourceMap,
deployedSourceMap: contract.evm.deployedBytecode.sourceMap,
legacyAST: standardOutput.sources[source_path].legacyAST,
ast: standardOutput.sources[source_path].ast,
abi: contract.abi,
bytecode: "0x" + contract.evm.bytecode.object,
deployedBytecode: "0x" + contract.evm.deployedBytecode.object,
unlinked_binary: "0x" + contract.evm.bytecode.object, // deprecated
compiler: {
"name": "solc",
"version": solc.version()
}
}
// Reorder ABI so functions are listed in the order they appear
// in the source file. Solidity tests need to execute in their expected sequence.
contract_definition.abi = orderABI(contract_definition);
if (options.rawData) {
contract_definition.rawData = contract;
}
// Go through the link references and replace them with older-style
// identifiers. We'll do this until we're ready to making a breaking
// change to this code.
Object.keys(contract.evm.bytecode.linkReferences).forEach(function(file_name) {
var fileLinks = contract.evm.bytecode.linkReferences[file_name];
Object.keys(fileLinks).forEach(function(library_name) {
var linkReferences = fileLinks[library_name] || [];
contract_definition.bytecode = replaceLinkReferences(contract_definition.bytecode, linkReferences, library_name);
contract_definition.unlinked_binary = replaceLinkReferences(contract_definition.unlinked_binary, linkReferences, library_name);
});
});
// Now for the deployed bytecode
Object.keys(contract.evm.deployedBytecode.linkReferences).forEach(function(file_name) {
var fileLinks = contract.evm.deployedBytecode.linkReferences[file_name];
Object.keys(fileLinks).forEach(function(library_name) {
var linkReferences = fileLinks[library_name] || [];
contract_definition.deployedBytecode = replaceLinkReferences(contract_definition.deployedBytecode, linkReferences, library_name);
});
});
returnVal[contract_name] = contract_definition;
});
});
callback(null, returnVal, files);
};
function replaceLinkReferences(bytecode, linkReferences, libraryName) {
var linkId = "__" + libraryName;
while (linkId.length < 40) {
linkId += "_";
}
linkReferences.forEach(function(ref) {
// ref.start is a byte offset. Convert it to character offset.
var start = (ref.start * 2) + 2;
bytecode = bytecode.substring(0, start) + linkId + bytecode.substring(start + 40);
});
return bytecode;
};
function orderABI(contract){
var contract_definition;
var ordered_function_names = [];
var ordered_functions = [];
for (var i = 0; i < contract.legacyAST.children.length; i++) {
var definition = contract.legacyAST.children[i];
// AST can have multiple contract definitions, make sure we have the
// one that matches our contract
if (definition.name !== "ContractDefinition" ||
definition.attributes.name !== contract.contract_name){
continue;
}
contract_definition = definition;
break;
}
if (!contract_definition) return contract.abi;
if (!contract_definition.children) return contract.abi;
contract_definition.children.forEach(function(child) {
if (child.name == "FunctionDefinition") {
ordered_function_names.push(child.attributes.name);
}
});
// Put function names in a hash with their order, lowest first, for speed.
var functions_to_remove = ordered_function_names.reduce(function(obj, value, index) {
obj[value] = index;
return obj;
}, {});
// Filter out functions from the abi
var function_definitions = contract.abi.filter(function(item) {
return functions_to_remove[item.name] != null;
});
// Sort removed function defintions
function_definitions = function_definitions.sort(function(item_a, item_b) {
var a = functions_to_remove[item_a.name];
var b = functions_to_remove[item_b.name];
if (a > b) return 1;
if (a < b) return -1;
return 0;
});
// Create a new ABI, placing ordered functions at the end.
var newABI = [];
contract.abi.forEach(function(item) {
if (functions_to_remove[item.name] != null) return;
newABI.push(item);
});
// Now pop the ordered functions definitions on to the end of the abi..
Array.prototype.push.apply(newABI, function_definitions);
return newABI;
}
// contracts_directory: String. Directory where .sol files can be found.
// quiet: Boolean. Suppress output. Defaults to false.
// strict: Boolean. Return compiler warnings as errors. Defaults to false.
compile.all = function(options, callback) {
var self = this;
find_contracts(options.contracts_directory, function(err, files) {
options.paths = files;
compile.with_dependencies(options, callback);
});
};
// contracts_directory: String. Directory where .sol files can be found.
// build_directory: String. Optional. Directory where .sol.js files can be found. Only required if `all` is false.
// all: Boolean. Compile all sources found. Defaults to true. If false, will compare sources against built files
// in the build directory to see what needs to be compiled.
// quiet: Boolean. Suppress output. Defaults to false.
// strict: Boolean. Return compiler warnings as errors. Defaults to false.
compile.necessary = function(options, callback) {
var self = this;
options.logger = options.logger || console;
Profiler.updated(options, function(err, updated) {
if (err) return callback(err);
if (updated.length == 0 && options.quiet != true) {
return callback(null, [], {});
}
options.paths = updated;
compile.with_dependencies(options, callback);
});
};
compile.with_dependencies = function(options, callback) {
options.logger = options.logger || console;
options.contracts_directory = options.contracts_directory || process.cwd();
expect.options(options, [
"paths",
"working_directory",
"contracts_directory",
"resolver"
]);
var config = Config.default().merge(options);
var self = this;
Profiler.required_sources(config.with({
paths: options.paths,
base_path: options.contracts_directory,
resolver: options.resolver
}), function(err, result) {
if (err) return callback(err);
if (options.quiet != true) {
Object.keys(result).sort().forEach(function(import_path) {
var display_path = import_path;
if (path.isAbsolute(import_path)) {
display_path = "." + path.sep + path.relative(options.working_directory, import_path);
}
options.logger.log("Compiling " + display_path + "...");
});
}
compile(result, options, callback);
});
};
module.exports = compile;