Skip to content

Commit 03dd8ca

Browse files
fix noctx linter
Co-authored-by: Stephen Buttolph <[email protected]> Signed-off-by: Joshua Kim <[email protected]>
1 parent 3b0f8d4 commit 03dd8ca

33 files changed

+326
-160
lines changed

app/app.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package app
55

66
import (
7+
"context"
78
"fmt"
89
"os"
910
"os/signal"
@@ -33,7 +34,7 @@ var _ App = (*app)(nil)
3334
type App interface {
3435
// Start kicks off the application and returns immediately.
3536
// Start should only be called once.
36-
Start()
37+
Start(ctx context.Context)
3738

3839
// Stop notifies the application to exit and returns immediately.
3940
// Stop should only be called after [Start].
@@ -45,7 +46,7 @@ type App interface {
4546
ExitCode() int
4647
}
4748

48-
func New(config nodeconfig.Config) (App, error) {
49+
func New(ctx context.Context, config nodeconfig.Config) (App, error) {
4950
// Set the data directory permissions to be read write.
5051
if err := perms.ChmodR(config.DatabaseConfig.Path, true, perms.ReadWriteExecute); err != nil {
5152
return nil, fmt.Errorf("failed to restrict the permissions of the database directory with: %w", err)
@@ -71,7 +72,7 @@ func New(config nodeconfig.Config) (App, error) {
7172
return nil, err
7273
}
7374

74-
n, err := node.New(&config, logFactory, log)
75+
n, err := node.New(ctx, &config, logFactory, log)
7576
if err != nil {
7677
log.Fatal("failed to initialize node", zap.Error(err))
7778
log.Stop()
@@ -86,9 +87,9 @@ func New(config nodeconfig.Config) (App, error) {
8687
}, nil
8788
}
8889

89-
func Run(app App) int {
90+
func Run(ctx context.Context, app App) int {
9091
// start running the application
91-
app.Start()
92+
app.Start(ctx)
9293

9394
// register terminationSignals to kill the application
9495
terminationSignals := make(chan os.Signal, 1)
@@ -138,7 +139,7 @@ type app struct {
138139

139140
// Start the business logic of the node (as opposed to config reading, etc).
140141
// Does not block until the node is done.
141-
func (a *app) Start() {
142+
func (a *app) Start(ctx context.Context) {
142143
// [p.ExitCode] will block until [p.exitWG.Done] is called
143144
a.exitWG.Add(1)
144145
go func() {
@@ -157,7 +158,7 @@ func (a *app) Start() {
157158
a.log.StopOnPanic()
158159
}()
159160

160-
err := a.node.Dispatch()
161+
err := a.node.Dispatch(ctx)
161162
a.log.Debug("dispatch returned",
162163
zap.Error(err),
163164
)

main/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package main
55

66
import (
7+
"context"
78
"encoding/json"
89
"errors"
910
"fmt"
@@ -61,12 +62,12 @@ func main() {
6162
fmt.Println(app.Header)
6263
}
6364

64-
nodeApp, err := app.New(nodeConfig)
65+
nodeApp, err := app.New(context.Background(), nodeConfig)
6566
if err != nil {
6667
fmt.Printf("couldn't start node: %s\n", err)
6768
os.Exit(1)
6869
}
6970

70-
exitCode := app.Run(nodeApp)
71+
exitCode := app.Run(context.Background(), nodeApp)
7172
os.Exit(exitCode)
7273
}

nat/nat.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package nat
55

66
import (
7+
"context"
78
"net/netip"
89
"sync"
910
"time"
@@ -33,15 +34,15 @@ type Router interface {
3334
}
3435

3536
// GetRouter returns a router on the current network.
36-
func GetRouter() Router {
37+
func GetRouter(ctx context.Context) Router {
3738
if r := getUPnPRouter(); r != nil {
3839
return r
3940
}
4041
if r := getPMPRouter(); r != nil {
4142
return r
4243
}
4344

44-
return NewNoRouter()
45+
return NewNoRouter(ctx)
4546
}
4647

4748
// Mapper attempts to open a set of ports on a router

nat/no_router.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package nat
55

66
import (
7+
"context"
78
"errors"
89
"net"
910
"net/netip"
@@ -40,8 +41,12 @@ func (r noRouter) ExternalIP() (netip.Addr, error) {
4041
return r.ip, r.ipErr
4142
}
4243

43-
func getOutboundIP() (netip.Addr, error) {
44-
conn, err := net.Dial("udp", googleDNSServer)
44+
func getOutboundIP(ctx context.Context) (netip.Addr, error) {
45+
conn, err := (&net.Dialer{}).DialContext(
46+
ctx,
47+
"udp",
48+
googleDNSServer,
49+
)
4550
if err != nil {
4651
return netip.Addr{}, err
4752
}
@@ -63,8 +68,8 @@ func getOutboundIP() (netip.Addr, error) {
6368
}
6469

6570
// NewNoRouter returns a router that assumes the network is public
66-
func NewNoRouter() Router {
67-
ip, err := getOutboundIP()
71+
func NewNoRouter(ctx context.Context) Router {
72+
ip, err := getOutboundIP(ctx)
6873
return &noRouter{
6974
ip: ip,
7075
ipErr: err,

network/dialer/dialer_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ func TestDialerDialCanceledContext(t *testing.T) {
3333
func TestDialerDial(t *testing.T) {
3434
require := require.New(t)
3535

36-
l, err := net.Listen("tcp", "127.0.0.1:0")
36+
l, err := (&net.ListenConfig{}).Listen(
37+
context.Background(),
38+
"tcp",
39+
"127.0.0.1:0",
40+
)
3741
require.NoError(err)
3842

3943
listenedAddrPort, err := netip.ParseAddrPort(l.Addr().String())

network/example_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ func ExampleNewTestNetwork() {
122122
// gossip will enable connecting to all the remaining nodes in the network.
123123
bootstrappers := genesis.SampleBootstrappers(constants.FujiID, 5)
124124
for _, bootstrapper := range bootstrappers {
125-
network.ManuallyTrack(bootstrapper.ID, bootstrapper.IP)
125+
network.ManuallyTrack(
126+
context.Background(),
127+
bootstrapper.ID,
128+
bootstrapper.IP,
129+
)
126130
}
127131

128132
// Typically network.StartClose() should be called based on receiving a
@@ -137,7 +141,7 @@ func ExampleNewTestNetwork() {
137141

138142
// Calling network.Dispatch() will block until a fatal error occurs or
139143
// network.StartClose() is called.
140-
err = network.Dispatch()
144+
err = network.Dispatch(context.Background())
141145
log.Info(
142146
"network exited",
143147
zap.Error(err),

network/network.go

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ type Network interface {
7575

7676
// Should only be called once, will run until either a fatal error occurs,
7777
// or the network is closed.
78-
Dispatch() error
78+
Dispatch(ctx context.Context) error
7979

8080
// Attempt to connect to this IP. The network will never stop attempting to
8181
// connect to this ID.
82-
ManuallyTrack(nodeID ids.NodeID, ip netip.AddrPort)
82+
ManuallyTrack(ctx context.Context, nodeID ids.NodeID, ip netip.AddrPort)
8383

8484
// PeerInfo returns information about peers. If [nodeIDs] is empty, returns
8585
// info about all peers that have finished the handshake. Otherwise, returns
@@ -506,10 +506,13 @@ func (n *network) AllowConnection(nodeID ids.NodeID) bool {
506506
return areWeAPrimaryNetworkAValidator || n.ipTracker.WantsConnection(nodeID)
507507
}
508508

509-
func (n *network) Track(claimedIPPorts []*ips.ClaimedIPPort) error {
509+
func (n *network) Track(
510+
ctx context.Context,
511+
claimedIPPorts []*ips.ClaimedIPPort,
512+
) error {
510513
_, areWeAPrimaryNetworkAValidator := n.config.Validators.GetValidator(constants.PrimaryNetworkID, n.config.MyNodeID)
511514
for _, ip := range claimedIPPorts {
512-
if err := n.track(ip, areWeAPrimaryNetworkAValidator); err != nil {
515+
if err := n.track(ctx, ip, areWeAPrimaryNetworkAValidator); err != nil {
513516
return err
514517
}
515518
}
@@ -521,17 +524,17 @@ func (n *network) Track(claimedIPPorts []*ips.ClaimedIPPort) error {
521524
// It is guaranteed that [Connected] will not be called with [nodeID] after this
522525
// call. Note that this is from the perspective of a single peer object, because
523526
// a peer with the same ID can reconnect to this network instance.
524-
func (n *network) Disconnected(nodeID ids.NodeID) {
527+
func (n *network) Disconnected(ctx context.Context, nodeID ids.NodeID) {
525528
n.peersLock.RLock()
526529
_, connecting := n.connectingPeers.GetByID(nodeID)
527530
peer, connected := n.connectedPeers.GetByID(nodeID)
528531
n.peersLock.RUnlock()
529532

530533
if connecting {
531-
n.disconnectedFromConnecting(nodeID)
534+
n.disconnectedFromConnecting(ctx, nodeID)
532535
}
533536
if connected {
534-
n.disconnectedFromConnected(peer, nodeID)
537+
n.disconnectedFromConnected(ctx, peer, nodeID)
535538
}
536539
}
537540

@@ -599,7 +602,7 @@ func (n *network) Peers(
599602

600603
// Dispatch starts accepting connections from other nodes attempting to connect
601604
// to this node.
602-
func (n *network) Dispatch() error {
605+
func (n *network) Dispatch(ctx context.Context) error {
603606
go n.runTimers() // Periodically perform operations
604607
go n.inboundConnUpgradeThrottler.Dispatch()
605608
for n.onCloseCtx.Err() == nil { // Continuously accept new connections
@@ -647,7 +650,7 @@ func (n *network) Dispatch() error {
647650
zap.Stringer("peerIP", ip),
648651
)
649652

650-
if err := n.upgrade(conn, n.serverUpgrader, true); err != nil {
653+
if err := n.upgrade(ctx, conn, n.serverUpgrader, true); err != nil {
651654
n.peerConfig.Log.Verbo("failed to upgrade connection",
652655
zap.String("direction", "inbound"),
653656
zap.Error(err),
@@ -670,7 +673,11 @@ func (n *network) Dispatch() error {
670673
return errs.Err
671674
}
672675

673-
func (n *network) ManuallyTrack(nodeID ids.NodeID, ip netip.AddrPort) {
676+
func (n *network) ManuallyTrack(
677+
ctx context.Context,
678+
nodeID ids.NodeID,
679+
ip netip.AddrPort,
680+
) {
674681
n.ipTracker.ManuallyTrack(nodeID)
675682

676683
n.peersLock.Lock()
@@ -688,11 +695,15 @@ func (n *network) ManuallyTrack(nodeID ids.NodeID, ip netip.AddrPort) {
688695
if !isTracked {
689696
tracked := newTrackedIP(ip)
690697
n.trackedIPs[nodeID] = tracked
691-
n.dial(nodeID, tracked)
698+
n.dial(ctx, nodeID, tracked)
692699
}
693700
}
694701

695-
func (n *network) track(ip *ips.ClaimedIPPort, trackAllSubnets bool) error {
702+
func (n *network) track(
703+
ctx context.Context,
704+
ip *ips.ClaimedIPPort,
705+
trackAllSubnets bool,
706+
) error {
696707
// To avoid signature verification when the IP isn't needed, we
697708
// optimistically filter out IPs. This can result in us not tracking an IP
698709
// that we otherwise would have. This case can only happen if the node
@@ -741,7 +752,7 @@ func (n *network) track(ip *ips.ClaimedIPPort, trackAllSubnets bool) error {
741752
tracked = newTrackedIP(ip.AddrPort)
742753
}
743754
n.trackedIPs[ip.NodeID] = tracked
744-
n.dial(ip.NodeID, tracked)
755+
n.dial(ctx, ip.NodeID, tracked)
745756
return nil
746757
}
747758

@@ -834,7 +845,10 @@ func (n *network) samplePeers(
834845
)
835846
}
836847

837-
func (n *network) disconnectedFromConnecting(nodeID ids.NodeID) {
848+
func (n *network) disconnectedFromConnecting(
849+
ctx context.Context,
850+
nodeID ids.NodeID,
851+
) {
838852
n.peersLock.Lock()
839853
defer n.peersLock.Unlock()
840854

@@ -846,7 +860,7 @@ func (n *network) disconnectedFromConnecting(nodeID ids.NodeID) {
846860
if n.ipTracker.WantsConnection(nodeID) {
847861
tracked := tracked.trackNewIP(tracked.ip)
848862
n.trackedIPs[nodeID] = tracked
849-
n.dial(nodeID, tracked)
863+
n.dial(ctx, nodeID, tracked)
850864
} else {
851865
tracked.stopTracking()
852866
delete(n.trackedIPs, nodeID)
@@ -856,7 +870,11 @@ func (n *network) disconnectedFromConnecting(nodeID ids.NodeID) {
856870
n.metrics.disconnected.Inc()
857871
}
858872

859-
func (n *network) disconnectedFromConnected(peer peer.Peer, nodeID ids.NodeID) {
873+
func (n *network) disconnectedFromConnected(
874+
ctx context.Context,
875+
peer peer.Peer,
876+
nodeID ids.NodeID,
877+
) {
860878
n.ipTracker.Disconnected(nodeID)
861879
n.router.Disconnected(nodeID)
862880

@@ -869,7 +887,7 @@ func (n *network) disconnectedFromConnected(peer peer.Peer, nodeID ids.NodeID) {
869887
if ip, wantsConnection := n.ipTracker.GetIP(nodeID); wantsConnection {
870888
tracked := newTrackedIP(ip.AddrPort)
871889
n.trackedIPs[nodeID] = tracked
872-
n.dial(nodeID, tracked)
890+
n.dial(ctx, nodeID, tracked)
873891
}
874892

875893
n.metrics.markDisconnected(peer)
@@ -894,7 +912,7 @@ func (n *network) disconnectedFromConnected(peer peer.Peer, nodeID ids.NodeID) {
894912
// If initiating a connection to [ip] fails, then dial will reattempt. However,
895913
// there is a randomized exponential backoff to avoid spamming connection
896914
// attempts.
897-
func (n *network) dial(nodeID ids.NodeID, ip *trackedIP) {
915+
func (n *network) dial(ctx context.Context, nodeID ids.NodeID, ip *trackedIP) {
898916
n.peerConfig.Log.Verbo("attempting to dial node",
899917
zap.Stringer("nodeID", nodeID),
900918
zap.Stringer("ip", ip.ip),
@@ -994,7 +1012,7 @@ func (n *network) dial(nodeID ids.NodeID, ip *trackedIP) {
9941012
zap.Stringer("peerIP", ip.ip),
9951013
)
9961014

997-
err = n.upgrade(conn, n.clientUpgrader, false)
1015+
err = n.upgrade(ctx, conn, n.clientUpgrader, false)
9981016
if err != nil {
9991017
n.peerConfig.Log.Verbo(
10001018
"failed to upgrade, attempting again",
@@ -1017,7 +1035,12 @@ func (n *network) dial(nodeID ids.NodeID, ip *trackedIP) {
10171035
// If the connection is desired by the node, then the resulting upgraded
10181036
// connection will be used to create a new peer. Otherwise the connection will
10191037
// be immediately closed.
1020-
func (n *network) upgrade(conn net.Conn, upgrader peer.Upgrader, isIngress bool) error {
1038+
func (n *network) upgrade(
1039+
ctx context.Context,
1040+
conn net.Conn,
1041+
upgrader peer.Upgrader,
1042+
isIngress bool,
1043+
) error {
10211044
upgradeTimeout := n.peerConfig.Clock.Time().Add(n.config.ReadHandshakeTimeout)
10221045
if err := conn.SetReadDeadline(upgradeTimeout); err != nil {
10231046
_ = conn.Close()
@@ -1027,7 +1050,7 @@ func (n *network) upgrade(conn net.Conn, upgrader peer.Upgrader, isIngress bool)
10271050
return err
10281051
}
10291052

1030-
nodeID, tlsConn, cert, err := upgrader.Upgrade(conn)
1053+
nodeID, tlsConn, cert, err := upgrader.Upgrade(ctx, conn)
10311054
if err != nil {
10321055
_ = conn.Close()
10331056
n.peerConfig.Log.Verbo("failed to upgrade connection",
@@ -1107,6 +1130,7 @@ func (n *network) upgrade(conn net.Conn, upgrader peer.Upgrader, isIngress bool)
11071130
// same [peerConfig.InboundMsgThrottler]. This is guaranteed by the above
11081131
// de-duplications for [connectingPeers] and [connectedPeers].
11091132
peer := peer.Start(
1133+
ctx,
11101134
n.peerConfig,
11111135
tlsConn,
11121136
cert,

0 commit comments

Comments
 (0)