diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/zoo/main.go | 32 | ||||
-rw-r--r-- | cmd/zoo/monkey/banana.go (renamed from cmd/monkey/banana.go) | 2 | ||||
-rw-r--r-- | cmd/zoo/monkey/feeder.go (renamed from cmd/monkey/feeder.go) | 22 | ||||
-rw-r--r-- | cmd/zoo/monkey/gambler.go (renamed from cmd/monkey/gambler.go) | 22 | ||||
-rw-r--r-- | cmd/zoo/monkey/monkey.go (renamed from cmd/monkey/monkey.go) | 79 |
5 files changed, 122 insertions, 35 deletions
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/monkey/banana.go b/cmd/zoo/monkey/banana.go index c776eba47..7ecb53fc9 100644 --- a/cmd/monkey/banana.go +++ b/cmd/zoo/monkey/banana.go @@ -1,4 +1,4 @@ -package main +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/monkey/feeder.go b/cmd/zoo/monkey/feeder.go index 7c1419b91..7a3db3a3a 100644 --- a/cmd/monkey/feeder.go +++ b/cmd/zoo/monkey/feeder.go @@ -15,7 +15,7 @@ // along with the dexon-consensus library. If not, see // <http://www.gnu.org/licenses/>. -package main +package monkey import ( "context" @@ -71,7 +71,7 @@ func (m *Monkey) DistributeBanana(contract common.Address) { time.Sleep(20 * time.Second) } -func (m *Monkey) Feed() { +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()) @@ -80,6 +80,7 @@ func (m *Monkey) Feed() { time.Sleep(5 * time.Second) nonce := uint64(0) +loop: for { fmt.Println("nonce", nonce) ctxs := make([]*transferContext, len(m.keys)) @@ -97,16 +98,27 @@ func (m *Monkey) Feed() { Nonce: nonce, Gas: 42000, } - if *batch { + if config.Batch { ctxs[i] = ctx } else { m.transfer(ctx) } } - if *batch { + if config.Batch { m.batchTransfer(ctxs) } + + if m.timer != nil { + select { + case <-m.timer: + break loop + default: + } + } + nonce += 1 - time.Sleep(time.Duration(*sleep) * time.Millisecond) + time.Sleep(time.Duration(config.Sleep) * time.Millisecond) } + + return nonce } diff --git a/cmd/monkey/gambler.go b/cmd/zoo/monkey/gambler.go index f8e57163c..33f749343 100644 --- a/cmd/monkey/gambler.go +++ b/cmd/zoo/monkey/gambler.go @@ -15,7 +15,7 @@ // along with the dexon-consensus library. If not, see // <http://www.gnu.org/licenses/>. -package main +package monkey import ( "fmt" @@ -45,7 +45,7 @@ func init() { } } -func (m *Monkey) Gamble() { +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()) @@ -71,6 +71,7 @@ func (m *Monkey) Gamble() { } nonce := uint64(0) +loop: for { fmt.Println("nonce", nonce) ctxs := make([]*transferContext, len(m.keys)) @@ -83,16 +84,27 @@ func (m *Monkey) Gamble() { Nonce: nonce, Gas: 210000, } - if *batch { + if config.Batch { ctxs[i] = ctx } else { m.transfer(ctx) } } - if *batch { + if config.Batch { m.batchTransfer(ctxs) } + + if m.timer != nil { + select { + case <-m.timer: + break loop + default: + } + } + nonce += 1 - time.Sleep(time.Duration(*sleep) * time.Millisecond) + time.Sleep(time.Duration(config.Sleep) * time.Millisecond) } + + return nonce } diff --git a/cmd/monkey/monkey.go b/cmd/zoo/monkey/monkey.go index bbf534952..5015a0b09 100644 --- a/cmd/monkey/monkey.go +++ b/cmd/zoo/monkey/monkey.go @@ -17,12 +17,11 @@ // A simple monkey that sends random transactions into the network. -package main +package monkey import ( "context" "crypto/ecdsa" - "flag" "fmt" "math" "math/big" @@ -36,22 +35,32 @@ import ( "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") +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) *Monkey { +func New(ep string, source *ecdsa.PrivateKey, num int, timeout time.Duration) *Monkey { client, err := ethclient.Dial(ep) if err != nil { panic(err) @@ -72,12 +81,18 @@ func New(ep string, source *ecdsa.PrivateKey, num int) *Monkey { panic(err) } - return &Monkey{ + 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 { @@ -232,9 +247,10 @@ func (m *Monkey) Distribute() { time.Sleep(20 * time.Second) } -func (m *Monkey) Crazy() { +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 { @@ -248,37 +264,52 @@ func (m *Monkey) Crazy() { Nonce: nonce, Gas: 21000, } - if *batch { + if config.Batch { ctxs[i] = ctx } else { m.transfer(ctx) } } - if *batch { + 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(*sleep) * time.Millisecond) + time.Sleep(time.Duration(config.Sleep) * time.Millisecond) } + + return nonce } -func main() { - flag.Parse() +func (m *Monkey) Keys() []*ecdsa.PrivateKey { + return m.keys +} - privKey, err := crypto.LoadECDSA(*key) +func Exec() (*Monkey, uint64) { + privKey, err := crypto.LoadECDSA(config.Key) if err != nil { panic(err) } - m := New(*endpoint, privKey, *n) + m := New(config.Endpoint, privKey, config.N, time.Duration(config.Timeout)) m.Distribute() - if *gambler { - m.Gamble() - } else if *feeder { - m.Feed() + var finalNonce uint64 + if config.Gambler { + finalNonce = m.Gamble() + } else if config.Feeder { + finalNonce = m.Feed() } else { - m.Crazy() + finalNonce = m.Crazy() } + + return m, finalNonce } |