-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
142 lines (110 loc) · 3.53 KB
/
context.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package engineio
import (
"encoding/json"
"errors"
"github.com/sarvalabs/go-moi-identifiers"
"github.com/sarvalabs/go-polo"
"gopkg.in/yaml.v3"
)
type (
// Hash is a 256-bit checksum digest
Hash = [32]byte
)
// CtxDriver represents an interface for accessing and manipulating
// context information of an account state. It is bounded to the context
// of particular account and can only mutate within applicable portions
// of the context state within the bounds of the logic's namespace
type CtxDriver interface {
Address() identifiers.Address
LogicID() identifiers.LogicID
GetStorageEntry([]byte) ([]byte, bool)
SetStorageEntry([]byte, []byte) bool
}
// ContextStateMatrix is matrix indicating the use of different
// types of context state and the element pointers for them
type ContextStateMatrix map[ContextStateKind]ElementPtr
// Persistent indicates if the ContextStateMatrix has an entry for PersistentState
func (matrix ContextStateMatrix) Persistent() bool {
_, exists := matrix[PersistentState]
return exists
}
// Ephemeral indicates if the ContextStateMatrix has an entry for EphemeralState
func (matrix ContextStateMatrix) Ephemeral() bool {
_, exists := matrix[EphemeralState]
return exists
}
// ContextStateKind represents the scope of stateful data in a context
type ContextStateKind int
const (
PersistentState ContextStateKind = iota
EphemeralState
)
var contextStateKindToString = map[ContextStateKind]string{
PersistentState: "persistent",
EphemeralState: "ephemeral",
}
var contextStateKindFromString = map[string]ContextStateKind{
"persistent": PersistentState,
"ephemeral": EphemeralState,
}
// String implements the Stringer interface for ContextStateKind
func (state ContextStateKind) String() string {
str, ok := contextStateKindToString[state]
if !ok {
panic("unknown ContextStateKind variant")
}
return str
}
// Polorize implements the polo.Polorizable interface for ContextStateKind
func (state ContextStateKind) Polorize() (*polo.Polorizer, error) {
polorizer := polo.NewPolorizer()
polorizer.PolorizeString(state.String())
return polorizer, nil
}
// Depolorize implements the polo.Depolorizable interface for ContextStateKind
func (state *ContextStateKind) Depolorize(depolorizer *polo.Depolorizer) error {
raw, err := depolorizer.DepolorizeString()
if err != nil {
return err
}
kind, ok := contextStateKindFromString[raw]
if !ok {
return errors.New("invalid ContextStateKind value")
}
*state = kind
return nil
}
// MarshalJSON implements the json.Marshaller interface for ContextStateKind
func (state ContextStateKind) MarshalJSON() ([]byte, error) {
return json.Marshal(state.String())
}
// UnmarshalJSON implements the json.Unmarshaller interface for ContextStateKind
func (state *ContextStateKind) UnmarshalJSON(data []byte) error {
raw := new(string)
if err := json.Unmarshal(data, raw); err != nil {
return err
}
kind, ok := contextStateKindFromString[*raw]
if !ok {
return errors.New("invalid ContextStateKind value")
}
*state = kind
return nil
}
// MarshalYAML implements the yaml.Marshaller interface for ContextStateKind
func (state ContextStateKind) MarshalYAML() (interface{}, error) {
return state.String(), nil
}
// UnmarshalYAML implements the yaml.Unmarshaller interface for ContextStateKind
func (state *ContextStateKind) UnmarshalYAML(node *yaml.Node) error {
raw := new(string)
if err := node.Decode(raw); err != nil {
return err
}
kind, ok := contextStateKindFromString[*raw]
if !ok {
return errors.New("invalid ContextStateKind value")
}
*state = kind
return nil
}