-
Notifications
You must be signed in to change notification settings - Fork 13
/
contractrunner.js
193 lines (153 loc) · 5.35 KB
/
contractrunner.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
//------------------------------------------------------------------------------
/*
This file is part of Codius: https://github.com/codius
Copyright (c) 2014 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
var fs = require('fs');
var crypto = require('crypto');
var events = require('events');
var util = require('util');
var Sandbox = require('codius-node-sandbox');
/**
* Class to run contracts.
*
* @param {Object} data.manifest
* @param {Object} data.apis
* @param {String} data.manifest_hash
*/
function ContractRunner(config, data) {
events.EventEmitter.call(this);
var self = this;
self.config = config;
if (typeof data !== 'object' ||
typeof data.manifest !== 'object') {
throw new Error('ApiHandler must be instantiated with the manifest');
}
self._manifest = data.manifest;
self._apis = {};
self._manifest_hash = data.manifest_hash;
self._instance_id = data.instance_id;
self._additional_libs = data.additional_libs || '';
self._env = data.env;
self._nextFreeFileDescriptor = 4;
// Add the manifest_hash to the manifest object so it will
// be passed to the API modules when they are called
self._manifest.manifest_hash = data.manifest_hash;
// Setup sandbox instance
self._sandbox = new Sandbox({
enableGdb: self.config.enableGdb,
enableValgrind: self.config.enableValgrind,
disableNacl: self.config.disableNacl
});
self._sandbox.on('exit', self.handleExit.bind(self));
}
util.inherits(ContractRunner, events.EventEmitter);
/**
* Expose a set of APIs to the running contract.
*
* Calling this method will overwrite any previously exposed APIs.
*/
ContractRunner.prototype.setApis = function(apis) {
this._apis = apis;
};
/**
* Call the specified method, in the specified module,
* with the contract manifest, the given data, and the callback
*
* @param {String} parameters.api
* @param {String} parameters.method
* @param {Object} parameters.data
* @param {Function} callback
*/
ContractRunner.prototype.callApi = function(parameters, callback) {
var self = this;
if (!self._apis.hasOwnProperty(parameters.api)) {
callback(new Error('Unknown or undeclared API Module: ' + parameters.api));
return;
}
var api = self._apis[parameters.api];
if (parameters.method.indexOf('_') === 0) {
callback(new Error('Cannot access private method: ' + parameters.method));
return;
}
var method = api.getMethod(parameters.method);
if (typeof method !== 'function') {
callback(new Error('Unknown API Method: ' + parameters.api + '.' + parameters.method));
return;
}
var args = parameters.data;
args.push(callback);
method.apply(api, args);
};
/**
* Function to handle messages sent from within the sandbox.
*
* @param {String} message.api
* @param {String} message.method
* @param {Object} [[]] message.data
* @param {Function} callback
*/
ContractRunner.prototype.handleMessage = function(message, callback) {
var self = this;
if (typeof message !== 'object' || !message.api || !message.method) {
callback(new Error('Invalid message: must pass an object with the fields `api`, `method`, and optionally `data`.'));
return;
}
var parameters = {
api: message.api,
method: message.method,
data: message.data
};
self.callApi(parameters, callback);
};
ContractRunner.prototype.run = function() {
var self = this;
var message_handler = self.handleMessage.bind(self);
self._sandbox.setApi(message_handler);
self._sandbox.passthroughStdio();
self._sandbox.run(self._manifest.main, { env: self._env });
};
ContractRunner.prototype.getManifest = function () {
return this._manifest;
};
ContractRunner.prototype.getManifestHash = function () {
return this._manifest_hash;
};
ContractRunner.prototype.getInstanceId = function() {
return this._instance_id || '';
};
ContractRunner.prototype.getPortListener = function (port) {
if (!this._apis.net) {
throw new Error('Tried to simulate a listener for a contract that does not support networking.');
}
return this._apis.net.getPortListener(port);
};
ContractRunner.prototype.notifyAboutPortListener = function (port, listener) {
this.emit('portListener', {
port: port,
listener: listener
});
};
ContractRunner.prototype.getNextFreeFileDescriptor = function () {
return this._nextFreeFileDescriptor++;
};
ContractRunner.prototype.handleExit = function (code, signal) {
var self = this;
self.emit('exit', code, signal);
};
ContractRunner.prototype.kill = function(message) {
var self = this;
self._sandbox.kill(message);
};
exports.ContractRunner = ContractRunner;