From 3be08e69cf9d3abd3420742ba89327c1e324f8cb Mon Sep 17 00:00:00 2001 From: maurycy Date: Fri, 1 Jun 2018 00:41:41 +0200 Subject: [PATCH] multi: 64bit aligment of atomic vars on arm/x86-32 --- autopilot/graph.go | 2 +- breacharbiter.go | 4 ++-- contractcourt/chain_arbitrator.go | 4 ++-- contractcourt/chain_watcher.go | 4 ++-- contractcourt/channel_arbitrator.go | 4 ++-- discovery/gossiper.go | 11 ++++++----- htlcswitch/decayedlog.go | 4 ++-- htlcswitch/mailbox.go | 4 ++-- htlcswitch/mock.go | 4 ++-- htlcswitch/queue.go | 26 +++++++++++++------------- htlcswitch/switch.go | 4 ++-- lnwallet/channel.go | 3 ++- lnwallet/sigpool.go | 4 ++-- lnwallet/wallet.go | 8 +++++--- peer.go | 14 ++++++++------ routing/chainview/bitcoind.go | 4 ++-- routing/chainview/btcd.go | 4 ++-- routing/chainview/neutrino.go | 4 ++-- routing/router.go | 8 ++++---- server.go | 3 ++- utxonursery.go | 4 ++-- 21 files changed, 67 insertions(+), 60 deletions(-) diff --git a/autopilot/graph.go b/autopilot/graph.go index a7dc0c45..623e6370 100644 --- a/autopilot/graph.go +++ b/autopilot/graph.go @@ -22,7 +22,7 @@ var ( _, _ = testSig.R.SetString("63724406601629180062774974542967536251589935445068131219452686511677818569431", 10) _, _ = testSig.S.SetString("18801056069249825825291287104931333862866033135609736119018462340006816851118", 10) - chanIDCounter uint64 + chanIDCounter uint64 // To be used atomically. ) // databaseChannelGraph wraps a channeldb.ChannelGraph instance with the diff --git a/breacharbiter.go b/breacharbiter.go index 938240aa..25a190d3 100644 --- a/breacharbiter.go +++ b/breacharbiter.go @@ -111,8 +111,8 @@ type BreachConfig struct { // counterparties. // TODO(roasbeef): closures in config for subsystem pointers to decouple? type breachArbiter struct { - started uint32 - stopped uint32 + started uint32 // To be used atomically. + stopped uint32 // To be used atomically. cfg *BreachConfig diff --git a/contractcourt/chain_arbitrator.go b/contractcourt/chain_arbitrator.go index c567dcf9..5fdb72c0 100644 --- a/contractcourt/chain_arbitrator.go +++ b/contractcourt/chain_arbitrator.go @@ -134,8 +134,8 @@ type ChainArbitratorConfig struct { // forcibly exit a contract, update the set of live signals for each contract, // and to receive reports on the state of contract resolution. type ChainArbitrator struct { - started int32 - stopped int32 + started int32 // To be used atomically. + stopped int32 // To be used atomically. sync.Mutex diff --git a/contractcourt/chain_watcher.go b/contractcourt/chain_watcher.go index feb18fa0..cf4888a5 100644 --- a/contractcourt/chain_watcher.go +++ b/contractcourt/chain_watcher.go @@ -102,8 +102,8 @@ type chainWatcherConfig struct { // that the channel has been closed, and also give them the materials necessary // to sweep the funds of the channel on chain eventually. type chainWatcher struct { - started int32 - stopped int32 + started int32 // To be used atomically. + stopped int32 // To be used atomically. quit chan struct{} wg sync.WaitGroup diff --git a/contractcourt/channel_arbitrator.go b/contractcourt/channel_arbitrator.go index 83816f1b..0ee68313 100644 --- a/contractcourt/channel_arbitrator.go +++ b/contractcourt/channel_arbitrator.go @@ -148,8 +148,8 @@ func newHtlcSet(htlcs []channeldb.HTLC) htlcSet { // broadcasting to ensure that we avoid any possibility of race conditions, and // sweep the output(s) without contest. type ChannelArbitrator struct { - started int32 - stopped int32 + started int32 // To be used atomically. + stopped int32 // To be used atomically. // log is a persistent log that the attendant will use to checkpoint // its next action, and the state of any unresolved contracts. diff --git a/discovery/gossiper.go b/discovery/gossiper.go index 84af3ab5..64302de9 100644 --- a/discovery/gossiper.go +++ b/discovery/gossiper.go @@ -139,9 +139,14 @@ type Config struct { // will be rejected by this struct. type AuthenticatedGossiper struct { // Parameters which are needed to properly handle the start and stop of - // the service. + // the service. To be used atomically. started uint32 stopped uint32 + + // bestHeight is the height of the block at the tip of the main chain + // as we know it. To be used atomically. + bestHeight uint32 + quit chan struct{} wg sync.WaitGroup @@ -186,10 +191,6 @@ type AuthenticatedGossiper struct { // forwarding policy of a set of channels is sent over. chanPolicyUpdates chan *chanPolicyUpdateRequest - // bestHeight is the height of the block at the tip of the main chain - // as we know it. - bestHeight uint32 - // selfKey is the identity public key of the backing Lightning node. selfKey *btcec.PublicKey diff --git a/htlcswitch/decayedlog.go b/htlcswitch/decayedlog.go index 662a87b4..93eb0e51 100644 --- a/htlcswitch/decayedlog.go +++ b/htlcswitch/decayedlog.go @@ -51,8 +51,8 @@ var ( // and the current block height. DecayedLog wraps boltdb for simplicity and // batches writes to the database to decrease write contention. type DecayedLog struct { - started int32 - stopped int32 + started int32 // To be used atomically. + stopped int32 // To be used atomically. dbPath string diff --git a/htlcswitch/mailbox.go b/htlcswitch/mailbox.go index 8a596db1..38872a12 100644 --- a/htlcswitch/mailbox.go +++ b/htlcswitch/mailbox.go @@ -60,8 +60,8 @@ type MailBox interface { // memoryMailBox is an implementation of the MailBox struct backed by purely // in-memory queues. type memoryMailBox struct { - started uint32 - stopped uint32 + started uint32 // To be used atomically. + stopped uint32 // To be used atomically. wireMessages *list.List wireHead *list.Element diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index 6c2c9af4..0ed96408 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -100,8 +100,8 @@ func (m *mockForwardingLog) AddForwardingEvents(events []channeldb.ForwardingEve } type mockServer struct { - started int32 - shutdown int32 + started int32 // To be used atomically. + shutdown int32 // To be used atomically. wg sync.WaitGroup quit chan struct{} diff --git a/htlcswitch/queue.go b/htlcswitch/queue.go index e2930852..36d055b0 100644 --- a/htlcswitch/queue.go +++ b/htlcswitch/queue.go @@ -17,6 +17,19 @@ import ( // to signal the number of slots available, and a condition variable to allow // the packetQueue to know when new items have been added to the queue. type packetQueue struct { + // queueLen is an internal counter that reflects the size of the queue + // at any given instance. This value is intended to be use atomically + // as this value is used by internal methods to obtain the length of + // the queue w/o grabbing the main lock. This allows callers to avoid a + // deadlock situation where the main goroutine is attempting a send + // with the lock held. + queueLen int32 + + // totalHtlcAmt is the sum of the value of all pending HTLC's currently + // residing within the overflow queue. This value should only read or + // modified *atomically*. + totalHtlcAmt int64 + queue []*htlcPacket wg sync.WaitGroup @@ -33,20 +46,7 @@ type packetQueue struct { // commitment transaction. outgoingPkts chan *htlcPacket - // totalHtlcAmt is the sum of the value of all pending HTLC's currently - // residing within the overflow queue. This value should only read or - // modified *atomically*. - totalHtlcAmt int64 - quit chan struct{} - - // queueLen is an internal counter that reflects the size of the queue - // at any given instance. This value is intended to be use atomically - // as this value is used by internal methods to obtain the length of - // the queue w/o grabbing the main lock. This allows callers to avoid a - // deadlock situation where the main goroutine is attempting a send - // with the lock held. - queueLen int32 } // newPacketQueue returns a new instance of the packetQueue. The maxFreeSlots diff --git a/htlcswitch/switch.go b/htlcswitch/switch.go index 52a24035..f11d1fd3 100644 --- a/htlcswitch/switch.go +++ b/htlcswitch/switch.go @@ -153,8 +153,8 @@ type Config struct { // HTLCs, forwarding HTLCs initiated from within the daemon, and finally // notifies users local-systems concerning their outstanding payment requests. type Switch struct { - started int32 - shutdown int32 + started int32 // To be used atomically. + shutdown int32 // To be used atomically. wg sync.WaitGroup quit chan struct{} diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 3eb5713b..7cb3ce62 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -1241,6 +1241,8 @@ func compactLogs(ourLog, theirLog *updateLog, // // See the individual comments within the above methods for further details. type LightningChannel struct { + shutdown int32 // To be used atomically. + // Signer is the main signer instances that will be responsible for // signing any HTLC and commitment transaction generated by the state // machine. @@ -1330,7 +1332,6 @@ type LightningChannel struct { cowg sync.WaitGroup wg sync.WaitGroup - shutdown int32 quit chan struct{} } diff --git a/lnwallet/sigpool.go b/lnwallet/sigpool.go index defbf8ba..66ef93cc 100644 --- a/lnwallet/sigpool.go +++ b/lnwallet/sigpool.go @@ -125,8 +125,8 @@ type signJobResp struct { // TODO(roasbeef): rename? // * ecdsaPool? type sigPool struct { - started uint32 - stopped uint32 + started uint32 // To be used atomically. + stopped uint32 // To be used atomically. signer Signer diff --git a/lnwallet/wallet.go b/lnwallet/wallet.go index 1d88e7d1..db30dad6 100644 --- a/lnwallet/wallet.go +++ b/lnwallet/wallet.go @@ -221,6 +221,11 @@ type addSingleFunderSigsMsg struct { // Bitcoin Core + ZeroMQ, etc. Eventually, the wallet won't require a full-node // at all, as SPV support is integrated into btcwallet. type LightningWallet struct { + started int32 // To be used atomically. + shutdown int32 // To be used atomically. + + nextFundingID uint64 // To be used atomically. + // Cfg is the configuration struct that will be used by the wallet to // access the necessary interfaces and default it needs to carry on its // duties. @@ -258,7 +263,6 @@ type LightningWallet struct { // monotonically integer. All requests concerning the channel MUST // carry a valid, active funding ID. fundingLimbo map[uint64]*ChannelReservation - nextFundingID uint64 limboMtx sync.RWMutex // lockedOutPoints is a set of the currently locked outpoint. This @@ -266,8 +270,6 @@ type LightningWallet struct { // the currently locked outpoints. lockedOutPoints map[wire.OutPoint]struct{} - started int32 - shutdown int32 quit chan struct{} wg sync.WaitGroup diff --git a/peer.go b/peer.go index 3ed9ae63..e4c86073 100644 --- a/peer.go +++ b/peer.go @@ -84,23 +84,25 @@ type chanSnapshotReq struct { // channels. // TODO(roasbeef): proper reconnection logic type peer struct { + // MUST be used atomically. + started int32 + disconnect int32 + // The following fields are only meant to be used *atomically* bytesReceived uint64 bytesSent uint64 + // pingTime is a rough estimate of the RTT (round-trip-time) between us // and the connected peer. This time is expressed in micro seconds. + // To be used atomically. // TODO(roasbeef): also use a WMA or EMA? pingTime int64 // pingLastSend is the Unix time expressed in nanoseconds when we sent - // our last ping message. + // our last ping message. To be used atomically. pingLastSend int64 - // MUST be used atomically. - started int32 - disconnect int32 - connReq *connmgr.ConnReq conn net.Conn @@ -624,7 +626,7 @@ func (p *peer) readNextMessage() (lnwire.Message, error) { // TODO(conner): use stream handler interface to abstract out stream // state/logging type msgStream struct { - streamShutdown int32 + streamShutdown int32 // To be used atomically. peer *peer diff --git a/routing/chainview/bitcoind.go b/routing/chainview/bitcoind.go index c4b5e454..db7ec301 100644 --- a/routing/chainview/bitcoind.go +++ b/routing/chainview/bitcoind.go @@ -21,8 +21,8 @@ import ( // BitcoindFilteredChainView is an implementation of the FilteredChainView // interface which is backed by bitcoind. type BitcoindFilteredChainView struct { - started int32 - stopped int32 + started int32 // To be used atomically. + stopped int32 // To be used atomically. // bestHeight is the height of the latest block added to the // blockQueue from the onFilteredConnectedMethod. It is used to diff --git a/routing/chainview/btcd.go b/routing/chainview/btcd.go index 286ca49e..e5dad71d 100644 --- a/routing/chainview/btcd.go +++ b/routing/chainview/btcd.go @@ -17,8 +17,8 @@ import ( // BtcdFilteredChainView is an implementation of the FilteredChainView // interface which is backed by an active websockets connection to btcd. type BtcdFilteredChainView struct { - started int32 - stopped int32 + started int32 // To be used atomically. + stopped int32 // To be used atomically. // bestHeight is the height of the latest block added to the // blockQueue from the onFilteredConnectedMethod. It is used to diff --git a/routing/chainview/neutrino.go b/routing/chainview/neutrino.go index cf622739..4664078f 100644 --- a/routing/chainview/neutrino.go +++ b/routing/chainview/neutrino.go @@ -20,8 +20,8 @@ import ( // blocks, the light client is able to query filters locally, to test if an // item in a block modifies any of our watched set of UTXOs. type CfFilteredChainView struct { - started int32 - stopped int32 + started int32 // To be used atomically. + stopped int32 // To be used atomically. // p2pNode is a pointer to the running GCS-filter supported Bitcoin // light clientl diff --git a/routing/router.go b/routing/router.go index 367a992e..d03a3f45 100644 --- a/routing/router.go +++ b/routing/router.go @@ -200,12 +200,12 @@ func newRouteTuple(amt lnwire.MilliSatoshi, dest []byte) routeTuple { // automatically as new blocks are discovered which spend certain known funding // outpoints, thereby closing their respective channels. type ChannelRouter struct { - ntfnClientCounter uint64 + ntfnClientCounter uint64 // To be used atomically. - started uint32 - stopped uint32 + started uint32 // To be used atomically. + stopped uint32 // To be used atomically. - bestHeight uint32 + bestHeight uint32 // To be used atomically. // cfg is a copy of the configuration struct that the ChannelRouter was // initialized with. diff --git a/server.go b/server.go index aebc111f..6f0bb092 100644 --- a/server.go +++ b/server.go @@ -762,7 +762,8 @@ func (s *server) peerBootstrapper(numTargetPeers uint32, // We'll use the number of attempts and errors to determine if we need // to increase the time between discovery epochs. - var epochErrors, epochAttempts uint32 + var epochErrors uint32 // To be used atomically. + var epochAttempts uint32 for { select { diff --git a/utxonursery.go b/utxonursery.go index 945c64f6..2e2d1f2e 100644 --- a/utxonursery.go +++ b/utxonursery.go @@ -220,8 +220,8 @@ type NurseryConfig struct { // the source wallet, returning the outputs so they can be used within future // channels, or regular Bitcoin transactions. type utxoNursery struct { - started uint32 - stopped uint32 + started uint32 // To be used atomically. + stopped uint32 // To be used atomically. cfg *NurseryConfig