forked from gakonst/eth-vegeta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
95 lines (77 loc) · 2.06 KB
/
main.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
package main
import (
"os"
"bufio"
"fmt"
"time"
vegeta "github.com/tsenart/vegeta/v12/lib"
)
// Load the RLP-encoded transaction which were produced
// by the binary.
func readTxs(path string) []string {
readFile, err := os.Open(path)
if err != nil {
panic(err)
}
defer readFile.Close()
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
txs := []string{}
for fileScanner.Scan() {
tx := fileScanner.Text()
txs = append(txs, tx)
}
return txs
}
func NewEthSendRawTransactionTargeter() vegeta.Targeter {
// Read file with all the RLP encoded transactions
fname := "./txs"
txs := readTxs(fname)
i := 0
return func(tgt *vegeta.Target) error {
if tgt == nil {
return vegeta.ErrNilTarget
}
tgt.Method = "POST"
tgt.URL = "http://localhost:8545"
var header = map[string][]string{"Content-type":[]string{"application/json"}}
tgt.Header = header
// pick one tx from the list
tx := txs[i % len(txs)]
payload := fmt.Sprintf(`{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["%s"],"id":1}`, tx)
// payload := `{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}`
tgt.Body = []byte(payload)
fmt.Println("Sending", payload)
return nil
}
}
func file(name string, create bool) (*os.File, error) {
switch name {
case "stdin":
return os.Stdin, nil
case "stdout":
return os.Stdout, nil
default:
if create {
return os.Create(name)
}
return os.Open(name)
}
}
func main() {
rate := vegeta.Rate{Freq: 1000, Per: time.Second}
duration := 10 * time.Second
targeter := NewEthSendRawTransactionTargeter();
attacker := vegeta.NewAttacker()
out, err := file("attack-out", true)
if err != nil {
panic(err)
}
defer out.Close()
enc := vegeta.NewEncoder(out)
for res := range attacker.Attack(targeter, rate, duration, "Big Bang!") {
if err = enc.Encode(res); err != nil {
panic(err)
}
}
}