Skip to content

Commit

Permalink
refactor mu
Browse files Browse the repository at this point in the history
  • Loading branch information
klim0v committed Dec 15, 2021
1 parent 30a59fb commit 09b6333
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions coreV2/appdb/appdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ func (appDB *AppDB) SetLastBlockHash(hash []byte) {

// GetLastHeight returns latest block height stored on disk
func (appDB *AppDB) GetLastHeight() uint64 {
appDB.mu.RLock()
defer appDB.mu.RUnlock()
return appDB.getLastHeight()
}
func (appDB *AppDB) getLastHeight() uint64 {
val := atomic.LoadUint64(&appDB.lastHeight)
if val != 0 {
return val
}

appDB.mu.RLock()
defer appDB.mu.RUnlock()

result, err := appDB.db.Get([]byte(heightPath))
if err != nil {
panic(err)
Expand Down Expand Up @@ -417,16 +419,20 @@ func (appDB *AppDB) Snapshot(height uint64, format uint32) (<-chan io.ReadCloser
if format != snapshottypes.CurrentFormat {
return nil, sdkerrors.Wrapf(snapshottypes.ErrUnknownFormat, "format %v", format)
}
if height == 0 {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "cannot snapshot height 0")
}
if height > appDB.GetLastHeight() {

var results []*types.SnapshotItem
appDB.mu.RLock()

if height != appDB.getLastHeight() {
appDB.mu.RUnlock()
return nil, sdkerrors.Wrapf(sdkerrors.ErrLogic, "cannot snapshot future height %v", height)
}

var results []*types.SnapshotItem
if height == 0 {
appDB.mu.RUnlock()
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "cannot snapshot height 0")
}

appDB.mu.RLock()
for _, name := range []string{validatorsPath, heightPath, hashPath, versionsPath, blocksTimePath, startHeightPath} {
result, err := appDB.db.Get([]byte(name))
if err != nil {
Expand Down

0 comments on commit 09b6333

Please sign in to comment.