Skip to content

Commit fbc61d2

Browse files
refactor(store/v2): get rid of WorkingHash (cosmos#20379)
1 parent b2fe0ff commit fbc61d2

File tree

4 files changed

+19
-98
lines changed

4 files changed

+19
-98
lines changed

store/v2/root/migrate_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ func (s *MigrateStoreTestSuite) TestMigrateState() {
108108
cs.Add([]byte(storeKey), []byte(fmt.Sprintf("key-%d-%d", latestVersion, i)), []byte(fmt.Sprintf("value-%d-%d", latestVersion, i)), false)
109109
}
110110
}
111-
_, err := s.rootStore.WorkingHash(cs)
112-
s.Require().NoError(err)
113111
_, err = s.rootStore.Commit(cs)
114112
s.Require().NoError(err)
115113

@@ -156,8 +154,6 @@ func (s *MigrateStoreTestSuite) TestMigrateState() {
156154
cs.Add([]byte(storeKey), []byte(fmt.Sprintf("key-%d-%d", version, i)), []byte(fmt.Sprintf("value-%d-%d", version, i)), false)
157155
}
158156
}
159-
_, err := s.rootStore.WorkingHash(cs)
160-
s.Require().NoError(err)
161157
_, err = s.rootStore.Commit(cs)
162158
s.Require().NoError(err)
163159
}

