-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathspecvals.go
More file actions
57 lines (48 loc) · 1.66 KB
/
specvals.go
File metadata and controls
57 lines (48 loc) · 1.66 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2025 pk910
// SPDX-License-Identifier: Apache-2.0
// This file is part of the dynamic-ssz library.
package dynssz
import (
"fmt"
"github.com/casbin/govaluate"
)
type cachedSpecValue struct {
resolved bool
value uint64
}
// ResolveSpecValue resolves a dynamic specification value by name. The name can
// be a simple identifier (e.g., "MAX_VALIDATORS_PER_COMMITTEE") or a mathematical
// expression referencing spec values. Results are cached for subsequent lookups.
//
// Returns whether the value was resolved, the uint64 value, and any parse error.
// If the name references undefined spec values, resolved will be false with no error.
func (d *DynSsz) ResolveSpecValue(name string) (bool, uint64, error) {
d.specCacheMutex.RLock()
cachedValue := d.specValueCache[name]
d.specCacheMutex.RUnlock()
if 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: %w", 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.specCacheMutex.Lock()
d.specValueCache[name] = cachedValue
d.specCacheMutex.Unlock()
return cachedValue.resolved, cachedValue.value, nil
}