|
|
|
|
@@ -51,17 +51,17 @@ func NewMinimalNetAdapter(cfg *config.Config) (*MinimalNetAdapter, error) {
|
|
|
|
|
// To simplify usage the return type contains only two routes:
|
|
|
|
|
// OutgoingRoute - for all outgoing messages
|
|
|
|
|
// IncomingRoute - for all incoming messages (excluding handshake messages)
|
|
|
|
|
func (nam *MinimalNetAdapter) Connect(address string) (*Routes, error) {
|
|
|
|
|
nam.lock.Lock()
|
|
|
|
|
defer nam.lock.Unlock()
|
|
|
|
|
func (mna *MinimalNetAdapter) Connect(address string) (*Routes, error) {
|
|
|
|
|
mna.lock.Lock()
|
|
|
|
|
defer mna.lock.Unlock()
|
|
|
|
|
|
|
|
|
|
err := nam.netAdapter.Connect(address)
|
|
|
|
|
err := mna.netAdapter.Connect(address)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
routes := <-nam.routesChan
|
|
|
|
|
err = handleHandshake(routes, nam.netAdapter.ID())
|
|
|
|
|
routes := <-mna.routesChan
|
|
|
|
|
err = handleHandshake(routes, mna.netAdapter.ID())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "Error in handshake")
|
|
|
|
|
}
|
|
|
|
|
@@ -76,6 +76,9 @@ func (nam *MinimalNetAdapter) Connect(address string) (*Routes, error) {
|
|
|
|
|
return routes, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handlePingPong makes sure that we are not disconnected due to not responding to pings.
|
|
|
|
|
// However, it only responds to pings, not sending its own, to conform to the minimal-ness
|
|
|
|
|
// of MinimalNetAdapter
|
|
|
|
|
func handlePingPong(routes *Routes) error {
|
|
|
|
|
for {
|
|
|
|
|
message, err := routes.pingRoute.Dequeue()
|
|
|
|
|
@@ -103,7 +106,7 @@ func handleHandshake(routes *Routes, ourID *id.ID) error {
|
|
|
|
|
|
|
|
|
|
versionMessage, ok := msg.(*wire.MsgVersion)
|
|
|
|
|
if !ok {
|
|
|
|
|
return errors.Errorf("Expected first message to be of type %s, but got %s", wire.CmdVersion, msg.Command())
|
|
|
|
|
return errors.Errorf("expected first message to be of type %s, but got %s", wire.CmdVersion, msg.Command())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = routes.OutgoingRoute.Enqueue(&wire.MsgVersion{
|
|
|
|
|
@@ -128,7 +131,7 @@ func handleHandshake(routes *Routes, ourID *id.ID) error {
|
|
|
|
|
|
|
|
|
|
_, ok = msg.(*wire.MsgVerAck)
|
|
|
|
|
if !ok {
|
|
|
|
|
return errors.Errorf("Expected second message to be of type %s, but got %s", wire.CmdVerAck, msg.Command())
|
|
|
|
|
return errors.Errorf("expected second message to be of type %s, but got %s", wire.CmdVerAck, msg.Command())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = routes.OutgoingRoute.Enqueue(&wire.MsgVerAck{})
|
|
|
|
|
@@ -140,11 +143,18 @@ func handleHandshake(routes *Routes, ourID *id.ID) error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func generateRouteInitializer() (netadapter.RouterInitializer, <-chan *Routes) {
|
|
|
|
|
everythingElse := make([]wire.MessageCommand, 0, len(wire.MessageCommandToString)-3)
|
|
|
|
|
cmdsWithBuiltInRoutes := []wire.MessageCommand{wire.CmdVerAck, wire.CmdVersion, wire.CmdPing}
|
|
|
|
|
|
|
|
|
|
everythingElse := make([]wire.MessageCommand, 0, len(wire.MessageCommandToString)-len(cmdsWithBuiltInRoutes))
|
|
|
|
|
outerLoop:
|
|
|
|
|
for command := range wire.MessageCommandToString {
|
|
|
|
|
if command != wire.CmdVersion && command != wire.CmdVerAck && command != wire.CmdPing {
|
|
|
|
|
everythingElse = append(everythingElse, command)
|
|
|
|
|
for _, cmdWithBuiltInRoute := range cmdsWithBuiltInRoutes {
|
|
|
|
|
if command == cmdWithBuiltInRoute {
|
|
|
|
|
continue outerLoop
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
everythingElse = append(everythingElse, command)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
routesChan := make(chan *Routes)
|
|
|
|
|
@@ -152,16 +162,16 @@ func generateRouteInitializer() (netadapter.RouterInitializer, <-chan *Routes) {
|
|
|
|
|
routeInitializer := func(router *router.Router, netConnection *netadapter.NetConnection) {
|
|
|
|
|
handshakeRoute, err := router.AddIncomingRoute([]wire.MessageCommand{wire.CmdVersion, wire.CmdVerAck})
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(errors.Wrap(err, "Error registering handshake route"))
|
|
|
|
|
panic(errors.Wrap(err, "error registering handshake route"))
|
|
|
|
|
}
|
|
|
|
|
pingRoute, err := router.AddIncomingRoute([]wire.MessageCommand{wire.CmdPing})
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(errors.Wrap(err, "Error registering ping route"))
|
|
|
|
|
panic(errors.Wrap(err, "error registering ping route"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
everythingElseRoute, err := router.AddIncomingRoute(everythingElse)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(errors.Wrap(err, "Error registering everythingElseRoute"))
|
|
|
|
|
panic(errors.Wrap(err, "error registering everythingElseRoute"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spawn("netAdapterMock-routeInitializer-sendRoutesToChan", func() {
|