aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/monkey/monkey.go
blob: 6ac9be5f3c98176744ab966c93e20c9e32ccd384 (plain) (blame)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// 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 main

import (
    "context"
    "crypto/ecdsa"
    "flag"
    "fmt"
    "math"
    "math/big"
    "math/rand"
    "time"

    dexon "github.com/dexon-foundation/dexon"
    "github.com/dexon-foundation/dexon/common"
    "github.com/dexon-foundation/dexon/core/types"
    "github.com/dexon-foundation/dexon/crypto"
    "github.com/dexon-foundation/dexon/ethclient"
)

var key = flag.String("key", "", "private key path")
var endpoint = flag.String("endpoint", "http://127.0.0.1:8545", "JSON RPC endpoint")
var n = flag.Int("n", 100, "number of random accounts")
var gambler = flag.Bool("gambler", false, "make this monkey a gambler")
var parallel = flag.Bool("parallel", false, "monkeys will send transaction in parallel")
var sleep = flag.Int("sleep", 500, "time in millisecond that monkeys sleep between each transaction")

type Monkey struct {
    client    *ethclient.Client
    source    *ecdsa.PrivateKey
    keys      []*ecdsa.PrivateKey
    networkID *big.Int
}

func New(ep string, source *ecdsa.PrivateKey, num int) *Monkey {
    client, err := ethclient.Dial(ep)
    if err != nil {
        panic(err)
    }

    var keys []*ecdsa.PrivateKey

    for i := 0; i < num; i++ {
        key, err := crypto.GenerateKey()
        if err != nil {
            panic(err)
        }
        keys = append(keys, key)
    }

    networkID, err := client.NetworkID(context.Background())
    if err != nil {
        panic(err)
    }

    return &Monkey{
        client:    client,
        source:    source,
        keys:      keys,
        networkID: networkID,
    }
}

func (m *Monkey) transfer(
    key *ecdsa.PrivateKey, toAddress common.Address, amount *big.Int, nonce uint64) {

    if nonce == math.MaxUint64 {
        var err error
        address := crypto.PubkeyToAddress(key.PublicKey)
        nonce, err = m.client.PendingNonceAt(context.Background(), address)
        if err != nil {
            panic(err)
        }
    }

    tx := types.NewTransaction(
        uint64(nonce),
        toAddress,
        amount,
        uint64(21000),
        big.NewInt(1e9),
        nil)

    signer := types.NewEIP155Signer(m.networkID)
    tx, err := types.SignTx(tx, signer, key)
    if err != nil {
        panic(err)
    }

    fmt.Println("Sending TX", "fullhash", tx.Hash().String())

    err = m.client.SendTransaction(context.Background(), tx)
    if err != nil {
        panic(err)
    }
}

func (m *Monkey) deploy(
    key *ecdsa.PrivateKey, code string, ctors []string, amount *big.Int, nonce uint64) common.Address {

    if nonce == math.MaxUint64 {
        var err error
        address := crypto.PubkeyToAddress(key.PublicKey)
        nonce, err = m.client.PendingNonceAt(context.Background(), address)
        if err != nil {
            panic(err)
        }
    }

    var input string
    for _, ctor := range ctors {
        input += fmt.Sprintf("%064s", ctor)
    }
    data := common.Hex2Bytes(code + input)

    gas, err := m.client.EstimateGas(context.Background(), dexon.CallMsg{
        Data: data,
    })
    if err != nil {
        panic(err)
    }

    tx := types.NewContractCreation(
        uint64(nonce),
        amount,
        gas,
        big.NewInt(1e9),
        data)

    signer := types.NewEIP155Signer(m.networkID)
    tx, err = types.SignTx(tx, signer, key)
    if err != nil {
        panic(err)
    }

    fmt.Println("Sending TX", "fullhash", tx.Hash().String())

    err = m.client.SendTransaction(context.Background(), tx)
    if err != nil {
        panic(err)
    }

    for {
        time.Sleep(500 * time.Millisecond)
        recp, err := m.client.TransactionReceipt(context.Background(), tx.Hash())
        if err != nil {
            if err == dexon.NotFound {
                continue
            }
            panic(err)
        }
        return recp.ContractAddress
    }
}

func (m *Monkey) call(
    key *ecdsa.PrivateKey, contract common.Address, input []byte, amount *big.Int, gas uint64, nonce uint64) {

    address := crypto.PubkeyToAddress(key.PublicKey)
    if nonce == math.MaxUint64 {
        var err error
        nonce, err = m.client.PendingNonceAt(context.Background(), address)
        if err != nil {
            panic(err)
        }
    }

    if gas == uint64(0) {
        var err error
        gas, err = m.client.EstimateGas(context.Background(), dexon.CallMsg{
            From:  address,
            To:    &contract,
            Value: amount,
            Data:  input,
        })
        if err != nil {
            panic(err)
        }
    }

    tx := types.NewTransaction(
        uint64(nonce),
        contract,
        amount,
        gas,
        big.NewInt(1e9),
        input)

    signer := types.NewEIP155Signer(m.networkID)
    tx, err := types.SignTx(tx, signer, key)
    if err != nil {
        panic(err)
    }

    fmt.Println("Sending TX", "fullhash", tx.Hash().String())

    err = m.client.SendTransaction(context.Background(), tx)
    if err != nil {
        panic(err)
    }
}

func (m *Monkey) Distribute() {
    fmt.Println("Distributing DEX to random accounts ...")
    address := crypto.PubkeyToAddress(m.source.PublicKey)
    nonce, err := m.client.PendingNonceAt(context.Background(), address)
    if err != nil {
        panic(err)
    }

    for _, key := range m.keys {
        address := crypto.PubkeyToAddress(key.PublicKey)
        amount := new(big.Int)
        amount.SetString("1000000000000000000", 10)
        m.transfer(m.source, address, amount, nonce)
        nonce += 1
    }
    time.Sleep(20 * time.Second)
}

func (m *Monkey) Crazy() {
    fmt.Println("Performing random transfers ...")
    nonce := uint64(0)
    transfer := func(key *ecdsa.PrivateKey, to common.Address, nonce uint64) {
        m.transfer(key, to, big.NewInt(1), nonce)
    }
    for {
        fmt.Println("nonce", nonce)
        for _, key := range m.keys {
            to := crypto.PubkeyToAddress(m.keys[rand.Int()%len(m.keys)].PublicKey)
            if *parallel {
                go transfer(key, to, nonce)
            } else {
                transfer(key, to, nonce)
            }
        }
        nonce += 1
        time.Sleep(time.Duration(*sleep) * time.Millisecond)
    }
}

func main() {
    flag.Parse()

    privKey, err := crypto.LoadECDSA(*key)
    if err != nil {
        panic(err)
    }

    m := New(*endpoint, privKey, *n)
    m.Distribute()
    if *gambler {
        m.Gamble()
    } else {
        m.Crazy()
    }
}