@@ -27,6 +27,7 @@ import (
2727 "github.com/ethereum/go-ethereum/common/mclock"
2828 "github.com/ethereum/go-ethereum/core/txpool"
2929 "github.com/ethereum/go-ethereum/core/types"
30+ "github.com/ethereum/go-ethereum/params"
3031)
3132
3233var (
@@ -993,15 +994,14 @@ func TestTransactionFetcherTimeoutTimerResets(t *testing.T) {
993994 })
994995}
995996
996- // Tests that if thousands of transactions are announces , only a small
997+ // Tests that if thousands of transactions are announced , only a small
997998// number of them will be requested at a time.
998999func TestTransactionFetcherRateLimiting (t * testing.T ) {
999- // Create a slew of transactions and to announce them
1000+ // Create a slew of transactions and announce them
10001001 var hashes []common.Hash
10011002 for i := 0 ; i < maxTxAnnounces ; i ++ {
10021003 hashes = append (hashes , common.Hash {byte (i / 256 ), byte (i % 256 )})
10031004 }
1004-
10051005 testTransactionFetcherParallel (t , txFetcherTest {
10061006 init : func () * TxFetcher {
10071007 return NewTxFetcher (
@@ -1029,6 +1029,68 @@ func TestTransactionFetcherRateLimiting(t *testing.T) {
10291029 })
10301030}
10311031
1032+ // Tests that if huge transactions are announced, only a small number of them will
1033+ // be requested at a time, to keep the responses below a resonable level.
1034+ func TestTransactionFetcherBandwidthLimiting (t * testing.T ) {
1035+ testTransactionFetcherParallel (t , txFetcherTest {
1036+ init : func () * TxFetcher {
1037+ return NewTxFetcher (
1038+ func (common.Hash ) bool { return false },
1039+ nil ,
1040+ func (string , []common.Hash ) error { return nil },
1041+ nil ,
1042+ )
1043+ },
1044+ steps : []interface {}{
1045+ // Announce mid size transactions from A to verify that multiple
1046+ // ones can be piled into a single request.
1047+ doTxNotify {peer : "A" ,
1048+ hashes : []common.Hash {{0x01 }, {0x02 }, {0x03 }, {0x04 }},
1049+ types : []byte {types .LegacyTxType , types .LegacyTxType , types .LegacyTxType , types .LegacyTxType },
1050+ sizes : []uint32 {48 * 1024 , 48 * 1024 , 48 * 1024 , 48 * 1024 },
1051+ },
1052+ // Announce exactly on the limit transactions to see that only one
1053+ // gets requested
1054+ doTxNotify {peer : "B" ,
1055+ hashes : []common.Hash {{0x05 }, {0x06 }},
1056+ types : []byte {types .LegacyTxType , types .LegacyTxType },
1057+ sizes : []uint32 {maxTxRetrievalSize , maxTxRetrievalSize },
1058+ },
1059+ // Announce oversized blob transactions to see that overflows are ok
1060+ doTxNotify {peer : "C" ,
1061+ hashes : []common.Hash {{0x07 }, {0x08 }},
1062+ types : []byte {types .BlobTxType , types .BlobTxType },
1063+ sizes : []uint32 {params .MaxBlobGasPerBlock , params .MaxBlobGasPerBlock },
1064+ },
1065+ doWait {time : txArriveTimeout , step : true },
1066+ isWaiting (nil ),
1067+ isScheduledWithMeta {
1068+ tracking : map [string ][]announce {
1069+ "A" : {
1070+ {common.Hash {0x01 }, typeptr (types .LegacyTxType ), sizeptr (48 * 1024 )},
1071+ {common.Hash {0x02 }, typeptr (types .LegacyTxType ), sizeptr (48 * 1024 )},
1072+ {common.Hash {0x03 }, typeptr (types .LegacyTxType ), sizeptr (48 * 1024 )},
1073+ {common.Hash {0x04 }, typeptr (types .LegacyTxType ), sizeptr (48 * 1024 )},
1074+ },
1075+ "B" : {
1076+ {common.Hash {0x05 }, typeptr (types .LegacyTxType ), sizeptr (maxTxRetrievalSize )},
1077+ {common.Hash {0x06 }, typeptr (types .LegacyTxType ), sizeptr (maxTxRetrievalSize )},
1078+ },
1079+ "C" : {
1080+ {common.Hash {0x07 }, typeptr (types .BlobTxType ), sizeptr (params .MaxBlobGasPerBlock )},
1081+ {common.Hash {0x08 }, typeptr (types .BlobTxType ), sizeptr (params .MaxBlobGasPerBlock )},
1082+ },
1083+ },
1084+ fetching : map [string ][]common.Hash {
1085+ "A" : {{0x02 }, {0x03 }, {0x04 }},
1086+ "B" : {{0x06 }},
1087+ "C" : {{0x08 }},
1088+ },
1089+ },
1090+ },
1091+ })
1092+ }
1093+
10321094// Tests that then number of transactions a peer is allowed to announce and/or
10331095// request at the same time is hard capped.
10341096func TestTransactionFetcherDoSProtection (t * testing.T ) {
@@ -1664,7 +1726,7 @@ func testTransactionFetcher(t *testing.T, tt txFetcherTest) {
16641726 if (meta == nil && (ann .kind != nil || ann .size != nil )) ||
16651727 (meta != nil && (ann .kind == nil || ann .size == nil )) ||
16661728 (meta != nil && (meta .kind != * ann .kind || meta .size != * ann .size )) {
1667- t .Errorf ("step %d, peer %s, hash %x: waitslot metadata mismatch: want %v, have %v/%v" , i , peer , ann .hash , meta , ann .kind , ann .size )
1729+ t .Errorf ("step %d, peer %s, hash %x: waitslot metadata mismatch: want %v, have %v/%v" , i , peer , ann .hash , meta , * ann .kind , * ann .size )
16681730 }
16691731 }
16701732 }
@@ -1733,7 +1795,7 @@ func testTransactionFetcher(t *testing.T, tt txFetcherTest) {
17331795 if (meta == nil && (ann .kind != nil || ann .size != nil )) ||
17341796 (meta != nil && (ann .kind == nil || ann .size == nil )) ||
17351797 (meta != nil && (meta .kind != * ann .kind || meta .size != * ann .size )) {
1736- t .Errorf ("step %d, peer %s, hash %x: announce metadata mismatch: want %v, have %v/%v" , i , peer , ann .hash , meta , ann .kind , ann .size )
1798+ t .Errorf ("step %d, peer %s, hash %x: announce metadata mismatch: want %v, have %v/%v" , i , peer , ann .hash , meta , * ann .kind , * ann .size )
17371799 }
17381800 }
17391801 }
0 commit comments