-
Notifications
You must be signed in to change notification settings - Fork 210
/
layers.go
270 lines (253 loc) · 8.32 KB
/
layers.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package layers
import (
"fmt"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/sql"
)
// SetWeakCoin for the layer.
func SetWeakCoin(db sql.Executor, lid types.LayerID, weakcoin bool) error {
if _, err := db.Exec(`insert into layers (id, weak_coin) values (?1, ?2)
on conflict(id) do update set weak_coin=?2;`,
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(lid))
stmt.BindBool(2, weakcoin)
}, nil); err != nil {
return fmt.Errorf("set weak coin %s: %w", lid, err)
}
return nil
}
// GetWeakCoin for layer.
func GetWeakCoin(db sql.Executor, lid types.LayerID) (bool, error) {
var (
weakcoin bool
err error
rows int
)
if rows, err = db.Exec("select weak_coin from layers where id = ?1;",
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(lid))
},
func(stmt *sql.Statement) bool {
if stmt.ColumnLen(0) == 0 {
err = fmt.Errorf("%w weak coin for %s is null", sql.ErrNotFound, lid)
return false
}
weakcoin = stmt.ColumnInt(0) == 1
return true
}); err != nil {
return false, fmt.Errorf("is empty %s: %w", lid, err)
} else if rows == 0 {
return false, fmt.Errorf("%w weak coin is not set for %s", sql.ErrNotFound, lid)
}
return weakcoin, err
}
// SetApplied for the layer to a block id.
func SetApplied(db sql.Executor, lid types.LayerID, applied types.BlockID) error {
if _, err := db.Exec(`insert into layers (id, applied_block) values (?1, ?2)
on conflict(id) do update set applied_block=?2;`,
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(lid))
stmt.BindBytes(2, applied[:])
}, nil); err != nil {
return fmt.Errorf("set applied %s: %w", lid, err)
}
return nil
}
// UnsetAppliedFrom updates the applied block to nil for layer >= `lid`.
func UnsetAppliedFrom(db sql.Executor, lid types.LayerID) error {
if _, err := db.Exec(
"update layers set applied_block = null, state_hash = null, aggregated_hash = null where id >= ?1;",
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(lid))
}, nil); err != nil {
return fmt.Errorf("unset applied %s: %w", lid, err)
}
return nil
}
// UpdateStateHash for the layer.
func UpdateStateHash(db sql.Executor, lid types.LayerID, hash types.Hash32) error {
if _, err := db.Exec(`insert into layers (id, state_hash) values (?1, ?2)
on conflict(id) do update set state_hash=?2;`,
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(lid))
stmt.BindBytes(2, hash[:])
}, nil); err != nil {
return fmt.Errorf("set applied %s: %w", lid, err)
}
return nil
}
// GetLatestStateHash loads latest state hash.
func GetLatestStateHash(db sql.Executor) (rst types.Hash32, err error) {
if rows, err := db.Exec("select state_hash from layers where state_hash is not null;",
nil,
func(stmt *sql.Statement) bool {
stmt.ColumnBytes(0, rst[:])
return false
}); err != nil {
return rst, fmt.Errorf("failed to load latest state root %w", err)
} else if rows == 0 {
return rst, fmt.Errorf("%w: state root doesnt exist", sql.ErrNotFound)
}
return rst, err
}
// GetStateHash loads state hash for the layer.
func GetStateHash(db sql.Executor, lid types.LayerID) (rst types.Hash32, err error) {
if rows, err := db.Exec("select state_hash from layers where id = ?1;",
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(lid))
},
func(stmt *sql.Statement) bool {
if stmt.ColumnLen(0) == 0 {
err = fmt.Errorf("%w: state_hash for %s is not set", sql.ErrNotFound, lid)
return false
}
stmt.ColumnBytes(0, rst[:])
return false
}); err != nil {
return rst, fmt.Errorf("failed to load state root for %v: %w", lid, err)
} else if rows == 0 {
return rst, fmt.Errorf("%w: %s doesnt exist", sql.ErrNotFound, lid)
}
return rst, err
}
// GetApplied for the applied block for layer.
func GetApplied(db sql.Executor, lid types.LayerID) (rst types.BlockID, err error) {
if rows, err := db.Exec("select applied_block from layers where id = ?1;",
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(lid))
},
func(stmt *sql.Statement) bool {
if stmt.ColumnLen(0) == 0 {
err = fmt.Errorf("%w applied for %s is null", sql.ErrNotFound, lid)
return false
}
stmt.ColumnBytes(0, rst[:])
return true
}); err != nil {
return rst, fmt.Errorf("is empty %s: %w", lid, err)
} else if rows == 0 {
return rst, fmt.Errorf("%w applied is not set for %s", sql.ErrNotFound, lid)
}
return rst, err
}
func FirstAppliedInEpoch(db sql.Executor, epoch types.EpochID) (types.BlockID, error) {
var (
result types.BlockID
err error
rows int
)
if rows, err = db.Exec(`
select applied_block from layers
where id between ?1 and ?2 and applied_block is not null and applied_block != ?3
order by id asc limit 1;`, func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(epoch.FirstLayer()))
stmt.BindInt64(2, int64((epoch+1).FirstLayer()-1))
stmt.BindBytes(3, types.EmptyBlockID[:])
}, func(stmt *sql.Statement) bool {
stmt.ColumnBytes(0, result[:])
return true
}); err != nil {
return types.EmptyBlockID, fmt.Errorf("FirstAppliedInEpoch %s: %w", epoch, err)
} else if rows == 0 {
return types.EmptyBlockID, fmt.Errorf("FirstAppliedInEpoch %s: %w", epoch, sql.ErrNotFound)
}
return result, nil
}
// GetLastApplied for the applied block for layer.
func GetLastApplied(db sql.Executor) (types.LayerID, error) {
var lid types.LayerID
if _, err := db.Exec("select max(id) from layers where applied_block is not null", nil,
func(stmt *sql.Statement) bool {
lid = types.LayerID(uint32(stmt.ColumnInt64(0)))
return true
}); err != nil {
return lid, fmt.Errorf("last applied: %w", err)
}
return lid, nil
}
// SetProcessed sets a layer processed.
func SetProcessed(db sql.Executor, lid types.LayerID) error {
if _, err := db.Exec(
`insert into layers (id, processed) values (?1, 1)
on conflict(id) do update set processed=1;`,
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(lid.Uint32()))
}, nil); err != nil {
return fmt.Errorf("set processed %v: %w", lid, err)
}
return nil
}
// GetProcessed gets the highest layer processed.
func GetProcessed(db sql.Executor) (types.LayerID, error) {
var lid types.LayerID
if _, err := db.Exec("select max(id) from layers where processed = 1;",
nil,
func(stmt *sql.Statement) bool {
lid = types.LayerID(uint32(stmt.ColumnInt64(0)))
return true
}); err != nil {
return lid, fmt.Errorf("processed layer: %w", err)
}
return lid, nil
}
// SetMeshHash sets the aggregated hash up to the specified layer.
func SetMeshHash(db sql.Executor, lid types.LayerID, aggHash types.Hash32) error {
if _, err := db.Exec(
`insert into layers (id, aggregated_hash) values (?1, ?2)
on conflict(id) do update set aggregated_hash=?2;`,
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(lid.Uint32()))
stmt.BindBytes(2, aggHash[:])
}, nil); err != nil {
return fmt.Errorf("set hashes %v: %w", lid, err)
}
return nil
}
// GetAggregatedHash for layer.
func GetAggregatedHash(db sql.Executor, lid types.LayerID) (types.Hash32, error) {
var rst types.Hash32
if rows, err := db.Exec("select aggregated_hash from layers where id = ?1",
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(lid.Uint32()))
},
func(stmt *sql.Statement) bool {
stmt.ColumnBytes(0, rst[:])
return true
}); err != nil {
return rst, fmt.Errorf("get agg hash %s: %w", lid, err)
} else if rows == 0 {
return rst, fmt.Errorf("%w layer %s", sql.ErrNotFound, lid)
}
return rst, nil
}
func GetAggHashes(db sql.Executor, from, to types.LayerID, by uint32) ([]types.Hash32, error) {
dist := to.Difference(from)
count := int(dist/by + 1)
if dist%by != 0 {
// last layer is not a multiple of By, so we need to add it
count++
}
hashes := make([]types.Hash32, 0, count)
if _, err := db.Exec(
`select aggregated_hash from layers
where id >= ?1 and id <= ?2 and
((id-?1)%?3 = 0 or id = ?2)
order by id asc;`,
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(from.Uint32()))
stmt.BindInt64(2, int64(to.Uint32()))
stmt.BindInt64(3, int64(by))
},
func(stmt *sql.Statement) bool {
var h types.Hash32
stmt.ColumnBytes(0, h[:])
hashes = append(hashes, h)
return true
}); err != nil {
return nil, fmt.Errorf("get aggHashes from %s to %s by %d: %w", from, to, by, err)
}
if len(hashes) != count {
return nil, fmt.Errorf("%w layers from %s to %s by %d", sql.ErrNotFound, from, to, by)
}
return hashes, nil
}