mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-26 17:52:25 +01:00
funding: use atomic.Uint64 for chanIDNonce
This lets us get rid of the mutex usage there. We also shift the algo slightly to increment by 1, then use that as the next value, which plays nicer with the atomics.
This commit is contained in:
parent
8ba3f3a317
commit
dd357fb847
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/blockchain"
|
||||
@ -567,8 +568,10 @@ type Manager struct {
|
||||
|
||||
// chanIDNonce is a nonce that's incremented for each new funding
|
||||
// reservation created.
|
||||
nonceMtx sync.RWMutex
|
||||
chanIDNonce uint64
|
||||
chanIDNonce atomic.Uint64
|
||||
|
||||
// nonceMtx is a mutex that guards the pendingMusigNonces.
|
||||
nonceMtx sync.RWMutex
|
||||
|
||||
// pendingMusigNonces is used to store the musig2 nonce we generate to
|
||||
// send funding locked until we receive a funding locked message from
|
||||
@ -804,13 +807,11 @@ type PendingChanID = [32]byte
|
||||
// nextPendingChanID returns the next free pending channel ID to be used to
|
||||
// identify a particular future channel funding workflow.
|
||||
func (f *Manager) nextPendingChanID() PendingChanID {
|
||||
// Obtain a fresh nonce. We do this by encoding the current nonce
|
||||
// counter, then incrementing it by one.
|
||||
f.nonceMtx.Lock()
|
||||
var nonce [8]byte
|
||||
binary.LittleEndian.PutUint64(nonce[:], f.chanIDNonce)
|
||||
f.chanIDNonce++
|
||||
f.nonceMtx.Unlock()
|
||||
// Obtain a fresh nonce. We do this by encoding the incremented nonce.
|
||||
nextNonce := f.chanIDNonce.Add(1)
|
||||
|
||||
var nonceBytes [8]byte
|
||||
binary.LittleEndian.PutUint64(nonceBytes[:], nextNonce)
|
||||
|
||||
// We'll generate the next pending channelID by "encrypting" 32-bytes
|
||||
// of zeroes which'll extract 32 random bytes from our stream cipher.
|
||||
@ -818,7 +819,9 @@ func (f *Manager) nextPendingChanID() PendingChanID {
|
||||
nextChanID PendingChanID
|
||||
zeroes [32]byte
|
||||
)
|
||||
salsa20.XORKeyStream(nextChanID[:], zeroes[:], nonce[:], &f.chanIDKey)
|
||||
salsa20.XORKeyStream(
|
||||
nextChanID[:], zeroes[:], nonceBytes[:], &f.chanIDKey,
|
||||
)
|
||||
|
||||
return nextChanID
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user