stasatdaglabs a34091991a [NOD-1538] Fix MinimalNetAdapter and don't insert BlockRelations before making sure the block's parents exist (#1074)
* [NOD-1538] Fix minimal net adapter.

* [NOD-1538] Don't insert block relation until we've validated that the block's parents exist.

* [NOD-1538] Don't hold addressManager in MinimalNetAdapter.

* [NOD-1538] Fix a comment in messages.proto.
2020-11-17 16:00:16 +02:00

56 lines
1.7 KiB
Go

package standalone
import (
"time"
"github.com/kaspanet/kaspad/infrastructure/network/netadapter"
"github.com/pkg/errors"
"github.com/kaspanet/kaspad/app/appmessage"
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/router"
)
// Routes holds the incoming and outgoing routes of a connection created by MinimalNetAdapter
type Routes struct {
netConnection *netadapter.NetConnection
IncomingRoute, OutgoingRoute *router.Route
handshakeRoute *router.Route
addressesRoute *router.Route
pingRoute *router.Route
}
// WaitForMessageOfType waits for a message of requested type up to `timeout`, skipping all messages of any other type
// received while waiting
func (r *Routes) WaitForMessageOfType(command appmessage.MessageCommand, timeout time.Duration) (appmessage.Message, error) {
timeoutTime := time.Now().Add(timeout)
for {
message, err := r.IncomingRoute.DequeueWithTimeout(timeoutTime.Sub(time.Now()))
if err != nil {
return nil, errors.Wrapf(err, "error waiting for message of type %s", command)
}
if message.Command() == command {
return message, nil
}
}
}
// WaitForDisconnect waits for a disconnect up to `timeout`, skipping all messages received while waiting
func (r *Routes) WaitForDisconnect(timeout time.Duration) error {
timeoutTime := time.Now().Add(timeout)
for {
_, err := r.IncomingRoute.DequeueWithTimeout(timeoutTime.Sub(time.Now()))
if errors.Is(err, router.ErrRouteClosed) {
return nil
}
if err != nil {
return errors.Wrap(err, "error waiting for disconnect")
}
}
}
// Disconnect closes the connection behind the routes, thus closing all routes
func (r *Routes) Disconnect() {
r.netConnection.Disconnect()
}