Browse Source

Fix incorrect initial CurrentIdx in kvdb

feature/sql-semaphore1
Eduard S 3 years ago
parent
commit
d7c56afa5c
5 changed files with 91 additions and 5 deletions
  1. +4
    -2
      coordinator/purger.go
  2. +8
    -3
      db/kvdb/kvdb.go
  3. +39
    -0
      db/kvdb/kvdb_test.go
  4. +5
    -0
      db/statedb/statedb.go
  5. +35
    -0
      db/statedb/statedb_test.go

+ 4
- 2
coordinator/purger.go

@ -133,9 +133,11 @@ func poolMarkInvalidOldNonces(l2DB *l2db.L2DB, stateDB *statedb.LocalStateDB,
if tracerr.Unwrap(err) != db.ErrNotFound { if tracerr.Unwrap(err) != db.ErrNotFound {
return tracerr.Wrap(err) return tracerr.Wrap(err)
} else if idx <= lastIdx { } else if idx <= lastIdx {
return tracerr.Wrap(fmt.Errorf("account with idx %v not found: %w", idx, err))
return tracerr.Wrap(fmt.Errorf("account with idx %v (lastIdx: %v) "+
"not found: %w", idx, lastIdx, err))
} else { } else {
return tracerr.Wrap(fmt.Errorf("unexpected stateDB error with idx %v: %w", idx, err))
return tracerr.Wrap(fmt.Errorf("unexpected stateDB error with idx %v "+
"(lastIdx: %v): %w", idx, lastIdx, err))
} }
} }
idxsNonce[i].Idx = idx idxsNonce[i].Idx = idx

+ 8
- 3
db/kvdb/kvdb.go

@ -135,7 +135,7 @@ func (kvdb *KVDB) reset(batchNum common.BatchNum, closeCurrent bool) error {
return tracerr.Wrap(err) return tracerr.Wrap(err)
} }
kvdb.db = sto kvdb.db = sto
kvdb.CurrentIdx = 255
kvdb.CurrentIdx = common.RollupConstReservedIDx // 255
kvdb.CurrentBatch = 0 kvdb.CurrentBatch = 0
return nil return nil
@ -204,7 +204,7 @@ func (kvdb *KVDB) ResetFromSynchronizer(batchNum common.BatchNum, synchronizerKV
return tracerr.Wrap(err) return tracerr.Wrap(err)
} }
kvdb.db = sto kvdb.db = sto
kvdb.CurrentIdx = 255
kvdb.CurrentIdx = common.RollupConstReservedIDx // 255
kvdb.CurrentBatch = 0 kvdb.CurrentBatch = 0
return nil return nil
@ -286,7 +286,7 @@ func (kvdb *KVDB) setCurrentBatch() error {
func (kvdb *KVDB) GetCurrentIdx() (common.Idx, error) { func (kvdb *KVDB) GetCurrentIdx() (common.Idx, error) {
idxBytes, err := kvdb.db.Get(keyCurrentIdx) idxBytes, err := kvdb.db.Get(keyCurrentIdx)
if tracerr.Unwrap(err) == db.ErrNotFound { if tracerr.Unwrap(err) == db.ErrNotFound {
return 0, nil
return common.RollupConstReservedIDx, nil // 255, nil
} }
if err != nil { if err != nil {
return 0, tracerr.Wrap(err) return 0, tracerr.Wrap(err)
@ -442,3 +442,8 @@ func pebbleMakeCheckpoint(source, dest string) error {
return nil return nil
} }
// Close the DB
func (kvdb *KVDB) Close() {
kvdb.db.Close()
}

+ 39
- 0
db/kvdb/kvdb_test.go

@ -195,3 +195,42 @@ func TestDeleteOldCheckpoints(t *testing.T) {
assert.LessOrEqual(t, len(checkpoints), keep) assert.LessOrEqual(t, len(checkpoints), keep)
} }
} }
func TestGetCurrentIdx(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.NoError(t, err)
defer require.NoError(t, os.RemoveAll(dir))
keep := 16
db, err := NewKVDB(dir, keep)
require.NoError(t, err)
idx, err := db.GetCurrentIdx()
require.NoError(t, err)
assert.Equal(t, common.Idx(255), idx)
db.Close()
db, err = NewKVDB(dir, keep)
require.NoError(t, err)
idx, err = db.GetCurrentIdx()
require.NoError(t, err)
assert.Equal(t, common.Idx(255), idx)
err = db.MakeCheckpoint()
require.NoError(t, err)
idx, err = db.GetCurrentIdx()
require.NoError(t, err)
assert.Equal(t, common.Idx(255), idx)
db.Close()
db, err = NewKVDB(dir, keep)
require.NoError(t, err)
idx, err = db.GetCurrentIdx()
require.NoError(t, err)
assert.Equal(t, common.Idx(255), idx)
}

+ 5
- 0
db/statedb/statedb.go

@ -341,6 +341,11 @@ func (s *StateDB) MTGetRoot() *big.Int {
return s.MT.Root().BigInt() return s.MT.Root().BigInt()
} }
// Close the StateDB
func (s *StateDB) Close() {
s.db.Close()
}
// LocalStateDB represents the local StateDB which allows to make copies from // LocalStateDB represents the local StateDB which allows to make copies from
// the synchronizer StateDB, and is used by the tx-selector and the // the synchronizer StateDB, and is used by the tx-selector and the
// batch-builder. LocalStateDB is an in-memory storage. // batch-builder. LocalStateDB is an in-memory storage.

+ 35
- 0
db/statedb/statedb_test.go

@ -532,3 +532,38 @@ func TestDeleteOldCheckpoints(t *testing.T) {
assert.LessOrEqual(t, len(checkpoints), keep) assert.LessOrEqual(t, len(checkpoints), keep)
} }
} }
func TestCurrentIdx(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.NoError(t, err)
defer require.NoError(t, os.RemoveAll(dir))
keep := 16
sdb, err := NewStateDB(dir, keep, TypeSynchronizer, 32)
require.NoError(t, err)
idx := sdb.CurrentIdx()
assert.Equal(t, common.Idx(255), idx)
sdb.Close()
sdb, err = NewStateDB(dir, keep, TypeSynchronizer, 32)
require.NoError(t, err)
idx = sdb.CurrentIdx()
assert.Equal(t, common.Idx(255), idx)
err = sdb.MakeCheckpoint()
require.NoError(t, err)
idx = sdb.CurrentIdx()
assert.Equal(t, common.Idx(255), idx)
sdb.Close()
sdb, err = NewStateDB(dir, keep, TypeSynchronizer, 32)
require.NoError(t, err)
idx = sdb.CurrentIdx()
assert.Equal(t, common.Idx(255), idx)
}

Loading…
Cancel
Save