-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.go
35 lines (32 loc) · 965 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// ssz: Go Simple Serialize (SSZ) codec library
// Copyright 2024 ssz Authors
// SPDX-License-Identifier: BSD-3-Clause
package ssz
import "fmt"
// PrecomputeStaticSizeCache is a helper to precompute SSZ (static) sizes for a
// monolith type on different forks.
//
// For non-monolith types that are constant across forks (or are not meant to be
// used across forks), all the sizes will be the same so might as well hard-code
// it instead.
func PrecomputeStaticSizeCache(obj Object) []uint32 {
var (
sizes = make([]uint32, ForkFuture)
sizer = &Sizer{codec: new(Codec)}
)
switch v := obj.(type) {
case StaticObject:
for fork := 0; fork < len(sizes); fork++ {
sizer.codec.fork = Fork(fork)
sizes[fork] = v.SizeSSZ(sizer)
}
case DynamicObject:
for fork := 0; fork < len(sizes); fork++ {
sizer.codec.fork = Fork(fork)
sizes[fork] = v.SizeSSZ(sizer, true)
}
default:
panic(fmt.Sprintf("unsupported type: %T", obj))
}
return sizes
}