forked from artheranet/perftool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_balances.go
221 lines (183 loc) · 3.99 KB
/
gen_balances.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"context"
"fmt"
"math"
"math/big"
"sync"
"sync/atomic"
"time"
"github.com/artheranet/arthera-node/logger"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
type BalancesGenerator struct {
tps uint32
chainId *big.Int
ks *keystore.KeyStore
amount *big.Int
payer accounts.Account
accs []accounts.Account
position uint
state genState
work sync.WaitGroup
done chan struct{}
sync.Mutex
logger.Instance
}
func NewBalancesGenerator(cfg *PerfConfig, ks *keystore.KeyStore, amount int64, name string) *BalancesGenerator {
g := &BalancesGenerator{
chainId: big.NewInt(cfg.ChainId),
ks: ks,
amount: big.NewInt(amount),
Instance: logger.New(name),
}
g.state.Log = g.Log
var found bool
for _, acc := range ks.Accounts() {
if err := ks.Unlock(acc, ""); err != nil {
panic(err)
}
if acc.Address == cfg.Payer {
g.payer = acc
found = true
} else {
g.accs = append(g.accs, acc)
}
}
if !found {
panic("payer not found in the keystore")
}
return g
}
func (g *BalancesGenerator) Start() <-chan *Transaction {
g.Lock()
defer g.Unlock()
if g.done != nil {
return nil
}
g.done = make(chan struct{})
output := make(chan *Transaction, 100)
g.work.Add(1)
go g.background(output)
return output
}
func (g *BalancesGenerator) Stop() {
g.Lock()
defer g.Unlock()
if g.done == nil {
return
}
close(g.done)
g.work.Wait()
g.done = nil
}
func (g *BalancesGenerator) getTPS() float64 {
tps := atomic.LoadUint32(&g.tps)
return float64(tps)
}
func (g *BalancesGenerator) SetTPS(tps float64) {
x := uint32(math.Ceil(tps))
atomic.StoreUint32(&g.tps, x)
}
func (g *BalancesGenerator) background(output chan<- *Transaction) {
defer g.work.Done()
defer close(output)
g.Log.Info("started")
defer g.Log.Info("stopped")
for {
begin := time.Now()
var (
generating time.Duration
sending time.Duration
)
tps := g.getTPS()
for count := tps; count > 0; count-- {
begin := time.Now()
tx := g.Yield()
if tx == nil {
return
}
generating += time.Since(begin)
begin = time.Now()
select {
case output <- tx:
sending += time.Since(begin)
continue
case <-g.done:
return
}
}
spent := time.Since(begin)
if spent >= time.Second {
g.Log.Debug("exceeded performance", "tps", tps, "generating", generating, "sending", sending)
continue
}
select {
case <-time.After(time.Second - spent):
continue
case <-g.done:
return
}
}
}
func (g *BalancesGenerator) Yield() *Transaction {
if !g.state.IsReady(g.done) {
return nil
}
tx := g.generate(g.position, &g.state)
if tx == nil {
return nil
}
g.Log.Debug("generated tx", "position", g.position, "dsc", tx.Dsc)
g.position++
return tx
}
func (g *BalancesGenerator) generate(position uint, state *genState) *Transaction {
count := uint(len(g.accs))
if position >= count {
return nil
}
from := g.payer
to := g.accs[position%count]
// wait every N
var callback TxCallback
if position%500 == 0 {
state.NotReady("wait for init tx")
callback = func(r *types.Receipt, e error) {
if e != nil {
panic(e)
}
state.Ready()
}
}
return &Transaction{
Make: g.transferTx(from, to, g.amount),
Dsc: fmt.Sprintf("%s --> %s", from.Address.String(), to.Address.String()),
Callback: callback,
}
}
func (g *BalancesGenerator) transferTx(from, to accounts.Account, amount *big.Int) TxMaker {
return func(client *ethclient.Client) (*types.Transaction, error) {
nonce, err := client.PendingNonceAt(context.Background(), from.Address)
if err != nil {
return nil, err
}
tx := types.NewTransaction(
uint64(nonce),
to.Address,
amount,
gasLimit,
gasPrice,
[]byte{},
)
signed, err := g.ks.SignTx(from, tx, g.chainId)
if err != nil {
panic(err)
}
err = client.SendTransaction(context.Background(), signed)
return signed, err
}
}