tx-spammer/pkg/auto/service.go

106 lines
2.7 KiB
Go
Raw Permalink 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
// 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 auto
import (
2020-10-28 16:03:41 +00:00
"fmt"
2020-10-23 15:30:33 +00:00
"github.com/sirupsen/logrus"
2022-12-20 22:56:12 +00:00
"github.com/cerc-io/tx-spammer/pkg/shared"
2020-10-23 15:30:33 +00:00
)
// Spammer underlying struct type for spamming service
type Spammer struct {
2020-10-28 16:03:41 +00:00
Deployer *ContractDeployer
Sender *EthSender
2020-10-23 17:27:49 +00:00
TxGenerator *TxGenerator
2020-10-28 16:03:41 +00:00
config *Config
2020-10-23 15:30:33 +00:00
}
// NewTxSpammer creates a new tx spamming service
func NewTxSpammer(config *Config) shared.Service {
2020-10-23 17:27:49 +00:00
gen := NewTxGenerator(config)
2020-10-23 15:30:33 +00:00
return &Spammer{
2020-10-28 16:03:41 +00:00
Deployer: NewContractDeployer(config, gen),
Sender: NewEthSender(config),
2020-10-23 17:27:49 +00:00
TxGenerator: gen,
2020-10-28 16:03:41 +00:00
config: config,
2020-10-23 15:30:33 +00:00
}
}
2020-10-23 17:27:49 +00:00
func (s *Spammer) Loop(quitChan <-chan bool) (<-chan bool, error) {
2020-10-28 16:03:41 +00:00
contractAddrs, err := s.Deployer.Deploy()
if err != nil {
return nil, fmt.Errorf("contract deployment error: %v", err)
2020-10-23 17:27:49 +00:00
}
2020-10-28 16:03:41 +00:00
genQuit := make(chan bool)
2020-10-23 17:27:49 +00:00
senderQuit := make(chan bool)
2020-10-28 16:03:41 +00:00
doneChan := make(chan bool)
watcher := NewTxWatcher(s.config.EthClient)
watcher.Start()
s.config.CallConfig.ContractAddrs = contractAddrs
genDoneChan, txChan, genErrChan := s.TxGenerator.GenerateTxs(genQuit)
sendDoneChan, sendErrChan := s.Sender.Send(senderQuit, txChan, watcher.PendingTxCh)
2020-10-23 15:30:33 +00:00
go func() {
2020-10-28 16:03:41 +00:00
defer close(doneChan)
2020-10-23 15:30:33 +00:00
for {
select {
2020-10-23 17:27:49 +00:00
case err := <-genErrChan:
2020-10-28 16:03:41 +00:00
logrus.Errorf("tx generation error: %v", err)
recoverClose(genQuit)
2020-10-28 16:03:41 +00:00
<-genDoneChan
recoverClose(senderQuit)
<-sendDoneChan
recoverClose(watcher.quitCh)
2020-10-28 16:03:41 +00:00
case err := <-sendErrChan:
logrus.Errorf("tx sending error: %v", err)
recoverClose(genQuit)
2020-10-28 16:03:41 +00:00
<-genDoneChan
recoverClose(senderQuit)
<-sendDoneChan
recoverClose(watcher.quitCh)
2020-10-23 17:27:49 +00:00
case <-quitChan:
logrus.Info("shutting down tx spammer")
recoverClose(genQuit)
2020-10-28 16:03:41 +00:00
<-genDoneChan
recoverClose(senderQuit)
<-sendDoneChan
recoverClose(watcher.quitCh)
2020-10-28 16:03:41 +00:00
case <-sendDoneChan:
recoverClose(watcher.quitCh)
2020-10-23 15:30:33 +00:00
return
case <-genDoneChan:
recoverClose(senderQuit)
2020-10-23 15:30:33 +00:00
}
}
}()
2020-10-23 17:27:49 +00:00
return doneChan, nil
2020-10-23 15:30:33 +00:00
}
func recoverClose(ch chan bool) (justClosed bool) {
defer func() {
if recover() != nil {
justClosed = false
}
}()
close(ch)
return true
}