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,
|
2023-09-05 18:00:40 +00:00
|
|
|
results,
|
|
|
|
".."/[aristo_desc, aristo_get]
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-03 20:10:35 +00:00
|
|
|
proc deltaMerge*(
|
2023-09-05 18:00:40 +00:00
|
|
|
db: AristoDbRef;
|
2024-06-03 20:10:35 +00:00
|
|
|
upper: LayerDeltaRef; # Src filter, `nil` is ok
|
|
|
|
lower: LayerDeltaRef; # Trg filter, `nil` is ok
|
|
|
|
beStateRoot: HashKey; # Merkle hash key
|
|
|
|
): Result[LayerDeltaRef,(VertexID,AristoError)] =
|
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
|
|
|
|
## stacked and the database access is `upper -> lower -> backend` whereas
|
|
|
|
## the `src/trg` matching logic goes the other way round.
|
|
|
|
##
|
|
|
|
## The resuting filter has no `FilterID` set.
|
|
|
|
##
|
|
|
|
## Comparing before and after merge
|
|
|
|
## ::
|
|
|
|
## arguments | merged result
|
|
|
|
## --------------------------------+------------------------------------
|
|
|
|
## (src2==trg1) --> upper --> trg2 |
|
|
|
|
## | (src1==trg0) --> newFilter --> trg2
|
|
|
|
## (src1==trg0) --> lower --> trg1 |
|
|
|
|
## |
|
|
|
|
## beStateRoot --> trg0 |
|
|
|
|
##
|
|
|
|
# Degenerate case: `upper` is void
|
|
|
|
if lower.isNil:
|
|
|
|
if upper.isNil:
|
|
|
|
# Even more degenerate case when both filters are void
|
2024-06-03 20:10:35 +00:00
|
|
|
return ok LayerDeltaRef(nil)
|
2023-09-05 18:00:40 +00:00
|
|
|
if upper.src != beStateRoot:
|
|
|
|
return err((VertexID(1),FilStateRootMismatch))
|
|
|
|
return ok(upper)
|
|
|
|
|
|
|
|
# Degenerate case: `upper` is non-trivial and `lower` is void
|
|
|
|
if upper.isNil:
|
|
|
|
if lower.src != beStateRoot:
|
|
|
|
return err((VertexID(0), FilStateRootMismatch))
|
|
|
|
return ok(lower)
|
|
|
|
|
|
|
|
# Verify stackability
|
2024-06-03 20:10:35 +00:00
|
|
|
let lowerTrg = lower.kMap.getOrVoid VertexID(1)
|
|
|
|
if upper.src != lowerTrg:
|
2023-09-05 18:00:40 +00:00
|
|
|
return err((VertexID(0), FilTrgSrcMismatch))
|
|
|
|
if lower.src != beStateRoot:
|
|
|
|
return err((VertexID(0), FilStateRootMismatch))
|
|
|
|
|
|
|
|
# There is no need to deep copy table vertices as they will not be modified.
|
2024-06-03 20:10:35 +00:00
|
|
|
let newFilter = LayerDeltaRef(
|
2023-09-05 18:00:40 +00:00
|
|
|
src: lower.src,
|
|
|
|
sTab: lower.sTab,
|
|
|
|
kMap: lower.kMap,
|
2024-06-03 20:10:35 +00:00
|
|
|
vGen: upper.vGen)
|
2023-09-05 18:00:40 +00:00
|
|
|
|
|
|
|
for (vid,vtx) in upper.sTab.pairs:
|
|
|
|
if vtx.isValid or not newFilter.sTab.hasKey vid:
|
|
|
|
newFilter.sTab[vid] = vtx
|
|
|
|
elif newFilter.sTab.getOrVoid(vid).isValid:
|
2024-04-26 13:43:52 +00:00
|
|
|
let rc = db.getVtxUbe vid
|
2023-09-05 18:00:40 +00:00
|
|
|
if rc.isOk:
|
|
|
|
newFilter.sTab[vid] = vtx # VertexRef(nil)
|
|
|
|
elif rc.error == GetVtxNotFound:
|
|
|
|
newFilter.sTab.del vid
|
|
|
|
else:
|
|
|
|
return err((vid,rc.error))
|
|
|
|
|
|
|
|
for (vid,key) in upper.kMap.pairs:
|
|
|
|
if key.isValid or not newFilter.kMap.hasKey vid:
|
|
|
|
newFilter.kMap[vid] = key
|
|
|
|
elif newFilter.kMap.getOrVoid(vid).isValid:
|
2024-04-26 13:43:52 +00:00
|
|
|
let rc = db.getKeyUbe vid
|
2023-09-05 18:00:40 +00:00
|
|
|
if rc.isOk:
|
2023-11-08 12:18:32 +00:00
|
|
|
newFilter.kMap[vid] = key
|
2023-09-05 18:00:40 +00:00
|
|
|
elif rc.error == GetKeyNotFound:
|
|
|
|
newFilter.kMap.del vid
|
|
|
|
else:
|
|
|
|
return err((vid,rc.error))
|
|
|
|
|
2024-02-01 21:27:48 +00:00
|
|
|
# Check consistency
|
2024-06-03 20:10:35 +00:00
|
|
|
if (newFilter.src == newFilter.kMap.getOrVoid(VertexID 1)) !=
|
2024-02-01 21:27:48 +00:00
|
|
|
(newFilter.sTab.len == 0 and newFilter.kMap.len == 0):
|
|
|
|
return err((VertexID(0),FilSrcTrgInconsistent))
|
|
|
|
|
2023-09-05 18:00:40 +00:00
|
|
|
ok newFilter
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|