Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth/downloader, core/types: take withdrawals-size into account in downloader queue #30276

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/types/withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package types

import (
"bytes"
"unsafe"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand All @@ -42,6 +43,10 @@ type withdrawalMarshaling struct {
Amount hexutil.Uint64
}

func (w Withdrawal) Size() common.StorageSize {
return common.StorageSize(unsafe.Sizeof(w))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid the use of package unsafe here. Since withdrawals are defined by the protocol and will never change, we can just hard-code the returned size here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var withdrawalSize = common.StorageSize(reflect.TypeOf(Withdrawal{}).Size())

func (w Withdrawal) Size() common.StorageSize {
	return common.StorageSize(withdrawalSize)
}

What do think about handling it like this, similar to the code in other parts?

}

// Withdrawals implements DerivableList for withdrawals.
type Withdrawals []*Withdrawal

Expand Down
3 changes: 3 additions & 0 deletions eth/downloader/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ func (q *queue) Results(block bool) []*fetchResult {
for _, tx := range result.Transactions {
size += common.StorageSize(tx.Size())
}
for _, withdrawal := range result.Withdrawals {
size += withdrawal.Size()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since withdrawals are constant size, perhaps put Size on Withdrawals instead, and do

size += common.StorageSize(result.Withdrawals.Size())

(yes, make the conversion to StorageSize here, not in the Size-function)

Then the change in types boils down to

var withdrawalSize = int(reflect.TypeOf(Withdrawal{}).Size())

func (s Withdrawals) Size() int {
	return withdrawalSize * len(s)
}

q.resultSize = common.StorageSize(blockCacheSizeWeight)*size +
(1-common.StorageSize(blockCacheSizeWeight))*q.resultSize
}
Expand Down