Skip to content

Commit a01688e

Browse files
SangIlMoholiman
andcommitted
ethclient/gethclient: testcase for createAccessList, make tabledriven (#30194)
Adds testcase for createAccessList when user requested gasPrice is less than baseFee, also makes the tests tabledriven --------- Co-authored-by: Martin Holst Swende <[email protected]>
1 parent 13413be commit a01688e

File tree

1 file changed

+80
-49
lines changed

1 file changed

+80
-49
lines changed

ethclient/gethclient/gethclient_test.go

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"encoding/json"
2323
"math/big"
24+
"strings"
2425
"testing"
2526

2627
"github.com/ethereum/go-ethereum"
@@ -164,55 +165,85 @@ func TestGethClient(t *testing.T) {
164165

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

0 commit comments

Comments
 (0)