This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathdeployment.js
373 lines (324 loc) · 10.9 KB
/
deployment.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
361
362
363
364
365
366
367
368
369
370
371
372
373
const debug = require("debug")("deployer:deployment"); // eslint-disable-line no-unused-vars
const sanitizeMessage = require("./sanitizeMessage");
/**
* @class Deployment
*/
class Deployment {
/**
* constructor
* @param {Number} confirmations confirmations needed to resolve an instance
*/
constructor(options) {
const networkConfig = options.networks[options.network] || {};
this.confirmations = options.confirmations || 0;
this.timeoutBlocks = options.timeoutBlocks || 0;
this.pollingInterval = networkConfig.deploymentPollingInterval || 4000;
this.promiEventEmitters = [];
this.confirmationsMap = {};
this.blockPoll;
this.options = options;
}
async emit(name, data) {
if (this.options && this.options.events) {
return await this.options.events.emit(name, data);
}
}
/**
* Helper to parse a deploy statement's overwrite option
* @private
* @param {Array} args arguments passed to deploy
* @param {Boolean} isDeployed is contract deployed?
* @return {Boolean} true if overwrite is ok
*/
_canOverwrite(args, isDeployed) {
const lastArg = args[args.length - 1];
const isObject = typeof lastArg === "object";
const overwrite = isObject && isDeployed && lastArg.overwrite === false;
return !overwrite;
}
/**
* Gets arbitrary values from constructor params, if they exist.
* @private
* @param {Array} args constructor params
* @return {Any|Undefined} gas value
*/
_extractFromArgs(args, key) {
let value;
args.forEach(arg => {
const hasKey =
!Array.isArray(arg) &&
typeof arg === "object" &&
Object.keys(arg).includes(key);
if (hasKey) value = arg[key];
});
return value;
}
/**
* Emits a `block` event on each new block heard. This polling is
* meant to be cancelled immediately on resolution of the
* contract instance or on error. (See stopBlockPolling)
* @private
* @param {Object} interfaceAdapter
*/
async _startBlockPolling(interfaceAdapter) {
const self = this;
const startTime = new Date().getTime();
let secondsWaited = 0;
let blocksWaited = 0;
let currentBlock = await interfaceAdapter.getBlockNumber();
self.blockPoll = setInterval(async () => {
const newBlock = await interfaceAdapter.getBlockNumber();
blocksWaited = newBlock - currentBlock + blocksWaited;
currentBlock = newBlock;
secondsWaited = Math.floor((new Date().getTime() - startTime) / 1000);
const data = {
blockNumber: newBlock,
blocksWaited: blocksWaited,
secondsWaited: secondsWaited
};
await self.emit("deployment:block", data);
}, self.pollingInterval);
}
/**
* Clears the interval timer initiated by `startBlockPolling
* @private
*/
_stopBlockPolling() {
clearInterval(this.blockPoll);
}
/**
* Waits `n` blocks after a tx is mined, firing a pseudo
* 'confirmation' event for each one.
* @private
* @param {Number} blocksToWait
* @param {Object} receipt
* @param {Object} interfaceAdapter
* @return {Promise} Resolves after `blockToWait` blocks
*/
async _waitBlocks(blocksToWait, state, interfaceAdapter) {
const self = this;
let currentBlock = await interfaceAdapter.getBlockNumber();
return new Promise(accept => {
let blocksHeard = 0;
const poll = setInterval(async () => {
const newBlock = await interfaceAdapter.getBlockNumber();
if (newBlock > currentBlock) {
blocksHeard = newBlock - currentBlock + blocksHeard;
currentBlock = newBlock;
const data = {
contractName: state.contractName,
receipt: state.receipt,
num: blocksHeard,
block: currentBlock
};
await self.emit("deployment:confirmation", data);
}
if (blocksHeard >= blocksToWait) {
clearInterval(poll);
accept();
}
}, self.pollingInterval);
});
}
/**
* Sanity checks catch-all:
* Are we connected?
* Is contract deployable?
* @private
* @param {Object} contract TruffleContract
* @return {Promise} throws on error
*/
async _preFlightCheck(contract) {
// Check that contract is not array
if (Array.isArray(contract)) {
const data = {
type: "noBatches",
contract
};
const message = await this.emit("deployment:error", data);
throw new Error(sanitizeMessage(message));
}
// Check bytecode
if (contract.bytecode === "0x") {
const data = {
type: "noBytecode",
contract
};
const message = await this.emit("deployment:error", data);
throw new Error(sanitizeMessage(message));
}
// Check network
await contract.detectNetwork();
}
// ----------------- Confirmations Handling (temporarily disabled) -------------------------------
/**
* There are outstanding issues at both geth (with websockets) & web3 (with confirmation handling
@@ -247,27 +221,6 @@ class Deployment {
});
}
/**
* Handler for contract's `confirmation` event. Rebroadcasts as a deployer event
* and maintains a table of txHashes & their current confirmation number. This
* table gets polled if the user needs to wait a few blocks before getting
* an instance back.
* @private
* @param {Object} parent Deployment instance. Local `this` belongs to promievent
* @param {Number} num Confirmation number
* @param {Object} receipt transaction receipt
*/
async _confirmationCb(parent, state, num, receipt) {
const eventArgs = {
contractName: state.contractName,
num: num,
receipt: receipt
};
parent.confirmationsMap[receipt.transactionHash] = num;
await parent.emitter.emit("confirmation", eventArgs);
}
// ----------------- Confirmations Handling (temporarily disabled) -------------------------------
/**
* There are outstanding issues at both geth (with websockets) & web3 (with confirmation handling
* over RPC) that impair the confirmations handlers' reliability. In the interim we're using
* simple block polling instead. (See also _confirmationCb )
*
* Queries the confirmations mapping periodically to see if we have
* heard enough confirmations for a given tx to allow `deploy` to complete.
* Resolves when this is true.
*
* @private
* @param {String} hash contract creation tx hash
* @return {Promise}
*/
async _waitForConfirmations(hash) {
let interval;
const self = this;
return new Promise(accept => {
interval = setInterval(() => {
if (self.confirmationsMap[hash] >= self.confirmations) {
clearInterval(interval);
accept();
}
}, self.pollingInterval);
});
}
// ------------------------------------ Methods --------------------------------------------------
/**
*
* @param {Object} contract Contract abstraction
* @param {Array} args Constructor arguments
* @return {Promise} Resolves an instance
*/
executeDeployment(contract, args) {
const self = this;
return async function () {
await self._preFlightCheck(contract);
let instance;
let eventArgs;
let shouldDeploy = true;
let state = {
contractName: contract.contractName
};
const isDeployed = contract.isDeployed();
const newArgs = await Promise.all(args);
const currentBlock = await contract.interfaceAdapter.getBlock("latest");
// Last arg can be an object that tells us not to overwrite.
if (newArgs.length > 0) {
shouldDeploy = self._canOverwrite(newArgs, isDeployed);
}
// Case: deploy:
if (shouldDeploy) {
/*
Set timeout override. If this value is zero,
@truffle/contract will defer to web3's defaults:
- 50 blocks (websockets) OR 50 * 15sec (http)
*/
contract.timeoutBlocks = self.timeoutBlocks;
eventArgs = {
state: state,
contract: contract,
deployed: isDeployed,
blockLimit: currentBlock.gasLimit,
gas: self._extractFromArgs(newArgs, "gas") || contract.defaults().gas,
gasPrice:
self._extractFromArgs(newArgs, "gasPrice") ||
contract.defaults().gasPrice,
from:
self._extractFromArgs(newArgs, "from") || contract.defaults().from
};
// Get an estimate for previews / detect constructor revert
// NB: web3 does not strip the revert msg here like it does for `deploy`
try {
eventArgs.estimate = await contract.new.estimateGas.apply(
contract,
newArgs
);
} catch (err) {
eventArgs.estimateError = err;
}
// Emit `deployment:start` & send transaction
await self.emit("deployment:start", eventArgs);
const promiEvent = contract.new.apply(contract, newArgs);
// Track emitters for cleanup on exit
self.promiEventEmitters.push(promiEvent);
// Subscribe to contract events / rebroadcast them to any reporters
promiEvent
.on("transactionHash", async hash => {
const data = {
contractName: state.contractName,
transactionHash: hash
};
await self.emit("deployment:txHash", data);
})
.on("receipt", receipt => {
// We want this receipt available for the post-deploy event
// so gas reporting is at hand there.
state.receipt = receipt;
});
await self._startBlockPolling(contract.interfaceAdapter);
// Get instance (or error)
try {
instance = await promiEvent;
self._stopBlockPolling();
} catch (err) {
self._stopBlockPolling();
eventArgs.error = err.error || err;
const message = await self.emit("deployment:failed", eventArgs);
self.close();
throw new Error(sanitizeMessage(message));
}
// Case: already deployed
} else {
instance = await contract.deployed();
}
// Emit `postDeploy`
eventArgs = {
contract: contract,
instance: instance,
deployed: shouldDeploy,
receipt: state.receipt
};
await self.emit("deployment:succeed", eventArgs);
// Wait for `n` blocks
if (self.confirmations !== 0 && shouldDeploy) {
await self._waitBlocks(
self.confirmations,
state,
contract.interfaceAdapter
);
}
// Finish: Ensure the address and tx-hash are set on the contract.
contract.address = instance.address;
contract.transactionHash = instance.transactionHash;
return instance;
};
}
/**
* Cleans up promiEvents' emitter listeners
*/
close() {
this.promiEventEmitters.forEach(item => {
item.removeAllListeners();
});
}
}
module.exports = Deployment;