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

Adjust Logic In Merkle DB History #1310

Merged
merged 5 commits into from
Apr 11, 2023
Merged
Changes from 2 commits
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
52 changes: 29 additions & 23 deletions x/merkledb/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,34 @@ func (th *trieHistory) getValueChanges(startRoot, endRoot ids.ID, start, end []b
return nil, ErrRootIDNotPresent
}

// [lastStartRootChange] is the latest appearance of [startRoot]
// which came before [lastEndRootChange].
var lastStartRootChange *changeSummaryAndIndex
th.history.DescendLessOrEqual(
lastEndRootChange,
func(item *changeSummaryAndIndex) bool {
if item == lastEndRootChange {
return true // Skip first iteration
}
if item.rootID == startRoot {
lastStartRootChange = item
return false
}
return true
},
)

// There's no change resulting in [startRoot] before the latest change resulting in [endRoot].
if lastStartRootChange == nil {
// [startRootChanges] is the last appearance of [startRoot]
startRootChanges, ok := th.lastChanges[startRoot]
dboehm-avalabs marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
return nil, ErrStartRootNotFound
}

// startRootChanges is after the lastEndRootChange, but that is just the latest appearance of start root
dboehm-avalabs marked this conversation as resolved.
Show resolved Hide resolved
// there may be an earlier entry, so attempt to find an entry that comes before lastEndRootChange
if startRootChanges.index > lastEndRootChange.index {
th.history.DescendLessOrEqual(
lastEndRootChange,
func(item *changeSummaryAndIndex) bool {
if item == lastEndRootChange {
return true // Skip first iteration
}
if item.rootID == startRoot {
startRootChanges = item
return false
}
return true
},
)
// There's no change resulting in [startRoot] before the latest change resulting in [endRoot].
if startRootChanges.index > lastEndRootChange.index {
return nil, ErrStartRootNotFound
}
}

// Keep changes sorted so the largest can be removed in order to stay within the maxLength limit.
sortedKeys := btree.NewG(
2,
Expand All @@ -135,13 +141,13 @@ func (th *trieHistory) getValueChanges(startRoot, endRoot ids.ID, start, end []b
// Only the key-value pairs with the greatest [maxLength] keys will be kept.
combinedChanges := newChangeSummary(maxLength)

// For each change after [lastStartRootChange] up to and including
// For each change after [startRootChanges] up to and including
// [lastEndRootChange], record the change in [combinedChanges].
th.history.AscendGreaterOrEqual(
lastStartRootChange,
startRootChanges,
func(item *changeSummaryAndIndex) bool {
if item == lastStartRootChange {
// Start from the first change after [lastStartRootChange].
if item == startRootChanges {
// Start from the first change after [startRootChanges].
return true
}
if item.index > lastEndRootChange.index {
Expand Down