-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress20.go
95 lines (78 loc) · 1.98 KB
/
stress20.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 (
"context"
"crypto/ecdsa"
"fmt"
"log"
"sync/atomic"
"github.com/blockchain-tps-test/samples/theta/tps"
"github.com/pkg/errors"
"github.com/thetatoken/theta/crypto"
)
func erc20StressTest(client *EthClient, ctx context.Context) {
addrs := make([]string, len(privs))
for i := range privs {
privateKey, err := crypto.HexToECDSA(privs[i])
// privateKey, err := crypto.HexToECDSA("2dad160420b1e9b6fc152cd691a686a7080a0cee41b98754597a2ce57cc5dab1")
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
fromAddress := pubkeyToAddress(*publicKeyECDSA)
addrs[i] = fromAddress.Hex()
}
wallet, err := tps.NewWallet(ctx, client, privs, addrs)
if err != nil {
logger.Fatal("err NewWallet: ", err)
}
taskDo := func(t tps.Task, id int) error {
task, ok := t.(*EthTask)
if !ok {
return errors.New("unexpected task type")
}
ctx, cancel := context.WithTimeout(context.Background(), Timeout)
defer cancel()
var (
priv = wallet.Priv(id)
currentNonce = wallet.IncrementNonce(priv)
)
if err = task.Do(ctx, client, priv, currentNonce, &queue, logger, Erc20Address); err != nil {
if errors.Is(err, tps.ErrWrongNonce) {
wallet.RecetNonce(priv, currentNonce)
task.tryCount = 0
queue.Push(task)
return nil
}
return errors.Wrap(err, "err Do")
}
return nil
}
worker := tps.NewWorker(taskDo)
if concurrency > MaxConcurrency {
logger.Warn(fmt.Sprintf("concurrency setting is over logical max(%d)", MaxConcurrency))
}
for i := 0; i < concurrency; i++ {
go worker.Run(&queue, i)
}
go func() {
count := 2
for {
if atomic.LoadUint32(&closing) == 1 {
break
}
if queue.CountTasks() > queueSize {
continue
}
queue.Push(&EthTask{
to: "0x27F6F1bb3e2977c3CB014e7d4B5639bB133A6032",
amount: 1,
tokenId: int64(count),
})
count++
}
}()
}