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
|
// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus library is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus library. If not, see
// <http://www.gnu.org/licenses/>.
// A simple monkey that sends random transactions into the network.
package monkey
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"fmt"
"math/big"
"math/rand"
"os"
"time"
"github.com/dexon-foundation/dexon/cmd/zoo/client"
"github.com/dexon-foundation/dexon/crypto"
)
var config *MonkeyConfig
type MonkeyConfig struct {
Key string
Endpoint string
N int
Gambler bool
Feeder bool
Batch bool
Sleep int
Timeout int
}
func Init(cfg *MonkeyConfig) {
config = cfg
}
type Monkey struct {
client.Client
source *ecdsa.PrivateKey
keys []*ecdsa.PrivateKey
timer <-chan time.Time
}
func New(ep string, source *ecdsa.PrivateKey, num int, timeout time.Duration) *Monkey {
client, err := client.New(ep)
if err != nil {
panic(err)
}
file := func() *os.File {
for i := 0; i < 100; i++ {
name := fmt.Sprintf("zoo-%d.keys", i)
file, err := os.OpenFile(name,
os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0600)
if err != nil {
continue
}
fmt.Printf("Save keys to file %s\n", name)
return file
}
return nil
}()
if file == nil {
panic(fmt.Errorf("Failed to create file for zoo keys"))
}
defer file.Close()
var keys []*ecdsa.PrivateKey
for i := 0; i < num; i++ {
key, err := crypto.GenerateKey()
if err != nil {
panic(err)
}
_, err = file.Write([]byte(hex.EncodeToString(crypto.FromECDSA(key)) + "\n"))
if err != nil {
panic(err)
}
keys = append(keys, key)
}
monkey := &Monkey{
Client: *client,
source: source,
keys: keys,
}
if timeout > 0 {
monkey.timer = time.After(timeout * time.Second)
}
return monkey
}
func (m *Monkey) Distribute() {
fmt.Println("Distributing DEX to random accounts ...")
address := crypto.PubkeyToAddress(m.source.PublicKey)
nonce, err := m.PendingNonceAt(context.Background(), address)
if err != nil {
panic(err)
}
ctxs := make([]*client.TransferContext, len(m.keys))
for i, key := range m.keys {
address := crypto.PubkeyToAddress(key.PublicKey)
amount := new(big.Int)
amount.SetString("1000000000000000000", 10)
ctxs[i] = &client.TransferContext{
Key: m.source,
ToAddress: address,
Amount: amount,
Nonce: nonce,
Gas: 21000,
}
nonce++
}
m.BatchTransfer(ctxs)
time.Sleep(20 * time.Second)
}
func (m *Monkey) Crazy() uint64 {
fmt.Println("Performing random transfers ...")
nonce := uint64(0)
loop:
for {
ctxs := make([]*client.TransferContext, len(m.keys))
for i, key := range m.keys {
to := crypto.PubkeyToAddress(m.keys[rand.Int()%len(m.keys)].PublicKey)
amount := new(big.Int)
amount.SetString(fmt.Sprintf("%d0000000000000", rand.Intn(10)+1), 10)
ctx := &client.TransferContext{
Key: key,
ToAddress: to,
Amount: amount,
Nonce: nonce,
Gas: 21000,
}
if config.Batch {
ctxs[i] = ctx
} else {
m.Transfer(ctx)
}
}
if config.Batch {
m.BatchTransfer(ctxs)
}
fmt.Printf("Sent %d transactions, nonce = %d\n", len(m.keys), nonce)
if m.timer != nil {
select {
case <-m.timer:
break loop
default:
}
}
nonce++
time.Sleep(time.Duration(config.Sleep) * time.Millisecond)
}
return nonce
}
func (m *Monkey) Keys() []*ecdsa.PrivateKey {
return m.keys
}
func Exec() (*Monkey, uint64) {
privKey, err := crypto.LoadECDSA(config.Key)
if err != nil {
panic(err)
}
m := New(config.Endpoint, privKey, config.N, time.Duration(config.Timeout))
m.Distribute()
var finalNonce uint64
if config.Gambler {
finalNonce = m.Gamble()
} else if config.Feeder {
finalNonce = m.Feed()
} else {
finalNonce = m.Crazy()
}
return m, finalNonce
}
|