aboutsummaryrefslogtreecommitdiffstats
path: root/cmd
diff options
context:
space:
mode:
authorWei-Ning Huang <w@dexon.org>2018-10-30 20:00:24 +0800
committerWei-Ning Huang <w@dexon.org>2019-04-09 21:32:52 +0800
commit699dbac51c7344c7659c6c63f3bc720160f20843 (patch)
treedd87c2c36feebea16b1bd59f2bf705e6fb12a6a0 /cmd
parentd4883d04e8cca644800a57cd842a01f577c1a16f (diff)
downloaddexon-699dbac51c7344c7659c6c63f3bc720160f20843.tar.gz
dexon-699dbac51c7344c7659c6c63f3bc720160f20843.tar.zst
dexon-699dbac51c7344c7659c6c63f3bc720160f20843.zip
cmd: monkey: create random accounts to simulate network traffic
Diffstat (limited to 'cmd')
-rw-r--r--cmd/monkey/monkey.go130
1 files changed, 96 insertions, 34 deletions
diff --git a/cmd/monkey/monkey.go b/cmd/monkey/monkey.go
index c155e5cf0..76a306c7a 100644
--- a/cmd/monkey/monkey.go
+++ b/cmd/monkey/monkey.go
@@ -21,8 +21,10 @@ package main
import (
"context"
+ "crypto/ecdsa"
"flag"
"fmt"
+ "math"
"math/big"
"math/rand"
"time"
@@ -34,60 +36,120 @@ import (
)
var key = flag.String("key", "", "private key path")
-var endpoint = flag.String("endpoint", "http://localhost:8545", "JSON RPC endpoint")
-var tps = flag.Int("tps", 10, "transactions per second")
+var endpoint = flag.String("endpoint", "http://127.0.0.1:8545", "JSON RPC endpoint")
+var n = flag.Int("n", 100, "number of random accounts")
+
+type Monkey struct {
+ client *ethclient.Client
+ source *ecdsa.PrivateKey
+ keys []*ecdsa.PrivateKey
+ networkID *big.Int
+}
-func randomAddress() common.Address {
- x := common.Address{}
- for i := 0; i < 20; i++ {
- x[i] = byte(rand.Int31() % 16)
+func New(ep string, source *ecdsa.PrivateKey, num int) *Monkey {
+ client, err := ethclient.Dial(ep)
+ if err != nil {
+ panic(err)
}
- return x
-}
-func main() {
- flag.Parse()
+ var keys []*ecdsa.PrivateKey
- privKey, err := crypto.LoadECDSA(*key)
+ 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)
}
- client, err := ethclient.Dial(*endpoint)
+ 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)
}
- address := crypto.PubkeyToAddress(privKey.PublicKey)
+ fmt.Println("Sending TX", "fullhash", tx.Hash().String())
- nonce, err := client.PendingNonceAt(context.Background(), address)
+ err = m.client.SendTransaction(context.Background(), tx)
if err != nil {
panic(err)
}
+}
- for {
- tx := types.NewTransaction(
- nonce,
- randomAddress(),
- big.NewInt(10),
- uint64(21000),
- big.NewInt(1e9),
- nil)
-
- signer := types.NewEIP155Signer(big.NewInt(237))
- tx, err = types.SignTx(tx, signer, privKey)
- 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)
+ }
- err := client.SendTransaction(context.Background(), tx)
- 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(60 * time.Second)
+}
+
+func (m *Monkey) Crazy() {
+ fmt.Println("Performing random transfers ...")
+ nonce := uint64(0)
+ for {
+ fmt.Println("nonce", nonce)
+ for _, key := range m.keys {
+ to := crypto.PubkeyToAddress(m.keys[rand.Int()%len(m.keys)].PublicKey)
+ m.transfer(key, to, big.NewInt(1), nonce)
}
+ nonce += 1
+ time.Sleep(500 * time.Millisecond)
+ }
+}
- fmt.Println("Sending TX", "fullhash", tx.Hash().String())
+func main() {
+ flag.Parse()
- nonce++
- time.Sleep(time.Duration(float32(time.Second) / float32(*tps)))
+ privKey, err := crypto.LoadECDSA(*key)
+ if err != nil {
+ panic(err)
}
+
+ m := New(*endpoint, privKey, *n)
+ m.Distribute()
+ m.Crazy()
}