-
Notifications
You must be signed in to change notification settings - Fork 320
/
managers.go
95 lines (83 loc) · 2.36 KB
/
managers.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package protocol
import (
"github.com/iotexproject/go-pkgs/hash"
"github.com/pkg/errors"
"github.com/iotexproject/iotex-core/state"
)
// NamespaceOption creates an option for given namesapce
func NamespaceOption(ns string) StateOption {
return func(sc *StateConfig) error {
sc.Namespace = ns
return nil
}
}
// KeyOption sets the key for call
func KeyOption(key []byte) StateOption {
return func(cfg *StateConfig) error {
cfg.Key = make([]byte, len(key))
copy(cfg.Key, key)
return nil
}
}
// KeysOption sets the key for call
func KeysOption(f func() ([][]byte, error)) StateOption {
return func(cfg *StateConfig) (err error) {
cfg.Keys, err = f()
return err
}
}
// LegacyKeyOption sets the key for call with legacy key
func LegacyKeyOption(key hash.Hash160) StateOption {
return func(cfg *StateConfig) error {
cfg.Key = make([]byte, len(key[:]))
copy(cfg.Key, key[:])
return nil
}
}
// CreateStateConfig creates a config for accessing stateDB
func CreateStateConfig(opts ...StateOption) (*StateConfig, error) {
cfg := StateConfig{}
for _, opt := range opts {
if err := opt(&cfg); err != nil {
return nil, errors.Wrap(err, "failed to execute state option")
}
}
return &cfg, nil
}
type (
// StateConfig is the config for accessing stateDB
StateConfig struct {
Namespace string // namespace used by state's storage
Key []byte
Keys [][]byte
}
// StateOption sets parameter for access state
StateOption func(*StateConfig) error
// StateReader defines an interface to read stateDB
StateReader interface {
Height() (uint64, error)
State(interface{}, ...StateOption) (uint64, error)
States(...StateOption) (uint64, state.Iterator, error)
ReadView(string) (interface{}, error)
}
// StateManager defines the stateDB interface atop IoTeX blockchain
StateManager interface {
StateReader
// Accounts
Snapshot() int
Revert(int) error
// General state
PutState(interface{}, ...StateOption) (uint64, error)
DelState(...StateOption) (uint64, error)
WriteView(string, interface{}) error
Dock
}
// Dock defines an interface for protocol to read/write their private data in StateReader/Manager
// data are stored as interface{}, user needs to type-assert on their own upon Unload()
Dock interface {
ProtocolDirty(string) bool
Load(string, string, interface{}) error
Unload(string, string, interface{}) error
Reset()
}
)