diff --git a/common/stream_services_test.go b/common/stream_services_test.go index a667d4bbe1..977a3bde67 100644 --- a/common/stream_services_test.go +++ b/common/stream_services_test.go @@ -4,9 +4,9 @@ import ( "fmt" "testing" - "github.com/dominant-strategies/go-quai/common/mocks" - "github.com/golang/mock/gomock" + mock_p2p "github.com/dominant-strategies/go-quai/p2p/mocks" "github.com/stretchr/testify/assert" + gomock "go.uber.org/mock/gomock" ) const ( @@ -24,13 +24,13 @@ func TestWriteMessageToStream(t *testing.T) { name string message []byte wantErr bool - setupMock func(*mocks.MockStream) + setupMock func(*mock_p2p.MockStream) }{ { name: "successful write with short message", message: []byte(shortMessage), wantErr: false, - setupMock: func(m *mocks.MockStream) { + setupMock: func(m *mock_p2p.MockStream) { m.EXPECT().SetWriteDeadline(gomock.Any()).Return(nil) gomock.InOrder( // first expect the length of the message to be written @@ -50,7 +50,7 @@ func TestWriteMessageToStream(t *testing.T) { name: "successful write with long message", message: []byte(longMessage), wantErr: false, - setupMock: func(m *mocks.MockStream) { + setupMock: func(m *mock_p2p.MockStream) { m.EXPECT().SetWriteDeadline(gomock.Any()).Return(nil) gomock.InOrder( // first expect the length of the message to be written @@ -70,7 +70,7 @@ func TestWriteMessageToStream(t *testing.T) { name: "error on write deadline", message: []byte(shortMessage), wantErr: true, - setupMock: func(m *mocks.MockStream) { + setupMock: func(m *mock_p2p.MockStream) { m.EXPECT().SetWriteDeadline(gomock.Any()).Return(fmt.Errorf("error")) }, }, @@ -81,7 +81,7 @@ func TestWriteMessageToStream(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockStream := mocks.NewMockStream(ctrl) + mockStream := mock_p2p.NewMockStream(ctrl) tt.setupMock(mockStream) err := WriteMessageToStream(mockStream, tt.message, "testproto", nil) @@ -98,13 +98,13 @@ func TestReadMessageFromStream(t *testing.T) { t.Skip("Todo: fix failing test") tests := []struct { name string - setupMock func(*mocks.MockStream) + setupMock func(*mock_p2p.MockStream) want []byte wantErr bool }{ { name: "successful read with short message", - setupMock: func(m *mocks.MockStream) { + setupMock: func(m *mock_p2p.MockStream) { m.EXPECT().SetReadDeadline(gomock.Any()).Return(nil) gomock.InOrder( // assert that the length of the message is read first @@ -127,7 +127,7 @@ func TestReadMessageFromStream(t *testing.T) { }, { name: "successful read with long message", - setupMock: func(m *mocks.MockStream) { + setupMock: func(m *mock_p2p.MockStream) { m.EXPECT().SetReadDeadline(gomock.Any()).Return(nil) gomock.InOrder( // assert that the length of the message is read first @@ -149,7 +149,7 @@ func TestReadMessageFromStream(t *testing.T) { }, { name: "error on read deadline", - setupMock: func(m *mocks.MockStream) { + setupMock: func(m *mock_p2p.MockStream) { m.EXPECT().SetReadDeadline(gomock.Any()).Return(fmt.Errorf("error")) }, wantErr: true, @@ -161,7 +161,7 @@ func TestReadMessageFromStream(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockStream := mocks.NewMockStream(ctrl) + mockStream := mock_p2p.NewMockStream(ctrl) tt.setupMock(mockStream) got, err := ReadMessageFromStream(mockStream, "testproto", nil) diff --git a/p2p/mocks/mockedConn.go b/p2p/mocks/mockedConn.go new file mode 100644 index 0000000000..aa44324122 --- /dev/null +++ b/p2p/mocks/mockedConn.go @@ -0,0 +1,426 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: libp2p/go-libp2p@v0.32.1/core/network/conn.go + +// Package mock_p2p is a generated GoMock package. +package mock_p2p + +import ( + context "context" + reflect "reflect" + + crypto "github.com/libp2p/go-libp2p/core/crypto" + network "github.com/libp2p/go-libp2p/core/network" + peer "github.com/libp2p/go-libp2p/core/peer" + multiaddr "github.com/multiformats/go-multiaddr" + gomock "go.uber.org/mock/gomock" +) + +// MockConn is a mock of Conn interface. +type MockConn struct { + ctrl *gomock.Controller + recorder *MockConnMockRecorder +} + +// MockConnMockRecorder is the mock recorder for MockConn. +type MockConnMockRecorder struct { + mock *MockConn +} + +// NewMockConn creates a new mock instance. +func NewMockConn(ctrl *gomock.Controller) *MockConn { + mock := &MockConn{ctrl: ctrl} + mock.recorder = &MockConnMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockConn) EXPECT() *MockConnMockRecorder { + return m.recorder +} + +// Close mocks base method. +func (m *MockConn) Close() error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Close") + ret0, _ := ret[0].(error) + return ret0 +} + +// Close indicates an expected call of Close. +func (mr *MockConnMockRecorder) Close() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockConn)(nil).Close)) +} + +// ConnState mocks base method. +func (m *MockConn) ConnState() network.ConnectionState { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ConnState") + ret0, _ := ret[0].(network.ConnectionState) + return ret0 +} + +// ConnState indicates an expected call of ConnState. +func (mr *MockConnMockRecorder) ConnState() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConnState", reflect.TypeOf((*MockConn)(nil).ConnState)) +} + +// GetStreams mocks base method. +func (m *MockConn) GetStreams() []network.Stream { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetStreams") + ret0, _ := ret[0].([]network.Stream) + return ret0 +} + +// GetStreams indicates an expected call of GetStreams. +func (mr *MockConnMockRecorder) GetStreams() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetStreams", reflect.TypeOf((*MockConn)(nil).GetStreams)) +} + +// ID mocks base method. +func (m *MockConn) ID() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ID") + ret0, _ := ret[0].(string) + return ret0 +} + +// ID indicates an expected call of ID. +func (mr *MockConnMockRecorder) ID() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ID", reflect.TypeOf((*MockConn)(nil).ID)) +} + +// IsClosed mocks base method. +func (m *MockConn) IsClosed() bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "IsClosed") + ret0, _ := ret[0].(bool) + return ret0 +} + +// IsClosed indicates an expected call of IsClosed. +func (mr *MockConnMockRecorder) IsClosed() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsClosed", reflect.TypeOf((*MockConn)(nil).IsClosed)) +} + +// LocalMultiaddr mocks base method. +func (m *MockConn) LocalMultiaddr() multiaddr.Multiaddr { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LocalMultiaddr") + ret0, _ := ret[0].(multiaddr.Multiaddr) + return ret0 +} + +// LocalMultiaddr indicates an expected call of LocalMultiaddr. +func (mr *MockConnMockRecorder) LocalMultiaddr() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LocalMultiaddr", reflect.TypeOf((*MockConn)(nil).LocalMultiaddr)) +} + +// LocalPeer mocks base method. +func (m *MockConn) LocalPeer() peer.ID { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LocalPeer") + ret0, _ := ret[0].(peer.ID) + return ret0 +} + +// LocalPeer indicates an expected call of LocalPeer. +func (mr *MockConnMockRecorder) LocalPeer() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LocalPeer", reflect.TypeOf((*MockConn)(nil).LocalPeer)) +} + +// NewStream mocks base method. +func (m *MockConn) NewStream(arg0 context.Context) (network.Stream, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "NewStream", arg0) + ret0, _ := ret[0].(network.Stream) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// NewStream indicates an expected call of NewStream. +func (mr *MockConnMockRecorder) NewStream(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewStream", reflect.TypeOf((*MockConn)(nil).NewStream), arg0) +} + +// RemoteMultiaddr mocks base method. +func (m *MockConn) RemoteMultiaddr() multiaddr.Multiaddr { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RemoteMultiaddr") + ret0, _ := ret[0].(multiaddr.Multiaddr) + return ret0 +} + +// RemoteMultiaddr indicates an expected call of RemoteMultiaddr. +func (mr *MockConnMockRecorder) RemoteMultiaddr() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoteMultiaddr", reflect.TypeOf((*MockConn)(nil).RemoteMultiaddr)) +} + +// RemotePeer mocks base method. +func (m *MockConn) RemotePeer() peer.ID { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RemotePeer") + ret0, _ := ret[0].(peer.ID) + return ret0 +} + +// RemotePeer indicates an expected call of RemotePeer. +func (mr *MockConnMockRecorder) RemotePeer() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemotePeer", reflect.TypeOf((*MockConn)(nil).RemotePeer)) +} + +// RemotePublicKey mocks base method. +func (m *MockConn) RemotePublicKey() crypto.PubKey { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RemotePublicKey") + ret0, _ := ret[0].(crypto.PubKey) + return ret0 +} + +// RemotePublicKey indicates an expected call of RemotePublicKey. +func (mr *MockConnMockRecorder) RemotePublicKey() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemotePublicKey", reflect.TypeOf((*MockConn)(nil).RemotePublicKey)) +} + +// Scope mocks base method. +func (m *MockConn) Scope() network.ConnScope { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Scope") + ret0, _ := ret[0].(network.ConnScope) + return ret0 +} + +// Scope indicates an expected call of Scope. +func (mr *MockConnMockRecorder) Scope() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Scope", reflect.TypeOf((*MockConn)(nil).Scope)) +} + +// Stat mocks base method. +func (m *MockConn) Stat() network.ConnStats { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Stat") + ret0, _ := ret[0].(network.ConnStats) + return ret0 +} + +// Stat indicates an expected call of Stat. +func (mr *MockConnMockRecorder) Stat() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Stat", reflect.TypeOf((*MockConn)(nil).Stat)) +} + +// MockConnSecurity is a mock of ConnSecurity interface. +type MockConnSecurity struct { + ctrl *gomock.Controller + recorder *MockConnSecurityMockRecorder +} + +// MockConnSecurityMockRecorder is the mock recorder for MockConnSecurity. +type MockConnSecurityMockRecorder struct { + mock *MockConnSecurity +} + +// NewMockConnSecurity creates a new mock instance. +func NewMockConnSecurity(ctrl *gomock.Controller) *MockConnSecurity { + mock := &MockConnSecurity{ctrl: ctrl} + mock.recorder = &MockConnSecurityMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockConnSecurity) EXPECT() *MockConnSecurityMockRecorder { + return m.recorder +} + +// ConnState mocks base method. +func (m *MockConnSecurity) ConnState() network.ConnectionState { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ConnState") + ret0, _ := ret[0].(network.ConnectionState) + return ret0 +} + +// ConnState indicates an expected call of ConnState. +func (mr *MockConnSecurityMockRecorder) ConnState() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConnState", reflect.TypeOf((*MockConnSecurity)(nil).ConnState)) +} + +// LocalPeer mocks base method. +func (m *MockConnSecurity) LocalPeer() peer.ID { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LocalPeer") + ret0, _ := ret[0].(peer.ID) + return ret0 +} + +// LocalPeer indicates an expected call of LocalPeer. +func (mr *MockConnSecurityMockRecorder) LocalPeer() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LocalPeer", reflect.TypeOf((*MockConnSecurity)(nil).LocalPeer)) +} + +// RemotePeer mocks base method. +func (m *MockConnSecurity) RemotePeer() peer.ID { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RemotePeer") + ret0, _ := ret[0].(peer.ID) + return ret0 +} + +// RemotePeer indicates an expected call of RemotePeer. +func (mr *MockConnSecurityMockRecorder) RemotePeer() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemotePeer", reflect.TypeOf((*MockConnSecurity)(nil).RemotePeer)) +} + +// RemotePublicKey mocks base method. +func (m *MockConnSecurity) RemotePublicKey() crypto.PubKey { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RemotePublicKey") + ret0, _ := ret[0].(crypto.PubKey) + return ret0 +} + +// RemotePublicKey indicates an expected call of RemotePublicKey. +func (mr *MockConnSecurityMockRecorder) RemotePublicKey() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemotePublicKey", reflect.TypeOf((*MockConnSecurity)(nil).RemotePublicKey)) +} + +// MockConnMultiaddrs is a mock of ConnMultiaddrs interface. +type MockConnMultiaddrs struct { + ctrl *gomock.Controller + recorder *MockConnMultiaddrsMockRecorder +} + +// MockConnMultiaddrsMockRecorder is the mock recorder for MockConnMultiaddrs. +type MockConnMultiaddrsMockRecorder struct { + mock *MockConnMultiaddrs +} + +// NewMockConnMultiaddrs creates a new mock instance. +func NewMockConnMultiaddrs(ctrl *gomock.Controller) *MockConnMultiaddrs { + mock := &MockConnMultiaddrs{ctrl: ctrl} + mock.recorder = &MockConnMultiaddrsMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockConnMultiaddrs) EXPECT() *MockConnMultiaddrsMockRecorder { + return m.recorder +} + +// LocalMultiaddr mocks base method. +func (m *MockConnMultiaddrs) LocalMultiaddr() multiaddr.Multiaddr { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LocalMultiaddr") + ret0, _ := ret[0].(multiaddr.Multiaddr) + return ret0 +} + +// LocalMultiaddr indicates an expected call of LocalMultiaddr. +func (mr *MockConnMultiaddrsMockRecorder) LocalMultiaddr() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LocalMultiaddr", reflect.TypeOf((*MockConnMultiaddrs)(nil).LocalMultiaddr)) +} + +// RemoteMultiaddr mocks base method. +func (m *MockConnMultiaddrs) RemoteMultiaddr() multiaddr.Multiaddr { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RemoteMultiaddr") + ret0, _ := ret[0].(multiaddr.Multiaddr) + return ret0 +} + +// RemoteMultiaddr indicates an expected call of RemoteMultiaddr. +func (mr *MockConnMultiaddrsMockRecorder) RemoteMultiaddr() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoteMultiaddr", reflect.TypeOf((*MockConnMultiaddrs)(nil).RemoteMultiaddr)) +} + +// MockConnStat is a mock of ConnStat interface. +type MockConnStat struct { + ctrl *gomock.Controller + recorder *MockConnStatMockRecorder +} + +// MockConnStatMockRecorder is the mock recorder for MockConnStat. +type MockConnStatMockRecorder struct { + mock *MockConnStat +} + +// NewMockConnStat creates a new mock instance. +func NewMockConnStat(ctrl *gomock.Controller) *MockConnStat { + mock := &MockConnStat{ctrl: ctrl} + mock.recorder = &MockConnStatMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockConnStat) EXPECT() *MockConnStatMockRecorder { + return m.recorder +} + +// Stat mocks base method. +func (m *MockConnStat) Stat() network.ConnStats { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Stat") + ret0, _ := ret[0].(network.ConnStats) + return ret0 +} + +// Stat indicates an expected call of Stat. +func (mr *MockConnStatMockRecorder) Stat() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Stat", reflect.TypeOf((*MockConnStat)(nil).Stat)) +} + +// MockConnScoper is a mock of ConnScoper interface. +type MockConnScoper struct { + ctrl *gomock.Controller + recorder *MockConnScoperMockRecorder +} + +// MockConnScoperMockRecorder is the mock recorder for MockConnScoper. +type MockConnScoperMockRecorder struct { + mock *MockConnScoper +} + +// NewMockConnScoper creates a new mock instance. +func NewMockConnScoper(ctrl *gomock.Controller) *MockConnScoper { + mock := &MockConnScoper{ctrl: ctrl} + mock.recorder = &MockConnScoperMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockConnScoper) EXPECT() *MockConnScoperMockRecorder { + return m.recorder +} + +// Scope mocks base method. +func (m *MockConnScoper) Scope() network.ConnScope { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Scope") + ret0, _ := ret[0].(network.ConnScope) + return ret0 +} + +// Scope indicates an expected call of Scope. +func (mr *MockConnScoperMockRecorder) Scope() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Scope", reflect.TypeOf((*MockConnScoper)(nil).Scope)) +} diff --git a/p2p/mocks/mockedHost.go b/p2p/mocks/mockedHost.go new file mode 100644 index 0000000000..e239d759c5 --- /dev/null +++ b/p2p/mocks/mockedHost.go @@ -0,0 +1,224 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/libp2p/go-libp2p@v0.32.1/core/host/host.go + +// Package mock_p2p is a generated GoMock package. +package mock_p2p + +import ( + context "context" + reflect "reflect" + + connmgr "github.com/libp2p/go-libp2p/core/connmgr" + event "github.com/libp2p/go-libp2p/core/event" + network "github.com/libp2p/go-libp2p/core/network" + peer "github.com/libp2p/go-libp2p/core/peer" + peerstore "github.com/libp2p/go-libp2p/core/peerstore" + protocol "github.com/libp2p/go-libp2p/core/protocol" + multiaddr "github.com/multiformats/go-multiaddr" + gomock "go.uber.org/mock/gomock" +) + +// MockHost is a mock of Host interface. +type MockHost struct { + ctrl *gomock.Controller + recorder *MockHostMockRecorder +} + +// MockHostMockRecorder is the mock recorder for MockHost. +type MockHostMockRecorder struct { + mock *MockHost +} + +// NewMockHost creates a new mock instance. +func NewMockHost(ctrl *gomock.Controller) *MockHost { + mock := &MockHost{ctrl: ctrl} + mock.recorder = &MockHostMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockHost) EXPECT() *MockHostMockRecorder { + return m.recorder +} + +// Addrs mocks base method. +func (m *MockHost) Addrs() []multiaddr.Multiaddr { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Addrs") + ret0, _ := ret[0].([]multiaddr.Multiaddr) + return ret0 +} + +// Addrs indicates an expected call of Addrs. +func (mr *MockHostMockRecorder) Addrs() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Addrs", reflect.TypeOf((*MockHost)(nil).Addrs)) +} + +// Close mocks base method. +func (m *MockHost) Close() error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Close") + ret0, _ := ret[0].(error) + return ret0 +} + +// Close indicates an expected call of Close. +func (mr *MockHostMockRecorder) Close() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockHost)(nil).Close)) +} + +// ConnManager mocks base method. +func (m *MockHost) ConnManager() connmgr.ConnManager { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ConnManager") + ret0, _ := ret[0].(connmgr.ConnManager) + return ret0 +} + +// ConnManager indicates an expected call of ConnManager. +func (mr *MockHostMockRecorder) ConnManager() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConnManager", reflect.TypeOf((*MockHost)(nil).ConnManager)) +} + +// Connect mocks base method. +func (m *MockHost) Connect(ctx context.Context, pi peer.AddrInfo) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Connect", ctx, pi) + ret0, _ := ret[0].(error) + return ret0 +} + +// Connect indicates an expected call of Connect. +func (mr *MockHostMockRecorder) Connect(ctx, pi any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Connect", reflect.TypeOf((*MockHost)(nil).Connect), ctx, pi) +} + +// EventBus mocks base method. +func (m *MockHost) EventBus() event.Bus { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "EventBus") + ret0, _ := ret[0].(event.Bus) + return ret0 +} + +// EventBus indicates an expected call of EventBus. +func (mr *MockHostMockRecorder) EventBus() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EventBus", reflect.TypeOf((*MockHost)(nil).EventBus)) +} + +// ID mocks base method. +func (m *MockHost) ID() peer.ID { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ID") + ret0, _ := ret[0].(peer.ID) + return ret0 +} + +// ID indicates an expected call of ID. +func (mr *MockHostMockRecorder) ID() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ID", reflect.TypeOf((*MockHost)(nil).ID)) +} + +// Mux mocks base method. +func (m *MockHost) Mux() protocol.Switch { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Mux") + ret0, _ := ret[0].(protocol.Switch) + return ret0 +} + +// Mux indicates an expected call of Mux. +func (mr *MockHostMockRecorder) Mux() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Mux", reflect.TypeOf((*MockHost)(nil).Mux)) +} + +// Network mocks base method. +func (m *MockHost) Network() network.Network { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Network") + ret0, _ := ret[0].(network.Network) + return ret0 +} + +// Network indicates an expected call of Network. +func (mr *MockHostMockRecorder) Network() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Network", reflect.TypeOf((*MockHost)(nil).Network)) +} + +// NewStream mocks base method. +func (m *MockHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (network.Stream, error) { + m.ctrl.T.Helper() + varargs := []any{ctx, p} + for _, a := range pids { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "NewStream", varargs...) + ret0, _ := ret[0].(network.Stream) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// NewStream indicates an expected call of NewStream. +func (mr *MockHostMockRecorder) NewStream(ctx, p any, pids ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{ctx, p}, pids...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewStream", reflect.TypeOf((*MockHost)(nil).NewStream), varargs...) +} + +// Peerstore mocks base method. +func (m *MockHost) Peerstore() peerstore.Peerstore { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Peerstore") + ret0, _ := ret[0].(peerstore.Peerstore) + return ret0 +} + +// Peerstore indicates an expected call of Peerstore. +func (mr *MockHostMockRecorder) Peerstore() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Peerstore", reflect.TypeOf((*MockHost)(nil).Peerstore)) +} + +// RemoveStreamHandler mocks base method. +func (m *MockHost) RemoveStreamHandler(pid protocol.ID) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "RemoveStreamHandler", pid) +} + +// RemoveStreamHandler indicates an expected call of RemoveStreamHandler. +func (mr *MockHostMockRecorder) RemoveStreamHandler(pid any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoveStreamHandler", reflect.TypeOf((*MockHost)(nil).RemoveStreamHandler), pid) +} + +// SetStreamHandler mocks base method. +func (m *MockHost) SetStreamHandler(pid protocol.ID, handler network.StreamHandler) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetStreamHandler", pid, handler) +} + +// SetStreamHandler indicates an expected call of SetStreamHandler. +func (mr *MockHostMockRecorder) SetStreamHandler(pid, handler any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetStreamHandler", reflect.TypeOf((*MockHost)(nil).SetStreamHandler), pid, handler) +} + +// SetStreamHandlerMatch mocks base method. +func (m *MockHost) SetStreamHandlerMatch(arg0 protocol.ID, arg1 func(protocol.ID) bool, arg2 network.StreamHandler) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetStreamHandlerMatch", arg0, arg1, arg2) +} + +// SetStreamHandlerMatch indicates an expected call of SetStreamHandlerMatch. +func (mr *MockHostMockRecorder) SetStreamHandlerMatch(arg0, arg1, arg2 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetStreamHandlerMatch", reflect.TypeOf((*MockHost)(nil).SetStreamHandlerMatch), arg0, arg1, arg2) +} diff --git a/p2p/mocks/mockedReporter.go b/p2p/mocks/mockedReporter.go new file mode 100644 index 0000000000..10708474d8 --- /dev/null +++ b/p2p/mocks/mockedReporter.go @@ -0,0 +1,157 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/libp2p/go-libp2p@v0.32.1/core/metrics/reporter.go +// + +// Package mock_p2p is a generated GoMock package. +package mock_p2p + + +import ( + reflect "reflect" + + metrics "github.com/libp2p/go-libp2p/core/metrics" + peer "github.com/libp2p/go-libp2p/core/peer" + protocol "github.com/libp2p/go-libp2p/core/protocol" + gomock "go.uber.org/mock/gomock" +) + +// MockReporter is a mock of Reporter interface. +type MockReporter struct { + ctrl *gomock.Controller + recorder *MockReporterMockRecorder +} + +// MockReporterMockRecorder is the mock recorder for MockReporter. +type MockReporterMockRecorder struct { + mock *MockReporter +} + +// NewMockReporter creates a new mock instance. +func NewMockReporter(ctrl *gomock.Controller) *MockReporter { + mock := &MockReporter{ctrl: ctrl} + mock.recorder = &MockReporterMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockReporter) EXPECT() *MockReporterMockRecorder { + return m.recorder +} + +// GetBandwidthByPeer mocks base method. +func (m *MockReporter) GetBandwidthByPeer() map[peer.ID]metrics.Stats { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetBandwidthByPeer") + ret0, _ := ret[0].(map[peer.ID]metrics.Stats) + return ret0 +} + +// GetBandwidthByPeer indicates an expected call of GetBandwidthByPeer. +func (mr *MockReporterMockRecorder) GetBandwidthByPeer() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBandwidthByPeer", reflect.TypeOf((*MockReporter)(nil).GetBandwidthByPeer)) +} + +// GetBandwidthByProtocol mocks base method. +func (m *MockReporter) GetBandwidthByProtocol() map[protocol.ID]metrics.Stats { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetBandwidthByProtocol") + ret0, _ := ret[0].(map[protocol.ID]metrics.Stats) + return ret0 +} + +// GetBandwidthByProtocol indicates an expected call of GetBandwidthByProtocol. +func (mr *MockReporterMockRecorder) GetBandwidthByProtocol() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBandwidthByProtocol", reflect.TypeOf((*MockReporter)(nil).GetBandwidthByProtocol)) +} + +// GetBandwidthForPeer mocks base method. +func (m *MockReporter) GetBandwidthForPeer(arg0 peer.ID) metrics.Stats { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetBandwidthForPeer", arg0) + ret0, _ := ret[0].(metrics.Stats) + return ret0 +} + +// GetBandwidthForPeer indicates an expected call of GetBandwidthForPeer. +func (mr *MockReporterMockRecorder) GetBandwidthForPeer(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBandwidthForPeer", reflect.TypeOf((*MockReporter)(nil).GetBandwidthForPeer), arg0) +} + +// GetBandwidthForProtocol mocks base method. +func (m *MockReporter) GetBandwidthForProtocol(arg0 protocol.ID) metrics.Stats { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetBandwidthForProtocol", arg0) + ret0, _ := ret[0].(metrics.Stats) + return ret0 +} + +// GetBandwidthForProtocol indicates an expected call of GetBandwidthForProtocol. +func (mr *MockReporterMockRecorder) GetBandwidthForProtocol(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBandwidthForProtocol", reflect.TypeOf((*MockReporter)(nil).GetBandwidthForProtocol), arg0) +} + +// GetBandwidthTotals mocks base method. +func (m *MockReporter) GetBandwidthTotals() metrics.Stats { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetBandwidthTotals") + ret0, _ := ret[0].(metrics.Stats) + return ret0 +} + +// GetBandwidthTotals indicates an expected call of GetBandwidthTotals. +func (mr *MockReporterMockRecorder) GetBandwidthTotals() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBandwidthTotals", reflect.TypeOf((*MockReporter)(nil).GetBandwidthTotals)) +} + +// LogRecvMessage mocks base method. +func (m *MockReporter) LogRecvMessage(arg0 int64) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "LogRecvMessage", arg0) +} + +// LogRecvMessage indicates an expected call of LogRecvMessage. +func (mr *MockReporterMockRecorder) LogRecvMessage(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LogRecvMessage", reflect.TypeOf((*MockReporter)(nil).LogRecvMessage), arg0) +} + +// LogRecvMessageStream mocks base method. +func (m *MockReporter) LogRecvMessageStream(arg0 int64, arg1 protocol.ID, arg2 peer.ID) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "LogRecvMessageStream", arg0, arg1, arg2) +} + +// LogRecvMessageStream indicates an expected call of LogRecvMessageStream. +func (mr *MockReporterMockRecorder) LogRecvMessageStream(arg0, arg1, arg2 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LogRecvMessageStream", reflect.TypeOf((*MockReporter)(nil).LogRecvMessageStream), arg0, arg1, arg2) +} + +// LogSentMessage mocks base method. +func (m *MockReporter) LogSentMessage(arg0 int64) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "LogSentMessage", arg0) +} + +// LogSentMessage indicates an expected call of LogSentMessage. +func (mr *MockReporterMockRecorder) LogSentMessage(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LogSentMessage", reflect.TypeOf((*MockReporter)(nil).LogSentMessage), arg0) +} + +// LogSentMessageStream mocks base method. +func (m *MockReporter) LogSentMessageStream(arg0 int64, arg1 protocol.ID, arg2 peer.ID) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "LogSentMessageStream", arg0, arg1, arg2) +} + +// LogSentMessageStream indicates an expected call of LogSentMessageStream. +func (mr *MockReporterMockRecorder) LogSentMessageStream(arg0, arg1, arg2 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LogSentMessageStream", reflect.TypeOf((*MockReporter)(nil).LogSentMessageStream), arg0, arg1, arg2) +} diff --git a/common/mocks/mockedStream.go b/p2p/mocks/mockedStream.go similarity index 92% rename from common/mocks/mockedStream.go rename to p2p/mocks/mockedStream.go index 775bf6c5d0..3551e35b63 100644 --- a/common/mocks/mockedStream.go +++ b/p2p/mocks/mockedStream.go @@ -1,16 +1,16 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: common/mocks/stream.go +// Source: p2p/mocks/stream_interface.go -// Package mocks is a generated GoMock package. -package mocks +// Package mock_p2p is a generated GoMock package. +package mock_p2p import ( reflect "reflect" time "time" - gomock "github.com/golang/mock/gomock" network "github.com/libp2p/go-libp2p/core/network" protocol "github.com/libp2p/go-libp2p/core/protocol" + gomock "go.uber.org/mock/gomock" ) // MockStream is a mock of Stream interface. @@ -130,7 +130,7 @@ func (m *MockStream) Read(p []byte) (int, error) { } // Read indicates an expected call of Read. -func (mr *MockStreamMockRecorder) Read(p interface{}) *gomock.Call { +func (mr *MockStreamMockRecorder) Read(p any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Read", reflect.TypeOf((*MockStream)(nil).Read), p) } @@ -172,7 +172,7 @@ func (m *MockStream) SetDeadline(arg0 time.Time) error { } // SetDeadline indicates an expected call of SetDeadline. -func (mr *MockStreamMockRecorder) SetDeadline(arg0 interface{}) *gomock.Call { +func (mr *MockStreamMockRecorder) SetDeadline(arg0 any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetDeadline", reflect.TypeOf((*MockStream)(nil).SetDeadline), arg0) } @@ -186,7 +186,7 @@ func (m *MockStream) SetProtocol(id protocol.ID) error { } // SetProtocol indicates an expected call of SetProtocol. -func (mr *MockStreamMockRecorder) SetProtocol(id interface{}) *gomock.Call { +func (mr *MockStreamMockRecorder) SetProtocol(id any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetProtocol", reflect.TypeOf((*MockStream)(nil).SetProtocol), id) } @@ -200,7 +200,7 @@ func (m *MockStream) SetReadDeadline(arg0 time.Time) error { } // SetReadDeadline indicates an expected call of SetReadDeadline. -func (mr *MockStreamMockRecorder) SetReadDeadline(arg0 interface{}) *gomock.Call { +func (mr *MockStreamMockRecorder) SetReadDeadline(arg0 any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetReadDeadline", reflect.TypeOf((*MockStream)(nil).SetReadDeadline), arg0) } @@ -214,7 +214,7 @@ func (m *MockStream) SetWriteDeadline(arg0 time.Time) error { } // SetWriteDeadline indicates an expected call of SetWriteDeadline. -func (mr *MockStreamMockRecorder) SetWriteDeadline(arg0 interface{}) *gomock.Call { +func (mr *MockStreamMockRecorder) SetWriteDeadline(arg0 any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetWriteDeadline", reflect.TypeOf((*MockStream)(nil).SetWriteDeadline), arg0) } @@ -243,7 +243,7 @@ func (m *MockStream) Write(p []byte) (int, error) { } // Write indicates an expected call of Write. -func (mr *MockStreamMockRecorder) Write(p interface{}) *gomock.Call { +func (mr *MockStreamMockRecorder) Write(p any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockStream)(nil).Write), p) } @@ -323,7 +323,7 @@ func (m *MockMuxedStream) Read(p []byte) (int, error) { } // Read indicates an expected call of Read. -func (mr *MockMuxedStreamMockRecorder) Read(p interface{}) *gomock.Call { +func (mr *MockMuxedStreamMockRecorder) Read(p any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Read", reflect.TypeOf((*MockMuxedStream)(nil).Read), p) } @@ -351,7 +351,7 @@ func (m *MockMuxedStream) SetDeadline(arg0 time.Time) error { } // SetDeadline indicates an expected call of SetDeadline. -func (mr *MockMuxedStreamMockRecorder) SetDeadline(arg0 interface{}) *gomock.Call { +func (mr *MockMuxedStreamMockRecorder) SetDeadline(arg0 any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetDeadline", reflect.TypeOf((*MockMuxedStream)(nil).SetDeadline), arg0) } @@ -365,7 +365,7 @@ func (m *MockMuxedStream) SetReadDeadline(arg0 time.Time) error { } // SetReadDeadline indicates an expected call of SetReadDeadline. -func (mr *MockMuxedStreamMockRecorder) SetReadDeadline(arg0 interface{}) *gomock.Call { +func (mr *MockMuxedStreamMockRecorder) SetReadDeadline(arg0 any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetReadDeadline", reflect.TypeOf((*MockMuxedStream)(nil).SetReadDeadline), arg0) } @@ -379,7 +379,7 @@ func (m *MockMuxedStream) SetWriteDeadline(arg0 time.Time) error { } // SetWriteDeadline indicates an expected call of SetWriteDeadline. -func (mr *MockMuxedStreamMockRecorder) SetWriteDeadline(arg0 interface{}) *gomock.Call { +func (mr *MockMuxedStreamMockRecorder) SetWriteDeadline(arg0 any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetWriteDeadline", reflect.TypeOf((*MockMuxedStream)(nil).SetWriteDeadline), arg0) } @@ -394,7 +394,7 @@ func (m *MockMuxedStream) Write(p []byte) (int, error) { } // Write indicates an expected call of Write. -func (mr *MockMuxedStreamMockRecorder) Write(p interface{}) *gomock.Call { +func (mr *MockMuxedStreamMockRecorder) Write(p any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockMuxedStream)(nil).Write), p) } diff --git a/common/mocks/stream_interface.go b/p2p/mocks/stream_interface.go similarity index 99% rename from common/mocks/stream_interface.go rename to p2p/mocks/stream_interface.go index 41bc529e34..00a0c6ce52 100644 --- a/common/mocks/stream_interface.go +++ b/p2p/mocks/stream_interface.go @@ -1,4 +1,4 @@ -package mocks +package mock_p2p import ( "io" diff --git a/p2p/node/streamManager/streamManager.go b/p2p/node/streamManager/streamManager.go index dfacc8da45..3a5b08fd38 100644 --- a/p2p/node/streamManager/streamManager.go +++ b/p2p/node/streamManager/streamManager.go @@ -37,7 +37,9 @@ const ( ) var ( - ErrStreamNotFound = errors.New("stream not found") + ErrStreamNotFound = errors.New("stream not found") + ErrStreamMismatch = errors.New("stream mismatch") + ErrorTooManyPendingRequests = errors.New("too many pending requests") ) type StreamManager interface { @@ -202,11 +204,11 @@ func (sm *basicStreamManager) GetHost() host.Host { func (sm *basicStreamManager) WriteMessageToStream(peerID p2p.PeerID, stream network.Stream, msg []byte, protoversion protocol.ID, reporter libp2pmetrics.Reporter) error { wrappedStream, found := sm.streamCache.Get(peerID) if !found { - return errors.New("stream not found") + return ErrStreamNotFound } if stream != wrappedStream.stream { // Indicate an unexpected case where the stream we stored and the stream we are requested to write to are not the same. - return errors.New("stream mismatch") + return ErrStreamMismatch } // Attempt to acquire semaphore before proceeding @@ -222,9 +224,9 @@ func (sm *basicStreamManager) WriteMessageToStream(peerID p2p.PeerID, stream net }).Warn("Had to close malfunctioning stream") // If c_maxPendingRequests have been dropped, the stream is likely in a bad state sm.CloseStream(peerID) - return errors.New("too many pending requests") + return ErrorTooManyPendingRequests } - return errors.New("too many pending requests") + return ErrorTooManyPendingRequests } defer func() { <-wrappedStream.semaphore diff --git a/p2p/node/streamManager/streamManager_test.go b/p2p/node/streamManager/streamManager_test.go new file mode 100644 index 0000000000..1fef3d3597 --- /dev/null +++ b/p2p/node/streamManager/streamManager_test.go @@ -0,0 +1,191 @@ +package streamManager + +import ( + "testing" + + gomock "go.uber.org/mock/gomock" + + peer "github.com/libp2p/go-libp2p/core/peer" + "github.com/pkg/errors" + "github.com/stretchr/testify/require" + + mock_p2p "github.com/dominant-strategies/go-quai/p2p/mocks" + "github.com/dominant-strategies/go-quai/p2p/protocol" + mock_protocol "github.com/dominant-strategies/go-quai/p2p/protocol/mocks" +) + +func setup(t *testing.T) (*gomock.Controller, *mock_protocol.MockQuaiP2PNode, *mock_p2p.MockHost, *basicStreamManager) { + ctrl := gomock.NewController(t) + mockNode := mock_protocol.NewMockQuaiP2PNode(ctrl) + mockHost := mock_p2p.NewMockHost(ctrl) + + sm, err := NewStreamManager(mockNode, mockHost) + require.NoError(t, err, "Failed to create stream manager") + sm.Start() + + return ctrl, mockNode, mockHost, sm +} + +func TestStreamManager(t *testing.T) { + ctrl, mockNode, mockHost, sm := setup(t) + defer ctrl.Finish() + + peerID := peer.ID("mockPeerID") + mockHost.EXPECT().ID().Return(peerID).Times(2) + + t.Run("Error case - NewStream returns error", func(t *testing.T) { + mockHost.EXPECT().NewStream(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("mock error")).Times(1) + + err := sm.OpenStream(mockHost.ID()) + require.Error(t, err, "Expected error when NewStream returns error") + }) + + t.Run("Success case - OpenStream, GetStream and CloseStream", func(t *testing.T) { + mockLibp2pStream := mock_p2p.NewMockStream(ctrl) + mockConn := mock_p2p.NewMockConn(ctrl) + + mockNode.EXPECT().GetBandwidthCounter().Return(nil).AnyTimes() + mockLibp2pStream.EXPECT().Close().Return(nil).AnyTimes() + mockLibp2pStream.EXPECT().Conn().Return(mockConn).AnyTimes() + mockLibp2pStream.EXPECT().Protocol().Return(protocol.ProtocolVersion).AnyTimes() + mockLibp2pStream.EXPECT().Read(gomock.Any()).Return(0, nil).AnyTimes() + mockConn.EXPECT().RemotePeer().Return(peerID).AnyTimes() + mockHost.EXPECT().NewStream(gomock.Any(), gomock.Any(), gomock.Any()).Return(mockLibp2pStream, nil).AnyTimes() + + // GetStream Error + entry, err := sm.GetStream(peerID) + require.Error(t, err, "Expected error when stream does not exist") + require.Nil(t, entry, "Expected nil entry when stream does not exist") + + err = sm.OpenStream(mockHost.ID()) + require.NoError(t, err, "Expected no error when opening stream") + + // Get Stream Success + entry, err = sm.GetStream(peerID) + require.NoError(t, err, "Expected no error when getting stream") + require.Equal(t, mockLibp2pStream, entry, "Expected correct stream entry") + + // Close stream assertions + err = sm.CloseStream(peerID) + require.NoError(t, err, "Expected no error when closing stream") + + // Stream already closed + err = sm.CloseStream(peerID) + require.Error(t, err, "Expected error when closing already closed stream") + }) + + t.Run("SetP2PBackend", func(t *testing.T) { + newMockNode := mock_protocol.NewMockQuaiP2PNode(ctrl) + // Ensure it's a new node + require.NotSame(t, newMockNode, sm.p2pBackend, "Expected different mock node") + sm.SetP2PBackend(newMockNode) + require.Same(t, newMockNode, sm.p2pBackend, "Expected new mock node to be set") + }) + + t.Run("Host accesors", func(t *testing.T) { + require.Same(t, mockHost, sm.GetHost(), "Expected same host") + newMockHost := mock_p2p.NewMockHost(ctrl) + require.NotSame(t, newMockHost, sm.host, "Expected different host") + sm.SetHost(newMockHost) + require.Same(t, newMockHost, sm.host, "Expected new host to be set") + }) +} + +func TestWriteMessageToStream(t *testing.T) { + ctrl, mockNode, mockHost, sm := setup(t) + defer ctrl.Finish() + + peerID := peer.ID("mockPeerID") + mockHost.EXPECT().ID().Return(peerID).Times(2) + + t.Run("WriteMessageToStream - Stream not found", func(t *testing.T) { + err := sm.WriteMessageToStream(peerID, nil, nil, protocol.ProtocolVersion, nil) + require.ErrorIs(t, err, ErrStreamNotFound, "Expected error when stream not found") + }) + + mockLibp2pStream := mock_p2p.NewMockStream(ctrl) + mockHost.EXPECT().NewStream(gomock.Any(), gomock.Any(), gomock.Any()).Return(mockLibp2pStream, nil).AnyTimes() + + MockConn := mock_p2p.NewMockConn(ctrl) + + mockNode.EXPECT().GetBandwidthCounter().Return(nil).AnyTimes() + mockLibp2pStream.EXPECT().Close().Return(nil).AnyTimes() + mockLibp2pStream.EXPECT().Conn().Return(MockConn).AnyTimes() + mockLibp2pStream.EXPECT().Protocol().Return(protocol.ProtocolVersion).AnyTimes() + mockLibp2pStream.EXPECT().Read(gomock.Any()).Return(0, nil).AnyTimes() + MockConn.EXPECT().RemotePeer().Return(peerID).AnyTimes() + + err := sm.OpenStream(mockHost.ID()) + require.NoError(t, err, "Expected no error when opening stream") + + t.Run("Stream mismatch", func(t *testing.T) { + + anotherStream := mock_p2p.NewMockStream(ctrl) + err = sm.WriteMessageToStream(peerID, anotherStream, []byte("message"), protocol.ProtocolVersion, nil) + require.ErrorIs(t, err, ErrStreamMismatch, "Expected error when stream mismatch") + }) + + t.Run("Too many pending requests", func(t *testing.T) { + // small semaphore to block the stream + wrappedStream := streamWrapper{ + stream: mockLibp2pStream, + semaphore: make(chan struct{}, 1), + errCount: 0, + } + // block semaphore + wrappedStream.semaphore <- struct{}{} + sm.streamCache.Add(peerID, wrappedStream) + err := sm.WriteMessageToStream(peerID, mockLibp2pStream, []byte("message"), protocol.ProtocolVersion, nil) + require.ErrorIs(t, err, ErrorTooManyPendingRequests, "Expected error when too many pending requests") + + // errCount is maxed out so it should close stream + wrappedStream.errCount = c_maxPendingRequests + sm.streamCache.Add(peerID, wrappedStream) + + // make sure stream exists + entry, err := sm.GetStream(peerID) + require.NoError(t, err, "Expected no error when getting stream") + require.Equal(t, mockLibp2pStream, entry, "Expected correct stream entry") + + err = sm.WriteMessageToStream(peerID, mockLibp2pStream, []byte("message"), protocol.ProtocolVersion, nil) + require.ErrorIs(t, err, ErrorTooManyPendingRequests, "Expected error when too many pending requests") + + // check if stream was closed + entry, err = sm.GetStream(peerID) + require.Nil(t, entry, "Expected nil entry") + require.ErrorIs(t, err, ErrStreamNotFound, "Expected error when stream not found") + }) + + t.Run("Failed to set write deadline", func(t *testing.T) { + err := sm.OpenStream(mockHost.ID()) + require.NoError(t, err, "Expected no error when opening stream") + + mockLibp2pStream.EXPECT().SetWriteDeadline(gomock.Any()).Return(errors.New("mock error")).Times(1) + + err = sm.WriteMessageToStream(peerID, mockLibp2pStream, []byte("message"), protocol.ProtocolVersion, nil) + require.Error(t, err, "Expected error when failed to set write deadline") + }) + + t.Run("Failed to write message to stream", func(t *testing.T) { + mockLibp2pStream.EXPECT().SetWriteDeadline(gomock.Any()).Return(nil).Times(1) + mockLibp2pStream.EXPECT().Write(gomock.Any()).Return(0, errors.New("mock error")).Times(1) + + err = sm.WriteMessageToStream(peerID, mockLibp2pStream, []byte("message"), protocol.ProtocolVersion, nil) + require.Error(t, err, "Expected error when failed to write message to stream") + }) + + t.Run("Succes write message to stream", func(t *testing.T) { + mockLibp2pStream.EXPECT().SetWriteDeadline(gomock.Any()).Return(nil).Times(2) + mockLibp2pStream.EXPECT().Write(gomock.Any()).Return(10, nil).Times(2) + + // without reporter + err = sm.WriteMessageToStream(peerID, mockLibp2pStream, []byte("message"), protocol.ProtocolVersion, nil) + require.NoError(t, err, "Expected no error when writing message to stream") + + //with reporter + mockReporter := mock_p2p.NewMockReporter(ctrl) + mockReporter.EXPECT().LogSentMessageStream(gomock.Any(), gomock.Any(), gomock.Any()).Times(1) + err = sm.WriteMessageToStream(peerID, mockLibp2pStream, []byte("message"), protocol.ProtocolVersion, mockReporter) + require.NoError(t, err, "Expected no error when writing message to stream") + }) +} diff --git a/p2p/protocol/mocks/mockedQuaiP2PNode.go b/p2p/protocol/mocks/mockedQuaip2pNode.go similarity index 74% rename from p2p/protocol/mocks/mockedQuaiP2PNode.go rename to p2p/protocol/mocks/mockedQuaip2pNode.go index e640ee08bc..8f151ebed3 100644 --- a/p2p/protocol/mocks/mockedQuaiP2PNode.go +++ b/p2p/protocol/mocks/mockedQuaip2pNode.go @@ -1,9 +1,9 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: p2p/protocol/interface.go +// Source: ../protocol/interface.go // // Generated by this command: // -// mockgen -source p2p/protocol/interface.go +// mockgen -source ../protocol/interface.go // // Package mock_protocol is a generated GoMock package. @@ -16,6 +16,7 @@ import ( common "github.com/dominant-strategies/go-quai/common" types "github.com/dominant-strategies/go-quai/core/types" requestManager "github.com/dominant-strategies/go-quai/p2p/node/requestManager" + metrics "github.com/libp2p/go-libp2p/core/metrics" network "github.com/libp2p/go-libp2p/core/network" peer "github.com/libp2p/go-libp2p/core/peer" gomock "go.uber.org/mock/gomock" @@ -58,6 +59,20 @@ func (mr *MockQuaiP2PNodeMockRecorder) Connect(arg0 any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Connect", reflect.TypeOf((*MockQuaiP2PNode)(nil).Connect), arg0) } +// GetBandwidthCounter mocks base method. +func (m *MockQuaiP2PNode) GetBandwidthCounter() metrics.Reporter { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetBandwidthCounter") + ret0, _ := ret[0].(metrics.Reporter) + return ret0 +} + +// GetBandwidthCounter indicates an expected call of GetBandwidthCounter. +func (mr *MockQuaiP2PNodeMockRecorder) GetBandwidthCounter() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBandwidthCounter", reflect.TypeOf((*MockQuaiP2PNode)(nil).GetBandwidthCounter)) +} + // GetBlockHashByNumber mocks base method. func (m *MockQuaiP2PNode) GetBlockHashByNumber(number *big.Int, location common.Location) *common.Hash { m.ctrl.T.Helper() @@ -114,3 +129,17 @@ func (mr *MockQuaiP2PNodeMockRecorder) GetWorkObject(hash, location any) *gomock mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetWorkObject", reflect.TypeOf((*MockQuaiP2PNode)(nil).GetWorkObject), hash, location) } + +// GetWorkObjectsFrom mocks base method. +func (m *MockQuaiP2PNode) GetWorkObjectsFrom(hash common.Hash, location common.Location, count int) []*types.WorkObjectBlockView { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetWorkObjectsFrom", hash, location, count) + ret0, _ := ret[0].([]*types.WorkObjectBlockView) + return ret0 +} + +// GetWorkObjectsFrom indicates an expected call of GetWorkObjectsFrom. +func (mr *MockQuaiP2PNodeMockRecorder) GetWorkObjectsFrom(hash, location, count any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetWorkObjectsFrom", reflect.TypeOf((*MockQuaiP2PNode)(nil).GetWorkObjectsFrom), hash, location, count) +} diff --git a/params/version.go b/params/version.go index 9d2fc0b617..ca097e5668 100644 --- a/params/version.go +++ b/params/version.go @@ -19,7 +19,7 @@ package params import ( "bytes" "errors" - "io/ioutil" + "os" "strconv" "strings" "sync/atomic" @@ -44,7 +44,7 @@ func InitVersion() { } func readVersionFile() (version, error) { - raw, err := ioutil.ReadFile("VERSION") + raw, err := os.ReadFile("VERSION") if err != nil { panic(err) }