package bridge import ( "net" "testing" "time" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/nostrino/pkg/chain" "git.smesh.lol/nostrino/pkg/crypto/chainhash" ) // TestABlockTheNodeNeverAnswersEndsTheSession: a node socket can be dead without // being closed. The client writes its getdata, the gateway forwards it into a // socket that will never answer, and nothing arrives. The gateway must end the // session inside the client's own read deadline, so the client gets a close it // can retry from instead of a timeout it cannot tell from a slow transfer. func TestABlockTheNodeNeverAnswersEndsTheSession(t *testing.T) { magic := testMagic address := nodeScript(t, magic, func(conn net.Conn, reader *btcwire.Reader) { // Take whatever the gateway forwards and never answer it. for { if _, err := reader.Next(); err != nil { return } } }) srv := &Server{ cfg: Config{ Nodes: map[string]Node{ "alpha": {Addr: address, Profile: &chain.Profile{Name: "alpha", Magic: magic}}, }, NodeDialTimeout: 2 * time.Second, IdleTimeout: 10 * time.Second, BlockStallTimeout: 300 * time.Millisecond, Logf: func(string, ...any) {}, }, conns: make(map[net.Conn]struct{}), wsPerIP: make(map[string]int), done: make(chan struct{}), } start := time.Now() runSession(srv, "alpha", func(c net.Conn) { req := &btcwire.GetData{InvList: []btcwire.InvVect{ {Type: btcwire.InvVectTypeBlock, Hash: chainhash.Hash{1}}, }} payload, err := req.EncodePayload() if err != nil { t.Error(err) return } frame, err := btcwire.EncodeMessage(magic, btcwire.CmdGetData, payload) if err != nil { t.Error(err) return } if _, err := c.Write(frame); err != nil { return } // Stay connected until the session closes. buf := make([]byte, 1) _, _ = c.Read(buf) }) if lived := time.Since(start); lived > 5*time.Second { t.Fatalf("the session lived %s with an unanswered block, the bound is %s", lived.Round(time.Millisecond), srv.cfg.BlockStallTimeout) } }