Skip to content

Commit

Permalink
core/state/snapshot: fix race condition (#24685)
Browse files Browse the repository at this point in the history
Fixes three race conditions found through fuzzing by David Theodore
  • Loading branch information
MariusVanDerWijden authored and unclezoro committed Sep 21, 2022
1 parent 5a06ca3 commit e8b9338
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
3 changes: 3 additions & 0 deletions core/state/snapshot/difflayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ func (dl *diffLayer) CorrectAccounts(accounts map[common.Hash][]byte) {

// Parent returns the subsequent layer of a diff layer.
func (dl *diffLayer) Parent() snapshot {
dl.lock.RLock()
defer dl.lock.RUnlock()

return dl.parent
}

Expand Down
37 changes: 23 additions & 14 deletions eth/protocols/snap/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ type Syncer struct {
storageSynced uint64 // Number of storage slots downloaded
storageBytes common.StorageSize // Number of storage trie bytes persisted to disk

extProgress *SyncProgress // progress that can be exposed to external caller.

// Request tracking during healing phase
trienodeHealIdlers map[string]struct{} // Peers that aren't serving trie node requests
bytecodeHealIdlers map[string]struct{} // Peers that aren't serving bytecode requests
Expand Down Expand Up @@ -478,6 +480,8 @@ func NewSyncer(db ethdb.KeyValueStore) *Syncer {
trienodeHealReqs: make(map[uint64]*trienodeHealRequest),
bytecodeHealReqs: make(map[uint64]*bytecodeHealRequest),
stateWriter: db.NewBatch(),

extProgress: new(SyncProgress),
}
}

Expand Down Expand Up @@ -634,6 +638,21 @@ func (s *Syncer) Sync(root common.Hash, cancel chan struct{}) error {
s.assignTrienodeHealTasks(trienodeHealResps, trienodeHealReqFails, cancel)
s.assignBytecodeHealTasks(bytecodeHealResps, bytecodeHealReqFails, cancel)
}
// Update sync progress
s.lock.Lock()
s.extProgress = &SyncProgress{
AccountSynced: s.accountSynced,
AccountBytes: s.accountBytes,
BytecodeSynced: s.bytecodeSynced,
BytecodeBytes: s.bytecodeBytes,
StorageSynced: s.storageSynced,
StorageBytes: s.storageBytes,
TrienodeHealSynced: s.trienodeHealSynced,
TrienodeHealBytes: s.trienodeHealBytes,
BytecodeHealSynced: s.bytecodeHealSynced,
BytecodeHealBytes: s.bytecodeHealBytes,
}
s.lock.Unlock()
// Wait for something to happen
select {
case <-s.update:
Expand Down Expand Up @@ -706,6 +725,9 @@ func (s *Syncer) loadSyncStatus() {
}
}
}
s.lock.Lock()
defer s.lock.Unlock()

s.snapped = len(s.tasks) == 0

s.accountSynced = progress.AccountSynced
Expand Down Expand Up @@ -803,25 +825,12 @@ func (s *Syncer) saveSyncStatus() {
func (s *Syncer) Progress() (*SyncProgress, *SyncPending) {
s.lock.Lock()
defer s.lock.Unlock()

progress := &SyncProgress{
AccountSynced: s.accountSynced,
AccountBytes: s.accountBytes,
BytecodeSynced: s.bytecodeSynced,
BytecodeBytes: s.bytecodeBytes,
StorageSynced: s.storageSynced,
StorageBytes: s.storageBytes,
TrienodeHealSynced: s.trienodeHealSynced,
TrienodeHealBytes: s.trienodeHealBytes,
BytecodeHealSynced: s.bytecodeHealSynced,
BytecodeHealBytes: s.bytecodeHealBytes,
}
pending := new(SyncPending)
if s.healer != nil {
pending.TrienodeHeal = uint64(len(s.healer.trieTasks))
pending.BytecodeHeal = uint64(len(s.healer.codeTasks))
}
return progress, pending
return s.extProgress, pending
}

// cleanAccountTasks removes account range retrieval tasks that have already been
Expand Down

0 comments on commit e8b9338

Please sign in to comment.