Skip to content

Commit 146e8d9

Browse files
core, trie, rpc: speed up tests (#28461)
* rpc: make subscription test faster reduces time for TestClientSubscriptionChannelClose from 25 sec to < 1 sec. * trie: cache trie nodes for faster sanity check This reduces the time spent on TestIncompleteSyncHash from ~25s to ~16s. * core/forkid: speed up validation test This takes the validation test from > 5s to sub 1 sec * core/state: improve snapshot test run brings the time for TestSnapshotRandom from 13s down to 6s * accounts/keystore: improve keyfile test This removes some unnecessary waits and reduces the runtime of TestUpdatedKeyfileContents from 5 to 3 seconds * trie: remove resolver * trie: only check ~5% of all trie nodes
1 parent 525db7b commit 146e8d9

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed

accounts/keystore/account_cache_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func waitWatcherStart(ks *KeyStore) bool {
6868

6969
func waitForAccounts(wantAccounts []accounts.Account, ks *KeyStore) error {
7070
var list []accounts.Account
71-
for t0 := time.Now(); time.Since(t0) < 5*time.Second; time.Sleep(200 * time.Millisecond) {
71+
for t0 := time.Now(); time.Since(t0) < 5*time.Second; time.Sleep(100 * time.Millisecond) {
7272
list = ks.Accounts()
7373
if reflect.DeepEqual(list, wantAccounts) {
7474
// ks should have also received change notifications
@@ -350,7 +350,7 @@ func TestUpdatedKeyfileContents(t *testing.T) {
350350
return
351351
}
352352
// needed so that modTime of `file` is different to its current value after forceCopyFile
353-
time.Sleep(time.Second)
353+
os.Chtimes(file, time.Now().Add(-time.Second), time.Now().Add(-time.Second))
354354

355355
// Now replace file contents
356356
if err := forceCopyFile(file, cachetestAccounts[1].URL.Path); err != nil {
@@ -366,7 +366,7 @@ func TestUpdatedKeyfileContents(t *testing.T) {
366366
}
367367

368368
// needed so that modTime of `file` is different to its current value after forceCopyFile
369-
time.Sleep(time.Second)
369+
os.Chtimes(file, time.Now().Add(-time.Second), time.Now().Add(-time.Second))
370370

371371
// Now replace file contents again
372372
if err := forceCopyFile(file, cachetestAccounts[2].URL.Path); err != nil {
@@ -382,7 +382,7 @@ func TestUpdatedKeyfileContents(t *testing.T) {
382382
}
383383

384384
// needed so that modTime of `file` is different to its current value after os.WriteFile
385-
time.Sleep(time.Second)
385+
os.Chtimes(file, time.Now().Add(-time.Second), time.Now().Add(-time.Second))
386386

387387
// Now replace file contents with crap
388388
if err := os.WriteFile(file, []byte("foo"), 0600); err != nil {

core/forkid/forkid_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,9 @@ func TestValidation(t *testing.T) {
366366
// TODO(karalabe): Enable this when Cancun is specced
367367
//{params.MainnetChainConfig, 20999999, 1677999999, ID{Hash: checksumToBytes(0x71147644), Next: 1678000000}, ErrLocalIncompatibleOrStale},
368368
}
369+
genesis := core.DefaultGenesisBlock().ToBlock()
369370
for i, tt := range tests {
370-
filter := newFilter(tt.config, core.DefaultGenesisBlock().ToBlock(), func() (uint64, uint64) { return tt.head, tt.time })
371+
filter := newFilter(tt.config, genesis, func() (uint64, uint64) { return tt.head, tt.time })
371372
if err := filter(tt.id); err != tt.err {
372373
t.Errorf("test %d: validation error mismatch: have %v, want %v", i, err, tt.err)
373374
}

core/state/statedb_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -426,23 +426,21 @@ func (test *snapshotTest) run() bool {
426426
state, _ = New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
427427
snapshotRevs = make([]int, len(test.snapshots))
428428
sindex = 0
429+
checkstates = make([]*StateDB, len(test.snapshots))
429430
)
430431
for i, action := range test.actions {
431432
if len(test.snapshots) > sindex && i == test.snapshots[sindex] {
432433
snapshotRevs[sindex] = state.Snapshot()
434+
checkstates[sindex] = state.Copy()
433435
sindex++
434436
}
435437
action.fn(action, state)
436438
}
437439
// Revert all snapshots in reverse order. Each revert must yield a state
438440
// that is equivalent to fresh state with all actions up the snapshot applied.
439441
for sindex--; sindex >= 0; sindex-- {
440-
checkstate, _ := New(types.EmptyRootHash, state.Database(), nil)
441-
for _, action := range test.actions[:test.snapshots[sindex]] {
442-
action.fn(action, checkstate)
443-
}
444442
state.RevertToSnapshot(snapshotRevs[sindex])
445-
if err := test.checkEqual(state, checkstate); err != nil {
443+
if err := test.checkEqual(state, checkstates[sindex]); err != nil {
446444
test.err = fmt.Errorf("state mismatch after revert to snapshot %d\n%v", sindex, err)
447445
return false
448446
}

rpc/client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ func TestClientSubscriptionChannelClose(t *testing.T) {
595595

596596
for i := 0; i < 100; i++ {
597597
ch := make(chan int, 100)
598-
sub, err := client.Subscribe(context.Background(), "nftest", ch, "someSubscription", maxClientSubscriptionBuffer-1, 1)
598+
sub, err := client.Subscribe(context.Background(), "nftest", ch, "someSubscription", 100, 1)
599599
if err != nil {
600600
t.Fatal(err)
601601
}

trie/sync_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package trie
1919
import (
2020
"bytes"
2121
"fmt"
22+
"math/rand"
2223
"testing"
2324

2425
"github.com/ethereum/go-ethereum/common"
@@ -587,6 +588,10 @@ func testIncompleteSync(t *testing.T, scheme string) {
587588
}
588589
// Sanity check that removing any node from the database is detected
589590
for i, path := range addedKeys {
591+
if rand.Int31n(100) > 5 {
592+
// Only check 5 percent of added keys as a sanity check
593+
continue
594+
}
590595
owner, inner := ResolvePath([]byte(path))
591596
nodeHash := addedHashes[i]
592597
value := rawdb.ReadTrieNode(diskdb, owner, inner, nodeHash, scheme)

0 commit comments

Comments
 (0)