2023-09-05 18:00:40 +00:00
|
|
|
# nimbus-eth1
|
2024-02-01 21:27:48 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-09-05 18:00:40 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/tables,
|
2023-11-08 12:18:32 +00:00
|
|
|
eth/common,
|
2024-07-18 21:32:32 +00:00
|
|
|
".."/[aristo_desc, aristo_layers]
|
2023-09-05 18:00:40 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-03 20:10:35 +00:00
|
|
|
proc deltaMerge*(
|
2024-07-18 21:32:32 +00:00
|
|
|
upper: LayerRef; # Think of `top`, `nil` is ok
|
|
|
|
modUpperOk: bool; # May re-use/modify `upper`
|
|
|
|
lower: LayerRef; # Think of `balancer`, `nil` is ok
|
|
|
|
modLowerOk: bool; # May re-use/modify `lower`
|
|
|
|
): LayerRef =
|
2023-09-05 18:00:40 +00:00
|
|
|
## Merge argument `upper` into the `lower` filter instance.
|
|
|
|
##
|
|
|
|
## Note that the namimg `upper` and `lower` indicate that the filters are
|
2024-07-18 21:32:32 +00:00
|
|
|
## stacked and the database access is `upper -> lower -> backend`.
|
2023-09-05 18:00:40 +00:00
|
|
|
##
|
|
|
|
if lower.isNil:
|
2024-07-18 21:32:32 +00:00
|
|
|
# Degenerate case: `upper` is void
|
|
|
|
result = upper
|
2023-09-05 18:00:40 +00:00
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
elif upper.isNil:
|
|
|
|
# Degenerate case: `lower` is void
|
|
|
|
result = lower
|
2023-09-05 18:00:40 +00:00
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
elif modLowerOk:
|
|
|
|
# Can modify `lower` which is the prefered action mode but applies only
|
|
|
|
# in cases where the `lower` argument is not shared.
|
|
|
|
lower.vTop = upper.vTop
|
|
|
|
layersMergeOnto(upper, lower[])
|
|
|
|
result = lower
|
2023-09-05 18:00:40 +00:00
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
elif not modUpperOk:
|
|
|
|
# Cannot modify any argument layers.
|
|
|
|
result = LayerRef(
|
|
|
|
sTab: lower.sTab, # shallow copy (entries will not be modified)
|
|
|
|
kMap: lower.kMap,
|
|
|
|
accLeaves: lower.accLeaves,
|
|
|
|
stoLeaves: lower.stoLeaves,
|
|
|
|
vTop: upper.vTop)
|
|
|
|
layersMergeOnto(upper, result[])
|
2023-09-05 18:00:40 +00:00
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
else:
|
2024-08-14 08:54:44 +00:00
|
|
|
# Otherwise avoid copying some tables by modifying `upper`. This is not
|
2024-07-18 21:32:32 +00:00
|
|
|
# completely free as the merge direction changes to merging the `lower`
|
|
|
|
# layer up into the higher prioritised `upper` layer (note that the `lower`
|
|
|
|
# argument filter is read-only.) Here again, the `upper` argument must not
|
|
|
|
# be a shared layer/filter.
|
|
|
|
for (rvid,vtx) in lower.sTab.pairs:
|
|
|
|
if not upper.sTab.hasKey(rvid):
|
|
|
|
upper.sTab[rvid] = vtx
|
2023-09-05 18:00:40 +00:00
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
for (rvid,key) in lower.kMap.pairs:
|
|
|
|
if not upper.kMap.hasKey(rvid):
|
|
|
|
upper.kMap[rvid] = key
|
2024-07-17 19:27:33 +00:00
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
for (accPath,leafVtx) in lower.accLeaves.pairs:
|
|
|
|
if not upper.accLeaves.hasKey(accPath):
|
|
|
|
upper.accLeaves[accPath] = leafVtx
|
2024-07-17 19:27:33 +00:00
|
|
|
|
2024-07-18 21:32:32 +00:00
|
|
|
for (mixPath,leafVtx) in lower.stoLeaves.pairs:
|
|
|
|
if not upper.stoLeaves.hasKey(mixPath):
|
|
|
|
upper.stoLeaves[mixPath] = leafVtx
|
|
|
|
result = upper
|
2023-09-05 18:00:40 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|