forked from klaytn/caver-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintTest.js
597 lines (511 loc) · 20.8 KB
/
intTest.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
Copyright 2018 The caver-js Authors
This file is part of the caver-js library.
The caver-js library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The caver-js library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the caver-js. If not, see <http://www.gnu.org/licenses/>.
*/
// Instruction
// $ git clone [email protected]:klaytn/klaytn-integration-tests.git test/klaytn-integration-tests
// $ cp test/klaytn-integration-tests/env.template.json test/klaytn-integration-tests/env.json
// $ mocha test/intTest.js
//
// To execute a specific test,
// $ mocha --grep INT-LEGACY/012 test/intTest.js
const _ = require('lodash')
const RLP = require('eth-lib/lib/rlp')
const Bytes = require('eth-lib/lib/bytes')
const elliptic = require('elliptic')
const secp256k1 = new elliptic.ec('secp256k1')
const path = require('path')
const fs = require('fs')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const Caver = require('../index.js')
const testEnv = require('./klaytn-integration-tests/env.json')
const conf = require('./klaytn-integration-tests/conf.json')
const { expect, assert } = require('./extendedChai')
const { overwriteSignature, getSenderTxHash } = require('../packages/caver-klay/caver-klay-accounts/src/makeRawTransaction')
const deployedContractAddr = {}
let caver
function makePublicKey(x, y) {
const keyPair = secp256k1.keyPair({
pub: {
x: x.replace('0x', ''),
y: y.replace('0x', ''),
},
pubEnc: 'hex',
})
return `0x${keyPair.getPublic(false, 'hex').slice(2)}`
}
function isJsonable(v) {
try {
return JSON.stringify(v) === JSON.stringify(JSON.parse(JSON.stringify(v)))
} catch (e) {
/* console.error("not a dict",e); */
return false
}
}
function isDict(v) {
return !!v && typeof v === 'object' && v !== null && !(v instanceof Array) && !(v instanceof Date) && isJsonable(v)
}
function replaceWithEnv(data) {
function replaceElemWithEnv(elem) {
if (deployedContractAddr[elem] !== undefined) return deployedContractAddr[elem].address.toLowerCase()
if (elem === undefined) return elem
if (elem === 'env.sender') return testEnv.sender.address
if (typeof elem !== 'string') return elem
if (elem === 'blockHash') {
if (testEnv.blockHash === '' && testEnv.receipt !== {}) {
return testEnv.receipt.blockHash
}
return testEnv.blockHash
}
if (elem === 'filterId') return testEnv.filterId
if (elem === 'senderTxHash') return testEnv.receipt.senderTxHash
if (elem === 'transactionIndex') return caver.utils.toHex(testEnv.receipt.transactionIndex)
if (elem === 'blockNumber') return caver.utils.toHex(testEnv.receipt.blockNumber)
if (elem === 'transactionHash') return testEnv.receipt.transactionHash
if (elem === 'rawTransaction') return testEnv.rawTransaction
const getRandomAccount = id => {
if (testEnv.random === undefined) testEnv.random = {}
if (testEnv.random[id] === undefined) {
const keyring = caver.wallet.keyring.generate()
caver.wallet.add(keyring)
testEnv.random[id] = keyring
}
return testEnv.random[id]
}
if (elem.endsWith('.privateKey')) {
return getRandomAccount(elem.replace('.privateKey', '')).key.privateKey
}
if (elem.startsWith('random')) {
return getRandomAccount(elem).address.toLowerCase()
}
if (elem.startsWith('contract')) {
return testEnv.contracts[elem]
}
if (elem.startsWith('env.accounts')) {
const k = elem.replace('env.accounts.', '')
return testEnv.accounts[k].address.toLowerCase()
}
return elem
}
if (isDict(data)) {
Object.keys(data).forEach(k => {
data[k] = replaceWithEnv(data[k])
})
return data
}
if (Array.isArray(data)) {
data.forEach(function(p, idx) {
data[idx] = replaceElemWithEnv(replaceWithEnv(p))
})
return data
}
return replaceElemWithEnv(data)
}
function resetRandomAccounts() {
testEnv.random = {}
testEnv.contracts = {}
testEnv.blockHash = ''
testEnv.receipt = {}
testEnv.rawTransaction = ''
testEnv.filterId = ''
}
function makeMultisigKey(multisig) {
const ret = {
threshold: multisig.threshold,
keys: [],
}
if (Array.isArray(multisig.keys)) {
multisig.keys.forEach(function(k) {
ret.keys.push({
weight: k.weight,
publicKey: makePublicKey(k.key.x, k.key.y),
})
})
}
return ret
}
function makeKey(key) {
let ret = {}
switch (key.keyType) {
case 0:
// Nil key. DO NOTHING.
break
case 1:
ret.legacyKey = true
break
case 2:
ret.publicKey = makePublicKey(key.key.x, key.key.y)
break
case 3:
ret.failKey = true
break
case 4:
ret = { multisig: makeMultisigKey(key.key) }
break
case 5:
if (key.key.length > 0) {
ret = Object.assign(ret, { roleTransactionKey: makeKey(key.key[0]) })
}
if (key.key.length > 1) {
ret = Object.assign(ret, { roleAccountUpdateKey: makeKey(key.key[1]) })
}
if (key.key.length > 2) {
ret = Object.assign(ret, { roleFeePayerKey: makeKey(key.key[2]) })
}
break
default:
throw new Error(`not implemented keytype ${key.keyType}`)
}
return ret
}
function processAccountKey(tx) {
if (tx.accountKey === undefined) return tx
const k = makeKey(tx.accountKey)
tx = Object.assign(tx, k)
return tx
}
async function processCall(t) {
const contractAddr = deployedContractAddr[t.call.to].address
const abi = deployedContractAddr[t.call.to].abi
const contract = new caver.contract(abi, contractAddr)
const from = replaceWithEnv(t.call.from)
const params = replaceWithEnv(t.call.params)
let value = await contract.methods[t.call.method](...params).call({ from })
if (_.isString(value)) value = value.toLowerCase()
expect(value).to.equal(replaceWithEnv(t.expected.returns))
}
async function processSend(t) {
const contractAddr = deployedContractAddr[t.send.to].address
const abi = deployedContractAddr[t.send.to].abi
const contract = new caver.contract(abi, contractAddr)
const from = replaceWithEnv(t.send.from)
let params = replaceWithEnv(t.send.params)
let actual = true
let receipt
try {
receipt = await contract.methods[t.send.method](...params).send({ from, gas: t.send.gas })
} catch (err) {
// console.log(err)
const receiptRaw = String(err).slice(String(err).indexOf('\n'))
try {
receipt = JSON.parse(receiptRaw)
} catch (error) {
actual = false
receipt = undefined
}
}
expect(actual).to.equal(t.expected.status)
// console.log(receipt)
if (t.expected.receipt) {
expect(receipt).to.not.undefined
if (t.expected.receipt.status) expect(receipt.status).to.equal(t.expected.receipt.status ? '0x1' : '0x0')
if (t.expected.receipt.txError) expect(receipt.txError).to.equal(t.expected.receipt.txError)
}
if (t.expected.rawEvents) {
Object.keys(t.expected.rawEvents).forEach(idx => {
expect(receipt.events[idx].raw.topics).to.deep.equal(t.expected.rawEvents[idx].topics)
if (t.expected.rawEvents[idx].data.encodeParameters) {
params = replaceWithEnv(t.expected.rawEvents[idx].data.encodeParameters)
const data = caver.abi.encodeParameters(...params)
expect(receipt.events[idx].raw.data).to.equal(data)
}
})
}
if (t.expected.events) {
Object.keys(t.expected.events).forEach(e => {
expect(receipt.events[e]).to.not.undefined
if (t.expected.events[e]) {
Object.keys(t.expected.events[e]).forEach(k => {
let expected = t.expected.events[e][k]
expected = replaceWithEnv(expected)
if (typeof receipt.events[e].returnValues[k] === 'string') {
expect(String(receipt.events[e].returnValues[k]).toLowerCase()).to.equal(expected.toLowerCase())
} else {
expect(receipt.events[e].returnValues[k]).to.equal(expected)
}
})
}
})
}
}
const getSignedRawTransaction = async t => {
if (t.rawTx !== undefined) {
return t.rawTx
}
let feePayer = t.tx.feePayer
delete t.tx.feePayer
t.tx.from = replaceWithEnv(t.tx.from)
if (t.tx.from === undefined) {
t.tx.from = testEnv.sender.address
}
t.tx.to = replaceWithEnv(t.tx.to)
t.tx.privateKey = replaceWithEnv(t.tx.privateKey)
let privateKey = testEnv.sender.privateKey
if (t.tx.privateKey !== undefined) {
privateKey = t.tx.privateKey
}
t.tx = processAccountKey(t.tx)
const signedRawTx = await caver.klay.accounts.signTransaction(t.tx, privateKey)
let rawTransaction = signedRawTx.rawTransaction
if (t.tx.v !== undefined || t.tx.r !== undefined || t.tx.s !== undefined) {
const txObj = caver.klay.decodeTransaction(rawTransaction, t.tx.type)
if (t.tx.v !== undefined) {
txObj.v = t.tx.v
}
if (t.tx.r !== undefined) {
txObj.r = t.tx.r
}
if (t.tx.s !== undefined) {
txObj.s = t.tx.s
}
rawTransaction = overwriteSignature(rawTransaction, txObj, [txObj.v, txObj.r, txObj.s])
}
// if transaction is fee delegated, sign with feePayer
if (t.tx.type.includes('FEE_DELEGATED_')) {
feePayer = replaceWithEnv(feePayer)
if (feePayer === undefined) {
feePayer = testEnv.feePayer.address
}
t.tx.feePayerPrivateKey = replaceWithEnv(t.tx.feePayerPrivateKey)
let feePayerPrivateKey = testEnv.feePayer.privateKey
if (t.tx.feePayerPrivateKey !== undefined) {
feePayerPrivateKey = t.tx.feePayerPrivateKey
}
const feePayerTx = {
feePayer,
senderRawTransaction: rawTransaction,
}
const signedFeePayerRawTx = await caver.klay.accounts.signTransaction(feePayerTx, feePayerPrivateKey)
rawTransaction = signedFeePayerRawTx.rawTransaction
// If v or r or s value is set in test case, overwrite with that.
if (t.tx.payerV !== undefined || t.tx.payerR !== undefined || t.tx.payerS !== undefined) {
const txObj = caver.klay.decodeTransaction(rawTransaction, t.tx.type)
if (t.tx.payerV !== undefined) {
txObj.payerV = t.tx.payerV
}
if (t.tx.payerR !== undefined) {
txObj.payerR = t.tx.payerR
}
if (t.tx.payerS !== undefined) {
txObj.payerS = t.tx.payerS
}
rawTransaction = overwriteSignature(rawTransaction, txObj, undefined, [txObj.payerV, txObj.payerR, txObj.payerS])
}
}
return rawTransaction
}
async function processTransaction(t) {
const rawTransaction = await getSignedRawTransaction(t)
let actual
let receipt
await caver.rpc.klay.sendRawTransaction(rawTransaction).then(
r => {
receipt = r
// console.log(r)
if (t.expected.receipt) {
if (t.expected.receipt.checkContractAddress) {
const addrHash = caver.utils.keccak256(RLP.encode([t.tx.from.toLowerCase(), Bytes.fromNat(t.tx.nonce)]))
const address = caver.utils.toChecksumAddress(`0x${addrHash.slice(-40)}`)
expect(r.contractAddress.toLowerCase()).to.equal(address.toLowerCase())
}
if (t.expected.receipt.codeFormat) expect(r.codeFormat).to.equal(t.expected.receipt.codeFormat)
if (t.expected.receipt.checkSenderTxHash) {
expect(r.senderTxHash).to.equal(getSenderTxHash(rawTransaction))
}
}
actual = true
},
err => {
// console.log(err)
const receiptRaw = String(err).slice(String(err).indexOf('\n'))
actual = true
try {
receipt = JSON.parse(receiptRaw)
} catch (error) {
receipt = undefined
actual = false
}
if (t.expected.errorString !== undefined) {
expect(String(err)).to.include(t.expected.errorString)
}
}
)
if (receipt !== undefined) {
if (t.deployedAddress && receipt.contractAddress) {
testEnv.contracts[t.deployedAddress] = receipt.contractAddress
}
if (t.expected.receipt && t.expected.receipt.status) {
expect(receipt.status).to.equal(t.expected.receipt.status ? '0x1' : '0x0')
}
if (t.expected.receipt && t.expected.receipt.txError) {
expect(receipt.txError).to.equal(t.expected.receipt.txError)
}
}
expect(actual).to.equal(t.expected.status)
}
async function processApi(t) {
if (t.api.pre !== undefined) {
const rawTransaction = await getSignedRawTransaction(t.api.pre)
testEnv.receipt = await caver.rpc.klay.sendRawTransaction(rawTransaction)
}
if (t.api.preSigned !== undefined) {
testEnv.rawTransaction = await getSignedRawTransaction(t.api.preSigned)
}
await new Promise((resolve, reject) => {
caver.rpc.klay._requestManager.send(
{
method: t.api.method,
params: replaceWithEnv(t.api.params),
},
(err, result) => {
if (err) reject(err)
else resolve(result)
}
)
}).then(
result => {
expect(result).not.to.null
if (
t.api.method === 'klay_newFilter' ||
t.api.method === 'klay_newBlockFilter' ||
t.api.method === 'klay_newPendingTransactionFilter'
) {
testEnv.filterId = result
}
if (t.api.method === 'klay_getBlockByNumber') testEnv.blockHash = result.hash
if (t.expected) {
if (t.expected.accType === 2) {
expect(result.accType).to.be.deep.equal(t.expected.accType)
} else {
expect(result).to.be.deep.equal(t.expected)
}
}
},
err => {
if (t.errorString) {
expect(String(err)).to.include(t.errorString)
} else {
expect(err).to.be.null
}
}
)
}
before(() => {
caver = new Caver(testEnv.URL)
// caver.klay.accounts.wallet.add(testEnv.sender.privateKey)
const keyring = caver.wallet.keyring.createFromPrivateKey(testEnv.sender.privateKey)
caver.wallet.add(keyring)
if (testEnv.accounts) {
Object.keys(testEnv.accounts).forEach(acc => {
// caver.klay.accounts.wallet.add(testEnv.accounts[acc].privateKey)
const testKeyring = caver.wallet.keyring.createFromPrivateKey(testEnv.accounts[acc].privateKey)
caver.wallet.add(testKeyring)
})
}
})
async function checkSolidityVersion() {
const { stdout } = await exec('solc --version')
const regex = /Version: ([0-9]+.[0-9]+.[0-9]+)/
const found = stdout.match(regex)
expect(found).to.not.null
const version = found[1]
expect(version).to.equal(conf.solidity)
}
describe('Integration tests', () => {
const directoryPath = path.join(__dirname, 'klaytn-integration-tests')
const intFiles = fs.readdirSync(directoryPath, { withFileTypes: true })
intFiles.forEach(function(intf) {
if (intf.isDirectory() === false) return
describe(`testing ${intf.name}`, () => {
before(function(done) {
// Check version of solidity compiler before INT-SOL integration test
if (intf.name === 'INT-SOL') {
checkSolidityVersion().then(done)
} else {
done()
}
})
const dir = path.join(__dirname, 'klaytn-integration-tests/', intf.name)
const files = fs.readdirSync(dir)
files.forEach(function(file) {
if (file.endsWith('.json') === false) return
const filename = path.join(dir, file)
describe(`fileName: ${filename}`, () => {
const tc = JSON.parse(fs.readFileSync(filename))
if (tc.skipJs) {
console.log(`Skip ${tc.tcID} / ${tc.tcName}`)
return
}
it('Resetting random accounts', () => {
resetRandomAccounts()
})
it('make abi and bin', async () => {
for (const k in tc.deploy) {
const { stdout } = await exec(`solc --abi --bin --allow-paths . ${path.join(dir, tc.deploy[k].file)}`)
const regex = `\n======= .*${tc.deploy[k].file}:${k} =======\nBinary: \n(.*)\nContract JSON ABI \n(.*)`
const found = stdout.match(regex)
expect(found).to.not.null
const bin = found[1]
const abi = JSON.parse(found[2])
const fromAddress = testEnv.sender.address
const gas = 100000000
const params = replaceWithEnv(tc.deploy[k].constructorParams)
const contractInstance = new caver.contract(abi)
const newContractInstance = await contractInstance
.deploy({
data: `0x${bin}`,
arguments: params,
})
.send({
from: fromAddress,
gas,
value: 0,
})
expect(newContractInstance).to.not.null
deployedContractAddr[k] = { address: newContractInstance.options.address, abi }
}
}).timeout(10000)
tc.test.forEach(function(t, idx) {
it(`testName:${tc.tcName}[${idx}]`, async () => {
try {
if (t.api !== undefined) {
await processApi(t)
} else if (t.tx !== undefined || t.rawTx !== undefined) {
await processTransaction(t)
} else if (t.call !== undefined) {
await processCall(t)
} else if (t.send !== undefined) {
await processSend(t)
}
} catch (err) {
if (String(err).includes('AssertionError: expected')) throw err
if (t.expected === undefined || t.expected.status === undefined || t.expected.status !== false) {
console.log(err)
}
expect(t.expected.status).to.be.false
if (t.expected) {
if (t.expected.errorStringJs) expect(String(err)).to.include(t.expected.errorStringJs)
else if (t.expected.errorString) expect(String(err)).to.include(t.expected.errorString)
} else {
console.log('catched err', err)
assert(false)
}
}
}).timeout(50000)
})
})
})
})
})
})