Skip to content

Commit 84852aa

Browse files
authored
Bound the outbound giga handshake with the p2p handshake timeout (CON-501) (#4437)
## Summary - The outbound giga handshake in `dialAndRunConn` now uses `cfg.P2P.HandshakeTimeout`, the same deadline the CometBFT accept loop and dialer already apply. A configured `0` is an immediate deadline on both meshes; an unset giga config value stays unbounded. - Both validator and fullnode dialers go through that call. The timeout covers the handshake only, so a connection that completes it keeps running. ## Test plan - [x] `TestGigaRouterCommon_OutboundHandshakeBoundedByTimeout` passed 20× under `-race` (a peer that accepts and never speaks must return). - [x] `TestGigaRouterCommon*` / giga node config tests passed under `-race`. - [ ] CI Made with [Cursor](https://cursor.com)
1 parent 6e8b8ef commit 84852aa

4 files changed

Lines changed: 45 additions & 8 deletions

File tree

‎sei-tendermint/internal/p2p/giga_router.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ func (a GigaNodeAddr) String() string {
3030
// GigaRouterCommonConfig is the slice of giga config shared by both
3131
// validator and fullnode constructors.
3232
type GigaRouterCommonConfig struct {
33-
DialInterval time.Duration
34-
ValidatorAddrs map[atypes.PublicKey]GigaNodeAddr
35-
GenDoc *types.GenesisDoc
33+
DialInterval time.Duration
34+
HandshakeTimeout utils.Option[time.Duration]
35+
ValidatorAddrs map[atypes.PublicKey]GigaNodeAddr
36+
GenDoc *types.GenesisDoc
3637
// PersistentStateDir is the absolute on-disk root for durable state
3738
// (BlockDB, hashvault, epoch snapshots, and the validator's consensus
3839
// persister in sibling subdirs). Required and must already exist.

‎sei-tendermint/internal/p2p/giga_router_common.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,11 +526,12 @@ func (r *gigaRouterCommon) dialAndRunConn(
526526
return fmt.Errorf("tcp.Dial(%v): %w", addrs[0], err)
527527
}
528528
s.SpawnBg(func() error { return tcpConn.Run(ctx) })
529-
// TODO: handshake needs a timeout.
530-
hConn, err := handshake(ctx, tcpConn, r.key, handshakeSpec{
531-
SelfAddr: r.selfAddr,
532-
SeiGigaConnection: true,
533-
}, r.offer)
529+
hConn, err := utils.WithOptTimeout1(ctx, r.cfg.HandshakeTimeout, func(ctx context.Context) (*handshakedConn, error) {
530+
return handshake(ctx, tcpConn, r.key, handshakeSpec{
531+
SelfAddr: r.selfAddr,
532+
SeiGigaConnection: true,
533+
}, r.offer)
534+
})
534535
if err != nil {
535536
return fmt.Errorf("handshake(): %w", err)
536537
}

‎sei-tendermint/internal/p2p/giga_router_common_test.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"io"
78
"net/url"
89
"sync/atomic"
910
"testing"
@@ -792,3 +793,35 @@ func TestCommitteeWeights_DuplicateKey(t *testing.T) {
792793
})
793794
require.Error(t, err)
794795
}
796+
797+
// A peer that accepts and then says nothing must return from the handshake.
798+
func TestGigaRouterCommon_OutboundHandshakeBoundedByTimeout(t *testing.T) {
799+
rng := utils.TestRng()
800+
addr := tcp.TestReserveAddr()
801+
listener, err := tcp.Listen(addr)
802+
require.NoError(t, err)
803+
t.Cleanup(func() { require.NoError(t, listener.Close()) })
804+
router := &gigaRouterCommon{
805+
cfg: &GigaRouterCommonConfig{HandshakeTimeout: utils.Some(100 * time.Millisecond)},
806+
key: makeKey(rng),
807+
}
808+
809+
err = scope.Run(t.Context(), func(ctx context.Context, s scope.Scope) error {
810+
s.SpawnBg(func() error {
811+
// Hold the accepted connection open without ever speaking.
812+
conn, err := listener.AcceptOrClose(ctx)
813+
if err != nil {
814+
return err
815+
}
816+
return conn.Run(ctx)
817+
})
818+
return router.dialAndRunConn(ctx, atypes.GenSecretKey(rng).Public(), makeKey(rng).Public(),
819+
tcp.HostPort{Hostname: addr.Addr().String(), Port: addr.Port()},
820+
func(context.Context, rpc.Client[giga.API]) error { return nil })
821+
})
822+
// Cancelling the handshake half-closes the socket, so the connection pump
823+
// often reports EOF before handshake returns the deadline.
824+
if !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, io.EOF) {
825+
t.Fatalf("dialAndRunConn() = %v, want context.DeadlineExceeded or io.EOF", err)
826+
}
827+
}

‎sei-tendermint/node/setup.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ func buildGigaRouter(
327327
// The GigaRouter builds and owns the equivocation guard itself; just pass the operator's
328328
// enable/disable decision through as plain config.
329329
valCfg.HashVaultDisabledUnsafe = cfg.HashVaultDisabledUnsafe
330+
valCfg.HandshakeTimeout = utils.Some(cfg.P2P.HandshakeTimeout)
330331
logger.Info("Autobahn: starting as validator", "validators", len(valCfg.ValidatorAddrs))
331332
dataState, err := p2p.BuildDataState(&valCfg.GigaRouterCommonConfig, blockStore)
332333
if err != nil {
@@ -350,6 +351,7 @@ func buildGigaRouter(
350351
// The GigaRouter builds and owns the equivocation guard itself; just pass the operator's
351352
// enable/disable decision through as plain config.
352353
fnCfg.HashVaultDisabledUnsafe = cfg.HashVaultDisabledUnsafe
354+
fnCfg.HandshakeTimeout = utils.Some(cfg.P2P.HandshakeTimeout)
353355
logger.Info("Autobahn: starting as fullnode", "mode", cfg.Mode, "validators", len(validatorAddrs))
354356
dataState, err := p2p.BuildDataState(fnCfg, blockStore)
355357
if err != nil {

0 commit comments

Comments
 (0)