|
6 | 6 | "fmt" |
7 | 7 | "io" |
8 | 8 | "sync" |
| 9 | + "sync/atomic" |
9 | 10 | "time" |
10 | 11 |
|
11 | 12 | "github.com/btcsuite/btcd/blockchain" |
@@ -568,8 +569,10 @@ type Manager struct { |
568 | 569 |
|
569 | 570 | // chanIDNonce is a nonce that's incremented for each new funding |
570 | 571 | // reservation created. |
571 | | - nonceMtx sync.RWMutex |
572 | | - chanIDNonce uint64 |
| 572 | + chanIDNonce atomic.Uint64 |
| 573 | + |
| 574 | + // nonceMtx is a mutex that guards the pendingMusigNonces. |
| 575 | + nonceMtx sync.RWMutex |
573 | 576 |
|
574 | 577 | // pendingMusigNonces is used to store the musig2 nonce we generate to |
575 | 578 | // send funding locked until we receive a funding locked message from |
@@ -805,21 +808,21 @@ type PendingChanID = [32]byte |
805 | 808 | // nextPendingChanID returns the next free pending channel ID to be used to |
806 | 809 | // identify a particular future channel funding workflow. |
807 | 810 | func (f *Manager) nextPendingChanID() PendingChanID { |
808 | | - // Obtain a fresh nonce. We do this by encoding the current nonce |
809 | | - // counter, then incrementing it by one. |
810 | | - f.nonceMtx.Lock() |
811 | | - var nonce [8]byte |
812 | | - binary.LittleEndian.PutUint64(nonce[:], f.chanIDNonce) |
813 | | - f.chanIDNonce++ |
814 | | - f.nonceMtx.Unlock() |
| 811 | + // Obtain a fresh nonce. We do this by encoding the incremented nonce. |
| 812 | + nextNonce := f.chanIDNonce.Add(1) |
| 813 | + |
| 814 | + var nonceBytes [8]byte |
| 815 | + binary.LittleEndian.PutUint64(nonceBytes[:], nextNonce) |
815 | 816 |
|
816 | 817 | // We'll generate the next pending channelID by "encrypting" 32-bytes |
817 | 818 | // of zeroes which'll extract 32 random bytes from our stream cipher. |
818 | 819 | var ( |
819 | 820 | nextChanID PendingChanID |
820 | 821 | zeroes [32]byte |
821 | 822 | ) |
822 | | - salsa20.XORKeyStream(nextChanID[:], zeroes[:], nonce[:], &f.chanIDKey) |
| 823 | + salsa20.XORKeyStream( |
| 824 | + nextChanID[:], zeroes[:], nonceBytes[:], &f.chanIDKey, |
| 825 | + ) |
823 | 826 |
|
824 | 827 | return nextChanID |
825 | 828 | } |
|
0 commit comments