forked from bnb-chain/bsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_prefetcher_test.go
148 lines (129 loc) · 3.79 KB
/
state_prefetcher_test.go
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
package core
import (
"math/big"
"testing"
"time"
"bytes"
"context"
"errors"
"fmt"
"runtime/pprof"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/google/pprof/profile"
)
func TestPrefetchLeaking(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var (
gendb = rawdb.NewMemoryDatabase()
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
address = crypto.PubkeyToAddress(key.PublicKey)
funds = big.NewInt(100000000000000000)
gspec = &Genesis{
Config: params.TestChainConfig,
Alloc: GenesisAlloc{address: {Balance: funds}},
BaseFee: big.NewInt(params.InitialBaseFee),
}
genesis = gspec.MustCommit(gendb)
signer = types.LatestSigner(gspec.Config)
)
blocks, _ := GenerateChain(gspec.Config, genesis, ethash.NewFaker(), gendb, 1, func(i int, block *BlockGen) {
block.SetCoinbase(common.Address{0x00})
for j := 0; j < 100; j++ {
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(address), common.Address{0x00}, big.NewInt(1000), params.TxGas, block.header.BaseFee, nil), signer, key)
if err != nil {
panic(err)
}
block.AddTx(tx)
}
})
archiveDb := rawdb.NewMemoryDatabase()
gspec.MustCommit(archiveDb)
archive, _ := NewBlockChain(archiveDb, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
defer archive.Stop()
block := blocks[0]
parent := archive.GetHeader(block.ParentHash(), block.NumberU64()-1)
statedb, _ := state.NewWithSharedPool(parent.Root, archive.stateCache, archive.snaps)
inter := make(chan struct{})
Track(ctx, t, func(ctx context.Context) {
close(inter)
go archive.prefetcher.Prefetch(block, statedb, &archive.vmConfig, inter)
time.Sleep(1 * time.Second)
})
}
func Track(ctx context.Context, t *testing.T, fn func(context.Context)) {
label := t.Name()
pprof.Do(ctx, pprof.Labels("test", label), fn)
if err := CheckNoGoroutines("test", label); err != nil {
t.Fatal("Leaked goroutines\n", err)
}
}
func CheckNoGoroutines(key, value string) error {
var pb bytes.Buffer
profiler := pprof.Lookup("goroutine")
if profiler == nil {
return fmt.Errorf("unable to find profile")
}
err := profiler.WriteTo(&pb, 0)
if err != nil {
return fmt.Errorf("unable to read profile: %w", err)
}
p, err := profile.ParseData(pb.Bytes())
if err != nil {
return fmt.Errorf("unable to parse profile: %w", err)
}
return summarizeGoroutines(p, key, value)
}
func summarizeGoroutines(p *profile.Profile, key, expectedValue string) error {
var b strings.Builder
for _, sample := range p.Sample {
if !matchesLabel(sample, key, expectedValue) {
continue
}
fmt.Fprintf(&b, "count %d @", sample.Value[0])
// format the stack trace for each goroutine
for _, loc := range sample.Location {
for i, ln := range loc.Line {
if i == 0 {
fmt.Fprintf(&b, "# %#8x", loc.Address)
if loc.IsFolded {
fmt.Fprint(&b, " [F]")
}
} else {
fmt.Fprint(&b, "# ")
}
if fn := ln.Function; fn != nil {
fmt.Fprintf(&b, " %-50s %s:%d", fn.Name, fn.Filename, ln.Line)
} else {
fmt.Fprintf(&b, " ???")
}
fmt.Fprintf(&b, "\n")
}
}
fmt.Fprintf(&b, "\n")
}
if b.Len() == 0 {
return nil
}
return errors.New(b.String())
}
func matchesLabel(sample *profile.Sample, key, expectedValue string) bool {
values, hasLabel := sample.Label[key]
if !hasLabel {
return false
}
for _, value := range values {
if value == expectedValue {
return true
}
}
return false
}