store/v2/root/store.go

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package root
33
import (
44
"bytes"
55
"fmt"
6-
"slices"
76
"sync"
87
"time"
98

@@ -41,9 +40,6 @@ type Store struct {
4140
// lastCommitInfo reflects the last version/hash that has been committed
4241
lastCommitInfo *proof.CommitInfo
4342

44-
// workingHash defines the current (yet to be committed) hash
45-
workingHash []byte
46-
4743
// telemetry reflects a telemetry agent responsible for emitting metrics (if any)
4844
telemetry metrics.StoreMetrics
4945

@@ -238,7 +234,6 @@ func (s *Store) loadVersion(v uint64) error {
238234
return fmt.Errorf("failed to load SC version %d: %w", v, err)
239235
}
240236

241-
s.workingHash = nil
242237
s.commitHeader = nil
243238

244239
// 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) {
256251
s.commitHeader = h
257252
}
258253

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.
288258
func (s *Store) Commit(cs *corestore.Changeset) ([]byte, error) {
289259
if s.telemetry != nil {
290260
now := time.Now()
291261
defer s.telemetry.MeasureSince(now, "root_store", "commit")
292262
}
293263

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
296267
}
297268

298269
version := s.lastCommitInfo.Version
@@ -318,7 +289,7 @@ func (s *Store) Commit(cs *corestore.Changeset) ([]byte, error) {
318289

319290
// commit SC async
320291
eg.Go(func() error {
321-
if err := s.commitSC(cs); err != nil {
292+
if err := s.commitSC(); err != nil {
322293
return fmt.Errorf("failed to commit SC: %w", err)
323294
}
324295

@@ -333,8 +304,6 @@ func (s *Store) Commit(cs *corestore.Changeset) ([]byte, error) {
333304
s.lastCommitInfo.Timestamp = s.commitHeader.Time
334305
}
335306

336-
s.workingHash = nil
337-
338307
return s.lastCommitInfo.Hash(), nil
339308
}
340309

@@ -441,24 +410,17 @@ func (s *Store) writeSC(cs *corestore.Changeset) error {
441410
}
442411

443412
// 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 {
448417
cInfo, err := s.stateCommitment.Commit(s.lastCommitInfo.Version)
449418
if err != nil {
450419
return fmt.Errorf("failed to commit SC store: %w", err)
451420
}
452421

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())
462424
}
463425

464426
return nil

store/v2/root/store_test.go

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,9 @@ func (s *RootStoreTestSuite) TestQuery() {
9494
cs := corestore.NewChangeset()
9595
cs.Add(testStoreKeyBytes, []byte("foo"), []byte("bar"), false)
9696

97-
workingHash, err := s.rootStore.WorkingHash(cs)
98-
s.Require().NoError(err)
99-
s.Require().NotNil(workingHash)
100-
10197
commitHash, err := s.rootStore.Commit(cs)
10298
s.Require().NoError(err)
10399
s.Require().NotNil(commitHash)
104-
s.Require().Equal(workingHash, commitHash)
105100

106101
// ensure the proof is non-nil for the corresponding version
107102
result, err := s.rootStore.Query([]byte(testStoreKey), 1, []byte("foo"), true)
@@ -146,9 +141,7 @@ func (s *RootStoreTestSuite) TestQueryProof() {
146141
cs.Add(testStoreKey3Bytes, []byte("key4"), []byte("value4"), false)
147142

148143
// commit
149-
_, err := s.rootStore.WorkingHash(cs)
150-
s.Require().NoError(err)
151-
_, err = s.rootStore.Commit(cs)
144+
_, err := s.rootStore.Commit(cs)
152145
s.Require().NoError(err)
153146

154147
// query proof for testStoreKey
@@ -174,14 +167,9 @@ func (s *RootStoreTestSuite) TestLoadVersion() {
174167
cs := corestore.NewChangeset()
175168
cs.Add(testStoreKeyBytes, []byte("key"), []byte(val), false)
176169

177-
workingHash, err := s.rootStore.WorkingHash(cs)
178-
s.Require().NoError(err)
179-
s.Require().NotNil(workingHash)
180-
181170
commitHash, err := s.rootStore.Commit(cs)
182171
s.Require().NoError(err)
183172
s.Require().NotNil(commitHash)
184-
s.Require().Equal(workingHash, commitHash)
185173
}
186174

187175
// ensure the latest version is correct
@@ -219,14 +207,9 @@ func (s *RootStoreTestSuite) TestLoadVersion() {
219207
cs := corestore.NewChangeset()
220208
cs.Add(testStoreKeyBytes, []byte("key"), []byte(val), false)
221209

222-
workingHash, err := s.rootStore.WorkingHash(cs)
223-
s.Require().NoError(err)
224-
s.Require().NotNil(workingHash)
225-
226210
commitHash, err := s.rootStore.Commit(cs)
227211
s.Require().NoError(err)
228212
s.Require().NotNil(commitHash)
229-
s.Require().Equal(workingHash, commitHash)
230213
}
231214

232215
// ensure the latest version is correct
@@ -259,17 +242,9 @@ func (s *RootStoreTestSuite) TestCommit() {
259242
cs.Add(testStoreKeyBytes, []byte(key), []byte(val), false)
260243
}
261244

262-
// committing w/o calling WorkingHash should error
263-
_, err = s.rootStore.Commit(cs)
264-
s.Require().Error(err)
265-
266-
// execute WorkingHash and Commit
267-
wHash, err := s.rootStore.WorkingHash(cs)
268-
s.Require().NoError(err)
269-
270245
cHash, err := s.rootStore.Commit(cs)
271246
s.Require().NoError(err)
272-
s.Require().Equal(wHash, cHash)
247+
s.Require().NotNil(cHash)
273248

274249
// ensure latest version is updated
275250
lv, err = s.rootStore.GetLatestVersion()
@@ -305,13 +280,10 @@ func (s *RootStoreTestSuite) TestStateAt() {
305280
cs.Add(testStoreKeyBytes, []byte(key), []byte(val), false)
306281
}
307282

308-
// execute WorkingHash and Commit
309-
wHash, err := s.rootStore.WorkingHash(cs)
310-
s.Require().NoError(err)
311-
283+
// execute Commit
312284
cHash, err := s.rootStore.Commit(cs)
313285
s.Require().NoError(err)
314-
s.Require().Equal(wHash, cHash)
286+
s.Require().NotNil(cHash)
315287
}
316288

317289
lv, err := s.rootStore.GetLatestVersion()

store/v2/store.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,11 @@ type RootStore interface {
4949
// queries based on block time need to be supported.
5050
SetCommitHeader(h *coreheader.Info)
5151

52-
// WorkingHash returns the current WIP commitment hash by applying the Changeset
53-
// to the SC backend. Typically, WorkingHash() is called prior to Commit() and
54-
// must be applied with the exact same Changeset. This is because WorkingHash()
55-
// is responsible for writing the Changeset to the SC backend and returning the
56-
// resulting root hash. Then, Commit() would return this hash and flush writes
57-
// to disk.
58-
WorkingHash(cs *corestore.Changeset) ([]byte, error)
59-
6052
// Commit should be responsible for taking the provided changeset and flushing
6153
// it to disk. Note, depending on the implementation, the changeset, at this
6254
// point, may already be written to the SC backends. Commit() should ensure
6355
// the changeset is committed to all SC and SC backends and flushed to disk.
64-
// It must return a hash of the merkle-ized committed state. This hash should
65-
// be the same as the hash returned by WorkingHash() prior to calling Commit().
56+
// It must return a hash of the merkle-ized committed state.
6657
Commit(cs *corestore.Changeset) ([]byte, error)
6758

6859
// LastCommitID returns a CommitID pertaining to the last commitment.

0 commit comments

Comments
 (0)