tx-spammer/pkg/auto/tx_generator.go

209 lines
6.0 KiB
Go
Raw Normal View History

2020-10-23 15:30:33 +00:00
// 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
2020-10-28 16:03:41 +00:00
// GNU Affero General Public License for more detailgen.
2020-10-23 15:30:33 +00:00
// 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 auto
import (
"context"
2020-10-23 15:30:33 +00:00
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/common/hexutil"
log "github.com/sirupsen/logrus"
2020-10-23 15:30:33 +00:00
"math/big"
"math/rand"
2020-10-28 16:03:41 +00:00
"sync"
"time"
2020-10-23 15:30:33 +00:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/tx_spammer/pkg/shared"
)
// TxGenerator generates and signs txs
type TxGenerator struct {
2020-10-28 16:03:41 +00:00
config *Config
2020-10-23 15:30:33 +00:00
// keep track of account nonces locally so we aren't spamming to determine the nonce
// this assumes these accounts are not sending txs outside this process
nonces map[common.Address]uint64
lock sync.Mutex
}
func (gen *TxGenerator) claimNonce(addr common.Address) uint64 {
gen.lock.Lock()
ret := gen.nonces[addr]
gen.nonces[addr] += 1
gen.lock.Unlock()
return ret
2020-10-23 15:30:33 +00:00
}
// NewTxGenerator creates a new tx generator
2020-10-23 17:27:49 +00:00
func NewTxGenerator(config *Config) *TxGenerator {
nonces := make(map[common.Address]uint64)
2020-10-23 15:30:33 +00:00
for _, addr := range config.SenderAddrs {
nonce, _ := config.EthClient.PendingNonceAt(context.Background(), addr)
nonces[addr] = nonce
2020-10-23 15:30:33 +00:00
}
return &TxGenerator{
2020-10-28 16:03:41 +00:00
nonces: nonces,
config: config,
2020-10-23 15:30:33 +00:00
}
}
// GenParams params for GenerateTx method calls
type GenParams struct {
Sender common.Address
SenderKey *ecdsa.PrivateKey
ChainID *big.Int
GasTipCap *big.Int
GasFeeCap *big.Int
GasLimit uint64
2020-10-23 15:30:33 +00:00
To *common.Address
Amount *big.Int
Data []byte
}
func (gen *TxGenerator) GenerateTxs(quitChan <-chan bool) (<-chan bool, <-chan []byte, <-chan error) {
txChan := make(chan []byte)
2020-10-28 16:03:41 +00:00
errChan := make(chan error)
wg := new(sync.WaitGroup)
for i, sender := range gen.config.SenderKeys {
if len(gen.config.SendConfig.DestinationAddresses) > 0 {
wg.Add(1)
go gen.genSends(wg, txChan, errChan, quitChan, sender, gen.config.SenderAddrs[i], gen.config.SendConfig)
2020-10-28 16:03:41 +00:00
}
if gen.config.CallConfig.TotalNumber > 0 {
2020-10-28 16:03:41 +00:00
wg.Add(1)
go gen.genCalls(wg, txChan, errChan, quitChan, sender, gen.config.SenderAddrs[i], gen.config.CallConfig)
2020-10-28 16:03:41 +00:00
}
}
doneChan := make(chan bool)
go func() {
wg.Wait()
close(doneChan)
}()
return doneChan, txChan, errChan
2020-10-28 16:03:41 +00:00
}
func (gen *TxGenerator) genSends(wg *sync.WaitGroup, txChan chan<- []byte, errChan chan<- error, quitChan <-chan bool, senderKey *ecdsa.PrivateKey, senderAddr common.Address, sendConfig *SendConfig) {
2020-10-28 16:03:41 +00:00
defer wg.Done()
ticker := time.NewTicker(sendConfig.Frequency)
for _, dst := range sendConfig.DestinationAddresses {
select {
case <-ticker.C:
log.Infof("Generating send from %s to %s.", senderAddr.Hex(), dst.Hex())
rawTx, _, err := gen.GenerateTx(&GenParams{
ChainID: sendConfig.ChainID,
To: &dst,
2020-10-28 16:03:41 +00:00
Sender: senderAddr,
SenderKey: senderKey,
GasLimit: sendConfig.GasLimit,
GasFeeCap: sendConfig.GasFeeCap,
GasTipCap: sendConfig.GasTipCap,
2020-10-28 16:03:41 +00:00
Amount: sendConfig.Amount,
})
if err != nil {
errChan <- err
continue
}
txChan <- rawTx
2020-10-28 16:03:41 +00:00
case <-quitChan:
return
}
}
log.Info("Done generating sends for ", senderAddr.Hex())
2020-10-28 16:03:41 +00:00
}
func (gen *TxGenerator) genCalls(wg *sync.WaitGroup, txChan chan<- []byte, errChan chan<- error, quitChan <-chan bool, senderKey *ecdsa.PrivateKey, senderAddr common.Address, callConfig *CallConfig) {
2020-10-28 16:03:41 +00:00
defer wg.Done()
ticker := time.NewTicker(callConfig.Frequency)
for i := 0; i < callConfig.TotalNumber; i++ {
2020-10-28 16:03:41 +00:00
select {
case <-ticker.C:
contractAddr := callConfig.ContractAddrs[rand.Intn(len(callConfig.ContractAddrs))]
log.Infof("Generating call from %s to %s.", senderAddr.Hex(), contractAddr.Hex())
data, err := callConfig.ABI.Pack(callConfig.MethodName, contractAddr, big.NewInt(time.Now().UnixNano()))
2020-10-28 16:03:41 +00:00
if err != nil {
errChan <- err
continue
}
rawTx, _, err := gen.GenerateTx(&GenParams{
2020-10-28 16:03:41 +00:00
Sender: senderAddr,
SenderKey: senderKey,
GasLimit: callConfig.GasLimit,
GasFeeCap: callConfig.GasFeeCap,
GasTipCap: callConfig.GasTipCap,
2020-10-28 16:03:41 +00:00
Data: data,
To: &contractAddr,
2020-10-28 16:03:41 +00:00
})
if err != nil {
errChan <- err
continue
}
txChan <- rawTx
2020-10-28 16:03:41 +00:00
case <-quitChan:
return
}
}
log.Info("Done generating calls for ", senderAddr.Hex())
2020-10-23 15:30:33 +00:00
}
// GenerateTx generates tx from the provided params
func (gen *TxGenerator) GenerateTx(params *GenParams) ([]byte, common.Address, error) {
nonce := gen.claimNonce(params.Sender)
2020-10-23 15:30:33 +00:00
tx := new(types.Transaction)
2020-10-28 16:03:41 +00:00
var contractAddr common.Address
var err error
2020-10-23 15:30:33 +00:00
if params.To == nil {
tx = types.NewTx(
&types.DynamicFeeTx{
ChainID: params.ChainID,
Nonce: nonce,
Gas: params.GasLimit,
GasTipCap: params.GasTipCap,
GasFeeCap: params.GasFeeCap,
To: nil,
Value: params.Amount,
Data: params.Data,
})
contractAddr, err = shared.WriteContractAddr("", params.Sender, nonce)
2020-10-28 16:03:41 +00:00
if err != nil {
return nil, common.Address{}, err
2020-10-23 15:30:33 +00:00
}
} else {
tx = types.NewTx(
&types.DynamicFeeTx{
ChainID: params.ChainID,
Nonce: nonce,
GasTipCap: params.GasTipCap,
GasFeeCap: params.GasFeeCap,
Gas: params.GasLimit,
To: params.To,
Value: params.Amount,
Data: nil,
})
2020-10-23 15:30:33 +00:00
}
2020-10-28 16:03:41 +00:00
signedTx, err := types.SignTx(tx, gen.config.Signer, params.SenderKey)
2020-10-23 15:30:33 +00:00
if err != nil {
2020-10-28 16:03:41 +00:00
return nil, common.Address{}, err
2020-10-23 15:30:33 +00:00
}
data, err := signedTx.MarshalBinary()
2020-10-23 15:30:33 +00:00
if err != nil {
2020-10-28 16:03:41 +00:00
return nil, common.Address{}, err
2020-10-23 15:30:33 +00:00
}
log.Debugf("Generated TX %s with bytes %s", signedTx.Hash().Hex(), hexutil.Encode(data))
return data, contractAddr, err
2020-10-23 15:30:33 +00:00
}