func (n *Node) versionExchange(addr *payload.AddressInfo) { resp := &VerDat{} defer func() { n.VerIn <- resp }() defer func() { if r := recover(); r != nil { resp.Err = fmt.Errorf("[ERR] version exchange did not complete (%v)", r) n.Log.Print(resp.Err) } }() n.Log.Printf("[INFO] version exchange with %v", addr.Addr()) conn, err := net.DialTimeout("tcp", addr.Addr(), defaultTimeout) if err != nil { panic(err) } defer conn.Close() n.verOutVerackIn(conn, addr, payload.ProtocolVersion) // wait for version message and send verack m := msg.Must(msg.ReadKind(conn, msg.Cversion)) resp.Ver, err = payload.VersionDecode(m.Payload()) if err != nil { panic(err) } if _, err := conn.Write(msg.New(msg.Cverack, []byte{}).Encode()); err != nil { panic(err) } n.sendInvAndAddr(conn, resp.Ver.Protocol()) // wait for addr and inv messages m = msg.Must(msg.ReadKind(conn, msg.Caddr)) resp.Peers, err = payload.AddrDecode(resp.Ver.Protocol(), m.Payload()) if err != nil { panic(err) } m = msg.Must(msg.ReadKind(conn, msg.Cinv)) resp.Inv, err = payload.InventoryDecode(resp.Ver.Protocol(), m.Payload()) if err != nil { panic(err) } n.Log.Printf("[INFO] version exchange with %v successful", addr.Addr()) }
func (n *Node) versionSequence(m *msg.Msg, conn net.Conn) { resp := &VerDat{} defer func() { if r := recover(); r != nil { resp.Err = fmt.Errorf("[ERR] version sequence did not complete (%v)", r) n.Log.Print(resp.Err) } }() var err error n.Log.Printf("[INFO] version sequence with %v", conn.RemoteAddr()) resp.Ver, err = payload.VersionDecode(m.Payload()) if err != nil { panic(err) } defer func() { n.VerIn <- resp }() if _, err := conn.Write(msg.New(msg.Cverack, []byte{}).Encode()); err != nil { panic(err) } n.verOutVerackIn(conn, resp.Ver.FromAddr, resp.Ver.Protocol()) n.sendInvAndAddr(conn, resp.Ver.Protocol()) // wait for addr and inv messages m = msg.Must(msg.ReadKind(conn, msg.Caddr)) resp.Peers, err = payload.AddrDecode(resp.Ver.Protocol(), m.Payload()) if err != nil { panic(err) } m = msg.Must(msg.ReadKind(conn, msg.Cinv)) resp.Inv, err = payload.InventoryDecode(resp.Ver.Protocol(), m.Payload()) if err != nil { panic(err) } n.Log.Printf("[INFO] version sequence with %v successful", conn.RemoteAddr()) }