Skip to content

Commit

Permalink
remove duplicated code from setHash func
Browse files Browse the repository at this point in the history
  • Loading branch information
BeniaminDrasovean committed Dec 2, 2024
1 parent 98869ad commit e8d5c1f
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions trie/branchNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,39 +102,14 @@ func (bn *branchNode) setHash(goRoutinesManager common.TrieGoroutinesManager) {
}

if !goRoutinesManager.CanStartGoRoutine() {
bn.children[i].setHash(goRoutinesManager)
if !goRoutinesManager.ShouldContinueProcessing() {
return
}

encChild, err := encodeNodeAndGetHash(bn.children[i])
if err != nil {
goRoutinesManager.SetError(err)
return
}

bn.childrenMutexes[i].Lock()
bn.EncodedChildren[i] = encChild
bn.childrenMutexes[i].Unlock()
bn.setHashForChild(i, goRoutinesManager)
continue
}

waitGroup.Add(1)
go func(childPos int) {
bn.children[childPos].setHash(goRoutinesManager)
if !goRoutinesManager.ShouldContinueProcessing() {
waitGroup.Done()
return
}
encChild, err := encodeNodeAndGetHash(bn.children[childPos])
if err != nil {
goRoutinesManager.SetError(err)
waitGroup.Done()
return
}
bn.childrenMutexes[childPos].Lock()
bn.EncodedChildren[childPos] = encChild
bn.childrenMutexes[childPos].Unlock()
bn.setHashForChild(childPos, goRoutinesManager)
goRoutinesManager.EndGoRoutineProcessing()
waitGroup.Done()
}(i)
}
Expand All @@ -149,6 +124,23 @@ func (bn *branchNode) setHash(goRoutinesManager common.TrieGoroutinesManager) {
bn.hash = hash
}

func (bn *branchNode) setHashForChild(childPos int, goRoutinesManager common.TrieGoroutinesManager) {
bn.children[childPos].setHash(goRoutinesManager)
if !goRoutinesManager.ShouldContinueProcessing() {
return
}

encChild, err := encodeNodeAndGetHash(bn.children[childPos])
if err != nil {
goRoutinesManager.SetError(err)
return
}

bn.childrenMutexes[childPos].Lock()
bn.EncodedChildren[childPos] = encChild
bn.childrenMutexes[childPos].Unlock()
}

func (bn *branchNode) shouldSetHashForChild(childPos int) bool {
bn.childrenMutexes[childPos].RLock()
defer bn.childrenMutexes[childPos].RUnlock()
Expand Down

0 comments on commit e8d5c1f

Please sign in to comment.