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/>.
|
|
|
|
|
|
|
|
package tx_spammer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-10-16 03:10:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2020-10-14 15:59:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TxParams struct {
|
|
|
|
// Name of this tx in the .toml file
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// HTTP Client for this tx type
|
|
|
|
Client *rpc.Client
|
|
|
|
|
|
|
|
// Type of the tx
|
|
|
|
Type TxType
|
|
|
|
|
2020-10-16 03:10:18 +00:00
|
|
|
// Chain ID
|
|
|
|
ChainID uint64
|
|
|
|
|
2020-10-14 15:59:06 +00:00
|
|
|
// Universal tx fields
|
2020-10-14 17:29:14 +00:00
|
|
|
To *common.Address
|
2020-10-14 15:59:06 +00:00
|
|
|
GasLimit uint64
|
|
|
|
GasPrice *big.Int // nil if eip1559
|
2020-10-14 17:29:14 +00:00
|
|
|
Amount *big.Int
|
|
|
|
Data []byte
|
|
|
|
Sender common.Address
|
2020-10-14 15:59:06 +00:00
|
|
|
|
|
|
|
// Optimism-specific metadata fields
|
|
|
|
L1SenderAddr *common.Address
|
2020-10-16 03:10:18 +00:00
|
|
|
L1RollupTxId *hexutil.Uint64
|
|
|
|
SigHashType types.SignatureHashType
|
|
|
|
QueueOrigin types.QueueOrigin
|
2020-10-14 15:59:06 +00:00
|
|
|
|
|
|
|
// EIP1559-specific fields
|
|
|
|
GasPremium *big.Int
|
2020-10-14 17:29:14 +00:00
|
|
|
FeeCap *big.Int
|
2020-10-14 15:59:06 +00:00
|
|
|
|
|
|
|
// Sender key, if left the senderKeyPath is empty we generate a new key
|
2020-10-16 03:10:18 +00:00
|
|
|
SenderKey *ecdsa.PrivateKey
|
|
|
|
StartingNonce uint64
|
2020-10-14 15:59:06 +00:00
|
|
|
|
|
|
|
// Sending params
|
|
|
|
// How often we send a tx of this type
|
|
|
|
Frequency time.Duration
|
|
|
|
// Total number of txs of this type to send
|
|
|
|
TotalNumber uint64
|
2020-10-16 03:10:18 +00:00
|
|
|
// Delay before beginning to send
|
|
|
|
Delay time.Duration
|
2020-10-14 15:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
ETH_TX_LIST = "ETH_TX_LIST"
|
|
|
|
|
2020-10-14 17:29:14 +00:00
|
|
|
typeSuffix = ".type"
|
|
|
|
httpPathSuffix = ".http"
|
|
|
|
toSuffix = ".to"
|
|
|
|
amountSuffix = ".amount"
|
|
|
|
gasLimitSuffix = ".gasLimit"
|
|
|
|
gasPriceSuffix = ".gasPrice"
|
|
|
|
gasPremiumSuffix = ".gasPremium"
|
|
|
|
feeCapSuffix = ".feeCap"
|
|
|
|
dataSuffix = ".data"
|
|
|
|
senderKeyPathSuffix = ".senderKeyPath"
|
2020-10-14 15:59:06 +00:00
|
|
|
writeSenderPathSuffix = ".writeSenderPath"
|
2020-10-14 17:29:14 +00:00
|
|
|
l1SenderSuffix = ".l1Sender"
|
|
|
|
l1RollupTxIdSuffix = ".l1RollupTxId"
|
|
|
|
sigHashTypeSuffix = ".sigHashType"
|
|
|
|
frequencySuffix = ".frequency"
|
|
|
|
totalNumberSuffix = ".totalNumber"
|
2020-10-16 03:10:18 +00:00
|
|
|
delaySuffix = ".delay"
|
|
|
|
startingNonceSuffix = ".startingNonce"
|
|
|
|
queueOriginSuffix = ".queueOrigin"
|
|
|
|
chainIDSuffix = ".chainID"
|
2020-10-14 15:59:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewConfig returns a new tx spammer config
|
|
|
|
func NewTxParams() ([]TxParams, error) {
|
|
|
|
viper.BindEnv("eth.txs", ETH_TX_LIST)
|
|
|
|
|
|
|
|
txs := viper.GetStringSlice("eth.txs")
|
|
|
|
txParams := make([]TxParams, len(txs))
|
|
|
|
for i, txName := range txs {
|
|
|
|
// Get http client
|
2020-10-14 17:29:14 +00:00
|
|
|
httpPathStr := viper.GetString(txName + httpPathSuffix)
|
2020-10-14 15:59:06 +00:00
|
|
|
if httpPathStr == "" {
|
|
|
|
return nil, fmt.Errorf("tx %s is missing an httpPath", txName)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(httpPathStr, "http://") {
|
|
|
|
httpPathStr = "http://" + httpPathStr
|
|
|
|
}
|
|
|
|
rpcClient, err := rpc.Dial(httpPathStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-16 03:10:18 +00:00
|
|
|
// Get tx type and chain id
|
2020-10-14 17:29:14 +00:00
|
|
|
txTypeStr := viper.GetString(txName + typeSuffix)
|
2020-10-14 15:59:06 +00:00
|
|
|
if txTypeStr == "" {
|
|
|
|
return nil, fmt.Errorf("need tx type for tx %s", txName)
|
|
|
|
}
|
|
|
|
txType, err := TxTypeFromString(txTypeStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-16 03:10:18 +00:00
|
|
|
chainID := viper.GetUint64(txName + chainIDSuffix)
|
2020-10-14 15:59:06 +00:00
|
|
|
|
|
|
|
// Get basic fields
|
2020-10-14 17:29:14 +00:00
|
|
|
toStr := viper.GetString(txName + toSuffix)
|
2020-10-14 15:59:06 +00:00
|
|
|
var toAddr *common.Address
|
|
|
|
if toStr != "" {
|
|
|
|
to := common.HexToAddress(toStr)
|
|
|
|
toAddr = &to
|
|
|
|
}
|
2020-10-14 17:29:14 +00:00
|
|
|
amountStr := viper.GetString(txName + amountSuffix)
|
2020-10-14 15:59:06 +00:00
|
|
|
amount := new(big.Int)
|
|
|
|
if amountStr != "" {
|
|
|
|
if _, ok := amount.SetString(amountStr, 10); !ok {
|
|
|
|
return nil, fmt.Errorf("amount (%s) for tx %s is not valid", amountStr, txName)
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 17:29:14 +00:00
|
|
|
gasPriceStr := viper.GetString(txName + gasPriceSuffix)
|
2020-10-14 15:59:06 +00:00
|
|
|
var gasPrice *big.Int
|
|
|
|
if gasPriceStr != "" {
|
|
|
|
gasPrice = new(big.Int)
|
|
|
|
if _, ok := gasPrice.SetString(gasPriceStr, 10); !ok {
|
|
|
|
return nil, fmt.Errorf("gasPrice (%s) for tx %s is not valid", gasPriceStr, txName)
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 17:29:14 +00:00
|
|
|
gasLimit := viper.GetUint64(txName + gasLimitSuffix)
|
|
|
|
hex := viper.GetString(txName + dataSuffix)
|
2020-10-14 15:59:06 +00:00
|
|
|
data := make([]byte, 0)
|
|
|
|
if hex != "" {
|
|
|
|
data = common.Hex2Bytes(hex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load or generate sender key
|
2020-10-14 17:29:14 +00:00
|
|
|
senderKeyPath := viper.GetString(txName + senderKeyPathSuffix)
|
2020-10-14 15:59:06 +00:00
|
|
|
var key *ecdsa.PrivateKey
|
|
|
|
if senderKeyPath != "" {
|
|
|
|
key, err = crypto.LoadECDSA(senderKeyPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to load ecdsa at %s key for tx %s", senderKeyPath, txName)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
key, err = crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to generate ecdsa key for tx %s", txName)
|
|
|
|
}
|
2020-10-14 17:29:14 +00:00
|
|
|
writePath := viper.GetString(txName + writeSenderPathSuffix)
|
2020-10-14 15:59:06 +00:00
|
|
|
if writePath != "" {
|
|
|
|
if err := crypto.SaveECDSA(writePath, key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to load Optimism fields
|
2020-10-14 17:29:14 +00:00
|
|
|
l1SenderStr := viper.GetString(txName + l1SenderSuffix)
|
2020-10-14 15:59:06 +00:00
|
|
|
var l1Sender *common.Address
|
|
|
|
if l1SenderStr != "" {
|
|
|
|
sender := common.HexToAddress(l1SenderStr)
|
|
|
|
l1Sender = &sender
|
|
|
|
}
|
2020-10-14 17:29:14 +00:00
|
|
|
l1RollupTxId := viper.GetUint64(txName + l1RollupTxIdSuffix)
|
2020-10-16 03:10:18 +00:00
|
|
|
l1rtid := (hexutil.Uint64)(l1RollupTxId)
|
2020-10-14 17:29:14 +00:00
|
|
|
sigHashType := viper.GetUint(txName + sigHashTypeSuffix)
|
2020-10-16 03:10:18 +00:00
|
|
|
queueOrigin := viper.GetInt64(txName + queueOriginSuffix)
|
2020-10-14 15:59:06 +00:00
|
|
|
|
|
|
|
// If gasPrice was empty, attempt to load EIP1559 fields
|
|
|
|
var feeCap, gasPremium *big.Int
|
|
|
|
if gasPrice == nil {
|
2020-10-14 17:29:14 +00:00
|
|
|
feeCapStr := viper.GetString(txName + feeCapSuffix)
|
|
|
|
gasPremiumString := viper.GetString(txName + gasPremiumSuffix)
|
2020-10-14 15:59:06 +00:00
|
|
|
if feeCapStr == "" {
|
|
|
|
return nil, fmt.Errorf("tx %s is missing feeCapStr", txName)
|
|
|
|
}
|
2020-10-14 17:29:14 +00:00
|
|
|
if gasPremiumString == "" {
|
2020-10-14 15:59:06 +00:00
|
|
|
return nil, fmt.Errorf("tx %s is missing gasPremiumStr", txName)
|
|
|
|
}
|
|
|
|
feeCap = new(big.Int)
|
|
|
|
gasPremium = new(big.Int)
|
|
|
|
if _, ok := feeCap.SetString(feeCapStr, 10); !ok {
|
|
|
|
return nil, fmt.Errorf("unable to set feeCap to %s for tx %s", feeCapStr, txName)
|
|
|
|
}
|
|
|
|
if _, ok := gasPremium.SetString(gasPremiumString, 10); !ok {
|
|
|
|
return nil, fmt.Errorf("unable to set gasPremium to %s for tx %s", gasPremiumString, txName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 03:10:18 +00:00
|
|
|
// Load starting nonce and sending params
|
|
|
|
startingNonce := viper.GetUint64(txName + startingNonceSuffix)
|
2020-10-14 17:29:14 +00:00
|
|
|
frequency := viper.GetDuration(txName + frequencySuffix)
|
|
|
|
totalNumber := viper.GetUint64(txName + totalNumberSuffix)
|
2020-10-16 03:10:18 +00:00
|
|
|
delay := viper.GetDuration(txName + delaySuffix)
|
2020-10-14 15:59:06 +00:00
|
|
|
|
|
|
|
txParams[i] = TxParams{
|
2020-10-16 03:10:18 +00:00
|
|
|
Client: rpcClient,
|
|
|
|
Type: txType,
|
|
|
|
Name: txName,
|
|
|
|
To: toAddr,
|
|
|
|
Amount: amount,
|
|
|
|
GasLimit: gasLimit,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
GasPremium: gasPremium,
|
|
|
|
FeeCap: feeCap,
|
|
|
|
Data: data,
|
|
|
|
L1SenderAddr: l1Sender,
|
|
|
|
L1RollupTxId: &l1rtid,
|
|
|
|
SigHashType: (types.SignatureHashType)(uint8(sigHashType)),
|
|
|
|
Frequency: frequency,
|
|
|
|
TotalNumber: totalNumber,
|
|
|
|
Delay: delay,
|
|
|
|
StartingNonce: startingNonce,
|
|
|
|
QueueOrigin: (types.QueueOrigin)(queueOrigin),
|
|
|
|
ChainID: chainID,
|
2020-10-14 15:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return txParams, nil
|
|
|
|
}
|