tx-spammer/pkg/auto/deployer.go

78 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
2020-10-23 17:27:49 +00:00
import (
"crypto/ecdsa"
"time"
2020-10-23 15:30:33 +00:00
2020-10-23 17:27:49 +00:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rpc"
"github.com/vulcanize/tx_spammer/pkg/shared"
)
const (
contractDeploymentDelay = time.Duration(15) * time.Second
)
// ContractDeployer is responsible for deploying contracts
2020-10-23 15:30:33 +00:00
type ContractDeployer struct {
2020-10-23 17:27:49 +00:00
client *rpc.Client
ty shared.TxType
txGenerator *TxGenerator
senderKeys []*ecdsa.PrivateKey
senderAddrs []common.Address
config *DeploymentConfig
2020-10-23 15:30:33 +00:00
}
2020-10-23 17:27:49 +00:00
// NewContractDeployer returns a new ContractDeployer
func NewContractDeployer(config *Config, gen *TxGenerator) *ContractDeployer {
2020-10-23 15:30:33 +00:00
return &ContractDeployer{
2020-10-23 17:27:49 +00:00
client: config.Client,
ty: config.Type,
txGenerator: gen,
config: config.DeploymentConfig,
senderKeys: config.SenderKeys,
senderAddrs: config.SenderAddrs,
2020-10-23 15:30:33 +00:00
}
}
2020-10-23 17:27:49 +00:00
// Deploy deploys the contracts according to the config provided at construction
2020-10-23 15:30:33 +00:00
func (cp *ContractDeployer) Deploy() error {
2020-10-23 17:27:49 +00:00
ticker := time.NewTicker(contractDeploymentDelay)
for i := uint64(0); i < cp.config.Number; i++ {
<- ticker.C
for i, key := range cp.senderKeys {
txBytes, err := cp.txGenerator.GenerateTx(cp.ty, &GenParams{
Sender: cp.senderAddrs[i],
SenderKey: key,
GasLimit: cp.config.GasLimit,
GasPrice: cp.config.GasPrice,
Data: cp.config.Data,
})
if err != nil {
return err
}
if err := shared.SendRawTransaction(cp.client, txBytes); err != nil {
return err
}
2020-10-23 15:30:33 +00:00
2020-10-23 17:27:49 +00:00
}
}
2020-10-23 15:30:33 +00:00
return nil
}