@@ -3,7 +3,6 @@ package root
3
3
import (
4
4
"bytes"
5
5
"fmt"
6
- "slices"
7
6
"sync"
8
7
"time"
9
8
@@ -41,9 +40,6 @@ type Store struct {
41
40
// lastCommitInfo reflects the last version/hash that has been committed
42
41
lastCommitInfo * proof.CommitInfo
43
42
44
- // workingHash defines the current (yet to be committed) hash
45
- workingHash []byte
46
-
47
43
// telemetry reflects a telemetry agent responsible for emitting metrics (if any)
48
44
telemetry metrics.StoreMetrics
49
45
@@ -238,7 +234,6 @@ func (s *Store) loadVersion(v uint64) error {
238
234
return fmt .Errorf ("failed to load SC version %d: %w" , v , err )
239
235
}
240
236
241
- s .workingHash = nil
242
237
s .commitHeader = nil
243
238
244
239
// set lastCommitInfo explicitly s.t. Commit commits the correct version, i.e. v+1
@@ -256,43 +251,19 @@ func (s *Store) SetCommitHeader(h *coreheader.Info) {
256
251
s .commitHeader = h
257
252
}
258
253
259
- // WorkingHash returns the working hash of the root store. Note, WorkingHash()
260
- // should only be called once per block once all writes are complete and prior
261
- // to Commit() being called.
262
- //
263
- // If working hash is nil, then we need to compute and set it on the root store
264
- // by constructing a CommitInfo object, which in turn creates and writes a batch
265
- // of the current changeset to the SC tree.
266
- func (s * Store ) WorkingHash (cs * corestore.Changeset ) ([]byte , error ) {
267
- if s .telemetry != nil {
268
- now := time .Now ()
269
- defer s .telemetry .MeasureSince (now , "root_store" , "working_hash" )
270
- }
271
-
272
- if s .workingHash == nil {
273
- if err := s .writeSC (cs ); err != nil {
274
- return nil , err
275
- }
276
-
277
- s .workingHash = s .lastCommitInfo .Hash ()
278
- }
279
-
280
- return slices .Clone (s .workingHash ), nil
281
- }
282
-
283
- // Commit commits all state changes to the underlying SS and SC backends. Note,
284
- // at the time of Commit(), we expect WorkingHash() to have already been called
285
- // with the same Changeset, which internally sets the working hash, retrieved by
286
- // writing a batch of the changeset to the SC tree, and CommitInfo on the root
287
- // store.
254
+ // Commit commits all state changes to the underlying SS and SC backends. It
255
+ // writes a batch of the changeset to the SC tree, and retrieves the CommitInfo
256
+ // from the SC tree. Finally, it commits the SC tree and returns the hash of the
257
+ // CommitInfo.
288
258
func (s * Store ) Commit (cs * corestore.Changeset ) ([]byte , error ) {
289
259
if s .telemetry != nil {
290
260
now := time .Now ()
291
261
defer s .telemetry .MeasureSince (now , "root_store" , "commit" )
292
262
}
293
263
294
- if s .workingHash == nil {
295
- return nil , fmt .Errorf ("working hash is nil; must call WorkingHash() before Commit()" )
264
+ // write the changeset to the SC tree and update lastCommitInfo
265
+ if err := s .writeSC (cs ); err != nil {
266
+ return nil , err
296
267
}
297
268
298
269
version := s .lastCommitInfo .Version
@@ -318,7 +289,7 @@ func (s *Store) Commit(cs *corestore.Changeset) ([]byte, error) {
318
289
319
290
// commit SC async
320
291
eg .Go (func () error {
321
- if err := s .commitSC (cs ); err != nil {
292
+ if err := s .commitSC (); err != nil {
322
293
return fmt .Errorf ("failed to commit SC: %w" , err )
323
294
}
324
295
@@ -333,8 +304,6 @@ func (s *Store) Commit(cs *corestore.Changeset) ([]byte, error) {
333
304
s .lastCommitInfo .Timestamp = s .commitHeader .Time
334
305
}
335
306
336
- s .workingHash = nil
337
-
338
307
return s .lastCommitInfo .Hash (), nil
339
308
}
340
309
@@ -441,24 +410,17 @@ func (s *Store) writeSC(cs *corestore.Changeset) error {
441
410
}
442
411
443
412
// commitSC commits the SC store. At this point, a batch of the current changeset
444
- // should have already been written to the SC via WorkingHash (). This method
445
- // solely commits that batch. An error is returned if commit fails or if the
446
- // resulting commit hash is not equivalent to the working hash .
447
- func (s * Store ) commitSC (cs * corestore. Changeset ) error {
413
+ // should have already been written to the SC via writeSC (). This method solely
414
+ // commits that batch. An error is returned if commit fails or the hash of the
415
+ // committed state does not match the hash of the working state .
416
+ func (s * Store ) commitSC () error {
448
417
cInfo , err := s .stateCommitment .Commit (s .lastCommitInfo .Version )
449
418
if err != nil {
450
419
return fmt .Errorf ("failed to commit SC store: %w" , err )
451
420
}
452
421
453
- commitHash := cInfo .Hash ()
454
-
455
- workingHash , err := s .WorkingHash (cs )
456
- if err != nil {
457
- return fmt .Errorf ("failed to get working hash: %w" , err )
458
- }
459
-
460
- if ! bytes .Equal (commitHash , workingHash ) {
461
- return fmt .Errorf ("unexpected commit hash; got: %X, expected: %X" , commitHash , workingHash )
422
+ if ! bytes .Equal (cInfo .Hash (), s .lastCommitInfo .Hash ()) {
423
+ return fmt .Errorf ("unexpected commit hash; got: %X, expected: %X" , cInfo .Hash (), s .lastCommitInfo .Hash ())
462
424
}
463
425
464
426
return nil
0 commit comments