From c2c6f9629f7ff386b9599cf3fce73de0a3c234a6 Mon Sep 17 00:00:00 2001 From: bojie Date: Fri, 18 Jan 2019 19:30:23 +0800 Subject: travis: add new CI test to test fullnode (#138) --- .travis.yml | 17 +++ build/fullnode-test.sh | 55 ++++++++ build/testtool/testtool.go | 166 ++++++++++++++++++++++++ cmd/monkey/banana.go | 4 - cmd/monkey/feeder.go | 112 ---------------- cmd/monkey/gambler.go | 98 -------------- cmd/monkey/monkey.go | 284 ---------------------------------------- cmd/zoo/main.go | 32 +++++ cmd/zoo/monkey/banana.go | 4 + cmd/zoo/monkey/feeder.go | 124 ++++++++++++++++++ cmd/zoo/monkey/gambler.go | 110 ++++++++++++++++ cmd/zoo/monkey/monkey.go | 315 +++++++++++++++++++++++++++++++++++++++++++++ test/genesis.json | 2 +- test/run_test.sh | 8 +- 14 files changed, 829 insertions(+), 502 deletions(-) create mode 100755 build/fullnode-test.sh create mode 100644 build/testtool/testtool.go delete mode 100644 cmd/monkey/banana.go delete mode 100644 cmd/monkey/feeder.go delete mode 100644 cmd/monkey/gambler.go delete mode 100644 cmd/monkey/monkey.go create mode 100644 cmd/zoo/main.go create mode 100644 cmd/zoo/monkey/banana.go create mode 100644 cmd/zoo/monkey/feeder.go create mode 100644 cmd/zoo/monkey/gambler.go create mode 100644 cmd/zoo/monkey/monkey.go diff --git a/.travis.yml b/.travis.yml index f4b9ee464..18a1dd476 100644 --- a/.travis.yml +++ b/.travis.yml @@ -231,6 +231,23 @@ matrix: submodules: false script: - go run build/ci.go purge -store dexon-builds -days 14 + - if: type = push + os: osx + go: 1.11.x + env: + - gcp-osx + git: + submodules: false + script: + - PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig make all + - cd test + - ./run_test.sh --ignore-log + - cd .. + - ./build/fullnode-test.sh + addons: + homebrew: + packages: + - ethereum before_install: - openssl aes-256-cbc -K $encrypted_556a2b2ff7f6_key -iv $encrypted_556a2b2ff7f6_iv -in .ci/DEXON-7548b3622930.json.enc -out ./COBINHOOD-7548b3622930.json -d diff --git a/build/fullnode-test.sh b/build/fullnode-test.sh new file mode 100755 index 000000000..589e707bf --- /dev/null +++ b/build/fullnode-test.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +sleep 20 + +tarAndUpload() +{ + name=travis-fail-$(date +%s).tar.gz + tar -zcvf $name test + echo "Verify fail and upload $name" + PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig go run build/testtool/testtool.go upload $name dexon-builds +} + +endpoint=http://127.0.0.1:8545 + +for round in 0 1 2 +do + +echo "Start verify round $round" + for index in 0 1 2 3 + do + echo "Verify gov master public key round $round index $index" + cmd="PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig go run build/testtool/testtool.go verifyGovMPK $endpoint $round $index" + eval $cmd + code=$? + + if [ $code == 1 ]; then + tarAndUpload + exit 1 + fi + done + +echo "Start verify CRS" +cmd="PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig go run build/testtool/testtool.go verifyGovCRS $endpoint $round" +eval $cmd +code=$? + +if [ $code == 1 ]; then + tarAndUpload + exit 1 +fi + +if [ $round -lt 2 ]; then + cmd="PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig go run build/testtool/testtool.go monkeyTest $endpoint" + eval $cmd + code=$? + + if [ $code == 1 ]; then + tarAndUpload + exit 1 + fi + + echo "Sleep 30 sec wait for next round" + sleep 30 +fi +done \ No newline at end of file diff --git a/build/testtool/testtool.go b/build/testtool/testtool.go new file mode 100644 index 000000000..801c343b6 --- /dev/null +++ b/build/testtool/testtool.go @@ -0,0 +1,166 @@ +package main + +import ( + "bytes" + "context" + "log" + "math/big" + "os" + "path/filepath" + "strconv" + "strings" + "time" + + "github.com/dexon-foundation/dexon" + "github.com/dexon-foundation/dexon/accounts/abi" + "github.com/dexon-foundation/dexon/cmd/zoo/monkey" + "github.com/dexon-foundation/dexon/core/vm" + "github.com/dexon-foundation/dexon/crypto" + "github.com/dexon-foundation/dexon/ethclient" + "github.com/dexon-foundation/dexon/internal/build" +) + +func main() { + if len(os.Args) < 2 { + log.Fatal("need subcommand as first argument") + } + + switch os.Args[1] { + case "verifyGovCRS": + doVerifyGovCRS(os.Args[2:]) + case "verifyGovMPK": + doVerifyGovMPK(os.Args[2:]) + case "monkeyTest": + doMonkeyTest(os.Args[2:]) + case "upload": + doUpload(os.Args[2:]) + } +} + +func doVerifyGovCRS(args []string) { + if len(args) < 2 { + log.Fatal("arg length is not enough") + } + + client, err := ethclient.Dial(args[0]) + if err != nil { + log.Fatalf("new ethclient fail: %v", err) + } + + abiObject, err := abi.JSON(strings.NewReader(vm.GovernanceABIJSON)) + if err != nil { + log.Fatalf("read abi fail: %v", err) + } + + round, err := strconv.Atoi(args[1]) + if err != nil { + log.Fatalf("pasre round from arg 2 fail: %v", err) + } + + input, err := abiObject.Pack("crs", big.NewInt(int64(round))) + if err != nil { + log.Fatalf("pack input fail: %v", err) + } + + result, err := client.CallContract(context.Background(), ethereum.CallMsg{ + To: &vm.GovernanceContractAddress, + Data: input, + }, nil) + if err != nil { + log.Fatalf("call contract fail: %v", err) + } + + if bytes.Equal(make([]byte, 32), result) { + log.Fatalf("round %s crs not found", args[1]) + } + + log.Printf("get round %s crs %x", args[1], result) +} + +func doVerifyGovMPK(args []string) { + if len(args) < 3 { + log.Fatal("arg length is not enough") + } + + client, err := ethclient.Dial(args[0]) + if err != nil { + log.Fatalf("new ethclient fail: %v", err) + } + + abiObject, err := abi.JSON(strings.NewReader(vm.GovernanceABIJSON)) + if err != nil { + log.Fatalf("read abi fail: %v", err) + } + + round, err := strconv.Atoi(args[1]) + if err != nil { + log.Fatalf("pasre round from arg 2 fail: %v", err) + } + + index, err := strconv.Atoi(args[2]) + if err != nil { + log.Fatalf("pasre round from arg 2 fail: %v", err) + } + + input, err := abiObject.Pack("dkgMasterPublicKeys", big.NewInt(int64(round)), big.NewInt(int64(index))) + if err != nil { + log.Fatalf("pack input fail: %v", err) + } + + result, err := client.CallContract(context.Background(), ethereum.CallMsg{ + To: &vm.GovernanceContractAddress, + Data: input, + }, nil) + if err != nil { + log.Fatalf("call contract fail: %v", err) + } + + if bytes.Equal(make([]byte, 0), result) { + log.Fatalf("round %s index %s crs not found", args[1], args[2]) + } + + log.Printf("get round %s index %s master public key %x", args[1], args[2], result) +} + +func doUpload(args []string) { + auth := build.GCPOption{ + CredentialPath: os.Getenv("GCP_CREDENTIAL_PATH"), + } + + if err := build.GCPFileUpload(args[0], args[1], filepath.Base(args[0]), auth); err != nil { + log.Fatalf("upload fail: %v", err) + } +} + +func doMonkeyTest(args []string) { + if len(args) < 1 { + log.Fatal("arg length is not enough") + } + + client, err := ethclient.Dial(args[0]) + if err != nil { + log.Fatalf("new ethclient fail: %v", err) + } + + monkey.Init(&monkey.MonkeyConfig{ + Key: "test/keystore/monkey.key", + Endpoint: args[0], + N: 30, + Sleep: 3000, + Timeout: 60, + }) + m, nonce := monkey.Exec() + + time.Sleep(10 * time.Second) + + for _, key := range m.Keys() { + currentNonce, err := client.NonceAt(context.Background(), crypto.PubkeyToAddress(key.PublicKey), nil) + if err != nil { + log.Fatalf("get address nonce fail: %v", err) + } + + if currentNonce != nonce+1 { + log.Fatalf("expect nonce %v but %v", nonce, currentNonce) + } + } +} diff --git a/cmd/monkey/banana.go b/cmd/monkey/banana.go deleted file mode 100644 index c776eba47..000000000 --- a/cmd/monkey/banana.go +++ /dev/null @@ -1,4 +0,0 @@ -package main - -var bananaContract = "608060405234801561001057600080fd5b5060038054600160a060020a03191633178155604080518082019091528181527f424e4e0000000000000000000000000000000000000000000000000000000000602090910190815261006691600491906100d0565b506040805180820190915260068082527f42616e616e61000000000000000000000000000000000000000000000000000060209092019182526100ab916005916100d0565b5068056bc75e2d6310000060018190553360009081526020819052604090205561016b565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061011157805160ff191683800117855561013e565b8280016001018555821561013e579182015b8281111561013e578251825591602001919060010190610123565b5061014a92915061014e565b5090565b61016891905b8082111561014a5760008155600101610154565b90565b610a398061017a6000396000f3fe6080604052600436106100be577c0100000000000000000000000000000000000000000000000000000000600035046306fdde0381146100c3578063095ea7b31461014d57806318160ddd1461019a57806323b872dd146101c1578063313ce56714610204578063661884631461022f57806370a08231146102685780638da5cb5b1461029b57806395d89b41146102cc578063a9059cbb146102e1578063d73dd6231461031a578063dd62ed3e14610353578063f2fde38b1461038e575b600080fd5b3480156100cf57600080fd5b506100d86103c3565b6040805160208082528351818301528351919283929083019185019080838360005b838110156101125781810151838201526020016100fa565b50505050905090810190601f16801561013f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561015957600080fd5b506101866004803603604081101561017057600080fd5b50600160a060020a038135169060200135610451565b604080519115158252519081900360200190f35b3480156101a657600080fd5b506101af6104b7565b60408051918252519081900360200190f35b3480156101cd57600080fd5b50610186600480360360608110156101e457600080fd5b50600160a060020a038135811691602081013590911690604001356104bd565b34801561021057600080fd5b50610219610634565b6040805160ff9092168252519081900360200190f35b34801561023b57600080fd5b506101866004803603604081101561025257600080fd5b50600160a060020a038135169060200135610639565b34801561027457600080fd5b506101af6004803603602081101561028b57600080fd5b5035600160a060020a0316610729565b3480156102a757600080fd5b506102b0610744565b60408051600160a060020a039092168252519081900360200190f35b3480156102d857600080fd5b506100d8610753565b3480156102ed57600080fd5b506101866004803603604081101561030457600080fd5b50600160a060020a0381351690602001356107ae565b34801561032657600080fd5b506101866004803603604081101561033d57600080fd5b50600160a060020a03813516906020013561088f565b34801561035f57600080fd5b506101af6004803603604081101561037657600080fd5b50600160a060020a0381358116916020013516610928565b34801561039a57600080fd5b506103c1600480360360208110156103b157600080fd5b5035600160a060020a0316610953565b005b6005805460408051602060026001851615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156104495780601f1061041e57610100808354040283529160200191610449565b820191906000526020600020905b81548152906001019060200180831161042c57829003601f168201915b505050505081565b336000818152600260209081526040808320600160a060020a038716808552908352818420869055815186815291519394909390927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925928290030190a350600192915050565b60015490565b6000600160a060020a03831615156104d457600080fd5b600160a060020a0384166000908152602081905260409020548211156104f957600080fd5b600160a060020a038416600090815260026020908152604080832033845290915290205482111561052957600080fd5b600160a060020a038416600090815260208190526040902054610552908363ffffffff6109e816565b600160a060020a038086166000908152602081905260408082209390935590851681522054610587908363ffffffff6109fa16565b600160a060020a038085166000908152602081815260408083209490945591871681526002825282812033825290915220546105c9908363ffffffff6109e816565b600160a060020a03808616600081815260026020908152604080832033845282529182902094909455805186815290519287169391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929181900390910190a35060019392505050565b601281565b336000908152600260209081526040808320600160a060020a03861684529091528120548083111561068e57336000908152600260209081526040808320600160a060020a03881684529091528120556106c3565b61069e818463ffffffff6109e816565b336000908152600260209081526040808320600160a060020a03891684529091529020555b336000818152600260209081526040808320600160a060020a0389168085529083529281902054815190815290519293927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929181900390910190a35060019392505050565b600160a060020a031660009081526020819052604090205490565b600354600160a060020a031681565b6004805460408051602060026001851615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156104495780601f1061041e57610100808354040283529160200191610449565b6000600160a060020a03831615156107c557600080fd5b336000908152602081905260409020548211156107e157600080fd5b33600090815260208190526040902054610801908363ffffffff6109e816565b3360009081526020819052604080822092909255600160a060020a03851681522054610833908363ffffffff6109fa16565b600160a060020a038416600081815260208181526040918290209390935580518581529051919233927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a350600192915050565b336000908152600260209081526040808320600160a060020a03861684529091528120546108c3908363ffffffff6109fa16565b336000818152600260209081526040808320600160a060020a0389168085529083529281902085905580519485525191937f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929081900390910190a350600192915050565b600160a060020a03918216600090815260026020908152604080832093909416825291909152205490565b600354600160a060020a0316331461096a57600080fd5b600160a060020a038116151561097f57600080fd5b600354604051600160a060020a038084169216907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a36003805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0392909216919091179055565b6000828211156109f457fe5b50900390565b81810182811015610a0757fe5b9291505056fea165627a7a7230582032a81f1b06fd8a2ba419349fde02bc9075bc1b944325ef313d4ae44b25c056690029" -var bananaABIJSON = "[{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_from\",\"type\":\"address\"},{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_subtractedValue\",\"type\":\"uint256\"}],\"name\":\"decreaseApproval\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"owner\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_addedValue\",\"type\":\"uint256\"}],\"name\":\"increaseApproval\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"},{\"name\":\"_spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"transferOwnership\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"previousOwner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"OwnershipTransferred\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"spender\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"}]" diff --git a/cmd/monkey/feeder.go b/cmd/monkey/feeder.go deleted file mode 100644 index 7c1419b91..000000000 --- a/cmd/monkey/feeder.go +++ /dev/null @@ -1,112 +0,0 @@ -// 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 -// . - -package main - -import ( - "context" - "fmt" - "math" - "math/big" - "math/rand" - "strings" - "time" - - "github.com/dexon-foundation/dexon/accounts/abi" - "github.com/dexon-foundation/dexon/common" - "github.com/dexon-foundation/dexon/crypto" -) - -var bananaABI abi.ABI - -func init() { - var err error - bananaABI, err = abi.JSON(strings.NewReader(bananaABIJSON)) - if err != nil { - panic(err) - } -} - -func (m *Monkey) DistributeBanana(contract common.Address) { - fmt.Println("Distributing Banana to random accounts ...") - address := crypto.PubkeyToAddress(m.source.PublicKey) - nonce, err := m.client.PendingNonceAt(context.Background(), address) - if err != nil { - panic(err) - } - - ctxs := make([]*transferContext, len(m.keys)) - amount := new(big.Int) - amount.SetString("10000000000000000", 10) - for i, key := range m.keys { - address := crypto.PubkeyToAddress(key.PublicKey) - input, err := bananaABI.Pack("transfer", address, amount) - if err != nil { - panic(err) - } - ctxs[i] = &transferContext{ - Key: m.source, - ToAddress: contract, - Data: input, - Nonce: nonce, - Gas: 100000, - } - nonce += 1 - } - m.batchTransfer(ctxs) - time.Sleep(20 * time.Second) -} - -func (m *Monkey) Feed() { - fmt.Println("Deploying contract ...") - contract := m.deploy(m.source, bananaContract, nil, new(big.Int), math.MaxUint64) - fmt.Println(" Contract deployed: ", contract.String()) - m.DistributeBanana(contract) - - time.Sleep(5 * time.Second) - - nonce := uint64(0) - for { - fmt.Println("nonce", nonce) - ctxs := make([]*transferContext, len(m.keys)) - for i, key := range m.keys { - to := crypto.PubkeyToAddress(m.keys[rand.Int()%len(m.keys)].PublicKey) - input, err := bananaABI.Pack("transfer", to, big.NewInt(rand.Int63n(100)+1)) - if err != nil { - panic(err) - } - - ctx := &transferContext{ - Key: key, - ToAddress: contract, - Data: input, - Nonce: nonce, - Gas: 42000, - } - if *batch { - ctxs[i] = ctx - } else { - m.transfer(ctx) - } - } - if *batch { - m.batchTransfer(ctxs) - } - nonce += 1 - time.Sleep(time.Duration(*sleep) * time.Millisecond) - } -} diff --git a/cmd/monkey/gambler.go b/cmd/monkey/gambler.go deleted file mode 100644 index f8e57163c..000000000 --- a/cmd/monkey/gambler.go +++ /dev/null @@ -1,98 +0,0 @@ -// 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 -// . - -package main - -import ( - "fmt" - "math" - "math/big" - "strings" - "time" - - "github.com/dexon-foundation/dexon/accounts/abi" -) - -var betContract = string("0x60806040523480156200001157600080fd5b5060405160208062000ee083398101806040528101908080519060200190929190505050336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a36200010b8162000112640100000000026401000000009004565b50620003f5565b60006200011e62000364565b6000620001396200029f640100000000026401000000009004565b15156200014557600080fd5b606484101515620001e4576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260248152602001807f4578706563746174696f6e2073686f756c64206265206c657373207468616e2081526020017f3130302e0000000000000000000000000000000000000000000000000000000081525060400191505060405180910390fd5b836001819055506200021061271085620002f664010000000002620008c4179091906401000000009004565b925060008260006064811015156200022457fe5b602002018181525050600190505b606481101562000285576200025f8184620003386401000000000262000902179091906401000000009004565b82826064811015156200026e57fe5b602002018181525050808060010191505062000232565b8160029060646200029892919062000388565b5050505050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614905090565b60008060008414156200030d576000915062000331565b82840290508284828115156200031f57fe5b041415156200032d57600080fd5b8091505b5092915050565b6000806000831115156200034b57600080fd5b82848115156200035757fe5b0490508091505092915050565b610c8060405190810160405280606490602082028038833980820191505090505090565b8260648101928215620003ba579160200282015b82811115620003b95782518255916020019190600101906200039c565b5b509050620003c99190620003cd565b5090565b620003f291905b80821115620003ee576000816000905550600101620003d4565b5090565b90565b610adb80620004056000396000f3006080604052600436106100a4576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630c60e0c3146100a9578063379607f5146100d6578063715018a6146101035780637365870b1461011a5780638da5cb5b1461013a5780638f32d59b14610191578063e1152343146101c0578063ed88c68e14610201578063f2fde38b1461020b578063fc1c39dc1461024e575b600080fd5b3480156100b557600080fd5b506100d460048036038101908080359060200190929190505050610279565b005b3480156100e257600080fd5b50610101600480360381019080803590602001909291905050506103cb565b005b34801561010f57600080fd5b506101186104be565b005b61013860048036038101908080359060200190929190505050610590565b005b34801561014657600080fd5b5061014f610803565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561019d57600080fd5b506101a661082c565b604051808215151515815260200191505060405180910390f35b3480156101cc57600080fd5b506101eb60048036038101908080359060200190929190505050610883565b6040518082815260200191505060405180910390f35b61020961089d565b005b34801561021757600080fd5b5061024c600480360381019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061089f565b005b34801561025a57600080fd5b506102636108be565b6040518082815260200191505060405180910390f35b6000610283610a26565b600061028d61082c565b151561029857600080fd5b606484101515610336576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260248152602001807f4578706563746174696f6e2073686f756c64206265206c657373207468616e2081526020017f3130302e0000000000000000000000000000000000000000000000000000000081525060400191505060405180910390fd5b83600181905550610352612710856108c490919063ffffffff16565b9250600082600060648110151561036557fe5b602002018181525050600190505b60648110156103b35761038f818461090290919063ffffffff16565b828260648110151561039d57fe5b6020020181815250508080600101915050610373565b8160029060646103c4929190610a4a565b5050505050565b6103d361082c565b15156103de57600080fd5b3073ffffffffffffffffffffffffffffffffffffffff1631811115151561046d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601c8152602001807f4e6f20656e6f756768206f6620746f6b656e20746f20636c61696d2e0000000081525060200191505060405180910390fd5b610475610803565b73ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f193505050501580156104ba573d6000803e3d6000fd5b5050565b6104c661082c565b15156104d157600080fd5b600073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a360008060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550565b60008060018311151561060b576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260188152602001807f5461726765742073686f756c64206265206269676765722e000000000000000081525060200191505060405180910390fd5b6001548311151515610685576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260198152602001807f5461726765742073686f756c6420626520736d616c6c65722e0000000000000081525060200191505060405180910390fd5b612710341115156106fe576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260158152602001807f4d696e696d756d206265742069732031303030302e000000000000000000000081525060200191505060405180910390fd5b600160642f81151561070c57fe5b0601915060009050828210156107a05761075661271061074860026001870360648110151561073757fe5b0154346108c490919063ffffffff16565b61090290919063ffffffff16565b90503373ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f1935050505015801561079e573d6000803e3d6000fd5b505b3373ffffffffffffffffffffffffffffffffffffffff167f97371a3349bea11f577edf6e64350a3dfb9de665d1154c7e6d08eb0805aa043084348460405180848152602001838152602001828152602001935050505060405180910390a2505050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614905090565b60028160648110151561089257fe5b016000915090505481565b565b6108a761082c565b15156108b257600080fd5b6108bb8161092c565b50565b60015481565b60008060008414156108d957600091506108fb565b82840290508284828115156108ea57fe5b041415156108f757600080fd5b8091505b5092915050565b60008060008311151561091457600080fd5b828481151561091f57fe5b0490508091505092915050565b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161415151561096857600080fd5b8073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b610c8060405190810160405280606490602082028038833980820191505090505090565b8260648101928215610a79579160200282015b82811115610a78578251825591602001919060010190610a5d565b5b509050610a869190610a8a565b5090565b610aac91905b80821115610aa8576000816000905550600101610a90565b5090565b905600a165627a7a7230582087d19571ab3ae7207fb8f6182b6b891f2414f00e1904790341a82c957044b5330029") -var betConstructor = []string{"62"} -var betABIJSON = ` -[ { "constant": false, "inputs": [], "name": "renounceOwnership", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "owner", "outputs": [ { "name": "", "type": "address" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "isOwner", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "uint256" } ], "name": "payout", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "newOwner", "type": "address" } ], "name": "transferOwnership", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "expectation", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "inputs": [ { "name": "_expectation", "type": "uint256" } ], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "_addr", "type": "address" }, { "indexed": false, "name": "_target", "type": "uint256" }, { "indexed": false, "name": "_value", "type": "uint256" }, { "indexed": false, "name": "_pay", "type": "uint256" } ], "name": "Bet", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "previousOwner", "type": "address" }, { "indexed": true, "name": "newOwner", "type": "address" } ], "name": "OwnershipTransferred", "type": "event" }, { "constant": false, "inputs": [ { "name": "_expectation", "type": "uint256" } ], "name": "updateExpectation", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_target", "type": "uint256" } ], "name": "bet", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_amount", "type": "uint256" } ], "name": "claim", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [], "name": "donate", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function" } ] -` -var betABI abi.ABI - -var oneDEX *big.Int - -func init() { - oneDEX = new(big.Int).Exp(big.NewInt(10), big.NewInt(19), nil) - var err error - betABI, err = abi.JSON(strings.NewReader(betABIJSON)) - if err != nil { - panic(err) - } -} - -func (m *Monkey) Gamble() { - fmt.Println("Deploying contract ...") - contract := m.deploy(m.source, betContract, betConstructor, new(big.Int), math.MaxUint64) - fmt.Println(" Contract deployed: ", contract.String()) - fmt.Println("Donating ...") - input, err := betABI.Pack("donate") - if err != nil { - panic(err) - } - m.transfer(&transferContext{ - Key: m.source, - ToAddress: contract, - Amount: new(big.Int).Set(oneDEX), - Data: input, - Gas: 0, - Nonce: math.MaxUint64, - }) - - time.Sleep(5 * time.Second) - - input, err = betABI.Pack("bet", big.NewInt(50)) - if err != nil { - panic(err) - } - - nonce := uint64(0) - for { - fmt.Println("nonce", nonce) - ctxs := make([]*transferContext, len(m.keys)) - for i, key := range m.keys { - ctx := &transferContext{ - Key: key, - ToAddress: contract, - Amount: big.NewInt(100000), - Data: input, - Nonce: nonce, - Gas: 210000, - } - if *batch { - ctxs[i] = ctx - } else { - m.transfer(ctx) - } - } - if *batch { - m.batchTransfer(ctxs) - } - nonce += 1 - time.Sleep(time.Duration(*sleep) * time.Millisecond) - } -} diff --git a/cmd/monkey/monkey.go b/cmd/monkey/monkey.go deleted file mode 100644 index bbf534952..000000000 --- a/cmd/monkey/monkey.go +++ /dev/null @@ -1,284 +0,0 @@ -// 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 -// . - -// 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 batch = flag.Bool("batch", false, "monkeys will send transaction in batch") -var sleep = flag.Int("sleep", 500, "time in millisecond that monkeys sleep between each transaction") -var feeder = flag.Bool("feeder", false, "make this monkey a feeder") - -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, - } -} - -type transferContext struct { - Key *ecdsa.PrivateKey - ToAddress common.Address - Amount *big.Int - Data []byte - Nonce uint64 - Gas uint64 -} - -func (m *Monkey) prepareTx(ctx *transferContext) *types.Transaction { - if ctx.Nonce == math.MaxUint64 { - var err error - address := crypto.PubkeyToAddress(ctx.Key.PublicKey) - ctx.Nonce, err = m.client.PendingNonceAt(context.Background(), address) - if err != nil { - panic(err) - } - } - - if ctx.Gas == uint64(0) { - var err error - ctx.Gas, err = m.client.EstimateGas(context.Background(), dexon.CallMsg{ - Data: ctx.Data, - }) - if err != nil { - panic(err) - } - } - - tx := types.NewTransaction( - ctx.Nonce, - ctx.ToAddress, - ctx.Amount, - ctx.Gas, - big.NewInt(1e9), - ctx.Data) - - signer := types.NewEIP155Signer(m.networkID) - tx, err := types.SignTx(tx, signer, ctx.Key) - if err != nil { - panic(err) - } - - return tx -} - -func (m *Monkey) transfer(ctx *transferContext) { - tx := m.prepareTx(ctx) - - err := m.client.SendTransaction(context.Background(), tx) - if err != nil { - panic(err) - } -} - -func (m *Monkey) batchTransfer(ctxs []*transferContext) { - txs := make([]*types.Transaction, len(ctxs)) - for i, ctx := range ctxs { - txs[i] = m.prepareTx(ctx) - } - - err := m.client.SendTransactions(context.Background(), txs) - if err != nil { - panic(err) - } -} - -func (m *Monkey) deploy( - key *ecdsa.PrivateKey, code string, ctors []string, amount *big.Int, nonce uint64) common.Address { - - 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) - } - } - - 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{ - From: address, - Data: data, - }) - if err != nil { - panic(err) - } - - tx := types.NewContractCreation( - 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) 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) - } - - ctxs := make([]*transferContext, len(m.keys)) - for i, key := range m.keys { - address := crypto.PubkeyToAddress(key.PublicKey) - amount := new(big.Int) - amount.SetString("100000000000000000000", 10) - ctxs[i] = &transferContext{ - Key: m.source, - ToAddress: address, - Amount: amount, - Nonce: nonce, - Gas: 21000, - } - nonce += 1 - } - m.batchTransfer(ctxs) - time.Sleep(20 * time.Second) -} - -func (m *Monkey) Crazy() { - fmt.Println("Performing random transfers ...") - nonce := uint64(0) - for { - ctxs := make([]*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("%d000000000000000", rand.Intn(10)+1), 10) - ctx := &transferContext{ - Key: key, - ToAddress: to, - Amount: amount, - Nonce: nonce, - Gas: 21000, - } - if *batch { - ctxs[i] = ctx - } else { - m.transfer(ctx) - } - } - if *batch { - m.batchTransfer(ctxs) - } - fmt.Printf("Sent %d transactions, nonce = %d\n", len(m.keys), 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 if *feeder { - m.Feed() - } else { - m.Crazy() - } -} diff --git a/cmd/zoo/main.go b/cmd/zoo/main.go new file mode 100644 index 000000000..195d5737c --- /dev/null +++ b/cmd/zoo/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "flag" + + "github.com/dexon-foundation/dexon/cmd/zoo/monkey" +) + +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 batch = flag.Bool("batch", false, "monkeys will send transaction in batch") +var sleep = flag.Int("sleep", 500, "time in millisecond that monkeys sleep between each transaction") +var feeder = flag.Bool("feeder", false, "make this monkey a feeder") +var timeout = flag.Int("timeout", 0, "execution time limit after start") + +func main() { + flag.Parse() + + monkey.Init(&monkey.MonkeyConfig{ + Key: *key, + Endpoint: *endpoint, + N: *n, + Gambler: *gambler, + Feeder: *feeder, + Batch: *batch, + Sleep: *sleep, + Timeout: *timeout, + }) + monkey.Exec() +} diff --git a/cmd/zoo/monkey/banana.go b/cmd/zoo/monkey/banana.go new file mode 100644 index 000000000..7ecb53fc9 --- /dev/null +++ b/cmd/zoo/monkey/banana.go @@ -0,0 +1,4 @@ +package monkey + +var bananaContract = "608060405234801561001057600080fd5b5060038054600160a060020a03191633178155604080518082019091528181527f424e4e0000000000000000000000000000000000000000000000000000000000602090910190815261006691600491906100d0565b506040805180820190915260068082527f42616e616e61000000000000000000000000000000000000000000000000000060209092019182526100ab916005916100d0565b5068056bc75e2d6310000060018190553360009081526020819052604090205561016b565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061011157805160ff191683800117855561013e565b8280016001018555821561013e579182015b8281111561013e578251825591602001919060010190610123565b5061014a92915061014e565b5090565b61016891905b8082111561014a5760008155600101610154565b90565b610a398061017a6000396000f3fe6080604052600436106100be577c0100000000000000000000000000000000000000000000000000000000600035046306fdde0381146100c3578063095ea7b31461014d57806318160ddd1461019a57806323b872dd146101c1578063313ce56714610204578063661884631461022f57806370a08231146102685780638da5cb5b1461029b57806395d89b41146102cc578063a9059cbb146102e1578063d73dd6231461031a578063dd62ed3e14610353578063f2fde38b1461038e575b600080fd5b3480156100cf57600080fd5b506100d86103c3565b6040805160208082528351818301528351919283929083019185019080838360005b838110156101125781810151838201526020016100fa565b50505050905090810190601f16801561013f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561015957600080fd5b506101866004803603604081101561017057600080fd5b50600160a060020a038135169060200135610451565b604080519115158252519081900360200190f35b3480156101a657600080fd5b506101af6104b7565b60408051918252519081900360200190f35b3480156101cd57600080fd5b50610186600480360360608110156101e457600080fd5b50600160a060020a038135811691602081013590911690604001356104bd565b34801561021057600080fd5b50610219610634565b6040805160ff9092168252519081900360200190f35b34801561023b57600080fd5b506101866004803603604081101561025257600080fd5b50600160a060020a038135169060200135610639565b34801561027457600080fd5b506101af6004803603602081101561028b57600080fd5b5035600160a060020a0316610729565b3480156102a757600080fd5b506102b0610744565b60408051600160a060020a039092168252519081900360200190f35b3480156102d857600080fd5b506100d8610753565b3480156102ed57600080fd5b506101866004803603604081101561030457600080fd5b50600160a060020a0381351690602001356107ae565b34801561032657600080fd5b506101866004803603604081101561033d57600080fd5b50600160a060020a03813516906020013561088f565b34801561035f57600080fd5b506101af6004803603604081101561037657600080fd5b50600160a060020a0381358116916020013516610928565b34801561039a57600080fd5b506103c1600480360360208110156103b157600080fd5b5035600160a060020a0316610953565b005b6005805460408051602060026001851615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156104495780601f1061041e57610100808354040283529160200191610449565b820191906000526020600020905b81548152906001019060200180831161042c57829003601f168201915b505050505081565b336000818152600260209081526040808320600160a060020a038716808552908352818420869055815186815291519394909390927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925928290030190a350600192915050565b60015490565b6000600160a060020a03831615156104d457600080fd5b600160a060020a0384166000908152602081905260409020548211156104f957600080fd5b600160a060020a038416600090815260026020908152604080832033845290915290205482111561052957600080fd5b600160a060020a038416600090815260208190526040902054610552908363ffffffff6109e816565b600160a060020a038086166000908152602081905260408082209390935590851681522054610587908363ffffffff6109fa16565b600160a060020a038085166000908152602081815260408083209490945591871681526002825282812033825290915220546105c9908363ffffffff6109e816565b600160a060020a03808616600081815260026020908152604080832033845282529182902094909455805186815290519287169391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929181900390910190a35060019392505050565b601281565b336000908152600260209081526040808320600160a060020a03861684529091528120548083111561068e57336000908152600260209081526040808320600160a060020a03881684529091528120556106c3565b61069e818463ffffffff6109e816565b336000908152600260209081526040808320600160a060020a03891684529091529020555b336000818152600260209081526040808320600160a060020a0389168085529083529281902054815190815290519293927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929181900390910190a35060019392505050565b600160a060020a031660009081526020819052604090205490565b600354600160a060020a031681565b6004805460408051602060026001851615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156104495780601f1061041e57610100808354040283529160200191610449565b6000600160a060020a03831615156107c557600080fd5b336000908152602081905260409020548211156107e157600080fd5b33600090815260208190526040902054610801908363ffffffff6109e816565b3360009081526020819052604080822092909255600160a060020a03851681522054610833908363ffffffff6109fa16565b600160a060020a038416600081815260208181526040918290209390935580518581529051919233927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a350600192915050565b336000908152600260209081526040808320600160a060020a03861684529091528120546108c3908363ffffffff6109fa16565b336000818152600260209081526040808320600160a060020a0389168085529083529281902085905580519485525191937f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929081900390910190a350600192915050565b600160a060020a03918216600090815260026020908152604080832093909416825291909152205490565b600354600160a060020a0316331461096a57600080fd5b600160a060020a038116151561097f57600080fd5b600354604051600160a060020a038084169216907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a36003805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0392909216919091179055565b6000828211156109f457fe5b50900390565b81810182811015610a0757fe5b9291505056fea165627a7a7230582032a81f1b06fd8a2ba419349fde02bc9075bc1b944325ef313d4ae44b25c056690029" +var bananaABIJSON = "[{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_from\",\"type\":\"address\"},{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_subtractedValue\",\"type\":\"uint256\"}],\"name\":\"decreaseApproval\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"owner\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_addedValue\",\"type\":\"uint256\"}],\"name\":\"increaseApproval\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"},{\"name\":\"_spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"transferOwnership\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"previousOwner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"OwnershipTransferred\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"spender\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"}]" diff --git a/cmd/zoo/monkey/feeder.go b/cmd/zoo/monkey/feeder.go new file mode 100644 index 000000000..7a3db3a3a --- /dev/null +++ b/cmd/zoo/monkey/feeder.go @@ -0,0 +1,124 @@ +// 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 +// . + +package monkey + +import ( + "context" + "fmt" + "math" + "math/big" + "math/rand" + "strings" + "time" + + "github.com/dexon-foundation/dexon/accounts/abi" + "github.com/dexon-foundation/dexon/common" + "github.com/dexon-foundation/dexon/crypto" +) + +var bananaABI abi.ABI + +func init() { + var err error + bananaABI, err = abi.JSON(strings.NewReader(bananaABIJSON)) + if err != nil { + panic(err) + } +} + +func (m *Monkey) DistributeBanana(contract common.Address) { + fmt.Println("Distributing Banana to random accounts ...") + address := crypto.PubkeyToAddress(m.source.PublicKey) + nonce, err := m.client.PendingNonceAt(context.Background(), address) + if err != nil { + panic(err) + } + + ctxs := make([]*transferContext, len(m.keys)) + amount := new(big.Int) + amount.SetString("10000000000000000", 10) + for i, key := range m.keys { + address := crypto.PubkeyToAddress(key.PublicKey) + input, err := bananaABI.Pack("transfer", address, amount) + if err != nil { + panic(err) + } + ctxs[i] = &transferContext{ + Key: m.source, + ToAddress: contract, + Data: input, + Nonce: nonce, + Gas: 100000, + } + nonce += 1 + } + m.batchTransfer(ctxs) + time.Sleep(20 * time.Second) +} + +func (m *Monkey) Feed() uint64 { + fmt.Println("Deploying contract ...") + contract := m.deploy(m.source, bananaContract, nil, new(big.Int), math.MaxUint64) + fmt.Println(" Contract deployed: ", contract.String()) + m.DistributeBanana(contract) + + time.Sleep(5 * time.Second) + + nonce := uint64(0) +loop: + for { + fmt.Println("nonce", nonce) + ctxs := make([]*transferContext, len(m.keys)) + for i, key := range m.keys { + to := crypto.PubkeyToAddress(m.keys[rand.Int()%len(m.keys)].PublicKey) + input, err := bananaABI.Pack("transfer", to, big.NewInt(rand.Int63n(100)+1)) + if err != nil { + panic(err) + } + + ctx := &transferContext{ + Key: key, + ToAddress: contract, + Data: input, + Nonce: nonce, + Gas: 42000, + } + if config.Batch { + ctxs[i] = ctx + } else { + m.transfer(ctx) + } + } + if config.Batch { + m.batchTransfer(ctxs) + } + + if m.timer != nil { + select { + case <-m.timer: + break loop + default: + } + } + + nonce += 1 + time.Sleep(time.Duration(config.Sleep) * time.Millisecond) + } + + return nonce +} diff --git a/cmd/zoo/monkey/gambler.go b/cmd/zoo/monkey/gambler.go new file mode 100644 index 000000000..33f749343 --- /dev/null +++ b/cmd/zoo/monkey/gambler.go @@ -0,0 +1,110 @@ +// 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 +// . + +package monkey + +import ( + "fmt" + "math" + "math/big" + "strings" + "time" + + "github.com/dexon-foundation/dexon/accounts/abi" +) + +var betContract = string("0x60806040523480156200001157600080fd5b5060405160208062000ee083398101806040528101908080519060200190929190505050336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a36200010b8162000112640100000000026401000000009004565b50620003f5565b60006200011e62000364565b6000620001396200029f640100000000026401000000009004565b15156200014557600080fd5b606484101515620001e4576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260248152602001807f4578706563746174696f6e2073686f756c64206265206c657373207468616e2081526020017f3130302e0000000000000000000000000000000000000000000000000000000081525060400191505060405180910390fd5b836001819055506200021061271085620002f664010000000002620008c4179091906401000000009004565b925060008260006064811015156200022457fe5b602002018181525050600190505b606481101562000285576200025f8184620003386401000000000262000902179091906401000000009004565b82826064811015156200026e57fe5b602002018181525050808060010191505062000232565b8160029060646200029892919062000388565b5050505050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614905090565b60008060008414156200030d576000915062000331565b82840290508284828115156200031f57fe5b041415156200032d57600080fd5b8091505b5092915050565b6000806000831115156200034b57600080fd5b82848115156200035757fe5b0490508091505092915050565b610c8060405190810160405280606490602082028038833980820191505090505090565b8260648101928215620003ba579160200282015b82811115620003b95782518255916020019190600101906200039c565b5b509050620003c99190620003cd565b5090565b620003f291905b80821115620003ee576000816000905550600101620003d4565b5090565b90565b610adb80620004056000396000f3006080604052600436106100a4576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630c60e0c3146100a9578063379607f5146100d6578063715018a6146101035780637365870b1461011a5780638da5cb5b1461013a5780638f32d59b14610191578063e1152343146101c0578063ed88c68e14610201578063f2fde38b1461020b578063fc1c39dc1461024e575b600080fd5b3480156100b557600080fd5b506100d460048036038101908080359060200190929190505050610279565b005b3480156100e257600080fd5b50610101600480360381019080803590602001909291905050506103cb565b005b34801561010f57600080fd5b506101186104be565b005b61013860048036038101908080359060200190929190505050610590565b005b34801561014657600080fd5b5061014f610803565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561019d57600080fd5b506101a661082c565b604051808215151515815260200191505060405180910390f35b3480156101cc57600080fd5b506101eb60048036038101908080359060200190929190505050610883565b6040518082815260200191505060405180910390f35b61020961089d565b005b34801561021757600080fd5b5061024c600480360381019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061089f565b005b34801561025a57600080fd5b506102636108be565b6040518082815260200191505060405180910390f35b6000610283610a26565b600061028d61082c565b151561029857600080fd5b606484101515610336576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260248152602001807f4578706563746174696f6e2073686f756c64206265206c657373207468616e2081526020017f3130302e0000000000000000000000000000000000000000000000000000000081525060400191505060405180910390fd5b83600181905550610352612710856108c490919063ffffffff16565b9250600082600060648110151561036557fe5b602002018181525050600190505b60648110156103b35761038f818461090290919063ffffffff16565b828260648110151561039d57fe5b6020020181815250508080600101915050610373565b8160029060646103c4929190610a4a565b5050505050565b6103d361082c565b15156103de57600080fd5b3073ffffffffffffffffffffffffffffffffffffffff1631811115151561046d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601c8152602001807f4e6f20656e6f756768206f6620746f6b656e20746f20636c61696d2e0000000081525060200191505060405180910390fd5b610475610803565b73ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f193505050501580156104ba573d6000803e3d6000fd5b5050565b6104c661082c565b15156104d157600080fd5b600073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a360008060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550565b60008060018311151561060b576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260188152602001807f5461726765742073686f756c64206265206269676765722e000000000000000081525060200191505060405180910390fd5b6001548311151515610685576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260198152602001807f5461726765742073686f756c6420626520736d616c6c65722e0000000000000081525060200191505060405180910390fd5b612710341115156106fe576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260158152602001807f4d696e696d756d206265742069732031303030302e000000000000000000000081525060200191505060405180910390fd5b600160642f81151561070c57fe5b0601915060009050828210156107a05761075661271061074860026001870360648110151561073757fe5b0154346108c490919063ffffffff16565b61090290919063ffffffff16565b90503373ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f1935050505015801561079e573d6000803e3d6000fd5b505b3373ffffffffffffffffffffffffffffffffffffffff167f97371a3349bea11f577edf6e64350a3dfb9de665d1154c7e6d08eb0805aa043084348460405180848152602001838152602001828152602001935050505060405180910390a2505050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614905090565b60028160648110151561089257fe5b016000915090505481565b565b6108a761082c565b15156108b257600080fd5b6108bb8161092c565b50565b60015481565b60008060008414156108d957600091506108fb565b82840290508284828115156108ea57fe5b041415156108f757600080fd5b8091505b5092915050565b60008060008311151561091457600080fd5b828481151561091f57fe5b0490508091505092915050565b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161415151561096857600080fd5b8073ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b610c8060405190810160405280606490602082028038833980820191505090505090565b8260648101928215610a79579160200282015b82811115610a78578251825591602001919060010190610a5d565b5b509050610a869190610a8a565b5090565b610aac91905b80821115610aa8576000816000905550600101610a90565b5090565b905600a165627a7a7230582087d19571ab3ae7207fb8f6182b6b891f2414f00e1904790341a82c957044b5330029") +var betConstructor = []string{"62"} +var betABIJSON = ` +[ { "constant": false, "inputs": [], "name": "renounceOwnership", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "owner", "outputs": [ { "name": "", "type": "address" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "isOwner", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "uint256" } ], "name": "payout", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "newOwner", "type": "address" } ], "name": "transferOwnership", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "expectation", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "inputs": [ { "name": "_expectation", "type": "uint256" } ], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "_addr", "type": "address" }, { "indexed": false, "name": "_target", "type": "uint256" }, { "indexed": false, "name": "_value", "type": "uint256" }, { "indexed": false, "name": "_pay", "type": "uint256" } ], "name": "Bet", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "previousOwner", "type": "address" }, { "indexed": true, "name": "newOwner", "type": "address" } ], "name": "OwnershipTransferred", "type": "event" }, { "constant": false, "inputs": [ { "name": "_expectation", "type": "uint256" } ], "name": "updateExpectation", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_target", "type": "uint256" } ], "name": "bet", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_amount", "type": "uint256" } ], "name": "claim", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [], "name": "donate", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function" } ] +` +var betABI abi.ABI + +var oneDEX *big.Int + +func init() { + oneDEX = new(big.Int).Exp(big.NewInt(10), big.NewInt(19), nil) + var err error + betABI, err = abi.JSON(strings.NewReader(betABIJSON)) + if err != nil { + panic(err) + } +} + +func (m *Monkey) Gamble() uint64 { + fmt.Println("Deploying contract ...") + contract := m.deploy(m.source, betContract, betConstructor, new(big.Int), math.MaxUint64) + fmt.Println(" Contract deployed: ", contract.String()) + fmt.Println("Donating ...") + input, err := betABI.Pack("donate") + if err != nil { + panic(err) + } + m.transfer(&transferContext{ + Key: m.source, + ToAddress: contract, + Amount: new(big.Int).Set(oneDEX), + Data: input, + Gas: 0, + Nonce: math.MaxUint64, + }) + + time.Sleep(5 * time.Second) + + input, err = betABI.Pack("bet", big.NewInt(50)) + if err != nil { + panic(err) + } + + nonce := uint64(0) +loop: + for { + fmt.Println("nonce", nonce) + ctxs := make([]*transferContext, len(m.keys)) + for i, key := range m.keys { + ctx := &transferContext{ + Key: key, + ToAddress: contract, + Amount: big.NewInt(100000), + Data: input, + Nonce: nonce, + Gas: 210000, + } + if config.Batch { + ctxs[i] = ctx + } else { + m.transfer(ctx) + } + } + if config.Batch { + m.batchTransfer(ctxs) + } + + if m.timer != nil { + select { + case <-m.timer: + break loop + default: + } + } + + nonce += 1 + time.Sleep(time.Duration(config.Sleep) * time.Millisecond) + } + + return nonce +} diff --git a/cmd/zoo/monkey/monkey.go b/cmd/zoo/monkey/monkey.go new file mode 100644 index 000000000..5015a0b09 --- /dev/null +++ b/cmd/zoo/monkey/monkey.go @@ -0,0 +1,315 @@ +// 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 +// . + +// A simple monkey that sends random transactions into the network. + +package monkey + +import ( + "context" + "crypto/ecdsa" + "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 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 *ethclient.Client + source *ecdsa.PrivateKey + keys []*ecdsa.PrivateKey + networkID *big.Int + timer <-chan time.Time +} + +func New(ep string, source *ecdsa.PrivateKey, num int, timeout time.Duration) *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) + } + + monkey := &Monkey{ + client: client, + source: source, + keys: keys, + networkID: networkID, + } + + if timeout > 0 { + monkey.timer = time.After(timeout * time.Second) + } + + return monkey +} + +type transferContext struct { + Key *ecdsa.PrivateKey + ToAddress common.Address + Amount *big.Int + Data []byte + Nonce uint64 + Gas uint64 +} + +func (m *Monkey) prepareTx(ctx *transferContext) *types.Transaction { + if ctx.Nonce == math.MaxUint64 { + var err error + address := crypto.PubkeyToAddress(ctx.Key.PublicKey) + ctx.Nonce, err = m.client.PendingNonceAt(context.Background(), address) + if err != nil { + panic(err) + } + } + + if ctx.Gas == uint64(0) { + var err error + ctx.Gas, err = m.client.EstimateGas(context.Background(), dexon.CallMsg{ + Data: ctx.Data, + }) + if err != nil { + panic(err) + } + } + + tx := types.NewTransaction( + ctx.Nonce, + ctx.ToAddress, + ctx.Amount, + ctx.Gas, + big.NewInt(1e9), + ctx.Data) + + signer := types.NewEIP155Signer(m.networkID) + tx, err := types.SignTx(tx, signer, ctx.Key) + if err != nil { + panic(err) + } + + return tx +} + +func (m *Monkey) transfer(ctx *transferContext) { + tx := m.prepareTx(ctx) + + err := m.client.SendTransaction(context.Background(), tx) + if err != nil { + panic(err) + } +} + +func (m *Monkey) batchTransfer(ctxs []*transferContext) { + txs := make([]*types.Transaction, len(ctxs)) + for i, ctx := range ctxs { + txs[i] = m.prepareTx(ctx) + } + + err := m.client.SendTransactions(context.Background(), txs) + if err != nil { + panic(err) + } +} + +func (m *Monkey) deploy( + key *ecdsa.PrivateKey, code string, ctors []string, amount *big.Int, nonce uint64) common.Address { + + 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) + } + } + + 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{ + From: address, + Data: data, + }) + if err != nil { + panic(err) + } + + tx := types.NewContractCreation( + 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) 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) + } + + ctxs := make([]*transferContext, len(m.keys)) + for i, key := range m.keys { + address := crypto.PubkeyToAddress(key.PublicKey) + amount := new(big.Int) + amount.SetString("100000000000000000000", 10) + ctxs[i] = &transferContext{ + Key: m.source, + ToAddress: address, + Amount: amount, + Nonce: nonce, + Gas: 21000, + } + nonce += 1 + } + 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([]*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("%d000000000000000", rand.Intn(10)+1), 10) + ctx := &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 += 1 + 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 +} diff --git a/test/genesis.json b/test/genesis.json index efa2727ad..7ab90e48b 100644 --- a/test/genesis.json +++ b/test/genesis.json @@ -27,7 +27,7 @@ "phiRatio": 0.667, "notarySetSize": 4, "dkgSetSize": 4, - "roundInterval": 600000, + "roundInterval": 90000, "minBlockInterval": 1000, "fineValues": [ "0x21e19e0c9bab2400000", diff --git a/test/run_test.sh b/test/run_test.sh index 5455dee2b..60f7cad82 100755 --- a/test/run_test.sh +++ b/test/run_test.sh @@ -29,7 +29,7 @@ with open('$GENESIS', 'r') as f: data = f.read() with open('$GENESIS', 'w') as f: - dMoment = int(time.time()) + 7 + dMoment = int(time.time()) + 15 f.write(re.sub('"dMoment": [0-9]+,', '"dMoment": %d,' % dMoment, data)) __FILE__ @@ -57,7 +57,7 @@ for i in $(seq 0 3); do $GDEX \ ${NETWORK} \ --bp \ - --verbosity=3 \ + --verbosity=4 \ --gcmode=archive \ --datadir=$datadir --nodekey=keystore/test$i.key \ --port=$((30305 + $i)) \ @@ -70,4 +70,6 @@ for i in $(seq 0 3); do > $logsdir/gdex.$i.log 2>&1 & done -tail -f $logsdir/gdex.*.log +if [ "$1" != "--ignore-log" ]; then + tail -f $logsdir/gdex.*.log +fi -- cgit v1.2.3