-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathprograms.go
More file actions
96 lines (74 loc) · 1.98 KB
/
programs.go
File metadata and controls
96 lines (74 loc) · 1.98 KB
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
package programs
import (
"sync"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/flow-go/fvm/state"
)
// TODO(patrick): Remove after emulator is updated.
type Programs struct {
lock sync.RWMutex
block *BlockPrograms
currentTxn *TransactionPrograms
logicalTime LogicalTime
}
func NewEmptyPrograms() *Programs {
block := NewEmptyBlockPrograms()
txn, err := block.NewTransactionPrograms(0, 0)
if err != nil {
panic(err)
}
return &Programs{
block: block,
currentTxn: txn,
logicalTime: 0,
}
}
func (p *Programs) ChildPrograms() *Programs {
p.lock.RLock()
defer p.lock.RUnlock()
childBlock := p.block.NewChildBlockPrograms()
txn, err := childBlock.NewTransactionPrograms(0, 0)
if err != nil {
panic(err)
}
return &Programs{
block: childBlock,
currentTxn: txn,
logicalTime: 0,
}
}
func (p *Programs) NextTxIndexForTestingOnly() uint32 {
return p.block.NextTxIndexForTestingOnly()
}
func (p *Programs) GetForTestingOnly(location common.Location) (*interpreter.Program, *state.State, bool) {
return p.Get(location)
}
// Get returns stored program, state which contains changes which correspond to loading this program,
// and boolean indicating if the value was found
func (p *Programs) Get(location common.Location) (*interpreter.Program, *state.State, bool) {
p.lock.RLock()
defer p.lock.RUnlock()
return p.currentTxn.Get(location)
}
func (p *Programs) Set(location common.Location, program *interpreter.Program, state *state.State) {
p.lock.RLock()
defer p.lock.RUnlock()
p.currentTxn.Set(location, program, state)
}
func (p *Programs) Cleanup(modifiedSets ModifiedSetsInvalidator) {
p.lock.Lock()
defer p.lock.Unlock()
p.currentTxn.AddInvalidator(modifiedSets)
var err error
err = p.currentTxn.Commit()
if err != nil {
panic(err)
}
p.logicalTime++
txn, err := p.block.NewTransactionPrograms(p.logicalTime, p.logicalTime)
if err != nil {
panic(err)
}
p.currentTxn = txn
}