-
Notifications
You must be signed in to change notification settings - Fork 175
/
spock_state.go
178 lines (144 loc) · 4.22 KB
/
spock_state.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
package state
import (
"encoding/binary"
"fmt"
"github.com/onflow/crypto/hash"
"github.com/onflow/flow-go/fvm/storage/snapshot"
"github.com/onflow/flow-go/model/flow"
)
var (
// Note: encoding the operation type as part of the spock hash
// prevents operation injection/substitution attacks.
getMarker = []byte("1")
setMarker = []byte("2")
dropChangesMarker = []byte("3")
mergeMarker = []byte("4")
)
type spockState struct {
*storageState
spockSecretHasher hash.Hasher
// NOTE: spockState is no longer accessible once Finalize is called. We
// can't support access after Finalize since spockSecretHasher.SumHash is
// not idempotent. Repeated calls to SumHash (without modifying the input)
// may return different hashes.
finalizedSpockSecret []byte
}
func newSpockState(base snapshot.StorageSnapshot) *spockState {
return &spockState{
storageState: newStorageState(base),
spockSecretHasher: hash.NewSHA3_256(),
}
}
func (state *spockState) NewChild() *spockState {
return &spockState{
storageState: state.storageState.NewChild(),
spockSecretHasher: hash.NewSHA3_256(),
}
}
func (state *spockState) Finalize() *snapshot.ExecutionSnapshot {
if state.finalizedSpockSecret == nil {
state.finalizedSpockSecret = state.spockSecretHasher.SumHash()
}
snapshot := state.storageState.Finalize()
snapshot.SpockSecret = state.finalizedSpockSecret
return snapshot
}
func (state *spockState) Merge(snapshot *snapshot.ExecutionSnapshot) error {
if state.finalizedSpockSecret != nil {
return fmt.Errorf("cannot Merge on a finalized state")
}
_, err := state.spockSecretHasher.Write(mergeMarker)
if err != nil {
return fmt.Errorf("merge SPoCK failed: %w", err)
}
_, err = state.spockSecretHasher.Write(snapshot.SpockSecret)
if err != nil {
return fmt.Errorf("merge SPoCK failed: %w", err)
}
return state.storageState.Merge(snapshot)
}
func (state *spockState) Set(
id flow.RegisterID,
value flow.RegisterValue,
) error {
if state.finalizedSpockSecret != nil {
return fmt.Errorf("cannot Set on a finalized state")
}
_, err := state.spockSecretHasher.Write(setMarker)
if err != nil {
return fmt.Errorf("set SPoCK failed: %w", err)
}
idBytes := id.Bytes()
// Note: encoding the register id / value length as part of spock hash
// to prevent string injection attacks.
err = binary.Write(
state.spockSecretHasher,
binary.LittleEndian,
int32(len(idBytes)))
if err != nil {
return fmt.Errorf("set SPoCK failed: %w", err)
}
_, err = state.spockSecretHasher.Write(idBytes)
if err != nil {
return fmt.Errorf("set SPoCK failed: %w", err)
}
err = binary.Write(
state.spockSecretHasher,
binary.LittleEndian,
int32(len(value)))
if err != nil {
return fmt.Errorf("set SPoCK failed: %w", err)
}
_, err = state.spockSecretHasher.Write(value)
if err != nil {
return fmt.Errorf("set SPoCK failed: %w", err)
}
return state.storageState.Set(id, value)
}
func (state *spockState) Get(
id flow.RegisterID,
) (
flow.RegisterValue,
error,
) {
if state.finalizedSpockSecret != nil {
return nil, fmt.Errorf("cannot Get on a finalized state")
}
_, err := state.spockSecretHasher.Write(getMarker)
if err != nil {
return nil, fmt.Errorf("get SPoCK failed: %w", err)
}
idBytes := id.Bytes()
// Note: encoding the register id length as part of spock hash to prevent
// string injection attacks.
err = binary.Write(
state.spockSecretHasher,
binary.LittleEndian,
int32(len(idBytes)))
if err != nil {
return nil, fmt.Errorf("get SPoCK failed: %w", err)
}
_, err = state.spockSecretHasher.Write(idBytes)
if err != nil {
return nil, fmt.Errorf("get SPoCK failed: %w", err)
}
return state.storageState.Get(id)
}
func (state *spockState) DropChanges() error {
if state.finalizedSpockSecret != nil {
return fmt.Errorf("cannot DropChanges on a finalized state")
}
_, err := state.spockSecretHasher.Write(dropChangesMarker)
if err != nil {
return fmt.Errorf("drop changes SPoCK failed: %w", err)
}
return state.storageState.DropChanges()
}
func (state *spockState) readSetSize() int {
return state.storageState.readSetSize()
}
func (state *spockState) interimReadSet(
accumulator map[flow.RegisterID]struct{},
) {
state.storageState.interimReadSet(accumulator)
}