Skip to content

Comments

Fix TxPool.PackTxsWithBytes to use encoded transaction size instead of unsafe.Sizeof#63

Open
1654574171 wants to merge 1 commit intoHuangLab-SYSU:mainfrom
1654574171:fix-txpool-pack-with-bytes
Open

Fix TxPool.PackTxsWithBytes to use encoded transaction size instead of unsafe.Sizeof#63
1654574171 wants to merge 1 commit intoHuangLab-SYSU:mainfrom
1654574171:fix-txpool-pack-with-bytes

Conversation

@1654574171
Copy link

Note

I noticed this might be a potential issue — please let me know if my understanding is off.

Summary

This PR fixes TxPool.PackTxsWithBytes so that it uses the encoded transaction size
instead of unsafe.Sizeof(*tx) when deciding how many transactions to pack.

Using unsafe.Sizeof only accounts for the in-memory struct header size and ignores
the actual payload stored behind slices, strings, etc. This can lead to significant
underestimation of the real block/tx batch size.

Changes

// Pack transaction for a proposal (use 'BlocksizeInBytes' to control)
func (txpool *TxPool) PackTxsWithBytes(max_bytes int) []*Transaction {
	txpool.lock.Lock()
	defer txpool.lock.Unlock()

	txNum := len(txpool.TxQueue)
	currentSize := 0
	for tx_idx, tx := range txpool.TxQueue {
        //currentSize += int(unsafe.Sizeof(*tx)) 
		currentSize += len(tx.Encode())
		if currentSize > max_bytes {
			txNum = tx_idx
			break
		}
	}

	txs_Packed := txpool.TxQueue[:txNum]
	txpool.TxQueue = txpool.TxQueue[txNum:]
	return txs_Packed
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant