@@ -2,6 +2,7 @@ package erigonstore
22
33import (
44 "reflect"
5+ "strings"
56
67 "github.com/ethereum/go-ethereum/common"
78 "github.com/pkg/errors"
2829// ObjectStorageRegistry is a registry for object storage
2930type ObjectStorageRegistry struct {
3031 contracts map [string ]map [reflect.Type ]int
31- fallback map [string ]int
32+ ns map [string ]int
33+ nsPrefix map [string ]int
3234}
3335
3436func init () {
@@ -38,7 +40,9 @@ func init() {
3840 assertions .MustNoError (storageRegistry .RegisterNamespace (state .CandsMapNamespace , CandidateMapContractIndex ))
3941 assertions .MustNoError (storageRegistry .RegisterNamespace (state .StakingNamespace , BucketPoolContractIndex ))
4042 assertions .MustNoError (storageRegistry .RegisterNamespace (state .StakingViewNamespace , StakingViewContractIndex ))
41- assertions .MustNoError (storageRegistry .RegisterNamespace (state .StakingNamespace , BucketPoolContractIndex ))
43+ assertions .MustNoError (storageRegistry .RegisterNamespace (state .StakingContractMetaNamespace , StakingViewContractIndex ))
44+ assertions .MustNoError (storageRegistry .RegisterNamespacePrefix (state .ContractStakingBucketNamespacePrefix , StakingViewContractIndex ))
45+ assertions .MustNoError (storageRegistry .RegisterNamespacePrefix (state .ContractStakingBucketTypeNamespacePrefix , StakingViewContractIndex ))
4246
4347 assertions .MustNoError (storageRegistry .RegisterObjectStorage (state .AccountKVNamespace , & state.Account {}, AccountIndex ))
4448 assertions .MustNoError (storageRegistry .RegisterObjectStorage (state .AccountKVNamespace , & state.CandidateList {}, PollLegacyCandidateListContractIndex ))
@@ -59,7 +63,8 @@ func GetObjectStorageRegistry() *ObjectStorageRegistry {
5963func newObjectStorageRegistry () * ObjectStorageRegistry {
6064 return & ObjectStorageRegistry {
6165 contracts : make (map [string ]map [reflect.Type ]int ),
62- fallback : make (map [string ]int ),
66+ ns : make (map [string ]int ),
67+ nsPrefix : make (map [string ]int ),
6368 }
6469}
6570
@@ -109,12 +114,28 @@ func (osr *ObjectStorageRegistry) RegisterNamespace(ns string, index int) error
109114 return osr .register (ns , nil , index )
110115}
111116
117+ // RegisterNamespacePrefix registers a namespace prefix object storage
118+ func (osr * ObjectStorageRegistry ) RegisterNamespacePrefix (prefix string , index int ) error {
119+ if index < AccountIndex || index >= SystemContractCount {
120+ return errors .Errorf ("invalid system contract index %d" , index )
121+ }
122+ return osr .registerPrefix (prefix , index )
123+ }
124+
125+ func (osr * ObjectStorageRegistry ) registerPrefix (ns string , index int ) error {
126+ if _ , exists := osr .nsPrefix [ns ]; exists {
127+ return errors .Wrapf (ErrObjectStorageAlreadyRegistered , "registered: %v" , osr .nsPrefix [ns ])
128+ }
129+ osr .nsPrefix [ns ] = index
130+ return nil
131+ }
132+
112133func (osr * ObjectStorageRegistry ) register (ns string , obj any , index int ) error {
113134 if obj == nil {
114- if _ , exists := osr .fallback [ns ]; exists {
115- return errors .Wrapf (ErrObjectStorageAlreadyRegistered , "registered: %v" , osr .fallback [ns ])
135+ if _ , exists := osr .ns [ns ]; exists {
136+ return errors .Wrapf (ErrObjectStorageAlreadyRegistered , "registered: %v" , osr .ns [ns ])
116137 }
117- osr .fallback [ns ] = index
138+ osr .ns [ns ] = index
118139 return nil
119140 }
120141 types , ok := osr .contracts [ns ]
@@ -130,6 +151,7 @@ func (osr *ObjectStorageRegistry) register(ns string, obj any, index int) error
130151}
131152
132153func (osr * ObjectStorageRegistry ) matchContractIndex (ns string , obj any ) (int , bool ) {
154+ // object specific storage
133155 if obj != nil {
134156 types , ok := osr .contracts [ns ]
135157 if ok {
@@ -139,6 +161,16 @@ func (osr *ObjectStorageRegistry) matchContractIndex(ns string, obj any) (int, b
139161 }
140162 }
141163 }
142- index , exist := osr .fallback [ns ]
143- return index , exist
164+ // namespace specific storage
165+ index , exist := osr .ns [ns ]
166+ if exist {
167+ return index , true
168+ }
169+ // namespace prefix specific storage
170+ for prefix , index := range osr .nsPrefix {
171+ if strings .HasPrefix (ns , prefix ) {
172+ return index , true
173+ }
174+ }
175+ return 0 , false
144176}
0 commit comments