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

latest finalized block metrics #12339

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
af480ab
Add LatestFinalizedBlock to HeadTracker
dhaidashenko Feb 19, 2024
9da949b
Added LatestFinalizedHead to Head
dhaidashenko Feb 19, 2024
9e580cc
Merge branch 'develop' into feature/BCI-2649-latest-finalized-block
dhaidashenko Feb 19, 2024
112679e
remove unused func
dhaidashenko Feb 20, 2024
334a4d1
fix flakey nil pointer
dhaidashenko Feb 20, 2024
83cf834
improve logs & address lint issue
dhaidashenko Feb 20, 2024
26e8d50
nitpicks
dhaidashenko Feb 20, 2024
962464c
Merge branch 'develop' into feature/BCI-2649-latest-finalized-block
dhaidashenko Feb 20, 2024
b86c872
fixed copy on heads on MarkFinalized
dhaidashenko Feb 21, 2024
00769f0
Merge branch 'feature/BCI-2649-latest-finalized-block' of github.com:…
dhaidashenko Feb 21, 2024
81774b4
error instead of panic
dhaidashenko Feb 21, 2024
c942663
return error instead of panic
dhaidashenko Feb 21, 2024
72a2380
Merge branch 'develop' into feature/BCI-2649-latest-finalized-block
dhaidashenko Feb 21, 2024
a01fb86
nitpicks
dhaidashenko Feb 21, 2024
d7a9d4e
Merge branch 'feature/BCI-2649-latest-finalized-block' of github.com:…
dhaidashenko Feb 21, 2024
faf61d9
Finalized block based history depth
dhaidashenko Feb 23, 2024
89a75b3
simplify trimming
dhaidashenko Feb 23, 2024
f7ab489
nit fixes
dhaidashenko Feb 23, 2024
d9d422c
Merge branch 'develop' into feature/BCI-2649-latest-finalized-block
dhaidashenko Feb 23, 2024
e77f529
fix build issues caused by merge
dhaidashenko Feb 23, 2024
908acf7
regen
dhaidashenko Feb 23, 2024
93b835d
FIx rpc client mock generation
dhaidashenko Feb 23, 2024
2f55403
nit fixes
dhaidashenko Feb 26, 2024
9f26066
Merge branch 'develop' into feature/BCI-2649-latest-finalized-block
dhaidashenko Feb 26, 2024
71a0803
Merge branch 'develop' into feature/BCI-2649-latest-finalized-block
dhaidashenko Feb 26, 2024
35c3302
nit fixes
dhaidashenko Feb 26, 2024
bd1ea1e
update comments
dhaidashenko Feb 27, 2024
6cc4fec
Merge branch 'develop' into feature/BCI-2649-latest-finalized-block
dhaidashenko Feb 27, 2024
83ea5d1
ensure that we trim redundant blocks both in slice and in chain in Heads
dhaidashenko Feb 28, 2024
2d5ae65
nit fix
dhaidashenko Feb 28, 2024
c99cea6
Merge branch 'develop' into feature/BCI-2649-latest-finalized-block
dhaidashenko Feb 28, 2024
f77a8ab
Update common/headtracker/head_tracker.go
dhaidashenko Feb 28, 2024
f7c786f
HeadTracker backfill test with 0 finality depth
dhaidashenko Feb 29, 2024
dee11fc
Merge branch 'feature/BCI-2649-latest-finalized-block' of github.com:…
dhaidashenko Feb 29, 2024
4b27f75
latest finalized block metrics
dhaidashenko Mar 7, 2024
672e09a
changelog & go generate fix
dhaidashenko Mar 7, 2024
4372344
Merge branch 'develop' into feature/BCI-2663-rpc-metrics-for-finalize…
dhaidashenko Mar 18, 2024
abab9f0
move nodeConfig back into the test pkg
dhaidashenko Mar 18, 2024
c32050e
rollback fields renaming
dhaidashenko Mar 18, 2024
3dd3f3c
nit
dhaidashenko Mar 18, 2024
971f35f
changeset
dhaidashenko Mar 18, 2024
63ec90e
removed unused func
dhaidashenko Mar 18, 2024
38871bd
Set default value for FinalizedBlockPollInterval
dhaidashenko Mar 21, 2024
d79c8b1
updated docs
dhaidashenko Mar 21, 2024
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
Prev Previous commit
Next Next commit
simplify trimming
  • Loading branch information
dhaidashenko committed Feb 23, 2024
commit 89a75b340e528623ef9c0afe487ff76944a9bda2
10 changes: 5 additions & 5 deletions core/chains/evm/headtracker/head_saver.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (hs *headSaver) Save(ctx context.Context, head *evmtypes.Head) error {
}

