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

Create helper for max size, as bounded by Limit #20

Closed
arnetheduck opened this issue Jan 3, 2022 · 1 comment · Fixed by #102
Closed

Create helper for max size, as bounded by Limit #20

arnetheduck opened this issue Jan 3, 2022 · 1 comment · Fixed by #102

Comments

@arnetheduck
Copy link
Member

Many types like Attestation have a useful max serialized size that can be computed by using the Limit of every List as upper bound - a helper to compute this value would be generally useful for example to harden the decompression code against overlong messages: status-im/nimbus-eth2#3230 (comment)

@arnetheduck
Copy link
Member Author

import
  std/typetraits,
  ../beacon_chain/spec/eth2_ssz_serialization,
  ../beacon_chain/spec/datatypes/[deneb]

proc maxSize(T: type): int =
  when isFixedSize(T):
    fixedPortionSize(T)
  elif T is HashList:
    maxSize(T.T)
  elif T is BitList:
    (T.maxLen div 8) + 1
  elif T is BitArray:
    (T.maxLen + 7) div 8
  elif T is List:
    type E = T.T
    when isFixedSize(E):
      T.maxLen * maxSize(E)
    else:
      T.maxLen * (maxSize(E) + 4)

  elif T is object|tuple:
    var size = fixedPortionSize(T)
    for field in fields(default(T)):
      when not isFixedSize(typeof(field)):
        size += maxSize(typeof(field))
    size
  else:
    0

proc printSizes(T: type, indent: string) =
  when T is object:
    for name, field in fieldPairs(default(T)):
      type F = typeof(field)
      echo indent, name, ": ", maxSize(F)

      when not isFixedSize(F):
        printSizes(F, indent & "  ")

proc printSizes(T: type) =
  echo T.name, ": ", maxSize(T)
  printSizes(T, "  ")

printSizes(deneb.SignedBeaconBlock)
printSizes(Attestation)

a humble beginning that gets us as far as the deneb block I think

etan-status added a commit that referenced this issue Jan 9, 2025
As requested in #20, add a helper to compute maximum theoretical length
of an SSZ object. Note that there is an extra limit of 4GB due to offset
size that is not considered by the function.
@etan-status etan-status linked a pull request Jan 9, 2025 that will close this issue
tersec pushed a commit that referenced this issue Jan 10, 2025
As requested in #20, add a helper to compute maximum theoretical length
of an SSZ object. Note that there is an extra limit of 4GB due to offset
size that is not considered by the function.
etan-status added a commit that referenced this issue Jan 10, 2025
Introduces `minSize` as a counterpart to `maxSize` (#20).
etan-status added a commit that referenced this issue Jan 10, 2025
Introduces `minSize` as a counterpart to `maxSize` (#20).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant