2022-03-09 19:33:18 +00:00
|
|
|
package stategen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state"
|
2022-03-09 19:33:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ErrNotInCache = errors.New("state not found in cache")
|
|
|
|
|
|
|
|
type CachedGetter interface {
|
2022-05-09 22:25:47 +00:00
|
|
|
ByBlockRoot([32]byte) (state.BeaconState, error)
|
2022-03-09 19:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CombinedCache struct {
|
|
|
|
getters []CachedGetter
|
|
|
|
}
|
|
|
|
|
2022-05-09 22:25:47 +00:00
|
|
|
func (c CombinedCache) ByBlockRoot(r [32]byte) (state.BeaconState, error) {
|
2022-03-09 19:33:18 +00:00
|
|
|
for _, getter := range c.getters {
|
2022-05-09 22:25:47 +00:00
|
|
|
st, err := getter.ByBlockRoot(r)
|
2022-03-09 19:33:18 +00:00
|
|
|
if err == nil {
|
|
|
|
return st, nil
|
|
|
|
}
|
|
|
|
if errors.Is(err, ErrNotInCache) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, ErrNotInCache
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ CachedGetter = &CombinedCache{}
|