From da9dd220d03238a6a580954fe61b61647ac9c1a3 Mon Sep 17 00:00:00 2001 From: Ian Norden Date: Fri, 23 Oct 2020 12:27:49 -0500 Subject: [PATCH] top level service; adjustments --- cmd/autoSend.go | 7 ++++-- cmd/sendTxs.go | 5 +++- environments/gen.toml | 4 +-- pkg/auto/config.go | 2 -- pkg/auto/deployer.go | 53 +++++++++++++++++++++++++++++++++++----- pkg/auto/env.go | 6 ----- pkg/auto/sender.go | 2 ++ pkg/auto/service.go | 35 ++++++++++++++++++-------- pkg/auto/tx_generator.go | 9 ++++--- pkg/manual/service.go | 4 +-- pkg/shared/interface.go | 2 +- 11 files changed, 92 insertions(+), 37 deletions(-) diff --git a/cmd/autoSend.go b/cmd/autoSend.go index e7c8dad..19c0afa 100644 --- a/cmd/autoSend.go +++ b/cmd/autoSend.go @@ -45,7 +45,10 @@ func autoSend() { } txSpammer := auto.NewTxSpammer(config) quitChan := make(chan bool) - doneChan := txSpammer.Loop(quitChan) + doneChan, err := txSpammer.Loop(quitChan) + if err != nil { + logWithCommand.Fatal(err) + } go func() { shutdown := make(chan os.Signal) @@ -54,8 +57,8 @@ func autoSend() { close(quitChan) }() <-doneChan - } + func init() { rootCmd.AddCommand(autoSendCmd) } \ No newline at end of file diff --git a/cmd/sendTxs.go b/cmd/sendTxs.go index dbd6f8f..e8428a0 100644 --- a/cmd/sendTxs.go +++ b/cmd/sendTxs.go @@ -46,7 +46,10 @@ func sendTxs() { } txSpammer := manual.NewTxSpammer(params) quitChan := make(chan bool) - doneChan := txSpammer.Loop(quitChan) + doneChan, err := txSpammer.Loop(quitChan) + if err != nil { + logWithCommand.Fatal(err) + } go func() { shutdown := make(chan os.Signal) diff --git a/environments/gen.toml b/environments/gen.toml index 79b5485..c23b447 100644 --- a/environments/gen.toml +++ b/environments/gen.toml @@ -21,12 +21,10 @@ frequency = 30 # how often to send a transaction (in seconds) - env: $ETH_CALL_FREQ totalNumber = 10000 # total number of transactions to send (across all senders) - env: $ETH_CALL_TOTAL_NUMBER abiPath = "" # path to the abi file for the contract we are calling - env: $ETH_CALL_ABI_PATH - # NOTE: we expect to be calling a method such as Put(uint256 pos, uint256 val) where the first argument is an + # NOTE: we expect to be calling a method such as Put(address addr, uint256 val) where the first argument is an # integer than we can increment to store values at new locations in the contract trie (to grow it) and # the second argument is an integer value that we store at these positions methodName = "Put" # the method name we are calling - env: $ETH_CALL_METHOD_NAME - positionStart = 0 # the starting value for the first argument - env: $ETH_CALL_POSITION_START - positionEnd = 1000 # the maximum value for the first argument - env: $ETH_CALL_POSITION_END storageValue = 1337 # the value we store at each position - env: $ETH_CALL_STORAGE_VALUE gasLimit = 0 # gasLimit to use for the eth call txs - env: $ETH_CALL_GAS_LIMIT gasPrice = "0" # gasPrice to use for the eth call txs - env: $ETH_CALL_GAS_PRICE diff --git a/pkg/auto/config.go b/pkg/auto/config.go index 5f9a037..9075827 100644 --- a/pkg/auto/config.go +++ b/pkg/auto/config.go @@ -284,8 +284,6 @@ func NewCallConfig() (*CallConfig, error) { GasLimit: viper.GetUint64(ethCallGasLimit), MethodName: methodName, ABI: parsedABI, - PositionEnd: viper.GetUint64(ethCallPositionEnd), - PositionStart: viper.GetUint64(ethCallPositionStart), StorageValue: viper.GetUint64(ethCallStorageValue), Frequency: viper.GetDuration(ethCallFrequency), }, nil diff --git a/pkg/auto/deployer.go b/pkg/auto/deployer.go index d281de6..536b27b 100644 --- a/pkg/auto/deployer.go +++ b/pkg/auto/deployer.go @@ -16,21 +16,62 @@ package auto -import "crypto/ecdsa" +import ( + "crypto/ecdsa" + "time" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rpc" + "github.com/vulcanize/tx_spammer/pkg/shared" +) + +const ( + contractDeploymentDelay = time.Duration(15) * time.Second +) + +// ContractDeployer is responsible for deploying contracts type ContractDeployer struct { - SenderKeys []*ecdsa.PrivateKey - Config *DeploymentConfig + client *rpc.Client + ty shared.TxType + txGenerator *TxGenerator + senderKeys []*ecdsa.PrivateKey + senderAddrs []common.Address + config *DeploymentConfig } -func NewContractDeployer(config *Config) *ContractDeployer { +// NewContractDeployer returns a new ContractDeployer +func NewContractDeployer(config *Config, gen *TxGenerator) *ContractDeployer { return &ContractDeployer{ - Config: config.DeploymentConfig, - SenderKeys: config.SenderKeys, + client: config.Client, + ty: config.Type, + txGenerator: gen, + config: config.DeploymentConfig, + senderKeys: config.SenderKeys, + senderAddrs: config.SenderAddrs, } } +// Deploy deploys the contracts according to the config provided at construction func (cp *ContractDeployer) Deploy() error { + ticker := time.NewTicker(contractDeploymentDelay) + for i := uint64(0); i < cp.config.Number; i++ { + <- ticker.C + for i, key := range cp.senderKeys { + txBytes, err := cp.txGenerator.GenerateTx(cp.ty, &GenParams{ + Sender: cp.senderAddrs[i], + SenderKey: key, + GasLimit: cp.config.GasLimit, + GasPrice: cp.config.GasPrice, + Data: cp.config.Data, + }) + if err != nil { + return err + } + if err := shared.SendRawTransaction(cp.client, txBytes); err != nil { + return err + } + } + } return nil } diff --git a/pkg/auto/env.go b/pkg/auto/env.go index d783706..344bf2c 100644 --- a/pkg/auto/env.go +++ b/pkg/auto/env.go @@ -40,8 +40,6 @@ const ( ETH_CALL_TOTAL_NUMBER = "ETH_CALL_TOTAL_NUMBER" ETH_CALL_ABI_PATH = "ETH_CALL_ABI_PATH" ETH_CALL_METHOD_NAME = "ETH_CALL_METHOD_NAME" - ETH_CALL_POSITION_START = "ETH_CALL_POSITION_START" - ETH_CALL_POSITION_END = "ETH_CALL_POSITION_END" ETH_CALL_STORAGE_VALUE = "ETH_CALL_STORAGE_VALUE" ETH_CALL_GAS_LIMIT = "ETH_CALL_GAS_LIMIT" ETH_CALL_GAS_PRICE = "ETH_CALL_GAS_PRICE" @@ -73,8 +71,6 @@ const ( ethCallTotalNumber = "contractSpammer.totalNumber" ethCallABIPath = "contractSpammer.abiPath" ethCallMethodName = "contractSpammer.methodName" - ethCallPositionStart = "contractSpammer.positionStart" - ethCallPositionEnd = "contractSpammer.positionEnd" ethCallStorageValue = "contractSpammer.storageValue" ethCallGasLimit = "contractSpammer.gasLimit" ethCallGasPrice = "contractSpammer.gasPrice" @@ -108,8 +104,6 @@ func bindEnv() { viper.BindEnv(ethCallGasLimit, ETH_CALL_GAS_LIMIT) viper.BindEnv(ethCallGasPrice, ETH_CALL_GAS_PRICE) viper.BindEnv(ethCallMethodName, ETH_CALL_METHOD_NAME) - viper.BindEnv(ethCallPositionEnd, ETH_CALL_POSITION_END) - viper.BindEnv(ethCallPositionStart, ETH_CALL_POSITION_START) viper.BindEnv(ethCallStorageValue, ETH_CALL_STORAGE_VALUE) viper.BindEnv(ethCallTotalNumber, ETH_CALL_TOTAL_NUMBER) diff --git a/pkg/auto/sender.go b/pkg/auto/sender.go index 36e85de..9a10667 100644 --- a/pkg/auto/sender.go +++ b/pkg/auto/sender.go @@ -18,6 +18,7 @@ package auto import ( "github.com/ethereum/go-ethereum/rpc" + "github.com/sirupsen/logrus" "github.com/vulcanize/tx_spammer/pkg/shared" ) @@ -47,6 +48,7 @@ func (s *EthSender) Send(quitChan <-chan bool, txRlpChan <-chan []byte) (<-chan errChan <- err } case <-quitChan: + logrus.Info("quitting the Send loop") close(doneChan) return } diff --git a/pkg/auto/service.go b/pkg/auto/service.go index f99ee21..7fbad57 100644 --- a/pkg/auto/service.go +++ b/pkg/auto/service.go @@ -24,33 +24,48 @@ import ( // Spammer underlying struct type for spamming service type Spammer struct { Deployer *ContractDeployer - Caller *ContractCaller Sender *EthSender + TxGenerator *TxGenerator } // NewTxSpammer creates a new tx spamming service func NewTxSpammer(config *Config) shared.Service { + gen := NewTxGenerator(config) return &Spammer{ - Deployer: NewContractDeployer(config), - Caller: NewContractCaller(config), + Deployer: NewContractDeployer(config, gen), Sender: NewEthSender(config), + TxGenerator: gen, } } -func (s *Spammer) Loop(quitChan <-chan bool) <-chan bool { - forwardQuit := make(chan bool) - doneChan, errChan := s.Sender.Send(forwardQuit) +func (s *Spammer) Loop(quitChan <-chan bool) (<-chan bool, error) { + if err := s.Deployer.Deploy(); err != nil { + return nil, err + } + senderQuit := make(chan bool) + generatorQuit := make(chan bool) + genDoneChan, txRlpChan, genErrChan := s.TxGenerator.GenerateTxs(generatorQuit) + + doneChan, errChan := s.Sender.Send(senderQuit, txRlpChan) go func() { for { select { + case <-genDoneChan: + logrus.Info("all txs have been generated, beginning shut down sequence") + senderQuit <- true + case err := <-genErrChan: + logrus.Error(err) + senderQuit <- true case err := <-errChan: logrus.Error(err) - case forwardQuit <- <-quitChan: - return - case <-doneChan: + senderQuit <- true // NOTE: sender will close doneChan when it receives a quit signal + case <-quitChan: + senderQuit <- true + case <-doneChan: // NOTE CONT: which will be received here so that this context can close down only once the sender and generator have + generatorQuit <- true return } } }() - return doneChan + return doneChan, nil } diff --git a/pkg/auto/tx_generator.go b/pkg/auto/tx_generator.go index 7f626b7..a88ba94 100644 --- a/pkg/auto/tx_generator.go +++ b/pkg/auto/tx_generator.go @@ -40,7 +40,7 @@ type TxGenerator struct { } // NewTxGenerator creates a new tx generator -func NewTxGenerator(config Config) *TxGenerator { +func NewTxGenerator(config *Config) *TxGenerator { nonces := make(map[common.Address]*uint64) for _, addr := range config.SenderAddrs { startingNonce := uint64(0) @@ -56,7 +56,6 @@ func NewTxGenerator(config Config) *TxGenerator { // GenParams params for GenerateTx method calls type GenParams struct { - Signer types.Signer Sender common.Address SenderKey *ecdsa.PrivateKey To *common.Address @@ -66,8 +65,10 @@ type GenParams struct { Data []byte } +// GenerateTxs loops and generates txs according the configuration passed in during construction func (tg TxGenerator) GenerateTxs(quitChan <-chan bool) (<-chan bool, <-chan []byte, <-chan error) { - + // TODO: this + return nil, nil, nil } // GenerateTx generates tx from the provided params @@ -98,7 +99,7 @@ func (gen TxGenerator) genL2(params *GenParams, op *OptimismConfig) ([]byte, err } else { tx = types.NewTransaction(nonce, *params.To, params.Amount, params.GasLimit, params.GasPrice, params.Data, op.L1SenderAddr, op.L1RollupTxId, op.QueueOrigin, op.SigHashType) } - signedTx, err := types.SignTx(tx, params.Signer, params.SenderKey) + signedTx, err := types.SignTx(tx, gen.signer, params.SenderKey) if err != nil { return nil, err } diff --git a/pkg/manual/service.go b/pkg/manual/service.go index a512b7f..78713db 100644 --- a/pkg/manual/service.go +++ b/pkg/manual/service.go @@ -31,7 +31,7 @@ func NewTxSpammer(params []TxParams) shared.Service { } } -func (s *Spammer) Loop(quitChan <-chan bool) <-chan bool { +func (s *Spammer) Loop(quitChan <-chan bool) (<-chan bool, error) { forwardQuit := make(chan bool) doneChan, errChan := s.Sender.Send(forwardQuit) go func() { @@ -46,5 +46,5 @@ func (s *Spammer) Loop(quitChan <-chan bool) <-chan bool { } } }() - return doneChan + return doneChan, nil } diff --git a/pkg/shared/interface.go b/pkg/shared/interface.go index c0cef64..5f7a1dd 100644 --- a/pkg/shared/interface.go +++ b/pkg/shared/interface.go @@ -18,5 +18,5 @@ package shared // Service looping interface type Service interface { - Loop(quitChan <-chan bool) (doneChan <-chan bool) + Loop(quitChan <-chan bool) (doneChan <-chan bool, err error) }