Example #1
0
func (n *Node) respondGetData(m *msg.Msg, conn net.Conn) {
	hashes, err := payload.GetDataDecode(payload.ProtocolVersion, m.Payload())
	if err != nil {
		n.Log.Printf("[ERR] failed to decode getdata payload from %v (%v)", conn.RemoteAddr(), err)
		return
	}

	for _, sum := range hashes {
		s := fmt.Sprint("%x", sum)
		if data, ok := n.MyInv[s]; ok {
			if _, err := conn.Write(data); err != nil {
				n.Log.Printf("[ERR] failed to send all requested objects to %v (%v)", conn.RemoteAddr(), err)
				break
			}
		} else {
			n.Log.Printf("[ERR] %v requested object we don't have", conn.RemoteAddr())
		}
	}
	n.Log.Printf("[INFO] sent %v requested objects to %v", len(hashes), conn.RemoteAddr())
}
Example #2
0
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())
}