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/>.
2020-10-22 19:12:05 +00:00
package manual
2020-10-14 15:59:06 +00:00
2020-10-16 03:10:18 +00:00
import (
"fmt"
2020-10-22 19:12:05 +00:00
"github.com/vulcanize/tx_spammer/pkg/shared"
2020-10-16 06:07:07 +00:00
"os"
"sync/atomic"
2020-10-16 03:10:18 +00:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
2020-10-16 06:07:07 +00:00
"github.com/ethereum/go-ethereum/crypto"
2020-10-16 03:10:18 +00:00
"github.com/ethereum/go-ethereum/rlp"
)
2020-10-14 15:59:06 +00:00
2020-10-16 06:07:07 +00:00
const (
defaultDeploymentAddrLogPathPrefix = "./accounts/addresses/"
)
2020-10-14 15:59:06 +00:00
// TxGenerator generates and signs txs
type TxGenerator struct {
2020-10-16 03:10:18 +00:00
// keep track of account nonces locally so we aren't spamming to determine the nonce
// this assumes these accounts are not sending txs outside this process
2020-10-16 06:07:07 +00:00
nonces map [ common . Address ] * uint64
2020-10-14 15:59:06 +00:00
}
2020-10-16 03:10:18 +00:00
// NewTxGenerator creates a new tx generator
2020-10-14 15:59:06 +00:00
func NewTxGenerator ( params [ ] TxParams ) * TxGenerator {
2020-10-16 06:07:07 +00:00
nonces := make ( map [ common . Address ] * uint64 )
2020-10-16 03:10:18 +00:00
for _ , p := range params {
2020-10-16 06:07:07 +00:00
nonces [ p . Sender ] = & p . StartingNonce
2020-10-16 03:10:18 +00:00
}
2020-10-14 15:59:06 +00:00
return & TxGenerator {
2020-10-16 03:10:18 +00:00
nonces : nonces ,
2020-10-14 15:59:06 +00:00
}
}
2020-10-16 03:10:18 +00:00
// GenerateTx generates tx from the provided params
func ( tg TxGenerator ) GenerateTx ( params TxParams ) ( [ ] byte , error ) {
tx := make ( [ ] byte , 0 )
switch params . Type {
2020-10-22 19:12:05 +00:00
case shared . Standard , shared . OptimismL1ToL2 , shared . OptimismL2 :
2020-10-16 03:10:18 +00:00
return tg . gen ( params )
2020-10-22 19:12:05 +00:00
case shared . EIP1559 :
2020-10-16 03:10:18 +00:00
return tg . gen1559 ( params )
default :
return nil , fmt . Errorf ( "unsupported tx type: %s" , params . Type . String ( ) )
}
return tx , nil
2020-10-14 15:59:06 +00:00
}
2020-10-16 03:10:18 +00:00
func ( gen TxGenerator ) gen ( params TxParams ) ( [ ] byte , error ) {
2020-10-16 06:07:07 +00:00
nonce := atomic . AddUint64 ( gen . nonces [ params . Sender ] , 1 )
2020-10-16 03:10:18 +00:00
tx := new ( types . Transaction )
if params . To == nil {
tx = types . NewContractCreation ( nonce , params . Amount , params . GasLimit , params . GasPrice , params . Data , params . L1SenderAddr , params . L1RollupTxId , params . QueueOrigin )
2020-10-16 06:07:07 +00:00
if err := writeContractAddr ( params . ContractAddrWritePath , params . Sender , nonce ) ; err != nil {
return nil , err
}
2020-10-16 03:10:18 +00:00
} else {
tx = types . NewTransaction ( nonce , * params . To , params . Amount , params . GasLimit , params . GasPrice , params . Data , params . L1SenderAddr , params . L1RollupTxId , params . QueueOrigin , params . SigHashType )
}
2020-10-22 19:12:05 +00:00
signer , err := shared . TxSigner ( params . Type , params . ChainID )
2020-10-16 03:10:18 +00:00
if err != nil {
return nil , err
}
signedTx , err := types . SignTx ( tx , signer , params . SenderKey )
if err != nil {
return nil , err
}
txRlp , err := rlp . EncodeToBytes ( signedTx )
if err != nil {
return nil , err
}
return txRlp , nil
2020-10-14 15:59:06 +00:00
}
2020-10-16 03:10:18 +00:00
func ( gen TxGenerator ) gen1559 ( params TxParams ) ( [ ] byte , error ) {
// TODO: support EIP1559; new to make a new major version, vendor it, or release with different pkg name so that we can import both optimism and eip1559 geth
return nil , fmt . Errorf ( "1559 support not yet available" )
2020-10-14 17:29:14 +00:00
}
2020-10-16 06:07:07 +00:00
func writeContractAddr ( filePath string , senderAddr common . Address , nonce uint64 ) error {
if filePath == "" {
filePath = defaultDeploymentAddrLogPathPrefix + senderAddr . Hex ( )
}
contractAddr := crypto . CreateAddress ( senderAddr , nonce )
f , err := os . OpenFile ( filePath , os . O_APPEND | os . O_CREATE | os . O_WRONLY , 0644 )
if err != nil {
return err
}
if _ , err := f . WriteString ( contractAddr . Hex ( ) + "\n" ) ; err != nil {
return err
}
return f . Close ( )
}