Skip to content

Commit 80427b0

Browse files
authored
Merge pull request #244 from thirdweb-dev/07-23-calculate_logsbloom_per_transaction_and_them_combine
group logs by transaction hash when calculating bloom filter
2 parents 8f33716 + 289ac27 commit 80427b0

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

internal/validation/root_calculator.go

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -253,39 +253,55 @@ func CalculateTransactionsRoot(transactions []common.Transaction) (string, error
253253
}
254254

255255
func CalculateLogsBloom(logs []common.Log) string {
256-
// Convert our logs to go-ethereum types.Log
257-
ethLogs := make([]*types.Log, len(logs))
258-
for i, log := range logs {
259-
// Convert address
260-
addr := gethCommon.HexToAddress(log.Address)
261-
262-
// Convert topics
263-
topics := make([]gethCommon.Hash, 0, 4)
264-
if log.Topic0 != "" {
265-
topics = append(topics, gethCommon.HexToHash(log.Topic0))
266-
}
267-
if log.Topic1 != "" {
268-
topics = append(topics, gethCommon.HexToHash(log.Topic1))
256+
// Group logs by transaction hash (order matters)
257+
txLogsMap := make(map[string][]common.Log)
258+
txOrder := make([]string, 0)
259+
for _, log := range logs {
260+
txHash := log.TransactionHash
261+
if _, exists := txLogsMap[txHash]; !exists {
262+
txOrder = append(txOrder, txHash)
269263
}
270-
if log.Topic2 != "" {
271-
topics = append(topics, gethCommon.HexToHash(log.Topic2))
264+
txLogsMap[txHash] = append(txLogsMap[txHash], log)
265+
}
266+
267+
// Initialize block bloom (256 bytes)
268+
var blockBloom [256]byte
269+
270+
for _, txHash := range txOrder {
271+
txLogs := txLogsMap[txHash]
272+
// Convert logs to go-ethereum types.Log
273+
ethLogs := make([]*types.Log, len(txLogs))
274+
for i, log := range txLogs {
275+
addr := gethCommon.HexToAddress(log.Address)
276+
topics := make([]gethCommon.Hash, 0, 4)
277+
if log.Topic0 != "" {
278+
topics = append(topics, gethCommon.HexToHash(log.Topic0))
279+
}
280+
if log.Topic1 != "" {
281+
topics = append(topics, gethCommon.HexToHash(log.Topic1))
282+
}
283+
if log.Topic2 != "" {
284+
topics = append(topics, gethCommon.HexToHash(log.Topic2))
285+
}
286+
if log.Topic3 != "" {
287+
topics = append(topics, gethCommon.HexToHash(log.Topic3))
288+
}
289+
ethLogs[i] = &types.Log{
290+
Address: addr,
291+
Topics: topics,
292+
}
272293
}
273-
if log.Topic3 != "" {
274-
topics = append(topics, gethCommon.HexToHash(log.Topic3))
294+
receipt := &types.Receipt{
295+
Logs: ethLogs,
275296
}
276-
277-
ethLogs[i] = &types.Log{
278-
Address: addr,
279-
Topics: topics,
297+
bloom := types.CreateBloom(receipt)
298+
// OR this tx's bloom into the block bloom
299+
for i := 0; i < 256; i++ {
300+
blockBloom[i] |= bloom[i]
280301
}
281302
}
282303

283-
receipt := &types.Receipt{
284-
Logs: ethLogs,
285-
}
286-
// Create bloom filter using go-ethereum's implementation
287-
bloom := types.CreateBloom(receipt)
288-
return "0x" + hex.EncodeToString(bloom[:])
304+
return "0x" + hex.EncodeToString(blockBloom[:])
289305
}
290306

291307
func safeUint256(b *big.Int) *uint256.Int {

0 commit comments

Comments
 (0)