2019-09-03 03:12:09 +00:00
|
|
|
# beacon_chain
|
2021-08-25 14:51:52 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2019-09-03 03:12:09 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
2019-09-03 03:12:09 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
macros,
|
2021-08-12 13:08:20 +00:00
|
|
|
../../beacon_chain/spec/datatypes/base,
|
|
|
|
../../beacon_chain/ssz/types
|
2019-09-03 03:12:09 +00:00
|
|
|
# digest is necessary for them to be printed as hex
|
|
|
|
|
2021-08-12 13:08:20 +00:00
|
|
|
export base.`==`
|
2019-09-03 03:12:09 +00:00
|
|
|
|
|
|
|
# ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
# This tool inspects and compare 2 instances of a type recursively
|
|
|
|
# highlighting the differences
|
|
|
|
|
|
|
|
const builtinTypes = [
|
|
|
|
"int", "int8", "int16", "int32", "int64",
|
|
|
|
"uint", "uint8", "uint16", "uint32", "uint64",
|
|
|
|
"byte", "float32", "float64",
|
|
|
|
# "array", "seq", # wrapped in nnkBracketExpr
|
|
|
|
"char", "string"
|
|
|
|
]
|
|
|
|
|
|
|
|
proc compareStmt(xSubField, ySubField: NimNode, stmts: var NimNode) =
|
|
|
|
let xStr = $xSubField.toStrLit
|
|
|
|
let yStr = $ySubField.toStrLit
|
|
|
|
|
2020-08-15 17:33:58 +00:00
|
|
|
let isEqual = bindSym("==") # Bind all expose equality, in particular for BlsValue
|
2019-09-03 03:12:09 +00:00
|
|
|
stmts.add quote do:
|
|
|
|
doAssert(
|
2020-08-15 17:33:58 +00:00
|
|
|
`isEqual`(`xSubField`, `ySubField`),
|
2019-09-03 03:12:09 +00:00
|
|
|
"\nDiff: " & `xStr` & " = " & $`xSubField` & "\n" &
|
|
|
|
"and " & `yStr` & " = " & $`ySubField` & "\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
proc compareContainerStmt(xSubField, ySubField: NimNode, stmts: var NimNode) =
|
|
|
|
let xStr = $xSubField.toStrLit
|
|
|
|
let yStr = $ySubField.toStrLit
|
|
|
|
|
2020-08-15 17:33:58 +00:00
|
|
|
let isEqual = bindSym("==") # Bind all expose equality, in particular for BlsValue
|
2019-09-03 03:12:09 +00:00
|
|
|
stmts.add quote do:
|
|
|
|
doAssert(
|
2020-08-15 17:33:58 +00:00
|
|
|
`isEqual`(`xSubField`.len, `ySubField`.len),
|
2019-09-03 03:12:09 +00:00
|
|
|
"\nDiff: " & `xStr` & ".len = " & $`xSubField`.len & "\n" &
|
|
|
|
"and " & `yStr` & ".len = " & $`ySubField`.len & "\n"
|
|
|
|
)
|
|
|
|
for idx in `xSubField`.low .. `xSubField`.high:
|
|
|
|
doAssert(
|
2020-08-15 17:33:58 +00:00
|
|
|
`isEqual`(`xSubField`[idx], `ySubField`[idx]),
|
2019-09-03 03:12:09 +00:00
|
|
|
"\nDiff: " & `xStr` & "[" & $idx & "] = " & $`xSubField`[idx] & "\n" &
|
|
|
|
"and " & `yStr` & "[" & $idx & "] = " & $`ySubField`[idx] & "\n"
|
|
|
|
)
|
|
|
|
|
2021-08-25 14:51:52 +00:00
|
|
|
func inspectType(tImpl, xSubField, ySubField: NimNode, stmts: var NimNode) =
|
2019-09-03 03:12:09 +00:00
|
|
|
# echo "kind: " & $tImpl.kind
|
|
|
|
# echo " -- field: " & $xSubField.toStrLit
|
|
|
|
case tImpl.kind
|
|
|
|
of nnkObjectTy:
|
|
|
|
# pass the records
|
|
|
|
let records = tImpl[2]
|
|
|
|
records.expectKind(nnkRecList)
|
|
|
|
for decl in records:
|
|
|
|
inspectType(
|
|
|
|
decl[1], # field type
|
|
|
|
nnkDotExpr.newTree(xSubField, decl[0]), # Accessor
|
|
|
|
nnkDotExpr.newTree(ySubField, decl[0]),
|
|
|
|
stmts
|
|
|
|
)
|
|
|
|
of {nnkRefTy, nnkDistinctTy}:
|
|
|
|
inspectType(tImpl[0], xSubField, ySubField, stmts)
|
|
|
|
of {nnkSym, nnkBracketExpr}:
|
|
|
|
if tImpl.kind == nnkBracketExpr:
|
2020-05-28 16:34:59 +00:00
|
|
|
# doAssert tImpl[0].eqIdent"List" or tImpl[0].eqIdent"seq" or tImpl[0].eqIdent"array", "Error: unsupported generic type: " & $tImpl[0]
|
2019-09-03 03:12:09 +00:00
|
|
|
compareContainerStmt(xSubField, ySubField, stmts)
|
|
|
|
elif $tImpl in builtinTypes:
|
|
|
|
compareStmt(xSubField, ySubField, stmts)
|
|
|
|
elif $tImpl in ["ValidatorSig", "ValidatorPubKey"]:
|
|
|
|
# Workaround BlsValue being a case object
|
|
|
|
compareStmt(xSubField, ySubField, stmts)
|
|
|
|
else:
|
|
|
|
inspectType(tImpl.getTypeImpl(), xSubField, ySubField, stmts)
|
|
|
|
else:
|
|
|
|
error "Unsupported kind: " & $tImpl.kind &
|
|
|
|
" for field \"" & $xSubField.toStrLit &
|
|
|
|
"\" of type \"" & tImpl.repr
|
|
|
|
|
2020-05-28 16:34:59 +00:00
|
|
|
macro reportDiff*(x, y: typed): untyped =
|
2019-11-22 15:47:08 +00:00
|
|
|
doAssert sameType(x, y)
|
2019-09-03 03:12:09 +00:00
|
|
|
result = newStmtList()
|
|
|
|
|
|
|
|
let typeImpl = x.getTypeImpl
|
|
|
|
inspectType(typeImpl, x, y, result)
|
|
|
|
|
|
|
|
# echo result.toStrLit
|
|
|
|
|
|
|
|
# -----------------------------------------
|