forked from pk910/dynamic-ssz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
specvals.go
45 lines (37 loc) · 1.22 KB
/
specvals.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
36
37
38
39
40
41
42
43
44
45
// dynssz: Dynamic SSZ encoding/decoding for Ethereum with fastssz efficiency.
// This file is part of the dynssz package.
// Copyright (c) 2024 by pk910. Refer to LICENSE for more information.
package dynssz
import (
"fmt"
"gopkg.in/Knetic/govaluate.v3"
)
type cachedSpecValue struct {
resolved bool
value uint64
}
func (d *DynSsz) getSpecValue(name string) (bool, uint64, error) {
if cachedValue := d.specValueCache[name]; cachedValue != nil {
return cachedValue.resolved, cachedValue.value, nil
}
cachedValue := &cachedSpecValue{}
expression, err := govaluate.NewEvaluableExpression(name)
if err != nil {
return false, 0, fmt.Errorf("error parsing dynamic spec expression: %v", err)
}
result, err := expression.Evaluate(d.specValues)
if err == nil {
value, ok := result.(float64)
if ok {
cachedValue.resolved = true
cachedValue.value = uint64(value)
if float64(cachedValue.value) < value {
// rounding issue - always round up to full bytes as we can't serialize parial bytes
cachedValue.value++
}
}
}
// fmt.Printf("spec lookup %v, ok: %v, value: %v\n", name, cachedValue.resolved, cachedValue.value)
d.specValueCache[name] = cachedValue
return cachedValue.resolved, cachedValue.value, nil
}