-
Notifications
You must be signed in to change notification settings - Fork 4
/
userSetupSiege.js
302 lines (258 loc) · 8.22 KB
/
userSetupSiege.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
'use strict';
const API_END_POINT = 'http://localhost:7001/testnet/v2/',
BATCH_SIZE = 2,
POLLING_INTERVAL = 5000, //5 secs
NUMBER_OF_USERS = 5,
CREDENTIALS_ARRAY = [
{
apiKey: 'b6db0c79b84d488df6e282a1d948f2ac',
apiSecret: 'c6936ef19b03b34076a50ba1af92e1b60b573a1e0602b2d800fb7ba35fe5e392',
apiEndPoint: 'http://localhost:7001/testnet/v2/'
}
];
const rootPrefix = '.',
OSTSDK = require('@ostdotcom/ost-sdk-js'),
GetTokenDetails = require(rootPrefix + '/tools/seige/userFlow/GetTokenDetails'),
CreateUsers = require(rootPrefix + '/tools/seige/userFlow/CreateUsers'),
GenerateRequiredAddresses = require(rootPrefix + '/tools/seige/userFlow/GenerateRequiredAddresses'),
GetUser = require(rootPrefix + '/tools/seige/userFlow/GetUser'),
GetDevice = require(rootPrefix + '/tools/seige/userFlow/GetDevice'),
ActivateUser = require(rootPrefix + '/tools/seige/userFlow/ActivateUser'),
AuthorizeDevice = require(rootPrefix + '/tools/seige/userFlow/AuthorizeDevice'),
RegisterDevice = require(rootPrefix + '/tools/seige/userFlow/RegisterDevice'),
SiegeUsersModel = require(rootPrefix + '/app/models/mysql/SiegeUser');
class SiegeInitialization {
/**
*
* @param params
*/
constructor() {
const oThis = this;
oThis.userIdsArray = [];
}
/**
* params array of hashes
* [{ apikey: 'ghjhghj',
* apiSecret: 'hjghjhjh',
* apiEndPoint: 'www.s6-stagingost.com:7000'
* },
* { apikey: 'ghjhghj',
* apiSecret: 'hjghjhjh',
* apiEndPoint: 'www.s6-stagingost.com:7000'
* }
* ]
* @param params
*/
perform(params) {
const oThis = this;
return oThis._asyncPerform(params).catch(function(err) {
console.log(' In catch block', err);
return Promise.resolve(err);
});
}
async _asyncPerform(params) {
const oThis = this;
for (let index = 0; index < params.length; index++) {
let apiKey = params[index].apiKey,
apiSecret = params[index].apiSecret,
apiEndPoint = params[index].apiEndPoint;
oThis.ostObj = new OSTSDK({
apiKey: apiKey,
apiSecret: apiSecret,
apiEndpoint: apiEndPoint
});
}
console.log('-------------------->Fetching token data');
await oThis._getTokenData();
//create users
console.log('-------------------->Creating users');
await oThis._createUsers();
//Generates required addresses and private key
console.log('-------------------->Generating required addresses');
await oThis._generateRequiredAddress();
//Start Siege
console.log('-------------------->Starting siege');
await oThis._startSiege();
}
async _getTokenData() {
let oThis = this,
getTokenDetailsObj = new GetTokenDetails({ ostObj: oThis.ostObj }),
tokenDetails = await getTokenDetailsObj.perform();
oThis.tokenId = tokenDetails.token.id;
}
/**
*
* @returns {Promise<void>}
* @private
*/
async _createUsers() {
let oThis = this,
createUsersObj = new CreateUsers({ ostObj: oThis.ostObj, numberOfUsers: NUMBER_OF_USERS });
oThis.userIdsArray = await createUsersObj.perform();
}
/**
*
* @returns {Promise<void>}
* @private
*/
async _generateRequiredAddress() {
let oThis = this,
generateRequiredAddressObj = new GenerateRequiredAddresses({ userIdsArray: oThis.userIdsArray });
oThis.requiredAddressesHash = await generateRequiredAddressObj.perform();
}
/**
*
* @returns {Promise<void>}
* @private
*/
async _clearOldEntries() {
let oThis = this,
siegeUsersObj = new SiegeUsersModel(),
deleteRsp = await siegeUsersObj
.delete()
.where(['id > 0 AND token_id = ?', oThis.tokenId])
.fire();
return Promise.resolve();
}
/**
*
* @returns {Promise<void>}
* @private
*/
async _insertAddressesInDB() {
let oThis = this,
siegeUsersObj = new SiegeUsersModel(),
insertColumns = ['token_id', 'user_uuid', 'device_address', 'device_pk', 'session_address', 'session_pk'],
insertMultipleValues = [];
for (let i = 0; i < oThis.userIdsArray.length; i++) {
let userUuid = oThis.userIdsArray[i],
addressesDetails = oThis.requiredAddressesHash[userUuid],
insertValueArray = [];
insertValueArray[0] = oThis.tokenId;
insertValueArray[1] = userUuid;
insertValueArray[2] = addressesDetails.deviceAddress;
insertValueArray[3] = addressesDetails.deviceAddressPrivateKey;
insertValueArray[4] = addressesDetails.sessionAddress;
insertValueArray[5] = addressesDetails.sessionPrivateKey;
insertMultipleValues.push(insertValueArray);
}
await siegeUsersObj.insertMultiple(insertColumns, insertMultipleValues).fire();
}
/**
*
* @returns {Promise<void>}
* @private
*/
async _startSiege() {
let oThis = this,
promiseArray = [];
for (let index = 0; index < oThis.userIdsArray.length; index++) {
let userUuid = oThis.userIdsArray[index],
addressesDetails = oThis.requiredAddressesHash[userUuid];
promiseArray.push(oThis._performOperationsForSingleUser(userUuid, addressesDetails));
if (promiseArray.length >= BATCH_SIZE || oThis.userIdsArray.length === index + 1) {
await Promise.all(promiseArray);
promiseArray = [];
}
}
}
/**
*
* @param {string} userUuid
* @param {hash} addressesData
* @returns {Promise<void>}
* @private
*/
async _performOperationsForSingleUser(userUuid, addressesData) {
let oThis = this,
deviceAddress = addressesData.deviceAddress,
deviceAddressPrivateKey = addressesData.deviceAddressPrivateKey,
sessionAddress = addressesData.sessionAddress;
//Register Device
console.log('****Registering Device****');
await oThis._registerDevice(userUuid, deviceAddress);
//Activate User
let paramsForActivateUser = {
tokenId: oThis.tokenId,
userUuid: userUuid,
deviceAddress: deviceAddress,
apiSignerPrivateKey: deviceAddressPrivateKey,
sessionAddress: sessionAddress
};
console.log('****Activating User****');
await oThis._activateUser(paramsForActivateUser);
//Start Polling to fetch user status
console.log('****Initiated Poll****');
let userData = await oThis._pollForUserStatus(userUuid, 'ACTIVATED'),
tokenHolderAddress = userData.user.token_holder_address;
console.log('****User activated*****');
return Promise.resolve();
}
/**
*
* @param uuid
* @param deviceAddress
* @returns {Promise<void>}
* @private
*/
async _registerDevice(uuid, deviceAddress) {
let oThis = this,
registerDeviceObj = new RegisterDevice({
userUuid: uuid,
deviceAddress: deviceAddress,
ostObj: oThis.ostObj
}),
deviceData = await registerDeviceObj.perform();
}
/**
*
* @param params
* @returns {Promise<void>}
* @private
*/
async _activateUser(params) {
let oThis = this,
activateUserObj = new ActivateUser({
tokenId: params.tokenId,
userUuid: params.userUuid,
deviceAddress: params.deviceAddress,
apiSignerPrivateKey: params.apiSignerPrivateKey,
sessionAddress: params.sessionAddress,
expirationHeight: '1000000000000',
spendingLimit: '100000000000000000000',
apiEndPoint: API_END_POINT
}),
activateUserRsp = await activateUserObj.perform();
}
/**
*
* @param userUuid
* @param status
* @returns {Promise<*>}
* @private
*/
async _pollForUserStatus(userUuid, status) {
let oThis = this;
return new Promise(async function(onResolve, onReject) {
const _getUserData = async function(userUuid) {
let getUserDataObj = new GetUser({ ostObj: oThis.ostObj, userUuid: userUuid }),
userData = await getUserDataObj.perform();
console.log('.');
if (userData.user.status == status) {
return onResolve(userData);
} else {
setTimeout(async function() {
_getUserData(userUuid);
}, POLLING_INTERVAL);
}
};
_getUserData(userUuid);
});
}
}
new SiegeInitialization().perform(CREDENTIALS_ARRAY).then(function() {
setTimeout(function() {
console.log('********** DONE ************');
process.exit(0);
}, 5000);
});