Skip to content

Commit fc0b413

Browse files
committed
Changes based on PR comments
1 parent d889d9e commit fc0b413

File tree

3 files changed

+32
-40
lines changed

3 files changed

+32
-40
lines changed

core/blockchain.go

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ type CacheConfig struct {
157157
}
158158

159159
// arbitrum: exposing CacheConfig.triedbConfig to be used by Nitro when initializing arbos in database
160-
func (c *CacheConfig) TriedbConfig(isVerkle bool) *triedb.Config {
161-
return c.triedbConfig(isVerkle)
160+
func (c *CacheConfig) TriedbConfig() *triedb.Config {
161+
return c.triedbConfig(false)
162162
}
163163

164164
// triedbConfig derives the configures for trie database.
@@ -773,14 +773,14 @@ func (bc *BlockChain) rewindHashHead(head *types.Header, root common.Hash, rewin
773773
}
774774

775775
// rewindPathHead implements the logic of rewindHead in the context of path scheme.
776-
func (bc *BlockChain) rewindPathHead(head *types.Header, root common.Hash, rewindLimit uint64) (*types.Header, uint64, bool) {
776+
func (bc *BlockChain) rewindPathHead(head *types.Header, root common.Hash) (*types.Header, uint64) {
777777
var (
778778
pivot = rawdb.ReadLastPivotNumber(bc.db) // Associated block number of pivot block
779779
rootNumber uint64 // Associated block number of requested root
780780

781-
// RootFound represents whether the requested root is already
781+
// BeyondRoot represents whether the requested root is already
782782
// crossed. The flag value is set to true if the root is empty.
783-
rootFound = root == common.Hash{}
783+
beyondRoot = root == common.Hash{}
784784

785785
// noState represents if the target state requested for search
786786
// is unavailable and impossible to be recovered.
@@ -789,10 +789,6 @@ func (bc *BlockChain) rewindPathHead(head *types.Header, root common.Hash, rewin
789789
start = time.Now() // Timestamp the rewinding is restarted
790790
logged = time.Now() // Timestamp last progress log was printed
791791
)
792-
793-
lastFullBlock := uint64(0)
794-
lastFullBlockHash := common.Hash{}
795-
gasRolledBack := uint64(0)
796792
// Rewind the head block tag until an available state is found.
797793
for {
798794
logger := log.Trace
@@ -802,64 +798,43 @@ func (bc *BlockChain) rewindPathHead(head *types.Header, root common.Hash, rewin
802798
}
803799
logger("Block state missing, rewinding further", "number", head.Number, "hash", head.Hash(), "elapsed", common.PrettyDuration(time.Since(start)))
804800

805-
if rewindLimit > 0 && lastFullBlock != 0 {
806-
// Arbitrum: track the amount of gas rolled back and stop the rollback early if necessary
807-
gasUsedInBlock := head.GasUsed
808-
if bc.chainConfig.IsArbitrum() {
809-
receipts := bc.GetReceiptsByHash(head.Hash())
810-
for _, receipt := range receipts {
811-
gasUsedInBlock -= receipt.GasUsedForL1
812-
}
813-
}
814-
gasRolledBack += gasUsedInBlock
815-
if gasRolledBack >= rewindLimit {
816-
rootNumber = lastFullBlock
817-
head = bc.GetHeader(lastFullBlockHash, lastFullBlock)
818-
log.Debug("Rewound to block with state but not snapshot", "number", head.Number.Uint64(), "hash", head.Hash())
819-
break
820-
}
821-
}
822801
// If a root threshold was requested but not yet crossed, check
823-
if !rootFound && head.Root == root {
824-
rootFound, rootNumber = true, head.Number.Uint64()
802+
if !beyondRoot && head.Root == root {
803+
beyondRoot, rootNumber = true, head.Number.Uint64()
825804
}
826805
// If the root threshold hasn't been crossed but the available
827806
// state is reached, quickly determine if the target state is
828807
// possible to be reached or not.
829-
if !rootFound && noState && bc.HasState(head.Root) {
830-
rootFound = true
808+
if !beyondRoot && noState && bc.HasState(head.Root) {
809+
beyondRoot = true
831810
log.Info("Disable the search for unattainable state", "root", root)
832811
}
833812
// Check if the associated state is available or recoverable if
834813
// the requested root has already been crossed.
835-
if rootFound && (bc.HasState(head.Root) || bc.stateRecoverable(head.Root)) {
814+
if beyondRoot && (bc.HasState(head.Root) || bc.stateRecoverable(head.Root)) {
836815
break
837816
}
838-
if (bc.HasState(head.Root) || bc.stateRecoverable(head.Root)) && lastFullBlock == 0 {
839-
lastFullBlock = head.Number.Uint64()
840-
lastFullBlockHash = head.Hash()
841-
}
842817
// If pivot block is reached, return the genesis block as the
843818
// new chain head. Theoretically there must be a persistent
844819
// state before or at the pivot block, prevent endless rewinding
845820
// towards the genesis just in case.
846821
if pivot != nil && *pivot >= head.Number.Uint64() {
847822
log.Info("Pivot block reached, resetting to genesis", "number", head.Number, "hash", head.Hash())
848-
return bc.genesisBlock.Header(), rootNumber, rootFound
823+
return bc.genesisBlock.Header(), rootNumber
849824
}
850825
// If the chain is gapped in the middle, return the genesis
851826
// block as the new chain head
852827
parent := bc.GetHeader(head.ParentHash, head.Number.Uint64()-1) // Keep rewinding
853828
if parent == nil {
854829
log.Error("Missing block in the middle, resetting to genesis", "number", head.Number.Uint64()-1, "hash", head.ParentHash)
855-
return bc.genesisBlock.Header(), rootNumber, rootFound
830+
return bc.genesisBlock.Header(), rootNumber
856831
}
857832
head = parent
858833

859834
// If the genesis block is reached, stop searching.
860835
if head.Number.Uint64() == 0 {
861836
log.Info("Genesis block reached", "number", head.Number, "hash", head.Hash())
862-
return head, rootNumber, rootFound
837+
return head, rootNumber
863838
}
864839
}
865840
// Recover if the target state if it's not available yet.
@@ -869,7 +844,7 @@ func (bc *BlockChain) rewindPathHead(head *types.Header, root common.Hash, rewin
869844
}
870845
}
871846
log.Info("Rewound to block with state", "number", head.Number, "hash", head.Hash())
872-
return head, rootNumber, rootFound
847+
return head, rootNumber
873848
}
874849

875850
// rewindHead searches the available states in the database and returns the associated
@@ -882,7 +857,8 @@ func (bc *BlockChain) rewindPathHead(head *types.Header, root common.Hash, rewin
882857
// and the whole snapshot should be auto-generated in case of head mismatch.
883858
func (bc *BlockChain) rewindHead(head *types.Header, root common.Hash, rewindLimit uint64) (*types.Header, uint64, bool) {
884859
if bc.triedb.Scheme() == rawdb.PathScheme {
885-
return bc.rewindPathHead(head, root, rewindLimit)
860+
head, rootNumber := bc.rewindPathHead(head, root)
861+
return head, rootNumber, head.Number.Uint64() != 0
886862
}
887863
return bc.rewindHashHead(head, root, rewindLimit)
888864
}

core/state/state_object.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io"
2323
"maps"
24+
"sort"
2425
"time"
2526

2627
"github.com/ethereum/go-ethereum/common"
@@ -358,6 +359,9 @@ func (s *stateObject) updateTrie() (Trie, error) {
358359
// Cache the items for preloading
359360
usedStorage = append(usedStorage, common.CopyBytes(key[:])) // Copy needed for closure
360361
}
362+
if s.db.Deterministic() {
363+
sort.Slice(deletions, func(i, j int) bool { return bytes.Compare(deletions[i][:], deletions[j][:]) < 0 })
364+
}
361365
for _, key := range deletions {
362366
if err := tr.DeleteStorage(s.address, key[:]); err != nil {
363367
s.db.setError(err)

metrics/sample_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ func BenchmarkUniformSample1028(b *testing.B) {
8787
benchmarkSample(b, NewUniformSample(1028))
8888
}
8989

90+
func BenchmarkSlidingWindowArraySample257(b *testing.B) {
91+
benchmarkSample(b, NewSlidingTimeWindowArraySample(257))
92+
}
93+
94+
func BenchmarkSlidingWindowArraySample514(b *testing.B) {
95+
benchmarkSample(b, NewSlidingTimeWindowArraySample(514))
96+
}
97+
98+
func BenchmarkSlidingWindowArraySample1028(b *testing.B) {
99+
benchmarkSample(b, NewSlidingTimeWindowArraySample(1028))
100+
}
101+
90102
func TestExpDecaySample(t *testing.T) {
91103
for _, tc := range []struct {
92104
reservoirSize int

0 commit comments

Comments
 (0)