-
Notifications
You must be signed in to change notification settings - Fork 179
/
programs.go
151 lines (122 loc) · 4.04 KB
/
programs.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
143
144
145
146
147
148
149
150
151
package handler
import (
"fmt"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/programs"
"github.com/onflow/flow-go/fvm/state"
)
type stackEntry struct {
state *state.State
location common.Location
}
// ProgramsHandler manages operations using Programs storage.
// It's separation of concern for hostEnv
// Cadence contract guarantees that Get/Set methods will be called in a LIFO manner,
// so we use stack based approach here. During successful execution stack should be cleared
// naturally, making cleanup method essentially no-op. But if something goes wrong, all nested
// views must be merged in order to make sure they are recorded
type ProgramsHandler struct {
masterState *state.StateHolder
viewsStack []stackEntry
Programs *programs.Programs
initialState *state.State
}
// NewProgramsHandler construts a new ProgramHandler
func NewProgramsHandler(programs *programs.Programs, stateHolder *state.StateHolder) *ProgramsHandler {
return &ProgramsHandler{
masterState: stateHolder,
viewsStack: nil,
Programs: programs,
initialState: stateHolder.State(),
}
}
func (h *ProgramsHandler) Set(location common.Location, program *interpreter.Program) error {
// ignore empty locations
if location == nil {
return nil
}
// we track only for AddressLocation, so for anything other simply put a value
if _, is := location.(common.AddressLocation); !is {
h.Programs.Set(location, program, nil)
return nil
}
if len(h.viewsStack) == 0 {
return fmt.Errorf("views stack empty while set called, for location %s", location.String())
}
// pop
last := h.viewsStack[len(h.viewsStack)-1]
h.viewsStack = h.viewsStack[0 : len(h.viewsStack)-1]
if last.location.ID() != location.ID() {
return fmt.Errorf("set called for type %s while last get was for %s", location.String(), last.location.String())
}
h.Programs.Set(location, program, last.state)
err := h.mergeState(last.state)
return err
}
func (h *ProgramsHandler) mergeState(state *state.State) error {
if len(h.viewsStack) == 0 {
// if this was last item, merge to the master state
h.masterState.SetActiveState(h.initialState)
} else {
h.masterState.SetActiveState(h.viewsStack[len(h.viewsStack)-1].state)
}
return h.masterState.State().MergeState(state, h.masterState.EnforceInteractionLimits())
}
func (h *ProgramsHandler) Get(location common.Location) (*interpreter.Program, bool) {
// ignore empty locations
if location == nil {
return nil, false
}
program, view, has := h.Programs.Get(location)
if has {
if view != nil { // handle view not set (ie. for non-address locations
err := h.mergeState(view)
if err != nil {
// ignore LedgerIntractionLimitExceededError errors
var interactionLimiExceededErr *errors.LedgerIntractionLimitExceededError
if !errors.As(err, &interactionLimiExceededErr) {
panic(fmt.Sprintf("merge error while getting program, panic: %s", err))
}
}
}
return program, true
}
// we track only for AddressLocation
if _, is := location.(common.AddressLocation); !is {
return nil, false
}
parentState := h.masterState.State()
if len(h.viewsStack) > 0 {
parentState = h.viewsStack[len(h.viewsStack)-1].state
}
childState := parentState.NewChild()
h.viewsStack = append(h.viewsStack, stackEntry{
state: childState,
location: location,
})
h.masterState.SetActiveState(childState)
return nil, false
}
func (h *ProgramsHandler) Cleanup() error {
stackLen := len(h.viewsStack)
if stackLen == 0 {
return nil
}
for i := stackLen - 1; i > 0; i-- {
entry := h.viewsStack[i]
err := h.viewsStack[i-1].state.MergeState(entry.state, h.masterState.EnforceInteractionLimits())
if err != nil {
return fmt.Errorf("cannot merge state while cleanup: %w", err)
}
}
err := h.initialState.MergeState(h.viewsStack[0].state, h.masterState.EnforceInteractionLimits())
if err != nil {
return err
}
// reset the stack
h.viewsStack = nil
h.masterState.SetActiveState(h.initialState)
return nil
}