service and command

This commit is contained in:
Ian Norden 2020-10-14 12:29:14 -05:00
parent 582296fccd
commit e441254891
9 changed files with 144 additions and 150 deletions

View File

@ -16,36 +16,42 @@
package cmd package cmd
import ( import (
"fmt" "os"
"os/signal"
"sync"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/vulcanize/tx_spammer/pkg"
) )
// sendTxsCmd represents the sendTxs command // sendTxsCmd represents the sendTxs command
var sendTxsCmd = &cobra.Command{ var sendTxsCmd = &cobra.Command{
Use: "sendTxs", Use: "sendTxs",
Short: "A brief description of your command", Short: "send large volumes of different tx types to different nodes",
Long: `A longer description that spans multiple lines and likely contains examples Long: `Loads tx configuration from .toml config file
and usage of using your command. For example: Generates txs from configuration and sends them to designated node according to set frequency and number`,
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Println("sendTxs called") sendTxs()
}, },
} }
func sendTxs() {
params, err := tx_spammer.NewTxParams()
if err != nil {
logWithCommand.Fatal(err)
}
txSpammer := tx_spammer.NewTxSpammer(params)
wg := new(sync.WaitGroup)
quitChan := make(chan bool)
txSpammer.Loop(wg, quitChan)
shutdown := make(chan os.Signal)
signal.Notify(shutdown, os.Interrupt)
<-shutdown
close(quitChan)
wg.Wait()
}
func init() { func init() {
rootCmd.AddCommand(sendTxsCmd) rootCmd.AddCommand(sendTxsCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// sendTxsCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// sendTxsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }

View File

@ -1,5 +1,5 @@
[eth] [eth]
txs = ["L2ContractPutCall", "L2ContractGetCall"] txs = ["L2ContractDeployment", "L2ContractPutCall", "L2ContractGetCall"]
[L2ContractDeployment] [L2ContractDeployment]
type = "L2" type = "L2"
@ -11,8 +11,8 @@
data = "" data = ""
senderKeyPath = "" senderKeyPath = ""
writeSenderPath = "" writeSenderPath = ""
frequency = 15 frequency = 1
totalNumber = 1500 totalNumber = 1
chainID = 420 chainID = 420
[L2ContractPutCall] [L2ContractPutCall]

View File

@ -19,58 +19,10 @@ package tx_spammer
import ( import (
"context" "context"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
// SendTxArgs represents the arguments to submit a transaction
type SendTxArgs struct {
From common.MixedcaseAddress `json:"from"`
To *common.MixedcaseAddress `json:"to"`
Gas hexutil.Uint64 `json:"gas"`
GasPrice hexutil.Big `json:"gasPrice"`
Value hexutil.Big `json:"value"`
Nonce hexutil.Uint64 `json:"nonce"`
// We accept "data" and "input" for backwards-compatibility reasons.
Data *hexutil.Bytes `json:"data"`
Input *hexutil.Bytes `json:"input,omitempty"`
}
/*
// SendTransaction creates a transaction for the given argument, sign it and submit it to the
// transaction pool.
func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args SendTxArgs) (common.Hash, error) {
// Look up the wallet containing the requested signer
account := accounts.Account{Address: args.From}
wallet, err := s.b.AccountManager().Find(account)
if err != nil {
return common.Hash{}, err
}
if args.Nonce == nil {
// Hold the addresse's mutex around signing to prevent concurrent assignment of
// the same nonce to multiple accounts.
s.nonceLock.LockAddr(args.From)
defer s.nonceLock.UnlockAddr(args.From)
}
// Set some sanity defaults and terminate on failure
if err := args.setDefaults(ctx, s.b); err != nil {
return common.Hash{}, err
}
// Assemble the transaction and sign with the wallet
tx := args.toTransaction()
signed, err := wallet.SignTx(account, tx, s.b.ChainConfig().ChainID)
if err != nil {
return common.Hash{}, err
}
return SubmitTransaction(ctx, s.b, signed)
}
*/
type TxSender struct { type TxSender struct {
TxGen *TxGenerator TxGen *TxGenerator
} }
@ -80,10 +32,15 @@ func NewTxSender(params []TxParams) *TxSender {
TxGen: NewTxGenerator(params), TxGen: NewTxGenerator(params),
} }
} }
func (s *TxSender) Send() <-chan error { func (s *TxSender) Send(quitChan <-chan bool) <-chan error {
errChan := make(chan error) errChan := make(chan error)
go func() { go func() {
for s.TxGen.Next() { for s.TxGen.Next() {
select {
case <-quitChan:
return
default:
}
if err := sendRawTransaction(s.TxGen.Current()); err != nil { if err := sendRawTransaction(s.TxGen.Current()); err != nil {
errChan <- err errChan <- err
} }
@ -92,6 +49,7 @@ func (s *TxSender) Send() <-chan error {
errChan <- s.TxGen.Error() errChan <- s.TxGen.Error()
} }
}() }()
return errChan
} }
func sendRawTransaction(rpcClient *rpc.Client, txRlp []byte) error { func sendRawTransaction(rpcClient *rpc.Client, txRlp []byte) error {

View File

@ -16,19 +16,39 @@
package tx_spammer package tx_spammer
import "sync" import (
"sync"
"github.com/sirupsen/logrus"
)
type Service interface { type Service interface {
Loop(wg *sync.WaitGroup) error Loop(wg *sync.WaitGroup, quitChan <-chan bool)
} }
type Tx struct { type Spammer struct {
Spammer *Sender Sender *TxSender
Generator *TxGenerator
Config *Config
} }
func NewTxSpammer(params []TxParams) (TxSpammer, error) { func NewTxSpammer(params []TxParams) Service {
return &Spammer{
return &txSpammer{}, nil Sender: NewTxSender(params),
}
}
func (s *Spammer) Loop(wg *sync.WaitGroup, quitChan <-chan bool) {
forwardQuit := make(chan bool)
errChan := s.Sender.Send(forwardQuit)
go func() {
wg.Add(1)
defer wg.Done()
for {
select {
case err := <-errChan:
logrus.Error(err)
case forwardQuit <- <-quitChan:
return
}
}
}()
} }

View File

View File

@ -1,10 +1,27 @@
// VulcanizeDB
// Copyright © 2020 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program 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 Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package tx_spammer package tx_spammer
import ( import (
"fmt" "fmt"
"math/big"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"math/big"
) )
// ChainConfig returns the appropriate ethereum chain config for the provided chain id // ChainConfig returns the appropriate ethereum chain config for the provided chain id
@ -16,9 +33,8 @@ func ChainConfig(chainID uint64) (*params.ChainConfig, error) {
return params.TestnetChainConfig, nil // Ropsten return params.TestnetChainConfig, nil // Ropsten
case 4: case 4:
return params.RinkebyChainConfig, nil return params.RinkebyChainConfig, nil
case 5: case 5, 420:
return params.GoerliChainConfig, nil return params.GoerliChainConfig, nil
case 420:
default: default:
return nil, fmt.Errorf("chain config for chainid %d not available", chainID) return nil, fmt.Errorf("chain config for chainid %d not available", chainID)
} }
@ -27,16 +43,10 @@ func ChainConfig(chainID uint64) (*params.ChainConfig, error) {
// ChainConfig returns the appropriate ethereum chain config for the provided chain id // ChainConfig returns the appropriate ethereum chain config for the provided chain id
func TxSigner(chainID uint64) (types.Signer, error) { func TxSigner(chainID uint64) (types.Signer, error) {
switch chainID { switch chainID {
case 1: case 1, 3, 4, 5:
return params.MainnetChainConfig, nil return types.NewEIP155Signer(new(big.Int).SetUint64(chainID)), nil
case 3:
return params.TestnetChainConfig, nil // Ropsten
case 4:
return params.RinkebyChainConfig, nil
case 5:
return params.GoerliChainConfig, nil
case 420: case 420:
return types.NewOVMSigner(big.NewInt()), nil return types.NewOVMSigner(new(big.Int).SetUint64(chainID)), nil
default: default:
return nil, fmt.Errorf("chain config for chainid %d not available", chainID) return nil, fmt.Errorf("chain config for chainid %d not available", chainID)
} }