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 677
/
requests.js
1296 lines (1081 loc) · 43.4 KB
/
requests.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var Web3 = require('web3');
var Web3WsProvider = require('web3-providers-ws');
var Transaction = require('ethereumjs-tx');
var utils = require('ethereumjs-util');
var assert = require('assert');
var Ganache = require("../index.js");
var solc = require("solc");
var fs = require("fs");
var to = require("../lib/utils/to");
var _ = require("lodash");
var source = fs.readFileSync("./test/Example.sol", {encoding: "utf8"});
var result = solc.compile(source, 1);
var secretKeys = [
'0xda09f8cdec20b7c8334ce05b27e6797bef01c1ad79c59381666467552c5012e3',
'0x0d14f32c8e3ed7417fb7db52ebab63572bf7cfcd557351d4ccf19a05edeecfa5',
'0x0d80aca78bfaf3ab47865a53e5977e285c41c028a15313f917fe78abe5a50ef7',
'0x00af8067d4c69abca7234194f154d7f31e13c0e53dae9260432f1bcc6d1d13fb',
'0x8939a6a37b48c47f9bc683c371dd96e819d65f6138f3b376a622ecb40379bd22',
'0x4a3665bf95efd38cb9820ce129a26fee03927f17930924c98908c8885ca53606',
'0x111bd4b380f2eeb0d00b025d574908d59c1bfa0030d7a69f69445c171d8cfa27',
'0x6aff34e843c3a99fe21dcc014e3b5bf6a160a4bb8c4c470ea79acd33d9bea41f',
'0x12ae0eb585babc60c88a74190a6074488a0d2f296124ce37f85dbec1d693906f',
'0xd46dc75904628a0b0eaffdda6acbe2687924299995708e30d05a1e8a2a1c5d45'
];
// Thanks solc. At least this works!
// This removes solc's overzealous uncaughtException event handler.
process.removeAllListeners("uncaughtException");
// Note: Certain properties of the following contract data are hardcoded to
// maintain repeatable tests. If you significantly change the solidity code,
// make sure to update the resulting contract data with the correct values.
var contract = {
solidity: source,
abi: result.contracts[":Example"].interface,
binary: "0x" + result.contracts[":Example"].bytecode,
runtimeBinary: '0x' + result.contracts[":Example"].runtimeBytecode,
position_of_value: "0x0000000000000000000000000000000000000000000000000000000000000000",
expected_default_value: 5,
call_data: {
gasPrice: '0x01', // This is important, as passing it has exposed errors in the past.
to: null, // set by test
data: '0x3fa4f245'
},
transaction_data: {
from: null, // set by test
to: null, // set by test
data: '0x552410770000000000000000000000000000000000000000000000000000000000000019', // sets value to 25 (base 10)
gas: 3141592
}
};
var tests = function(web3) {
var accounts;
var personalAccount;
before('create and fund personal account', function() {
return web3.eth.getAccounts()
.then(function(accs) {
accounts = accs.map(function(val) {
return val.toLowerCase();
});
return web3.eth.personal.newAccount("password")
}).then(function(acct) {
personalAccount = acct
})
});
describe("eth_accounts", function() {
it("should return 10 addresses", function(done) {
assert.deepEqual(accounts.length, 10);
done();
});
});
describe("eth_blockNumber", function() {
it("should return initial block number of zero", function(done) {
var number = web3.eth.getBlockNumber(function(err, result) {
if (err) return done(err);
assert.deepEqual(result, 0);
done();
});
// Note: We'll assert the block number changes on transactions.
});
});
describe("eth_coinbase", function() {
it("should return correct address", function(done) {
web3.eth.getCoinbase(function(err, coinbase) {
if (err) return done(err);
assert.equal(coinbase, accounts[0]);
done();
});
});
});
describe("eth_mining", function() {
it("should return true", function(done) {
web3.eth.isMining(function(err, result) {
if (err) return done(err);
assert.deepEqual(result, true);
done();
});
});
});
describe("eth_hashrate", function() {
it("should return hashrate of zero", function(done) {
web3.eth.getHashrate(function(err, result) {
if (err) return done(err);
assert.deepEqual(result, 0);
done();
});
});
});
describe("eth_gasPrice", function() {
it("should return gas price of 2 gwei", function(done) {
web3.eth.getGasPrice(function(err, result) {
if (err) return done(err);
assert.equal(to.hexWithZeroPadding(result), to.hexWithZeroPadding(2000000000));
done();
});
});
});
describe("eth_getBalance", function() {
it("should return initial balance", function(done) {
web3.eth.getBalance(accounts[0], function(err, result) {
if (err) return done(err);
assert.deepEqual(result, "100000000000000000000");
done();
});
});
it("should return 0 for non-existent account", function(done) {
web3.eth.getBalance("0x1234567890123456789012345678901234567890", function(err, result) {
if (err) return done(err);
assert.equal("0x" + result.toString(16), "0x0");
done();
});
});
});
describe("eth_getBlockByNumber", function() {
it("should return block given the block number", function(done) {
web3.eth.getBlock(0, true, function(err, block) {
if (err) return done(err);
var expectedFirstBlock = {
number: 0,
hash: block.hash, // Don't test this one
mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
nonce: '0x0000000000000000',
sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
stateRoot: '0x7caba99698b405652a6bcb1038efa16db54b3338af71fa832a0b99a3e6c344bc',
receiptsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
miner: '0x0000000000000000000000000000000000000000',
difficulty: "0",
totalDifficulty: "0",
extraData: '0x',
size: 1000,
gasLimit: 6721975,
gasUsed: 0,
timestamp: block.timestamp, // Don't test this one.
transactions: [],
uncles: []
};
assert.deepEqual(block, expectedFirstBlock);
var now = (new Date()).getTime();
var then = block.timestamp * 1000; // block.timestamp is in seconds.
assert.equal(then.toString().length, now.toString().length, "Invalid timestamp length");
assert(then < now, "Time returned was greater than the current time");
done();
});
});
it("should return null given a future block number", function(done) {
web3.eth.getBlock(10000, true, function(err, block) {
if (err) return done(err);
assert.deepEqual(block, null);
done();
});
});
it("should return transactions in the block as well", function(done) {
web3.eth.sendTransaction({
from: accounts[0],
data: contract.binary,
gas: 3141592
}, function(err, tx_hash) {
if (err) return done(err);
// Assume it was processed correctly.
assert.deepEqual(tx_hash.length, 66);
web3.eth.getBlock("latest", true, function(err, block) {
if (err) return done(err);
assert.equal(block.transactions.length, 1, "Latest block should have one transaction");
assert.equal(block.transactions[0].hash, tx_hash, "Transaction hashes don't match");
//Retest, with transaction only as hash
web3.eth.getBlock("latest", false, function(err, block) {
if (err) return done(err);
assert.equal(block.transactions.length, 1, "Latest block should have one transaction");
assert.equal(block.transactions[0], tx_hash, "Transaction hashes don't match");
done()
});
});
});
});
});
// Relies on the validity of eth_getBlockByNumber above.
describe("eth_getBlockByHash", function() {
it("should return block given the block hash", function(done) {
web3.eth.getBlock(0, true, function(err, blockByNumber) {
if (err) return done(err);
web3.eth.getBlock(blockByNumber.hash, true, function(err, blockByHash) {
if (err) return done(err);
assert.deepEqual(blockByHash, blockByNumber);
done();
});
});
});
});
describe("eth_getBlockTransactionCountByNumber", function(){
it("should return the number of transactions given the block number (0 transactions)", function(done) {
//Block 0 should have 0 transactions as per test eth_getBlockByNumber
web3.eth.getBlock(0, true, function(err, block) {
if (err) return done(err);
web3.eth.getBlockTransactionCount(0, function(err, blockTransactionCount) {
assert.equal(block.transactions.length, blockTransactionCount, "Block transaction count should be 0.");
assert.equal(0, blockTransactionCount, "Block transaction count should be 0.");
done();
});
});
});
it("should return the number of transactions given the block number (1 transaction)", function(done) {
// Create a transaction and check
// Account 0 seems to be running out of gas before all tests are complete
var payingAccount = 2;
web3.eth.sendTransaction({
from: accounts[payingAccount],
data: contract.binary,
gas: 3141592
}, function(err, tx_hash) {
if (err) return done(err);
// Assume it was processed correctly.
assert.deepEqual(tx_hash.length, 66);
web3.eth.getBlock("latest", true, function(err, block) {
if (err) return done(err);
web3.eth.getBlockTransactionCount(block.number , function(err, blockTransactionCount) {
if (err) return done(err);
assert.equal(block.transactions.length, blockTransactionCount, "Block transaction count should be 1.");
assert.equal(1, blockTransactionCount, "Block transaction count should be 1.");
done();
});
});
});
});
it("should return 0 transactions when the block doesn't exist", function(done) {
web3.eth.getBlockTransactionCount(1000000, function(err, blockTransactionCount) {
if (err) return done(err);
assert.equal(0, blockTransactionCount, "Block transaction count should be 0.");
done();
});
});
});
// Dependent upon validity of eth_getBlockTransactionCountByNumber
describe("eth_getBlockTransactionCountByHash", function(){
it("should return the number of transactions given the hash", function(done) {
web3.eth.getBlock(0, true, function(err, blockByNumber) {
if (err) return done(err);
web3.eth.getBlockTransactionCount(blockByNumber.number, true, function(err, txCountByHash) {
if (err) return done(err);
web3.eth.getBlockTransactionCount(blockByNumber.hash , function(err, txCountByNumber) {
if (err) return done(err);
assert.equal(txCountByHash, txCountByNumber, "Txn count for block retrieved by hash should equal count retrieved by number.");
done();
});
});
});
});
});
describe("eth_sign", function() {
var accounts;
var signingWeb3;
// This account produces an edge case signature when it signs the hex-encoded buffer:
// '0x07091653daf94aafce9acf09e22dbde1ddf77f740f9844ac1f0ab790334f0627'. (See Issue #190)
var acc = {
balance: "0x00",
secretKey: "0xe6d66f02cd45a13982b99a5abf3deab1f67cf7be9fee62f0a072cb70896342e4"
};
// Load account.
before(function( done ){
signingWeb3 = new Web3();
signingWeb3.setProvider(Ganache.provider({
accounts: [ acc ]
}));
signingWeb3.eth.getAccounts(function(err, accs) {
if (err) return done(err);
accounts = accs.map(function(val) {
return val.toLowerCase();
});
done();
});
});
it("should produce a signature whose signer can be recovered", function() {
var msg = utils.toBuffer("asparagus");
var msgHash = utils.hashPersonalMessage(msg);
return signingWeb3.eth.sign(utils.bufferToHex(msg), accounts[0]).then(sgn => {
sgn = utils.stripHexPrefix(sgn);
var r = Buffer.from(sgn.slice(0, 64), 'hex');
var s = Buffer.from(sgn.slice(64, 128), 'hex');
var v = parseInt(sgn.slice(128, 130), 16) + 27;
var pub = utils.ecrecover(msgHash, v, r, s);
var addr = utils.setLength(utils.fromSigned(utils.pubToAddress(pub)), 20);
addr = to.hex(addr);
assert.deepEqual(addr, accounts[0]);
});
});
it("should work if ecsign produces 'r' or 's' components that start with 0", function() {
// This message produces a zero prefixed 'r' component when signed by ecsign
// w/ the account set in this test's 'before' block.
var msgHex = '0x07091653daf94aafce9acf09e22dbde1ddf77f740f9844ac1f0ab790334f0627';
var edgeCaseMsg = utils.toBuffer(msgHex);
var msgHash = utils.hashPersonalMessage(edgeCaseMsg);
return signingWeb3.eth.sign(msgHex, accounts[0]).then(sgn => {
sgn = utils.stripHexPrefix(sgn);
var r = Buffer.from(sgn.slice(0, 64), 'hex');
var s = Buffer.from(sgn.slice(64, 128), 'hex');
var v = parseInt(sgn.slice(128, 130), 16) + 27;
var pub = utils.ecrecover(msgHash, v, r, s);
var addr = utils.setLength(utils.fromSigned(utils.pubToAddress(pub)), 20);
addr = to.hex(addr);
assert.deepEqual(addr, accounts[0]);
});
})
after("shutdown", function(done) {
let provider = signingWeb3._provider;
signingWeb3.setProvider()
provider.close(done)
});
});
describe('eth_sendRawTransaction', () => {
it("should fail with bad nonce (too low)", function(done) {
var provider = web3.currentProvider;
var transaction = new Transaction({
"value": "0x10000000",
"gasLimit": "0x33450",
"from": accounts[0],
"to": accounts[1],
"nonce": "0x00", // too low nonce
})
var secretKeyBuffer = Buffer.from(secretKeys[0].substr(2), 'hex')
transaction.sign(secretKeyBuffer)
web3.eth.sendSignedTransaction(transaction.serialize(), function(err, result) {
assert(err.message.indexOf("the tx doesn't have the correct nonce. account has nonce of: 1 tx has nonce of: 0") >= 0, `Incorrect error message: ${err.message}`);
done()
})
})
it("should fail with bad nonce (too high)", function(done) {
var provider = web3.currentProvider;
var transaction = new Transaction({
"value": "0x10000000",
"gasLimit": "0x33450",
"from": accounts[0],
"to": accounts[1],
"nonce": "0xff", // too low nonce
})
var secretKeyBuffer = Buffer.from(secretKeys[0].substr(2), 'hex')
transaction.sign(secretKeyBuffer)
web3.eth.sendSignedTransaction(transaction.serialize(), function(err, result) {
assert(err.message.indexOf("the tx doesn't have the correct nonce. account has nonce of: 1 tx has nonce of: 255") >= 0);
done()
})
})
it("should succeed with right nonce (1)", function(done) {
var provider = web3.currentProvider;
var transaction = new Transaction({
"value": "0x10000000",
"gasLimit": "0x33450",
"from": accounts[0],
"to": accounts[1],
"nonce": "0x01"
})
var secretKeyBuffer = Buffer.from(secretKeys[0].substr(2), 'hex')
transaction.sign(secretKeyBuffer)
web3.eth.sendSignedTransaction(transaction.serialize(), function(err, result) {
done(err)
})
})
it("should respond with correct txn hash", function(done) {
var provider = web3.currentProvider;
var transaction = new Transaction({
"value": "0x00",
"gasLimit": "0x5208",
"from": accounts[0],
"to": accounts[1],
"nonce": "0x02"
})
var secretKeyBuffer = Buffer.from(secretKeys[0].substr(2), 'hex')
transaction.sign(secretKeyBuffer)
web3.eth.sendSignedTransaction(transaction.serialize(), function(err, result) {
assert.equal(result, to.hex(transaction.hash()))
done(err)
})
})
})
describe("contract scenario", function() {
// These are expected to be run in order.
var initialTransaction;
var contractAddress;
var contractCreationBlockNumber;
it("should add a contract to the network (eth_sendTransaction)", function(done) {
web3.eth.sendTransaction({
from: accounts[0],
data: contract.binary,
gas: 3141592,
value: 1
}, function(err, hash) {
if (err) return done(err);
assert.deepEqual(hash.length, 66);
initialTransaction = hash
web3.eth.getTransactionReceipt(hash, function(err, receipt) {
if (err) return done(err);
contractCreationBlockNumber = receipt.blockNumber; // For defaultBlock test
assert(receipt)
done();
})
});
});
it("should verify the transaction immediately (eth_getTransactionReceipt)", function(done) {
web3.eth.getTransactionReceipt(initialTransaction, function(err, receipt) {
if (err) return done(err);
contractAddress = receipt.contractAddress;
assert.notEqual(receipt, null, "Transaction receipt shouldn't be null");
assert.notEqual(contractAddress, null, "Transaction did not create a contract");
done();
});
});
it("should return null if asked for a receipt for a nonexistent transaction (eth_getTransactionReceipt)", function(done) {
web3.eth.getTransactionReceipt("0xdeadbeef", function(err, receipt) {
if (err) return done(err);
assert.equal(receipt, null, "Transaction receipt should be null");
done();
});
});
it("should verify the code at the address matches the runtimeBinary (eth_getCode)", function(done) {
web3.eth.getCode(contractAddress, function(err, result) {
if (err) return done(err);
assert.equal(result, contract.runtimeBinary);
done();
});
});
it("should have balance of 1 (eth_getBalance)", function(done) {
web3.eth.getBalance(contractAddress, function(err, result) {
if (err) return done(err);
assert.equal(result, 1);
done();
});
});
it("should be able to read data via a call (eth_call)", function(done) {
var call_data = contract.call_data;
call_data.to = contractAddress;
call_data.from = accounts[0];
var starting_block_number = null;
// TODO: Removing this callback hell would be nice.
web3.eth.getBlockNumber(function(err, result) {
if (err) return done(err);
starting_block_number = result;
web3.eth.call(call_data, function(err, result) {
if (err) return done(err);
assert.equal(to.number(result), 5);
web3.eth.getBlockNumber(function(err, result) {
if (err) return done(err);
assert.equal(result, starting_block_number, "eth_call increased block count when it shouldn't have");
done();
});
});
});
});
it("should get back a runtime error on a bad call (eth_call)", function(done) {
var call_data = _.cloneDeep(contract.call_data);
call_data.to = contractAddress;
call_data.from = accounts[0];
// TODO: Removing this callback hell would be nice.
web3.eth.estimateGas(call_data, function (err, result) {
if (err) return done(err);
// set a low gas limit to force a runtime error
call_data.gas = result - 1;
web3.eth.call(call_data, function (err, result) {
// should have received an error
assert(err, "did not return runtime error");
assert(/.*out of gas.*/.test(err.message), `Did not receive an 'out of gas' error. got '${err.message}' instead.`)
done();
});
});
});
it("should be able to make a call from an address not in the accounts list (eth_call)", function(done) {
var from = "0x1234567890123456789012345678901234567890";
// Assert precondition: Ensure from address isn't in the accounts list.
accounts.forEach(function(account) {
assert.notEqual(from, account, "Test preconditions not met: from address must not be within the accounts list, please rerun");
});
var call_data = contract.call_data;
call_data.to = contractAddress;
call_data.from = from;
web3.eth.call(call_data, function(err, result) {
if (err) return done(err);
assert.equal(to.number(result), 5);
done();
});
});
it("should be able to make a call when no address is listed (eth_call)", function(done) {
var call_data = contract.call_data;
call_data.to = contractAddress;
delete call_data.from;
web3.eth.call(call_data, function(err, result) {
if (err) return done(err);
assert.equal(to.number(result), 5);
done();
});
});
it("should represent the block number correctly in the Oracle contract (oracle.blockhash0)", function(done){
var oracleSol = fs.readFileSync("./test/Oracle.sol", {encoding: "utf8"});
var oracleOutput = solc.compile(oracleSol).contracts[":Oracle"]
web3.eth.personal.unlockAccount(accounts[0], "password", function(err, result) {
var contract = new web3.eth.Contract(JSON.parse(oracleOutput.interface));
contract.deploy({
data: oracleOutput.bytecode,
}).send({
from: accounts[0],
gas: 3141592
}).then(function(oracle) {
// TODO: ugly workaround - not sure why this is necessary.
if (!oracle._requestManager.provider) {
oracle._requestManager.setProvider(web3.eth._provider);
}
web3.eth.getBlock(0, true, function(err, block){
if (err) return done(err)
oracle.methods.blockhash0().call(function(err, blockhash){
if (err) return done(err)
assert.equal(blockhash, block.hash);
done()
});
});
});
});
});
it("should be able to estimate gas of a transaction (eth_estimateGas)", function(done){
var tx_data = contract.transaction_data;
tx_data.to = contractAddress;
tx_data.from = accounts[0];
var starting_block_number = null;
// TODO: Removing this callback hell would be nice.
web3.eth.getBlockNumber(function(err, result) {
if (err) return done(err);
starting_block_number = result;
web3.eth.estimateGas(tx_data, function(err, result) {
if (err) return done(err);
assert.equal(result, 27693);
web3.eth.getBlockNumber(function(err, result) {
if (err) return done(err);
assert.equal(result, starting_block_number, "eth_estimateGas increased block count when it shouldn't have");
done();
});
});
});
});
it("should be able to estimate gas from an account not within the accounts list (eth_estimateGas)", function(done){
var tx_data = contract.transaction_data;
tx_data.to = contractAddress;
tx_data.from = "0x1234567890123456789012345678901234567890";;
var starting_block_number = null;
web3.eth.estimateGas(tx_data, function(err, result) {
if (err) return done(err);
assert.equal(result, 27693);
done();
});
});
it("should be able to estimate gas when no account is listed (eth_estimateGas)", function(done){
var tx_data = contract.transaction_data;
tx_data.to = contractAddress;
delete tx_data.from;
var starting_block_number = null;
web3.eth.estimateGas(tx_data, function(err, result) {
if (err) return done(err);
assert.equal(result, 27693);
done();
});
});
it("should be able to send a state changing transaction (eth_sendTransaction)", function(done) {
var tx_data = contract.transaction_data;
tx_data.to = contractAddress;
tx_data.from = accounts[0];
var call_data = contract.call_data;
call_data.from = accounts[0];
call_data.to = contractAddress;
web3.eth.sendTransaction(tx_data, function(err, tx) {
if (err) return done(err);
// Now double check the data was set properly.
// NOTE: Because ethereumjs-testrpc processes transactions immediately,
// we can do this. Calling the call immediately after the transaction would
// fail on a different Ethereum client.
web3.eth.getTransactionReceipt(tx, function(err, receipt) {
if (err) return done(err);
assert.equal(receipt.logs.length, 1, "Receipt had wrong amount of logs");
assert.equal(receipt.logs[0].blockHash, receipt.blockHash, "Logs blockhash doesn't match block blockhash");
//console.log(call_data);
web3.eth.call(call_data, function(err, result) {
if (err) return done(err);
assert.equal(to.number(result), 25);
done();
});
});
});
});
// NB: relies on the previous test setting value to 25 and the contract deployment setting
// original value to 5. `contractCreationBlockNumber` is set in the first test of this
// describe block.
it("should read data via a call at a specified blockNumber (eth_call)", function(done){
var startingBlockNumber = null;
var call_data = contract.call_data;
web3.eth.getBlockNumber().then(function(result){
startingBlockNumber = result;
return web3.eth.call(call_data)
}).then(function(result){
assert.equal(to.number(result), 25, "value retrieved from latest block should be 25");
return web3.eth.call(call_data, contractCreationBlockNumber)
}).then(function(result){
assert.equal(to.number(result), 5, "value retrieved from contract creation block should be 5");
return web3.eth.getBlockNumber()
}).then(function(result){
assert.equal(result, startingBlockNumber, "eth_call w/defaultBlock increased block count");
return web3.eth.call(call_data);
}).then(function(result){
assert.equal(to.number(result), 25, "stateTrie root was corrupted by defaultBlock call");
done();
});
});
it('should read data via a call when specified blockNumber is "earliest" (eth_call)', function(done) {
var call_data = contract.call_data;
web3.eth.call(call_data, "earliest").then(function(result){
assert.equal(to.number(result), 0, "value retrieved from earliest block should be zero");
done();
})
});
it('should read data via a call when specified blockNumber is "pending" (eth_call)', function(done){
var call_data = contract.call_data;
web3.eth.call(call_data, "pending").then(function(result){
assert.equal(to.number(result), 25, "value retrieved from pending block should be 25");
done();
});
});
it("should error when reading data via a call at a non-existent blockNumber (eth_call)", function(done){
var nonExistentBlock;
var call_data = contract.call_data;
web3.eth.getBlockNumber().then(function(result){
nonExistentBlock = result + 1;
return web3.eth.call(call_data, nonExistentBlock);
}).then(function(result){
assert.fail();
}).catch(function(error){
assert(error.message.includes('index out of range'));
assert(error.message.includes(nonExistentBlock));
done();
});
});
it("should only be able to send an unsigned state changing transaction from an address within the accounts list (eth_sendTransaction)", function(done) {
var badAddress = "0x1234567890123456789012345678901234567890";
var tx_data = {};
tx_data.to = "0x1111111111000000000011111111110000000000";
tx_data.from = badAddress;
tx_data.value = "0x1";
web3.eth.sendTransaction(tx_data, function(err, result) {
if (err) {
assert(/sender account not recognized/.test(err.message), `Expected error message containing 'sender account not recognized', but got ${err.message}`)
done();
} else {
assert.fail("Should have received an error")
}
});
});
it("should get the data from storage (eth_getStorageAt) with padded hex", function(done) {
web3.eth.getStorageAt(contractAddress, contract.position_of_value, function(err, result) {
assert.equal(to.number(result), 25);
done();
});
});
it("should get the data from storage (eth_getStorageAt) with unpadded hex", function(done) {
web3.eth.getStorageAt(contractAddress, '0x0', function(err, result) {
assert.equal(to.number(result), 25);
done();
});
});
it("should get the data from storage (eth_getStorageAt) with number", function(done) {
web3.eth.getStorageAt(contractAddress, 0, function(err, result) {
assert.equal(to.number(result), 25);
done();
});
});
});
describe("contract scenario (raw tx)", function() {
var tx = new Transaction({
data: contract.binary,
gasLimit: to.hex(3141592)
})
var privateKey = Buffer.from('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')
var senderAddress = '0x'+utils.privateToAddress(privateKey).toString('hex')
tx.sign(privateKey)
var rawTx = '0x'+tx.serialize().toString('hex')
// These are expected to be run in order.
var initialTransaction;
var blockHash;
var blockNumber;
var contractAddress;
it("should first populate senders address", function(done) {
// populate senders balance
web3.eth.sendTransaction({
from: accounts[0],
to: senderAddress,
value: '0x3141592',
gas: 3141592
}, function(err, hash) {
if (err) return done(err);
web3.eth.getTransactionReceipt(hash, function(err, receipt) {
if (err) return done(err)
assert(receipt);
done();
})
});
});
it("should add a contract to the network (eth_sendRawTransaction)", function(done) {
web3.eth.sendSignedTransaction(rawTx, function(err, result) {
if (err) return done(err);
initialTransaction = result;
done();
});
});
it("should verify the transaction immediately (eth_getTransactionReceipt)", function(done) {
web3.eth.getTransactionReceipt(initialTransaction, function(err, receipt) {
if (err) return done(err);
contractAddress = receipt.contractAddress;
blockHash = receipt.blockHash;
blockNumber = receipt.blockNumber;
assert.notEqual(receipt, null, "Transaction receipt shouldn't be null");
assert.notEqual(contractAddress, null, "Transaction did not create a contract");
done();
});
});
it("should verify the transaction immediately (eth_getTransactionByHash)", function(done) {
web3.eth.getTransaction(initialTransaction, function(err, result) {
if (err) return done(err);
assert.notEqual(result, null, "Transaction result shouldn't be null");
assert.equal(result.hash, initialTransaction, "Resultant hash isn't what we expected")
done();
});
});
it("should return null if transaction doesn't exist (eth_getTransactionByHash)", function(done) {
web3.eth.getTransaction("0x401b8ebb563ec9425b052aba8896cb74e07635563111b5a0663289d1baa8eb12", function(err, result) {
if (err) return done(err);
assert.equal(result, null, "Receipt should be null");
done();
});
});
it("should verify there's code at the address (eth_getCode)", function(done) {
web3.eth.getCode(contractAddress, function(err, result) {
if (err) return done(err);
assert.notEqual(result, null);
assert.notEqual(result, "0x");
// NOTE: We can't test the code returned is correct because the results
// of getCode() are *supposed* to be different than the code that was
// added to the chain.
done();
});
});
it("should be able to get the transaction from the block (eth_getTransactionByBlockHashAndIndex)", function(done) {
web3.eth.getTransactionFromBlock(blockHash, 0, function(err, result) {
if (err) return done(err);
assert.equal(result.hash, initialTransaction);
assert.equal(result.blockNumber, blockNumber);
assert.equal(result.blockHash, blockHash);
done();
});
});
it("should return null if block doesn't exist (eth_getTransactionByBlockHashAndIndex)", function(done) {
var badBlockHash = "0xaaaaaaeb03ec5e3c000d150df2c9e7ffc31e728d12aaaedc5f6cccaca5aaaaaa";
web3.eth.getTransactionFromBlock(badBlockHash, 0, function(err, result) {
if (err) return done(err);
assert.equal(result, null);
done();
});
});
it("should be able to get the transaction from the block (eth_getTransactionByBlockNumberAndIndex)", function(done) {
web3.eth.getTransactionFromBlock(blockNumber, 0, function(err, result) {
if (err) return done(err);
assert.equal(result.hash, initialTransaction);
assert.equal(result.blockNumber, blockNumber);
assert.equal(result.blockHash, blockHash);
done();
});
});
it("should throw error for transactions that don't exist in block (eth_getTransactionByBlockNumberAndIndex)", function(done) {
web3.eth.getTransactionFromBlock(blockNumber, 3, function(err, result) {
// We want an error because there is no transaction with id 3.
if (err) return done();
done(new Error("We didn't receive an error like we expected"));
});
});
});
describe("eth_getTransactionCount", function() {
it("should return 0 for non-existent account", function(done) {
web3.eth.getTransactionCount("0x1234567890123456789012345678901234567890", function(err, result) {
if (err) return done(err);
assert.equal(result, "0x0");
done();
});
});
});
describe("eth_getTransactionCount", function() {
it("should error for non-existent block", function(done) {
web3.eth.getTransactionCount("0x1234567890123456789012345678901234567890", 9999999, function(err, result) {
assert(err, "Error with message 'Unknown block number' expected, instead no error was returned");
assert(err.message.indexOf("Unknown block number") > -1);
done();
});
});
});
describe("eth_compileSolidity (not supported)", function() {
this.timeout(5000);
it("correctly compiles solidity code", function(done) {
web3.eth.compile.solidity(source, function(err, result) {
assert(err != null)