-
Notifications
You must be signed in to change notification settings - Fork 175
/
block_data.go
232 lines (190 loc) · 5.64 KB
/
block_data.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package primary
import (
"fmt"
"sync"
"github.com/onflow/flow-go/fvm/storage/errors"
"github.com/onflow/flow-go/fvm/storage/logical"
"github.com/onflow/flow-go/fvm/storage/snapshot"
"github.com/onflow/flow-go/fvm/storage/state"
"github.com/onflow/flow-go/model/flow"
)
const (
conflictErrorTemplate = "invalid transaction: committed txn %d conflicts " +
"with executing txn %d with snapshot at %d (Conflicting register: %v)"
)
// BlockData is a rudimentary in-memory MVCC database for storing (RegisterID,
// RegisterValue) pairs for a particular block. The database enforces
// atomicity, consistency, and isolation, but not durability (The transactions
// are made durable by the block computer using aggregated execution snapshots).
type BlockData struct {
mutex sync.RWMutex
latestSnapshot timestampedSnapshotTree // Guarded by mutex
}
type TransactionData struct {
block *BlockData
executionTime logical.Time
isSnapshotReadTransaction bool
snapshot *rebaseableTimestampedSnapshotTree
state.NestedTransactionPreparer
finalizedExecutionSnapshot *snapshot.ExecutionSnapshot
}
// Note: storageSnapshot must be thread safe.
func NewBlockData(
storageSnapshot snapshot.StorageSnapshot,
snapshotTime logical.Time,
) *BlockData {
return &BlockData{
latestSnapshot: newTimestampedSnapshotTree(
storageSnapshot,
logical.Time(snapshotTime)),
}
}
func (block *BlockData) LatestSnapshot() timestampedSnapshotTree {
block.mutex.RLock()
defer block.mutex.RUnlock()
return block.latestSnapshot
}
func (block *BlockData) newTransactionData(
isSnapshotReadTransaction bool,
executionTime logical.Time,
parameters state.StateParameters,
) *TransactionData {
snapshot := newRebaseableTimestampedSnapshotTree(block.LatestSnapshot())
return &TransactionData{
block: block,
executionTime: executionTime,
snapshot: snapshot,
isSnapshotReadTransaction: isSnapshotReadTransaction,
NestedTransactionPreparer: state.NewTransactionState(
snapshot,
parameters),
}
}
func (block *BlockData) NewTransactionData(
executionTime logical.Time,
parameters state.StateParameters,
) (
*TransactionData,
error,
) {
if executionTime < 0 ||
executionTime > logical.LargestNormalTransactionExecutionTime {
return nil, fmt.Errorf(
"invalid tranaction: execution time out of bound")
}
txn := block.newTransactionData(
false,
executionTime,
parameters)
if txn.SnapshotTime() > executionTime {
return nil, fmt.Errorf(
"invalid transaction: snapshot > execution: %v > %v",
txn.SnapshotTime(),
executionTime)
}
return txn, nil
}
func (block *BlockData) NewSnapshotReadTransactionData(
parameters state.StateParameters,
) *TransactionData {
return block.newTransactionData(
true,
logical.EndOfBlockExecutionTime,
parameters)
}
func (txn *TransactionData) SnapshotTime() logical.Time {
return txn.snapshot.SnapshotTime()
}
func (txn *TransactionData) validate(
latestSnapshot timestampedSnapshotTree,
) error {
validatedSnapshotTime := txn.SnapshotTime()
if latestSnapshot.SnapshotTime() <= validatedSnapshotTime {
// transaction's snapshot is up-to-date.
return nil
}
var readSet map[flow.RegisterID]struct{}
if txn.finalizedExecutionSnapshot != nil {
readSet = txn.finalizedExecutionSnapshot.ReadSet
} else {
readSet = txn.InterimReadSet()
}
updates, err := latestSnapshot.UpdatesSince(validatedSnapshotTime)
if err != nil {
return fmt.Errorf("invalid transaction: %w", err)
}
for i, writeSet := range updates {
hasConflict, registerId := intersect(writeSet, readSet)
if hasConflict {
return errors.NewRetryableConflictError(
conflictErrorTemplate,
validatedSnapshotTime+logical.Time(i),
txn.executionTime,
validatedSnapshotTime,
registerId)
}
}
txn.snapshot.Rebase(latestSnapshot)
return nil
}
func (txn *TransactionData) Validate() error {
return txn.validate(txn.block.LatestSnapshot())
}
func (txn *TransactionData) Finalize() error {
executionSnapshot, err := txn.FinalizeMainTransaction()
if err != nil {
return err
}
// NOTE: Since cadence does not support the notion of read only execution,
// snapshot read transaction execution can inadvertently produce a non-empty
// write set. We'll just drop these updates.
if txn.isSnapshotReadTransaction {
executionSnapshot.WriteSet = nil
}
txn.finalizedExecutionSnapshot = executionSnapshot
return nil
}
func (block *BlockData) commit(txn *TransactionData) error {
if txn.finalizedExecutionSnapshot == nil {
return fmt.Errorf("invalid transaction: transaction not finalized.")
}
block.mutex.Lock()
defer block.mutex.Unlock()
err := txn.validate(block.latestSnapshot)
if err != nil {
return err
}
// Don't perform actual commit for snapshot read transaction since they
// do not advance logical time.
if txn.isSnapshotReadTransaction {
return nil
}
latestSnapshotTime := block.latestSnapshot.SnapshotTime()
if latestSnapshotTime < txn.executionTime {
// i.e., transactions are committed out-of-order.
return fmt.Errorf(
"invalid transaction: missing commit range [%v, %v)",
latestSnapshotTime,
txn.executionTime)
}
if block.latestSnapshot.SnapshotTime() > txn.executionTime {
// i.e., re-commiting an already committed transaction.
return fmt.Errorf(
"invalid transaction: non-increasing time (%v >= %v)",
latestSnapshotTime-1,
txn.executionTime)
}
block.latestSnapshot = block.latestSnapshot.Append(
txn.finalizedExecutionSnapshot)
return nil
}
func (txn *TransactionData) Commit() (
*snapshot.ExecutionSnapshot,
error,
) {
err := txn.block.commit(txn)
if err != nil {
return nil, err
}
return txn.finalizedExecutionSnapshot, nil
}