func (hs *headSaver) Load(ctx context.Context, latestFinalized *evmtypes.Head) (chain *evmtypes.Head, err error) {
minBlockNumber := hs.calculateDeepestToKeep(latestFinalized.BlockNumber())
minBlockNumber := hs.calculateMinBlockToKeep(latestFinalized.BlockNumber())
heads, err := hs.orm.LatestHeads(ctx, minBlockNumber)
if err != nil {
return nil, err
Expand All @@ -54,7 +54,7 @@ func (hs *headSaver) Load(ctx context.Context, latestFinalized *evmtypes.Head) (
return hs.heads.LatestHead(), nil
}

func (hs *headSaver) calculateDeepestToKeep(latestFinalized int64) int64 {
func (hs *headSaver) calculateMinBlockToKeep(latestFinalized int64) int64 {
return latestFinalized - int64(hs.htConfig.HistoryDepth())
}

Expand All @@ -78,12 +78,12 @@ func (hs *headSaver) Chain(hash common.Hash) *evmtypes.Head {
}

func (hs *headSaver) MarkFinalized(ctx context.Context, finalized *evmtypes.Head) error {
deepestToKeep := hs.calculateDeepestToKeep(finalized.BlockNumber())
if !hs.heads.MarkFinalized(finalized.BlockHash(), deepestToKeep) {
minBlockToKeep := hs.calculateMinBlockToKeep(finalized.BlockNumber())
if !hs.heads.MarkFinalized(finalized.BlockHash(), minBlockToKeep) {
return fmt.Errorf("failed to find %s block in the canonical chain to mark it as finalized", finalized)
}

return hs.orm.TrimOldHeads(ctx, deepestToKeep)
return hs.orm.TrimOldHeads(ctx, minBlockToKeep)
}

var NullSaver httypes.HeadSaver = &nullSaver{}
Expand Down
56 changes: 18 additions & 38 deletions core/chains/evm/headtracker/heads.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ type Heads interface {
AddHeads(newHeads ...*evmtypes.Head)
// Count returns number of heads in the collection.
Count() int
// MarkFinalized - finds `finalized` in the LatestHead and marks it and all direct ancestors as finalized
MarkFinalized(finalized common.Hash, deepestToKeep int64) bool
// MarkFinalized - finds `finalized` in the LatestHead and marks it and all direct ancestors as finalized.
// Trims old blocks whose height is smaller than minBlockToKeep
MarkFinalized(finalized common.Hash, minBlockToKeep int64) bool
}

type heads struct {
Expand Down Expand Up @@ -63,8 +64,8 @@ func (h *heads) Count() int {
}

// MarkFinalized - marks block with has equal to finalized and all it's direct ancestors as finalized.
// Trims old blocks whose height is smaller than deepestToKeep
func (h *heads) MarkFinalized(finalized common.Hash, deepestToKeep int64) bool {
// Trims old blocks whose height is smaller than minBlockToKeep
func (h *heads) MarkFinalized(finalized common.Hash, minBlockToKeep int64) bool {
h.mu.Lock()
defer h.mu.Unlock()

Expand All @@ -73,7 +74,7 @@ func (h *heads) MarkFinalized(finalized common.Hash, deepestToKeep int64) bool {
}

// deep copy to avoid race
h.heads = deepCopy(h.heads)
h.heads = deepCopy(h.heads, minBlockToKeep)

head := h.heads[0]
foundFinalized := false
Expand All @@ -88,40 +89,10 @@ func (h *heads) MarkFinalized(finalized common.Hash, deepestToKeep int64) bool {
head = head.Parent
}

// trim blocks that are too deep
h.trimRedundantBlocks(deepestToKeep)

return foundFinalized
}

// trimRedundantBlocks - trims all the blocks whose blockNumber < deepestToKeep
// Not thread safe. Must be called on fresh copy of h.heads
func (h *heads) trimRedundantBlocks(deepestToKeep int64) {
if len(h.heads) == 0 {
return
}

deepestBlock := h.heads[0].HeadAtHeight(deepestToKeep)
if deepestBlock == nil {
return
}

for i, head := range h.heads {
// ensure that uncle chains and canonical chain do not go deeper than deepestToKeep
if deepestBlock.Parent == head.Parent {
head.Parent = nil
}
// trim slice
if head == deepestBlock {
h.heads = h.heads[:i+1]
return
}
}

panic("invariant violation: expected deepestToKeep to present in the heads slice since we've seen it before")
}

func deepCopy(oldHeads []*evmtypes.Head) []*evmtypes.Head {
func deepCopy(oldHeads []*evmtypes.Head, minBlockToKeep int64) []*evmtypes.Head {
headsMap := make(map[common.Hash]*evmtypes.Head, len(oldHeads))
for _, head := range oldHeads {
if head.Hash == head.ParentHash {
Expand Down Expand Up @@ -155,11 +126,20 @@ func deepCopy(oldHeads []*evmtypes.Head) []*evmtypes.Head {
return heads[i].Number > heads[j].Number
})

// yeah, we could have used binarySearch here, but the code was much longer and more complex and did not
// solve any performance issues
for i := range heads {
if heads[i].BlockNumber() < minBlockToKeep {
heads = heads[:i]
break
}
}

// assign parents
for i := 0; i < len(heads)-1; i++ {
head := heads[i]
parent, exists := headsMap[head.ParentHash]
if exists {
if exists && parent.BlockNumber() >= minBlockToKeep {
head.Parent = parent
}
}
Expand All @@ -172,5 +152,5 @@ func (h *heads) AddHeads(newHeads ...*evmtypes.Head) {
defer h.mu.Unlock()

// deep copy to avoid race
h.heads = deepCopy(append(h.heads, newHeads...))
h.heads = deepCopy(append(h.heads, newHeads...), 0)
}
Loading