tx-spammer/pkg/manual/sender.go

91 lines
2.4 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/>.
2020-10-22 19:12:05 +00:00
package manual
2020-10-14 15:59:06 +00:00
import (
2020-10-16 03:10:18 +00:00
"fmt"
"sync"
"time"
2020-10-14 15:59:06 +00:00
2020-10-23 15:30:33 +00:00
"github.com/sirupsen/logrus"
2020-10-23 15:28:03 +00:00
"github.com/vulcanize/tx_spammer/pkg/shared"
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
}
2020-10-23 15:28:03 +00:00
logrus.Infof("sending tx %s", p.Name)
return shared.SendRawTransaction(p.Client, tx)
2020-10-23 15:30:33 +00:00
}