tx-spammer/pkg/sender.go

96 lines
2.7 KiB
Go
Raw Normal View History

2020-10-14 15:59:06 +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
// 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
import (
"context"
2020-10-16 03:10:18 +00:00
"fmt"
"sync"
"time"
2020-10-14 15:59:06 +00:00
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
2020-10-16 03:10:18 +00:00
"github.com/sirupsen/logrus"
2020-10-14 15:59:06 +00:00
)
2020-10-16 03:10:18 +00:00
// TxSender type for
2020-10-14 15:59:06 +00:00
type TxSender struct {
2020-10-16 03:10:18 +00:00
TxGen *TxGenerator
TxParams []TxParams
2020-10-14 15:59:06 +00:00
}
2020-10-16 03:10:18 +00:00
// NewTxSender returns a new tx sender
2020-10-14 15:59:06 +00:00
func NewTxSender(params []TxParams) *TxSender {
return &TxSender{
2020-10-16 03:10:18 +00:00
TxGen: NewTxGenerator(params),
TxParams: params,
2020-10-14 15:59:06 +00:00
}
}
2020-10-16 03:10:18 +00:00
func (s *TxSender) Send(quitChan <-chan bool) (<-chan bool, <-chan error) {
// done channel to signal completion of all jobs
doneChan := make(chan bool)
// err channel returned to calling context
2020-10-14 15:59:06 +00:00
errChan := make(chan error)
2020-10-16 03:10:18 +00:00
// for each tx param set, spin up a goroutine to generate and send the tx at the specified delay and frequency
wg := new(sync.WaitGroup)
for _, txParams := range s.TxParams {
wg.Add(1)
go func(p TxParams) {
defer wg.Done()
// send the first tx after the delay
timer := time.NewTimer(p.Delay)
<-timer.C
if err := s.genAndSend(p); err != nil {
errChan <- fmt.Errorf("tx %s initial genAndSend error: %v", p.Name, err)
2020-10-14 17:29:14 +00:00
return
}
2020-10-16 03:10:18 +00:00
// send any remaining ones at the provided frequency, also check for quit signal
ticker := time.NewTicker(p.Frequency)
for i := uint64(1); i < p.TotalNumber; i++ {
select {
case <-ticker.C:
if err := s.genAndSend(p); err != nil {
errChan <- fmt.Errorf("tx %s number %d genAndSend error: %v", p.Name, i, err)
return
}
case <-quitChan:
return
}
2020-10-14 15:59:06 +00:00
}
2020-10-16 03:10:18 +00:00
}(txParams)
}
go func() {
wg.Wait()
close(doneChan)
2020-10-14 15:59:06 +00:00
}()
2020-10-16 03:10:18 +00:00
return doneChan, errChan
}
func (s *TxSender) genAndSend(p TxParams) error {
tx, err := s.TxGen.GenerateTx(p)
if err != nil {
return err
}
return sendRawTransaction(p.Client, tx, p.Name)
2020-10-14 15:59:06 +00:00
}
2020-10-16 03:10:18 +00:00
func sendRawTransaction(rpcClient *rpc.Client, txRlp []byte, name string) error {
logrus.Infof("sending tx %s", name)
2020-10-14 15:59:06 +00:00
return rpcClient.CallContext(context.Background(), nil, "eth_sendRawTransaction", hexutil.Encode(txRlp))
2020-10-14 17:29:14 +00:00
}