@@ -59,9 +59,12 @@ func TestSimulatedBackend(t *testing.T) {
59
59
}
60
60
61
61
// generate a transaction and confirm you can retrieve it
62
+ head , _ := sim .HeaderByNumber (context .Background (), nil ) // Should be child's, good enough
63
+ gasPrice := new (big.Int ).Add (head .BaseFee , big .NewInt (1 ))
64
+
62
65
code := `6060604052600a8060106000396000f360606040526008565b00`
63
66
var gas uint64 = 3000000
64
- tx := types .NewContractCreation (0 , big .NewInt (0 ), gas , big . NewInt ( 1 ) , common .FromHex (code ))
67
+ tx := types .NewContractCreation (0 , big .NewInt (0 ), gas , gasPrice , common .FromHex (code ))
65
68
tx , _ = types .SignTx (tx , types.HomesteadSigner {}, key )
66
69
67
70
err = sim .SendTransaction (context .Background (), tx )
@@ -111,14 +114,14 @@ var expectedReturn = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
111
114
func simTestBackend (testAddr common.Address ) * SimulatedBackend {
112
115
return NewSimulatedBackend (
113
116
core.GenesisAlloc {
114
- testAddr : {Balance : big .NewInt (10000000000 )},
117
+ testAddr : {Balance : big .NewInt (10000000000000000 )},
115
118
}, 10000000 ,
116
119
)
117
120
}
118
121
119
122
func TestNewSimulatedBackend (t * testing.T ) {
120
123
testAddr := crypto .PubkeyToAddress (testKey .PublicKey )
121
- expectedBal := big .NewInt (10000000000 )
124
+ expectedBal := big .NewInt (10000000000000000 )
122
125
sim := simTestBackend (testAddr )
123
126
defer sim .Close ()
124
127
@@ -157,8 +160,12 @@ func TestAdjustTime(t *testing.T) {
157
160
func TestNewAdjustTimeFail (t * testing.T ) {
158
161
testAddr := crypto .PubkeyToAddress (testKey .PublicKey )
159
162
sim := simTestBackend (testAddr )
163
+
160
164
// Create tx and send
161
- tx := types .NewTransaction (0 , testAddr , big .NewInt (1000 ), params .TxGas , big .NewInt (1 ), nil )
165
+ head , _ := sim .HeaderByNumber (context .Background (), nil ) // Should be child's, good enough
166
+ gasPrice := new (big.Int ).Add (head .BaseFee , big .NewInt (1 ))
167
+
168
+ tx := types .NewTransaction (0 , testAddr , big .NewInt (1000 ), params .TxGas , gasPrice , nil )
162
169
signedTx , err := types .SignTx (tx , types.HomesteadSigner {}, testKey )
163
170
if err != nil {
164
171
t .Errorf ("could not sign tx: %v" , err )
@@ -179,7 +186,7 @@ func TestNewAdjustTimeFail(t *testing.T) {
179
186
t .Errorf ("adjusted time not equal to a minute. prev: %v, new: %v" , prevTime , newTime )
180
187
}
181
188
// Put a transaction after adjusting time
182
- tx2 := types .NewTransaction (1 , testAddr , big .NewInt (1000 ), params .TxGas , big . NewInt ( 1 ) , nil )
189
+ tx2 := types .NewTransaction (1 , testAddr , big .NewInt (1000 ), params .TxGas , gasPrice , nil )
183
190
signedTx2 , err := types .SignTx (tx2 , types.HomesteadSigner {}, testKey )
184
191
if err != nil {
185
192
t .Errorf ("could not sign tx: %v" , err )
@@ -194,7 +201,7 @@ func TestNewAdjustTimeFail(t *testing.T) {
194
201
195
202
func TestBalanceAt (t * testing.T ) {
196
203
testAddr := crypto .PubkeyToAddress (testKey .PublicKey )
197
- expectedBal := big .NewInt (10000000000 )
204
+ expectedBal := big .NewInt (10000000000000000 )
198
205
sim := simTestBackend (testAddr )
199
206
defer sim .Close ()
200
207
bgCtx := context .Background ()
@@ -282,7 +289,10 @@ func TestNonceAt(t *testing.T) {
282
289
}
283
290
284
291
// create a signed transaction to send
285
- tx := types .NewTransaction (nonce , testAddr , big .NewInt (1000 ), params .TxGas , big .NewInt (1 ), nil )
292
+ head , _ := sim .HeaderByNumber (context .Background (), nil ) // Should be child's, good enough
293
+ gasPrice := new (big.Int ).Add (head .BaseFee , big .NewInt (1 ))
294
+
295
+ tx := types .NewTransaction (nonce , testAddr , big .NewInt (1000 ), params .TxGas , gasPrice , nil )
286
296
signedTx , err := types .SignTx (tx , types.HomesteadSigner {}, testKey )
287
297
if err != nil {
288
298
t .Errorf ("could not sign tx: %v" , err )
@@ -323,7 +333,10 @@ func TestSendTransaction(t *testing.T) {
323
333
bgCtx := context .Background ()
324
334
325
335
// create a signed transaction to send
326
- tx := types .NewTransaction (uint64 (0 ), testAddr , big .NewInt (1000 ), params .TxGas , big .NewInt (1 ), nil )
336
+ head , _ := sim .HeaderByNumber (context .Background (), nil ) // Should be child's, good enough
337
+ gasPrice := new (big.Int ).Add (head .BaseFee , big .NewInt (1 ))
338
+
339
+ tx := types .NewTransaction (uint64 (0 ), testAddr , big .NewInt (1000 ), params .TxGas , gasPrice , nil )
327
340
signedTx , err := types .SignTx (tx , types.HomesteadSigner {}, testKey )
328
341
if err != nil {
329
342
t .Errorf ("could not sign tx: %v" , err )
@@ -351,14 +364,17 @@ func TestTransactionByHash(t *testing.T) {
351
364
352
365
sim := NewSimulatedBackend (
353
366
core.GenesisAlloc {
354
- testAddr : {Balance : big .NewInt (10000000000 )},
367
+ testAddr : {Balance : big .NewInt (10000000000000000 )},
355
368
}, 10000000 ,
356
369
)
357
370
defer sim .Close ()
358
371
bgCtx := context .Background ()
359
372
360
373
// create a signed transaction to send
361
- tx := types .NewTransaction (uint64 (0 ), testAddr , big .NewInt (1000 ), params .TxGas , big .NewInt (1 ), nil )
374
+ head , _ := sim .HeaderByNumber (context .Background (), nil ) // Should be child's, good enough
375
+ gasPrice := new (big.Int ).Add (head .BaseFee , big .NewInt (1 ))
376
+
377
+ tx := types .NewTransaction (uint64 (0 ), testAddr , big .NewInt (1000 ), params .TxGas , gasPrice , nil )
362
378
signedTx , err := types .SignTx (tx , types.HomesteadSigner {}, testKey )
363
379
if err != nil {
364
380
t .Errorf ("could not sign tx: %v" , err )
@@ -534,16 +550,16 @@ func TestEstimateGasWithPrice(t *testing.T) {
534
550
To : & recipient ,
535
551
Gas : 0 ,
536
552
GasPrice : big .NewInt (0 ),
537
- Value : big .NewInt (1000 ),
553
+ Value : big .NewInt (100000000000 ),
538
554
Data : nil ,
539
555
}, 21000 , nil },
540
556
541
557
{"EstimateWithPrice" , ethereum.CallMsg {
542
558
From : addr ,
543
559
To : & recipient ,
544
560
Gas : 0 ,
545
- GasPrice : big .NewInt (1000 ),
546
- Value : big .NewInt (1000 ),
561
+ GasPrice : big .NewInt (100000000000 ),
562
+ Value : big .NewInt (100000000000 ),
547
563
Data : nil ,
548
564
}, 21000 , nil },
549
565
@@ -561,23 +577,23 @@ func TestEstimateGasWithPrice(t *testing.T) {
561
577
To : & recipient ,
562
578
Gas : 0 ,
563
579
GasPrice : big .NewInt (2e14 ), // gascost = 4.2ether
564
- Value : big .NewInt (1000 ),
580
+ Value : big .NewInt (100000000000 ),
565
581
Data : nil ,
566
582
}, 21000 , errors .New ("gas required exceeds allowance (10999)" )}, // 10999=(2.2ether-1000wei)/(2e14)
567
583
}
568
- for _ , c := range cases {
584
+ for i , c := range cases {
569
585
got , err := sim .EstimateGas (context .Background (), c .message )
570
586
if c .expectError != nil {
571
587
if err == nil {
572
- t .Fatalf ("Expect error, got nil" )
588
+ t .Fatalf ("test %d: expect error, got nil" , i )
573
589
}
574
590
if c .expectError .Error () != err .Error () {
575
- t .Fatalf ("Expect error, want %v, got %v" , c .expectError , err )
591
+ t .Fatalf ("test %d: expect error, want %v, got %v" , i , c .expectError , err )
576
592
}
577
593
continue
578
594
}
579
595
if got != c .expect {
580
- t .Fatalf ("Gas estimation mismatch, want %d, got %d" , c .expect , got )
596
+ t .Fatalf ("test %d: gas estimation mismatch, want %d, got %d" , i , c .expect , got )
581
597
}
582
598
}
583
599
}
@@ -669,9 +685,11 @@ func TestTransactionCount(t *testing.T) {
669
685
if count != 0 {
670
686
t .Errorf ("expected transaction count of %v does not match actual count of %v" , 0 , count )
671
687
}
672
-
673
688
// create a signed transaction to send
674
- tx := types .NewTransaction (uint64 (0 ), testAddr , big .NewInt (1000 ), params .TxGas , big .NewInt (1 ), nil )
689
+ head , _ := sim .HeaderByNumber (context .Background (), nil ) // Should be child's, good enough
690
+ gasPrice := new (big.Int ).Add (head .BaseFee , big .NewInt (1 ))
691
+
692
+ tx := types .NewTransaction (uint64 (0 ), testAddr , big .NewInt (1000 ), params .TxGas , gasPrice , nil )
675
693
signedTx , err := types .SignTx (tx , types.HomesteadSigner {}, testKey )
676
694
if err != nil {
677
695
t .Errorf ("could not sign tx: %v" , err )
@@ -724,9 +742,11 @@ func TestTransactionInBlock(t *testing.T) {
724
742
if pendingNonce != uint64 (0 ) {
725
743
t .Errorf ("expected pending nonce of 0 got %v" , pendingNonce )
726
744
}
727
-
728
745
// create a signed transaction to send
729
- tx := types .NewTransaction (uint64 (0 ), testAddr , big .NewInt (1000 ), params .TxGas , big .NewInt (1 ), nil )
746
+ head , _ := sim .HeaderByNumber (context .Background (), nil ) // Should be child's, good enough
747
+ gasPrice := new (big.Int ).Add (head .BaseFee , big .NewInt (1 ))
748
+
749
+ tx := types .NewTransaction (uint64 (0 ), testAddr , big .NewInt (1000 ), params .TxGas , gasPrice , nil )
730
750
signedTx , err := types .SignTx (tx , types.HomesteadSigner {}, testKey )
731
751
if err != nil {
732
752
t .Errorf ("could not sign tx: %v" , err )
@@ -781,7 +801,10 @@ func TestPendingNonceAt(t *testing.T) {
781
801
}
782
802
783
803
// create a signed transaction to send
784
- tx := types .NewTransaction (uint64 (0 ), testAddr , big .NewInt (1000 ), params .TxGas , big .NewInt (1 ), nil )
804
+ head , _ := sim .HeaderByNumber (context .Background (), nil ) // Should be child's, good enough
805
+ gasPrice := new (big.Int ).Add (head .BaseFee , big .NewInt (1 ))
806
+
807
+ tx := types .NewTransaction (uint64 (0 ), testAddr , big .NewInt (1000 ), params .TxGas , gasPrice , nil )
785
808
signedTx , err := types .SignTx (tx , types.HomesteadSigner {}, testKey )
786
809
if err != nil {
787
810
t .Errorf ("could not sign tx: %v" , err )
@@ -804,7 +827,7 @@ func TestPendingNonceAt(t *testing.T) {
804
827
}
805
828
806
829
// make a new transaction with a nonce of 1
807
- tx = types .NewTransaction (uint64 (1 ), testAddr , big .NewInt (1000 ), params .TxGas , big . NewInt ( 1 ) , nil )
830
+ tx = types .NewTransaction (uint64 (1 ), testAddr , big .NewInt (1000 ), params .TxGas , gasPrice , nil )
808
831
signedTx , err = types .SignTx (tx , types.HomesteadSigner {}, testKey )
809
832
if err != nil {
810
833
t .Errorf ("could not sign tx: %v" , err )
@@ -833,7 +856,10 @@ func TestTransactionReceipt(t *testing.T) {
833
856
bgCtx := context .Background ()
834
857
835
858
// create a signed transaction to send
836
- tx := types .NewTransaction (uint64 (0 ), testAddr , big .NewInt (1000 ), params .TxGas , big .NewInt (1 ), nil )
859
+ head , _ := sim .HeaderByNumber (context .Background (), nil ) // Should be child's, good enough
860
+ gasPrice := new (big.Int ).Add (head .BaseFee , big .NewInt (1 ))
861
+
862
+ tx := types .NewTransaction (uint64 (0 ), testAddr , big .NewInt (1000 ), params .TxGas , gasPrice , nil )
837
863
signedTx , err := types .SignTx (tx , types.HomesteadSigner {}, testKey )
838
864
if err != nil {
839
865
t .Errorf ("could not sign tx: %v" , err )
@@ -1259,7 +1285,10 @@ func TestForkResendTx(t *testing.T) {
1259
1285
// 1.
1260
1286
parent := sim .blockchain .CurrentBlock ()
1261
1287
// 2.
1262
- _tx := types .NewTransaction (0 , testAddr , big .NewInt (1000 ), params .TxGas , big .NewInt (1 ), nil )
1288
+ head , _ := sim .HeaderByNumber (context .Background (), nil ) // Should be child's, good enough
1289
+ gasPrice := new (big.Int ).Add (head .BaseFee , big .NewInt (1 ))
1290
+
1291
+ _tx := types .NewTransaction (0 , testAddr , big .NewInt (1000 ), params .TxGas , gasPrice , nil )
1263
1292
tx , _ := types .SignTx (_tx , types.HomesteadSigner {}, testKey )
1264
1293
sim .SendTransaction (context .Background (), tx )
1265
1294
sim .Commit ()
0 commit comments