Skip to content

Commit

Permalink
Adjust Logic In Merkle DB History (#1310)
Browse files Browse the repository at this point in the history
Co-authored-by: Darioush Jalali <[email protected]>
  • Loading branch information
dboehm-avalabs and darioush authored Apr 11, 2023
1 parent 725108f commit 75eb071
Showing 1 changed file with 29 additions and 23 deletions.
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]
if !ok {
return nil, ErrStartRootNotFound
}

// startRootChanges is after the lastEndRootChange, but that is just the latest appearance of start root
// 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

0 comments on commit 75eb071

Please sign in to comment.