Skip to content

Commit e4a8ecb

Browse files
authored
core/txpool/legacypool: fix flaky test TestAllowedTxSize #30975 (#31836)
Some tests involving transactions near the txMaxSize limit were flaky. This was due to ECDSA signatures occasionally having leading zeros, which are omitted during RLP encoding — making the final transaction size 1 byte smaller than expected. To address this, a new helper function pricedDataTransactionWithFixedSignature was added. It ensures both r and s are exactly 32 bytes (i.e., no leading zeros), producing transactions with deterministic size.
1 parent 24771fd commit e4a8ecb

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

core/txpool/legacypool/legacypool_test.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,33 @@ func pricedTransaction(nonce uint64, gaslimit uint64, gasprice *big.Int, key *ec
108108
return tx
109109
}
110110

111-
func pricedDataTransaction(nonce uint64, gaslimit uint64, gasprice *big.Int, key *ecdsa.PrivateKey, bytes uint64) *types.Transaction {
112-
data := make([]byte, bytes)
113-
crand.Read(data)
114-
115-
tx, _ := types.SignTx(types.NewTransaction(nonce, common.Address{}, big.NewInt(0), gaslimit, gasprice, data), types.HomesteadSigner{}, key)
111+
// pricedDataTransaction generates a signed transaction with fixed-size data,
112+
// and ensures that the resulting signature components (r and s) are exactly 32 bytes each,
113+
// producing transactions with deterministic size.
114+
//
115+
// This avoids variability in transaction size caused by leading zeros being omitted in
116+
// RLP encoding of r/s. Since r and s are derived from ECDSA, they occasionally have leading
117+
// zeros and thus can be shorter than 32 bytes.
118+
//
119+
// For example:
120+
//
121+
// r: 0 leading zeros, bytesSize: 32, bytes: [221 ... 101]
122+
// s: 1 leading zeros, bytesSize: 31, bytes: [0 75 ... 47]
123+
func pricedDataTransaction(nonce uint64, gaslimit uint64, gasprice *big.Int, key *ecdsa.PrivateKey, dataBytes uint64) *types.Transaction {
124+
var tx *types.Transaction
125+
126+
// 10 attempts is statistically sufficient since leading zeros in ECDSA signatures are rare and randomly distributed.
127+
var retryTimes = 10
128+
for i := 0; i < retryTimes; i++ {
129+
data := make([]byte, dataBytes)
130+
crand.Read(data)
131+
132+
tx, _ = types.SignTx(types.NewTransaction(nonce, common.Address{}, big.NewInt(0), gaslimit, gasprice, data), types.HomesteadSigner{}, key)
133+
_, r, s := tx.RawSignatureValues()
134+
if len(r.Bytes()) == 32 && len(s.Bytes()) == 32 {
135+
break
136+
}
137+
}
116138
return tx
117139
}
118140

@@ -1239,7 +1261,7 @@ func TestAllowedTxSize(t *testing.T) {
12391261
const largeDataLength = txMaxSize - 200 // enough to have a 5 bytes RLP encoding of the data length number
12401262
txWithLargeData := pricedDataTransaction(0, pool.currentHead.Load().GasLimit, big.NewInt(1), key, largeDataLength)
12411263
maxTxLengthWithoutData := txWithLargeData.Size() - largeDataLength // 103 bytes
1242-
maxTxDataLength := txMaxSize - maxTxLengthWithoutData // 131072 - 103 = 130953 bytes
1264+
maxTxDataLength := txMaxSize - maxTxLengthWithoutData // 131072 - 103 = 130969 bytes
12431265

12441266
// Try adding a transaction with maximal allowed size
12451267
tx := pricedDataTransaction(0, pool.currentHead.Load().GasLimit, big.NewInt(1), key, maxTxDataLength)

0 commit comments

Comments
 (0)