From f01340f2052dec38fa6d27b399fce129ec7536fd Mon Sep 17 00:00:00 2001 From: psogv0308 Date: Wed, 7 Aug 2024 23:53:33 +0900 Subject: [PATCH 1/3] eth/downloader, core/types: adding withdrawal size to downloader queue --- core/types/withdrawal.go | 5 +++++ eth/downloader/queue.go | 3 +++ 2 files changed, 8 insertions(+) diff --git a/core/types/withdrawal.go b/core/types/withdrawal.go index d1ad918f985c..0fea969e6728 100644 --- a/core/types/withdrawal.go +++ b/core/types/withdrawal.go @@ -18,6 +18,7 @@ package types import ( "bytes" + "unsafe" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -42,6 +43,10 @@ type withdrawalMarshaling struct { Amount hexutil.Uint64 } +func (w Withdrawal) Size() common.StorageSize { + return common.StorageSize(unsafe.Sizeof(w)) +} + // Withdrawals implements DerivableList for withdrawals. type Withdrawals []*Withdrawal diff --git a/eth/downloader/queue.go b/eth/downloader/queue.go index 267c23407f43..8346d4023298 100644 --- a/eth/downloader/queue.go +++ b/eth/downloader/queue.go @@ -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() + } q.resultSize = common.StorageSize(blockCacheSizeWeight)*size + (1-common.StorageSize(blockCacheSizeWeight))*q.resultSize } From 1638bd3d05fe2043fc65ce8d6c8c8e9ff77b0884 Mon Sep 17 00:00:00 2001 From: psogv0308 Date: Thu, 8 Aug 2024 01:13:22 +0900 Subject: [PATCH 2/3] Remove unsafe --- core/types/withdrawal.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/types/withdrawal.go b/core/types/withdrawal.go index 0fea969e6728..7671fd60dfc3 100644 --- a/core/types/withdrawal.go +++ b/core/types/withdrawal.go @@ -18,7 +18,7 @@ package types import ( "bytes" - "unsafe" + "reflect" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -43,8 +43,10 @@ type withdrawalMarshaling struct { Amount hexutil.Uint64 } +var withdrawalSize = common.StorageSize(reflect.TypeOf(Withdrawal{}).Size()) + func (w Withdrawal) Size() common.StorageSize { - return common.StorageSize(unsafe.Sizeof(w)) + return common.StorageSize(withdrawalSize) } // Withdrawals implements DerivableList for withdrawals. From 330582e1b3da8d41fd830862d95491adc6273501 Mon Sep 17 00:00:00 2001 From: psogv0308 Date: Thu, 8 Aug 2024 16:51:56 +0900 Subject: [PATCH 3/3] apply comments --- core/types/withdrawal.go | 12 ++++++------ eth/downloader/queue.go | 4 +--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/core/types/withdrawal.go b/core/types/withdrawal.go index 7671fd60dfc3..6f99e53b56ad 100644 --- a/core/types/withdrawal.go +++ b/core/types/withdrawal.go @@ -43,18 +43,18 @@ type withdrawalMarshaling struct { Amount hexutil.Uint64 } -var withdrawalSize = common.StorageSize(reflect.TypeOf(Withdrawal{}).Size()) - -func (w Withdrawal) Size() common.StorageSize { - return common.StorageSize(withdrawalSize) -} - // Withdrawals implements DerivableList for withdrawals. type Withdrawals []*Withdrawal // Len returns the length of s. func (s Withdrawals) Len() int { return len(s) } +var withdrawalSize = int(reflect.TypeOf(Withdrawal{}).Size()) + +func (s Withdrawals) Size() int { + return withdrawalSize * len(s) +} + // EncodeIndex encodes the i'th withdrawal to w. Note that this does not check for errors // because we assume that *Withdrawal will only ever contain valid withdrawals that were either // constructed by decoding or via public API in this package. diff --git a/eth/downloader/queue.go b/eth/downloader/queue.go index 8346d4023298..5441ad118791 100644 --- a/eth/downloader/queue.go +++ b/eth/downloader/queue.go @@ -385,9 +385,7 @@ 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() - } + size += common.StorageSize(result.Withdrawals.Size()) q.resultSize = common.StorageSize(blockCacheSizeWeight)*size + (1-common.StorageSize(blockCacheSizeWeight))*q.resultSize }