Skip to content

Commit

Permalink
Merge pull request #220 from lightninglabs/config-broadcast-timeout
Browse files Browse the repository at this point in the history
neutrino+pushtx: make broadcast timeout configurable with fallback
  • Loading branch information
Roasbeef authored Apr 29, 2021
2 parents 4dea380 + 0494024 commit e6974af
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
18 changes: 18 additions & 0 deletions neutrino.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,16 @@ type Config struct {
// up and this filter header state has diverged, then it'll remove the
// current on disk filter headers to sync them anew.
AssertFilterHeader *headerfs.FilterHeader

// BroadcastTimeout is the amount of time we'll wait before giving up on
// a transaction broadcast attempt. Broadcasting transactions consists
// of three steps:
//
// 1. Neutrino sends an inv for the transaction.
// 2. The recipient node determines if the inv is known, and if it's
// not, replies with a getdata message.
// 3. Neutrino sends the raw transaction.
BroadcastTimeout time.Duration
}

// peerSubscription holds a peer subscription which we'll notify about any
Expand Down Expand Up @@ -649,12 +659,19 @@ type ChainService struct {

nameResolver func(string) ([]net.IP, error)
dialer func(net.Addr) (net.Conn, error)

broadcastTimeout time.Duration
}

// NewChainService returns a new chain service configured to connect to the
// bitcoin network type specified by chainParams. Use start to begin syncing
// with peers.
func NewChainService(cfg Config) (*ChainService, error) {
// Use the default broadcast timeout if one isn't provided.
if cfg.BroadcastTimeout == 0 {
cfg.BroadcastTimeout = pushtx.DefaultBroadcastTimeout
}

// First, we'll sort out the methods that we'll use to established
// outbound TCP connections, as well as perform any DNS queries.
//
Expand Down Expand Up @@ -702,6 +719,7 @@ func NewChainService(cfg Config) (*ChainService, error) {
nameResolver: nameResolver,
dialer: dialer,
persistToDisk: cfg.PersistToDisk,
broadcastTimeout: cfg.BroadcastTimeout,
}
s.workManager = query.New(&query.Config{
ConnectedPeers: s.ConnectedPeers,
Expand Down
4 changes: 4 additions & 0 deletions pushtx/broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var (
)

const (
// DefaultBroadcastTimeout is the default timeout used when broadcasting
// transactions to network peers.
DefaultBroadcastTimeout = 5 * time.Second

// DefaultRebroadcastInterval is the default period that we'll wait
// between blocks to attempt another rebroadcast.
DefaultRebroadcastInterval = time.Minute
Expand Down
4 changes: 1 addition & 3 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -1114,10 +1114,8 @@ func (s *ChainService) sendTransaction(tx *wire.MsgTx, options ...QueryOption) e
rejections[*broadcastErr]++
}
},
// Default to 10s timeout. Default for queryAllPeers is a
// single try.
append(
[]QueryOption{Timeout(time.Second * 10)},
[]QueryOption{Timeout(s.broadcastTimeout)},
options...,
)...,
)
Expand Down

0 comments on commit e6974af

Please sign in to comment.