Skip to content

Commit 3262033

Browse files
committed
ethclient/gethclient: make test tabledriven
1 parent 675bd3d commit 3262033

File tree

1 file changed

+78
-91
lines changed

1 file changed

+78
-91
lines changed

ethclient/gethclient/gethclient_test.go

Lines changed: 78 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -165,98 +165,85 @@ func TestGethClient(t *testing.T) {
165165

166166
func testAccessList(t *testing.T, client *rpc.Client) {
167167
ec := New(client)
168-
// Test transfer
169-
msg := ethereum.CallMsg{
170-
From: testAddr,
171-
To: &common.Address{},
172-
Gas: 21000,
173-
GasPrice: big.NewInt(875000000),
174-
Value: big.NewInt(1),
175-
}
176-
al, gas, vmErr, err := ec.CreateAccessList(context.Background(), msg)
177-
if err != nil {
178-
t.Fatalf("unexpected error: %v", err)
179-
}
180-
if vmErr != "" {
181-
t.Fatalf("unexpected vm error: %v", vmErr)
182-
}
183-
if gas != 21000 {
184-
t.Fatalf("unexpected gas used: %v", gas)
185-
}
186-
if len(*al) != 0 {
187-
t.Fatalf("unexpected length of accesslist: %v", len(*al))
188-
}
189-
// Test reverting transaction
190-
msg = ethereum.CallMsg{
191-
From: testAddr,
192-
To: nil,
193-
Gas: 100000,
194-
GasPrice: big.NewInt(1000000000),
195-
Value: big.NewInt(1),
196-
Data: common.FromHex("0x608060806080608155fd"),
197-
}
198-
al, gas, vmErr, err = ec.CreateAccessList(context.Background(), msg)
199-
if err != nil {
200-
t.Fatalf("unexpected error: %v", err)
201-
}
202-
if vmErr == "" {
203-
t.Fatalf("wanted vmErr, got none")
204-
}
205-
if gas == 21000 {
206-
t.Fatalf("unexpected gas used: %v", gas)
207-
}
208-
if len(*al) != 1 || al.StorageKeys() != 1 {
209-
t.Fatalf("unexpected length of accesslist: %v", len(*al))
210-
}
211-
// address changes between calls, so we can't test for it.
212-
if (*al)[0].Address == common.HexToAddress("0x0") {
213-
t.Fatalf("unexpected address: %v", (*al)[0].Address)
214-
}
215-
if (*al)[0].StorageKeys[0] != common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000081") {
216-
t.Fatalf("unexpected storage key: %v", (*al)[0].StorageKeys[0])
217-
}
218168

219-
// error when gasPrice is less than baseFee
220-
msg = ethereum.CallMsg{
221-
From: testAddr,
222-
To: &common.Address{},
223-
Gas: 21000,
224-
GasPrice: big.NewInt(1), // less than baseFee
225-
Value: big.NewInt(1),
226-
}
227-
al, gas, vmErr, err = ec.CreateAccessList(context.Background(), msg)
228-
if err != nil && !strings.Contains(err.Error(), core.ErrFeeCapTooLow.Error()) {
229-
t.Fatalf("unexpected error: %v", err)
230-
}
231-
if vmErr != "" {
232-
t.Fatalf("unexpected vm error: %v", vmErr)
233-
}
234-
if gas != 0 {
235-
t.Fatalf("unexpected gas used: %v", gas)
236-
}
237-
if al != nil {
238-
t.Fatalf("unexpected accesslist: %v", len(*al))
239-
}
240-
241-
// when gasPrice is not specified
242-
msg = ethereum.CallMsg{
243-
From: testAddr,
244-
To: &common.Address{},
245-
Gas: 21000,
246-
Value: big.NewInt(1),
247-
}
248-
al, gas, vmErr, err = ec.CreateAccessList(context.Background(), msg)
249-
if err != nil {
250-
t.Fatalf("unexpected error: %v", err)
251-
}
252-
if vmErr != "" {
253-
t.Fatalf("unexpected vm error: %v", vmErr)
254-
}
255-
if gas != 21000 {
256-
t.Fatalf("unexpected gas used: %v", gas)
257-
}
258-
if len(*al) != 0 {
259-
t.Fatalf("unexpected length of accesslist: %v", len(*al))
169+
for i, tc := range []struct {
170+
msg ethereum.CallMsg
171+
wantGas uint64
172+
wantErr string
173+
wantVMErr string
174+
wantAL string
175+
}{
176+
{ // Test transfer
177+
msg: ethereum.CallMsg{
178+
From: testAddr,
179+
To: &common.Address{},
180+
Gas: 21000,
181+
GasPrice: big.NewInt(875000000),
182+
Value: big.NewInt(1),
183+
},
184+
wantGas: 21000,
185+
wantAL: `[]`,
186+
},
187+
{ // Test reverting transaction
188+
msg: ethereum.CallMsg{
189+
From: testAddr,
190+
To: nil,
191+
Gas: 100000,
192+
GasPrice: big.NewInt(1000000000),
193+
Value: big.NewInt(1),
194+
Data: common.FromHex("0x608060806080608155fd"),
195+
},
196+
wantGas: 77496,
197+
wantVMErr: "execution reverted",
198+
wantAL: `[
199+
{
200+
"address": "0x3a220f351252089d385b29beca14e27f204c296a",
201+
"storageKeys": [
202+
"0x0000000000000000000000000000000000000000000000000000000000000081"
203+
]
204+
}
205+
]`,
206+
},
207+
{ // error when gasPrice is less than baseFee
208+
msg: ethereum.CallMsg{
209+
From: testAddr,
210+
To: &common.Address{},
211+
Gas: 21000,
212+
GasPrice: big.NewInt(1), // less than baseFee
213+
Value: big.NewInt(1),
214+
},
215+
wantErr: "max fee per gas less than block base fee",
216+
},
217+
{ // when gasPrice is not specified
218+
msg: ethereum.CallMsg{
219+
From: testAddr,
220+
To: &common.Address{},
221+
Gas: 21000,
222+
Value: big.NewInt(1),
223+
},
224+
wantGas: 21000,
225+
wantAL: `[]`,
226+
},
227+
} {
228+
al, gas, vmErr, err := ec.CreateAccessList(context.Background(), tc.msg)
229+
if tc.wantErr != "" {
230+
if !strings.Contains(err.Error(), tc.wantErr) {
231+
t.Fatalf("test %d: wrong error: %v", i, err)
232+
}
233+
continue
234+
} else if err != nil {
235+
t.Fatalf("test %d: wrong error: %v", i, err)
236+
}
237+
if have, want := vmErr, tc.wantVMErr; have != want {
238+
t.Fatalf("test %d: vmErr wrong, have %v want %v", i, have, want)
239+
}
240+
if have, want := gas, tc.wantGas; have != want {
241+
t.Fatalf("test %d: gas wrong, have %v want %v", i, have, want)
242+
}
243+
haveList, _ := json.MarshalIndent(al, "", " ")
244+
if have, want := string(haveList), tc.wantAL; have != want {
245+
t.Fatalf("test %d: access list wrong, have:\n%v\nwant:\n%v", i, have, want)
246+
}
260247
}
261248
}
262249

0 commit comments

Comments
 (0)