This repository has been archived by the owner on May 13, 2022. It is now read-only.
/
call_frame.go
65 lines (56 loc) · 1.79 KB
/
call_frame.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
package engine
import (
"github.com/hyperledger/burrow/acm/acmstate"
"github.com/hyperledger/burrow/execution/errors"
)
type CallFrame struct {
// Cache this State wraps
*acmstate.Cache
// Where we sync
backend acmstate.ReaderWriter
// In order for nested cache to inherit any options
cacheOptions []acmstate.CacheOption
// Depth of the call stack
callStackDepth uint64
// Max call stack depth
maxCallStackDepth uint64
}
// Create a new CallFrame to hold state updates at a particular level in the call stack
func NewCallFrame(st acmstate.ReaderWriter, cacheOptions ...acmstate.CacheOption) *CallFrame {
return newCallFrame(st, 0, 0, cacheOptions...)
}
func newCallFrame(st acmstate.ReaderWriter, stackDepth uint64, maxCallStackDepth uint64, cacheOptions ...acmstate.CacheOption) *CallFrame {
return &CallFrame{
Cache: acmstate.NewCache(st, cacheOptions...),
backend: st,
cacheOptions: cacheOptions,
callStackDepth: stackDepth,
maxCallStackDepth: maxCallStackDepth,
}
}
// Put this CallFrame in permanent read-only mode
func (st *CallFrame) ReadOnly() *CallFrame {
acmstate.ReadOnly(st.Cache)
return st
}
func (st *CallFrame) WithMaxCallStackDepth(max uint64) *CallFrame {
st.maxCallStackDepth = max
return st
}
func (st *CallFrame) NewFrame(cacheOptions ...acmstate.CacheOption) (*CallFrame, error) {
if st.maxCallStackDepth > 0 && st.maxCallStackDepth == st.callStackDepth {
return nil, errors.Codes.CallStackOverflow
}
return newCallFrame(st.Cache, st.callStackDepth+1, st.maxCallStackDepth,
append(st.cacheOptions, cacheOptions...)...), nil
}
func (st *CallFrame) Sync() error {
err := st.Cache.Sync(st.backend)
if err != nil {
return errors.AsException(err)
}
return nil
}
func (st *CallFrame) CallStackDepth() uint64 {
return st.callStackDepth
}