tx-spammer/pkg/auto/service.go

72 lines
2.1 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
// 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 (
"github.com/sirupsen/logrus"
"github.com/vulcanize/tx_spammer/pkg/shared"
)
// Spammer underlying struct type for spamming service
type Spammer struct {
Deployer *ContractDeployer
Sender *EthSender
2020-10-23 17:27:49 +00:00
TxGenerator *TxGenerator
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-23 17:27:49 +00:00
Deployer: NewContractDeployer(config, gen),
2020-10-23 15:30:33 +00:00
Sender: NewEthSender(config),
2020-10-23 17:27:49 +00:00
TxGenerator: gen,
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) {
if err := s.Deployer.Deploy(); err != nil {
return nil, err
}
senderQuit := make(chan bool)
generatorQuit := make(chan bool)
genDoneChan, txRlpChan, genErrChan := s.TxGenerator.GenerateTxs(generatorQuit)
doneChan, errChan := s.Sender.Send(senderQuit, txRlpChan)
2020-10-23 15:30:33 +00:00
go func() {
for {
select {
2020-10-23 17:27:49 +00:00
case <-genDoneChan:
logrus.Info("all txs have been generated, beginning shut down sequence")
senderQuit <- true
case err := <-genErrChan:
logrus.Error(err)
senderQuit <- true
2020-10-23 15:30:33 +00:00
case err := <-errChan:
logrus.Error(err)
2020-10-23 17:27:49 +00:00
senderQuit <- true // NOTE: sender will close doneChan when it receives a quit signal
case <-quitChan:
senderQuit <- true
case <-doneChan: // NOTE CONT: which will be received here so that this context can close down only once the sender and generator have
generatorQuit <- true
2020-10-23 15:30:33 +00:00
return
}
}
}()
2020-10-23 17:27:49 +00:00
return doneChan, nil
2020-10-23 15:30:33 +00:00
}