Use send batch txs

This commit is contained in:
mark.lin 2017-07-11 15:41:32 +08:00
parent 1822929738
commit 7553121e98
2 changed files with 16 additions and 6 deletions

View File

@ -10,7 +10,7 @@ import random
ADMIN='c921c91aa4c5f9886bd1e084a848e7e564644d5cc2f265beebb0d51bd251b7e7'
ACCS=100
TX_NUM=100
TX_NUM=120
PERIOD=600
NODES=['http://127.0.0.1:8811', 'http://127.0.0.1:8822', 'http://127.0.0.1:8833', 'http://127.0.0.1:8844']

View File

@ -138,6 +138,15 @@ func sendTx(conn *ethclient.Client, tx *types.Transaction) error {
return nil
}
func sendTxs(conn *ethclient.Client, txs []*types.Transaction) error {
err := conn.SendTransactions(context.Background(), txs)
if err != nil {
return err
}
fmt.Println("txs", len(txs))
return nil
}
func sendEther(prv *ecdsa.PrivateKey, to common.Address, amount *big.Int, nonce uint64) (*types.Transaction, error) {
signer := types.FrontierSigner{}
tx := types.NewTransaction(nonce, to, amount, big.NewInt(GAS_LIMIT), big.NewInt(GAS_PRICE), []byte{})
@ -200,19 +209,20 @@ func batchSend(conn *ethclient.Client, prv *ecdsa.PrivateKey, to common.Address,
return
default:
fmt.Println("round", round)
var txs []*types.Transaction
for i := 0; i < count; i++ {
tx, err := sendEther(prv, to, common.Big1, nonce)
if err != nil {
fmt.Printf("Failed to gen tx, err:%v\n", err)
continue
}
err = sendTx(conn, tx)
if err != nil {
fmt.Printf("Failed to send tx, err:%v\n", err)
continue
}
txs = append(txs, tx)
nonce++
}
err := sendTxs(conn, txs)
if err != nil {
fmt.Printf("Failed to send txs, err:%v\n", err)
}
round++
<-time.After(time.Duration(20+rand.Int()%10) * time.Second)
}