Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
Merge pull request #851 from iotaledger/fix/commitment-naming
Browse files Browse the repository at this point in the history
Fix: Naming issue of commitments
  • Loading branch information
piotrm50 authored Mar 19, 2024
2 parents e8621d2 + 361a0a0 commit e6f0f25
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
13 changes: 11 additions & 2 deletions pkg/core/promise/promise.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,17 @@ func New[T any](optResolver ...func(p *Promise[T])) *Promise[T] {
return p
}

// Resolve resolves the promise with the given result.
func (p *Promise[T]) Resolve(result T) *Promise[T] {
// ResolveDynamically resolves the promise with the result of the given resolve function.
func (p *Promise[T]) ResolveDynamically(resolve func() T) *Promise[T] {
p.mutex.Lock()
defer p.mutex.Unlock()

if p.complete {
return p
}

result := resolve()

//nolint:revive
p.successCallbacks.ForEach(func(key types.UniqueID, callback func(T)) bool {
callback(result)
Expand All @@ -79,6 +81,13 @@ func (p *Promise[T]) Resolve(result T) *Promise[T] {
return p
}

// Resolve resolves the promise with the given result.
func (p *Promise[T]) Resolve(result T) *Promise[T] {
return p.ResolveDynamically(func() T {
return result
})
}

// Reject rejects the promise with the given error.
func (p *Promise[T]) Reject(err error) *Promise[T] {
p.mutex.Lock()
Expand Down
11 changes: 5 additions & 6 deletions pkg/protocol/commitments.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,13 @@ func (c *Commitments) publishCommitment(commitment *model.Commitment) (published
}

// otherwise try to publish it and determine if we were the goroutine that published it
publishedCommitment = newCommitment(c, commitment)
cachedRequest.Resolve(publishedCommitment).OnSuccess(func(resolvedCommitment *Commitment) {
if published = resolvedCommitment == publishedCommitment; !published {
publishedCommitment = resolvedCommitment
}
cachedRequest.ResolveDynamically(func() *Commitment {
published = true

return newCommitment(c, commitment)
})

return publishedCommitment, published, nil
return cachedRequest.Result(), published, nil
}

// cachedRequest returns a singleton Promise for the given commitmentID. If the Promise does not exist yet, it will be
Expand Down

0 comments on commit e6f0f25

Please sign in to comment.