cmd/devp2p/internal/ethtest: add more tx propagation tests (#22630)

This adds a test for large tx announcement messages, as well as a test to
check that announced tx hashes are requested by the node.
This commit is contained in:
rene
2021-04-23 18:14:39 +02:00
committed by GitHub
parent 49281ab84f
commit cac1b21d39
5 changed files with 278 additions and 58 deletions

View File

@ -17,12 +17,15 @@
package ethtest
import (
"math/big"
"strings"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/internal/utesting"
"github.com/ethereum/go-ethereum/params"
)
//var faucetAddr = common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7")
@ -40,7 +43,9 @@ func sendSuccessfulTxWithConn(t *utesting.T, s *Suite, tx *types.Transaction, se
if err := sendConn.Write(&Transactions{tx}); err != nil {
t.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
// update last nonce seen
nonce = tx.Nonce()
recvConn := s.setupConnection(t)
// Wait for the transaction announcement
switch msg := recvConn.ReadAndServe(s.chain, timeout).(type) {
@ -66,6 +71,60 @@ func sendSuccessfulTxWithConn(t *utesting.T, s *Suite, tx *types.Transaction, se
}
}
var nonce = uint64(99)
func sendMultipleSuccessfulTxs(t *utesting.T, s *Suite, sendConn *Conn, txs []*types.Transaction) {
txMsg := Transactions(txs)
t.Logf("sending %d txs\n", len(txs))
recvConn := s.setupConnection(t)
defer recvConn.Close()
// Send the transactions
if err := sendConn.Write(&txMsg); err != nil {
t.Fatal(err)
}
// update nonce
nonce = txs[len(txs)-1].Nonce()
// Wait for the transaction announcement(s) and make sure all sent txs are being propagated
recvHashes := make([]common.Hash, 0)
// all txs should be announced within 3 announcements
for i := 0; i < 3; i++ {
switch msg := recvConn.ReadAndServe(s.chain, timeout).(type) {
case *Transactions:
for _, tx := range *msg {
recvHashes = append(recvHashes, tx.Hash())
}
case *NewPooledTransactionHashes:
recvHashes = append(recvHashes, *msg...)
default:
if !strings.Contains(pretty.Sdump(msg), "i/o timeout") {
t.Fatalf("unexpected message while waiting to receive txs: %s", pretty.Sdump(msg))
}
}
// break once all 2000 txs have been received
if len(recvHashes) == 2000 {
break
}
if len(recvHashes) > 0 {
_, missingTxs := compareReceivedTxs(recvHashes, txs)
if len(missingTxs) > 0 {
continue
} else {
t.Logf("successfully received all %d txs", len(txs))
return
}
}
}
_, missingTxs := compareReceivedTxs(recvHashes, txs)
if len(missingTxs) > 0 {
for _, missing := range missingTxs {
t.Logf("missing tx: %v", missing.Hash())
}
t.Fatalf("missing %d txs", len(missingTxs))
}
}
func waitForTxPropagation(t *utesting.T, s *Suite, txs []*types.Transaction, recvConn *Conn) {
// Wait for another transaction announcement
switch msg := recvConn.ReadAndServe(s.chain, time.Second*8).(type) {
@ -75,7 +134,7 @@ func waitForTxPropagation(t *utesting.T, s *Suite, txs []*types.Transaction, rec
for i, recvTx := range *msg {
recvTxs[i] = recvTx.Hash()
}
badTxs := containsTxs(recvTxs, txs)
badTxs, _ := compareReceivedTxs(recvTxs, txs)
if len(badTxs) > 0 {
for _, tx := range badTxs {
t.Logf("received bad tx: %v", tx)
@ -83,7 +142,7 @@ func waitForTxPropagation(t *utesting.T, s *Suite, txs []*types.Transaction, rec
t.Fatalf("received %d bad txs", len(badTxs))
}
case *NewPooledTransactionHashes:
badTxs := containsTxs(*msg, txs)
badTxs, _ := compareReceivedTxs(*msg, txs)
if len(badTxs) > 0 {
for _, tx := range badTxs {
t.Logf("received bad tx: %v", tx)
@ -98,18 +157,27 @@ func waitForTxPropagation(t *utesting.T, s *Suite, txs []*types.Transaction, rec
}
}
// containsTxs checks whether the hashes of the received transactions are present in
// the given set of txs
func containsTxs(recvTxs []common.Hash, txs []*types.Transaction) []common.Hash {
containedTxs := make([]common.Hash, 0)
for _, recvTx := range recvTxs {
for _, tx := range txs {
if recvTx == tx.Hash() {
containedTxs = append(containedTxs, recvTx)
}
// compareReceivedTxs compares the received set of txs against the given set of txs,
// returning both the set received txs that were present within the given txs, and
// the set of txs that were missing from the set of received txs
func compareReceivedTxs(recvTxs []common.Hash, txs []*types.Transaction) (present []*types.Transaction, missing []*types.Transaction) {
// create a map of the hashes received from node
recvHashes := make(map[common.Hash]common.Hash)
for _, hash := range recvTxs {
recvHashes[hash] = hash
}
// collect present txs and missing txs separately
present = make([]*types.Transaction, 0)
missing = make([]*types.Transaction, 0)
for _, tx := range txs {
if _, exists := recvHashes[tx.Hash()]; exists {
present = append(present, tx)
} else {
missing = append(missing, tx)
}
}
return containedTxs
return present, missing
}
func unknownTx(t *utesting.T, s *Suite) *types.Transaction {
@ -119,7 +187,7 @@ func unknownTx(t *utesting.T, s *Suite) *types.Transaction {
to = *tx.To()
}
txNew := types.NewTransaction(tx.Nonce()+1, to, tx.Value(), tx.Gas(), tx.GasPrice(), tx.Data())
return signWithFaucet(t, txNew)
return signWithFaucet(t, s.chain.chainConfig, txNew)
}
func getNextTxFromChain(t *utesting.T, s *Suite) *types.Transaction {
@ -138,6 +206,30 @@ func getNextTxFromChain(t *utesting.T, s *Suite) *types.Transaction {
return tx
}
func generateTxs(t *utesting.T, s *Suite, numTxs int) (map[common.Hash]common.Hash, []*types.Transaction) {
txHashMap := make(map[common.Hash]common.Hash, numTxs)
txs := make([]*types.Transaction, numTxs)
nextTx := getNextTxFromChain(t, s)
gas := nextTx.Gas()
nonce = nonce + 1
// generate txs
for i := 0; i < numTxs; i++ {
tx := generateTx(t, s.chain.chainConfig, nonce, gas)
txHashMap[tx.Hash()] = tx.Hash()
txs[i] = tx
nonce = nonce + 1
}
return txHashMap, txs
}
func generateTx(t *utesting.T, chainConfig *params.ChainConfig, nonce uint64, gas uint64) *types.Transaction {
var to common.Address
tx := types.NewTransaction(nonce, to, big.NewInt(1), gas, big.NewInt(1), []byte{})
return signWithFaucet(t, chainConfig, tx)
}
func getOldTxFromChain(t *utesting.T, s *Suite) *types.Transaction {
var tx *types.Transaction
for _, blocks := range s.fullChain.blocks[:s.chain.Len()-1] {
@ -160,7 +252,7 @@ func invalidNonceTx(t *utesting.T, s *Suite) *types.Transaction {
to = *tx.To()
}
txNew := types.NewTransaction(tx.Nonce()-2, to, tx.Value(), tx.Gas(), tx.GasPrice(), tx.Data())
return signWithFaucet(t, txNew)
return signWithFaucet(t, s.chain.chainConfig, txNew)
}
func hugeAmount(t *utesting.T, s *Suite) *types.Transaction {
@ -171,7 +263,7 @@ func hugeAmount(t *utesting.T, s *Suite) *types.Transaction {
to = *tx.To()
}
txNew := types.NewTransaction(tx.Nonce(), to, amount, tx.Gas(), tx.GasPrice(), tx.Data())
return signWithFaucet(t, txNew)
return signWithFaucet(t, s.chain.chainConfig, txNew)
}
func hugeGasPrice(t *utesting.T, s *Suite) *types.Transaction {
@ -182,7 +274,7 @@ func hugeGasPrice(t *utesting.T, s *Suite) *types.Transaction {
to = *tx.To()
}
txNew := types.NewTransaction(tx.Nonce(), to, tx.Value(), tx.Gas(), gasPrice, tx.Data())
return signWithFaucet(t, txNew)
return signWithFaucet(t, s.chain.chainConfig, txNew)
}
func hugeData(t *utesting.T, s *Suite) *types.Transaction {
@ -192,11 +284,11 @@ func hugeData(t *utesting.T, s *Suite) *types.Transaction {
to = *tx.To()
}
txNew := types.NewTransaction(tx.Nonce(), to, tx.Value(), tx.Gas(), tx.GasPrice(), largeBuffer(2))
return signWithFaucet(t, txNew)
return signWithFaucet(t, s.chain.chainConfig, txNew)
}
func signWithFaucet(t *utesting.T, tx *types.Transaction) *types.Transaction {
signer := types.HomesteadSigner{}
func signWithFaucet(t *utesting.T, chainConfig *params.ChainConfig, tx *types.Transaction) *types.Transaction {
signer := types.LatestSigner(chainConfig)
signedTx, err := types.SignTx(tx, signer, faucetKey)
if err != nil {
t.Fatalf("could not sign tx: %v\n", err)