tx-spammer/pkg/auto/deployer.go

80 lines
2.4 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
2020-10-28 16:03:41 +00:00
ty shared.TxType
2020-10-23 17:27:49 +00:00
txGenerator *TxGenerator
2020-10-28 16:03:41 +00:00
senderKeys []*ecdsa.PrivateKey
2020-10-23 17:27:49 +00:00
senderAddrs []common.Address
2020-10-28 16:03:41 +00:00
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-28 16:03:41 +00:00
client: config.Client,
ty: config.Type,
2020-10-23 17:27:49 +00:00
txGenerator: gen,
2020-10-28 16:03:41 +00:00
config: config.DeploymentConfig,
senderKeys: config.SenderKeys,
2020-10-23 17:27:49 +00:00
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-28 16:03:41 +00:00
func (cp *ContractDeployer) Deploy() ([]common.Address, error) {
contractAddrs := make([]common.Address, 0, cp.config.Number*uint64(len(cp.senderKeys)))
2020-10-23 17:27:49 +00:00
ticker := time.NewTicker(contractDeploymentDelay)
2020-10-28 16:03:41 +00:00
defer ticker.Stop()
2020-10-23 17:27:49 +00:00
for i := uint64(0); i < cp.config.Number; i++ {
2020-10-28 16:03:41 +00:00
<-ticker.C
2020-10-23 17:27:49 +00:00
for i, key := range cp.senderKeys {
2020-10-28 16:03:41 +00:00
txBytes, contractAddr, err := cp.txGenerator.GenerateTx(cp.ty, &GenParams{
Sender: cp.senderAddrs[i],
2020-10-23 17:27:49 +00:00
SenderKey: key,
2020-10-28 16:03:41 +00:00
GasLimit: cp.config.GasLimit,
GasPrice: cp.config.GasPrice,
Data: cp.config.Data,
2020-10-23 17:27:49 +00:00
})
if err != nil {
2020-10-28 16:03:41 +00:00
return nil, err
2020-10-23 17:27:49 +00:00
}
if err := shared.SendRawTransaction(cp.client, txBytes); err != nil {
2020-10-28 16:03:41 +00:00
return nil, err
2020-10-23 17:27:49 +00:00
}
2020-10-28 16:03:41 +00:00
contractAddrs = append(contractAddrs, contractAddr)
2020-10-23 17:27:49 +00:00
}
}
2020-10-28 16:03:41 +00:00
return contractAddrs, nil
2020-10-23 15:30:33 +00:00
}