diff --git a/go.mod b/go.mod index fdae1619..d29252ad 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/ava-labs/avalanchego v1.10.18 github.com/ava-labs/hypersdk v0.0.16 github.com/chain4travel/caminotravelvm v0.0.0-20240419161941-a32dadd85f51 - github.com/google/uuid v1.4.0 github.com/klauspost/compress v1.17.3 github.com/mattn/go-sqlite3 v1.14.19 github.com/spf13/pflag v1.0.5 diff --git a/internal/app/app.go b/internal/app/app.go index 9955bb25..db90e93e 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -2,7 +2,6 @@ package app import ( "context" - "fmt" "github.com/chain4travel/camino-messenger-bot/config" diff --git a/internal/matrix/matrix_messenger.go b/internal/matrix/matrix_messenger.go index e5d756cc..6bea60d7 100644 --- a/internal/matrix/matrix_messenger.go +++ b/internal/matrix/matrix_messenger.go @@ -8,8 +8,6 @@ import ( "sync" "time" - "github.com/chain4travel/camino-messenger-bot/internal/compression" - "github.com/ava-labs/avalanchego/utils/crypto/secp256k1" "github.com/ava-labs/avalanchego/utils/formatting" "github.com/chain4travel/camino-messenger-bot/config" diff --git a/internal/matrix/mock_room_handler.go b/internal/matrix/mock_room_handler.go index a85dc612..b3555a1d 100644 --- a/internal/matrix/mock_room_handler.go +++ b/internal/matrix/mock_room_handler.go @@ -10,6 +10,7 @@ package matrix import ( + context "context" reflect "reflect" gomock "go.uber.org/mock/gomock" @@ -42,75 +43,76 @@ func (m *MockClient) EXPECT() *MockClientMockRecorder { } // CreateRoom mocks base method. -func (m *MockClient) CreateRoom(arg0 *mautrix.ReqCreateRoom) (*mautrix.RespCreateRoom, error) { +func (m *MockClient) CreateRoom(arg0 context.Context, arg1 *mautrix.ReqCreateRoom) (*mautrix.RespCreateRoom, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CreateRoom", arg0) + ret := m.ctrl.Call(m, "CreateRoom", arg0, arg1) ret0, _ := ret[0].(*mautrix.RespCreateRoom) ret1, _ := ret[1].(error) return ret0, ret1 } // CreateRoom indicates an expected call of CreateRoom. -func (mr *MockClientMockRecorder) CreateRoom(arg0 any) *gomock.Call { +func (mr *MockClientMockRecorder) CreateRoom(arg0, arg1 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateRoom", reflect.TypeOf((*MockClient)(nil).CreateRoom), arg0) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateRoom", reflect.TypeOf((*MockClient)(nil).CreateRoom), arg0, arg1) } // IsEncrypted mocks base method. -func (m *MockClient) IsEncrypted(arg0 id.RoomID) bool { +func (m *MockClient) IsEncrypted(arg0 context.Context, arg1 id.RoomID) (bool, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "IsEncrypted", arg0) + ret := m.ctrl.Call(m, "IsEncrypted", arg0, arg1) ret0, _ := ret[0].(bool) - return ret0 + ret1, _ := ret[1].(error) + return ret0, ret1 } // IsEncrypted indicates an expected call of IsEncrypted. -func (mr *MockClientMockRecorder) IsEncrypted(arg0 any) *gomock.Call { +func (mr *MockClientMockRecorder) IsEncrypted(arg0, arg1 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsEncrypted", reflect.TypeOf((*MockClient)(nil).IsEncrypted), arg0) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsEncrypted", reflect.TypeOf((*MockClient)(nil).IsEncrypted), arg0, arg1) } // JoinedMembers mocks base method. -func (m *MockClient) JoinedMembers(arg0 id.RoomID) (*mautrix.RespJoinedMembers, error) { +func (m *MockClient) JoinedMembers(arg0 context.Context, arg1 id.RoomID) (*mautrix.RespJoinedMembers, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "JoinedMembers", arg0) + ret := m.ctrl.Call(m, "JoinedMembers", arg0, arg1) ret0, _ := ret[0].(*mautrix.RespJoinedMembers) ret1, _ := ret[1].(error) return ret0, ret1 } // JoinedMembers indicates an expected call of JoinedMembers. -func (mr *MockClientMockRecorder) JoinedMembers(arg0 any) *gomock.Call { +func (mr *MockClientMockRecorder) JoinedMembers(arg0, arg1 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "JoinedMembers", reflect.TypeOf((*MockClient)(nil).JoinedMembers), arg0) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "JoinedMembers", reflect.TypeOf((*MockClient)(nil).JoinedMembers), arg0, arg1) } // JoinedRooms mocks base method. -func (m *MockClient) JoinedRooms() (*mautrix.RespJoinedRooms, error) { +func (m *MockClient) JoinedRooms(arg0 context.Context) (*mautrix.RespJoinedRooms, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "JoinedRooms") + ret := m.ctrl.Call(m, "JoinedRooms", arg0) ret0, _ := ret[0].(*mautrix.RespJoinedRooms) ret1, _ := ret[1].(error) return ret0, ret1 } // JoinedRooms indicates an expected call of JoinedRooms. -func (mr *MockClientMockRecorder) JoinedRooms() *gomock.Call { +func (mr *MockClientMockRecorder) JoinedRooms(arg0 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "JoinedRooms", reflect.TypeOf((*MockClient)(nil).JoinedRooms)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "JoinedRooms", reflect.TypeOf((*MockClient)(nil).JoinedRooms), arg0) } // SendStateEvent mocks base method. -func (m *MockClient) SendStateEvent(arg0 id.RoomID, arg1 event.Type, arg2 string, arg3 any) (*mautrix.RespSendEvent, error) { +func (m *MockClient) SendStateEvent(arg0 context.Context, arg1 id.RoomID, arg2 event.Type, arg3 string, arg4 any) (*mautrix.RespSendEvent, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SendStateEvent", arg0, arg1, arg2, arg3) + ret := m.ctrl.Call(m, "SendStateEvent", arg0, arg1, arg2, arg3, arg4) ret0, _ := ret[0].(*mautrix.RespSendEvent) ret1, _ := ret[1].(error) return ret0, ret1 } // SendStateEvent indicates an expected call of SendStateEvent. -func (mr *MockClientMockRecorder) SendStateEvent(arg0, arg1, arg2, arg3 any) *gomock.Call { +func (mr *MockClientMockRecorder) SendStateEvent(arg0, arg1, arg2, arg3, arg4 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendStateEvent", reflect.TypeOf((*MockClient)(nil).SendStateEvent), arg0, arg1, arg2, arg3) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendStateEvent", reflect.TypeOf((*MockClient)(nil).SendStateEvent), arg0, arg1, arg2, arg3, arg4) } diff --git a/internal/matrix/room_handler.go b/internal/matrix/room_handler.go index 690f9a5d..bb4946a9 100644 --- a/internal/matrix/room_handler.go +++ b/internal/matrix/room_handler.go @@ -11,7 +11,7 @@ import ( ) type Client interface { - IsEncrypted(ctx context.Context, roomID id.RoomID) bool + IsEncrypted(ctx context.Context, roomID id.RoomID) (bool, error) CreateRoom(ctx context.Context, req *mautrix.ReqCreateRoom) (*mautrix.RespCreateRoom, error) SendStateEvent(ctx context.Context, roomID id.RoomID, eventType event.Type, stateKey string, content interface{}) (*mautrix.RespSendEvent, error) JoinedRooms(ctx context.Context) (*mautrix.RespJoinedRooms, error) @@ -23,7 +23,7 @@ type wrappedClient struct { *mautrix.Client } -func (c *wrappedClient) IsEncrypted(ctx context.Context, roomID id.RoomID) bool { +func (c *wrappedClient) IsEncrypted(ctx context.Context, roomID id.RoomID) (bool, error) { return c.StateStore.IsEncrypted(ctx, roomID) } @@ -32,7 +32,7 @@ func NewClient(mautrixClient *mautrix.Client) Client { } type RoomHandler interface { - GetOrCreateRoomForRecipient(recipient id.UserID) (id.RoomID, error) + GetOrCreateRoomForRecipient(ctx context.Context, recipient id.UserID) (id.RoomID, error) } type roomHandler struct { client Client @@ -99,7 +99,7 @@ func (r *roomHandler) getEncryptedRoomForRecipient(ctx context.Context, recipien return "", false } for _, roomID := range rooms.JoinedRooms { - if !r.client.IsEncrypted(ctx, roomID) { + if encrypted, err := r.client.IsEncrypted(ctx, roomID); err != nil || !encrypted { continue } members, err := r.client.JoinedMembers(ctx, roomID) diff --git a/internal/matrix/room_handler_test.go b/internal/matrix/room_handler_test.go index e7135439..16d6b6c2 100644 --- a/internal/matrix/room_handler_test.go +++ b/internal/matrix/room_handler_test.go @@ -6,6 +6,7 @@ package matrix import ( + "context" "errors" "testing" @@ -51,9 +52,9 @@ func TestGetOrCreateRoomForRecipient(t *testing.T) { rooms: map[id.UserID]id.RoomID{}, }, mocks: func(*roomHandler) { - mockRoomClient.EXPECT().JoinedRooms().Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil) - mockRoomClient.EXPECT().IsEncrypted(roomID).Times(1).Return(false) - mockRoomClient.EXPECT().CreateRoom(&mautrix.ReqCreateRoom{ + mockRoomClient.EXPECT().JoinedRooms(gomock.Any()).Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil) + mockRoomClient.EXPECT().IsEncrypted(gomock.Any(), roomID).Times(1).Return(false, nil) + mockRoomClient.EXPECT().CreateRoom(gomock.Any(), &mautrix.ReqCreateRoom{ Visibility: "private", Preset: "private_chat", Invite: []id.UserID{userID}, @@ -67,14 +68,14 @@ func TestGetOrCreateRoomForRecipient(t *testing.T) { rooms: map[id.UserID]id.RoomID{}, }, mocks: func(*roomHandler) { - mockRoomClient.EXPECT().JoinedRooms().Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil) - mockRoomClient.EXPECT().IsEncrypted(roomID).Times(1).Return(false) - mockRoomClient.EXPECT().CreateRoom(&mautrix.ReqCreateRoom{ + mockRoomClient.EXPECT().JoinedRooms(gomock.Any()).Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil) + mockRoomClient.EXPECT().IsEncrypted(gomock.Any(), roomID).Times(1).Return(false, nil) + mockRoomClient.EXPECT().CreateRoom(gomock.Any(), &mautrix.ReqCreateRoom{ Visibility: "private", Preset: "private_chat", Invite: []id.UserID{userID}, }).Times(1).Return(&mautrix.RespCreateRoom{RoomID: newRoomID}, nil) - mockRoomClient.EXPECT().SendStateEvent(newRoomID, event.StateEncryption, "", + mockRoomClient.EXPECT().SendStateEvent(gomock.Any(), newRoomID, event.StateEncryption, "", event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1}).Times(1).Return(nil, errEnableEncryptionFailed) }, args: args{recipient: userID}, @@ -92,9 +93,9 @@ func TestGetOrCreateRoomForRecipient(t *testing.T) { rooms: map[id.UserID]id.RoomID{}, }, mocks: func(*roomHandler) { - mockRoomClient.EXPECT().JoinedRooms().Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil) - mockRoomClient.EXPECT().IsEncrypted(roomID).Times(1).Return(true) - mockRoomClient.EXPECT().JoinedMembers(roomID).Times(1).Return(&mautrix.RespJoinedMembers{Joined: map[id.UserID]mautrix.JoinedMember{userID: {}}}, nil) + mockRoomClient.EXPECT().JoinedRooms(gomock.Any()).Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil) + mockRoomClient.EXPECT().IsEncrypted(gomock.Any(), roomID).Times(1).Return(true, nil) + mockRoomClient.EXPECT().JoinedMembers(gomock.Any(), roomID).Times(1).Return(&mautrix.RespJoinedMembers{Joined: map[id.UserID]mautrix.JoinedMember{userID: {}}}, nil) }, args: args{recipient: userID}, want: roomID, @@ -104,13 +105,13 @@ func TestGetOrCreateRoomForRecipient(t *testing.T) { rooms: map[id.UserID]id.RoomID{}, }, mocks: func(*roomHandler) { - mockRoomClient.EXPECT().JoinedRooms().Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{}}, nil) - mockRoomClient.EXPECT().CreateRoom(&mautrix.ReqCreateRoom{ + mockRoomClient.EXPECT().JoinedRooms(gomock.Any()).Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{}}, nil) + mockRoomClient.EXPECT().CreateRoom(gomock.Any(), &mautrix.ReqCreateRoom{ Visibility: "private", Preset: "private_chat", Invite: []id.UserID{userID}, }).Times(1).Return(&mautrix.RespCreateRoom{RoomID: newRoomID}, nil) - mockRoomClient.EXPECT().SendStateEvent(newRoomID, event.StateEncryption, "", + mockRoomClient.EXPECT().SendStateEvent(gomock.Any(), newRoomID, event.StateEncryption, "", event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1}).Times(1).Return(nil, nil) }, args: args{recipient: userID}, @@ -121,14 +122,14 @@ func TestGetOrCreateRoomForRecipient(t *testing.T) { rooms: map[id.UserID]id.RoomID{}, }, mocks: func(*roomHandler) { - mockRoomClient.EXPECT().JoinedRooms().Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil) - mockRoomClient.EXPECT().IsEncrypted(roomID).Times(1).Return(false) - mockRoomClient.EXPECT().CreateRoom(&mautrix.ReqCreateRoom{ + mockRoomClient.EXPECT().JoinedRooms(gomock.Any()).Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil) + mockRoomClient.EXPECT().IsEncrypted(gomock.Any(), roomID).Times(1).Return(false, nil) + mockRoomClient.EXPECT().CreateRoom(gomock.Any(), &mautrix.ReqCreateRoom{ Visibility: "private", Preset: "private_chat", Invite: []id.UserID{userID}, }).Times(1).Return(&mautrix.RespCreateRoom{RoomID: newRoomID}, nil) - mockRoomClient.EXPECT().SendStateEvent(newRoomID, event.StateEncryption, "", + mockRoomClient.EXPECT().SendStateEvent(gomock.Any(), newRoomID, event.StateEncryption, "", event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1}).Times(1).Return(nil, nil) }, args: args{recipient: userID}, @@ -146,7 +147,7 @@ func TestGetOrCreateRoomForRecipient(t *testing.T) { tt.mocks(r) } - got, err := r.GetOrCreateRoomForRecipient(tt.args.recipient) + got, err := r.GetOrCreateRoomForRecipient(context.Background(), tt.args.recipient) require.ErrorIs(t, err, tt.err, "GetOrCreateRoomForRecipient() error = %w, wantErr %w", err, tt.err) require.Equal(t, got, tt.want, "GetOrCreateRoomForRecipient() got = %v, expRoomID %v", got, tt.want) }) diff --git a/internal/messaging/processor.go b/internal/messaging/processor.go index 1475f07c..f8080668 100644 --- a/internal/messaging/processor.go +++ b/internal/messaging/processor.go @@ -9,10 +9,10 @@ import ( "github.com/chain4travel/camino-messenger-bot/config" "github.com/chain4travel/camino-messenger-bot/internal/metadata" - "go.uber.org/zap" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" + "go.uber.org/zap" "google.golang.org/grpc" grpc_metadata "google.golang.org/grpc/metadata" ) diff --git a/internal/rpc/server/server.go b/internal/rpc/server/server.go index 6f8c5b9d..31e00ec0 100644 --- a/internal/rpc/server/server.go +++ b/internal/rpc/server/server.go @@ -206,4 +206,4 @@ func (s *server) processMetadata(ctx context.Context, id trace.TraceID) (metadat md.Stamp(fmt.Sprintf("%s-%s", s.Checkpoint(), "received")) err := md.ExtractMetadata(ctx) return md, err -} \ No newline at end of file +} diff --git a/internal/tracing/exporter.go b/internal/tracing/exporter.go index 5d22a06c..5dd180ef 100644 --- a/internal/tracing/exporter.go +++ b/internal/tracing/exporter.go @@ -8,20 +8,22 @@ package tracing import ( "context" "fmt" + "time" + "github.com/chain4travel/camino-messenger-bot/config" utils "github.com/chain4travel/camino-messenger-bot/utils/tls" - "time" "go.opentelemetry.io/otel/exporters/otlp/otlptrace" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" "go.opentelemetry.io/otel/sdk/trace" ) -const exportTimeout = 10 * time.Second -const exporterInstantiationTimeout = 5 * time.Second +const ( + exportTimeout = 10 * time.Second + exporterInstantiationTimeout = 5 * time.Second +) func newExporter(cfg *config.TracingConfig) (trace.SpanExporter, error) { - var client otlptrace.Client opts := []otlptracegrpc.Option{ otlptracegrpc.WithEndpoint(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)), @@ -32,7 +34,7 @@ func newExporter(cfg *config.TracingConfig) (trace.SpanExporter, error) { } else { creds, err := utils.LoadTLSCredentials(cfg.CertFile, cfg.KeyFile) if err != nil { - return nil, fmt.Errorf("could not load TLS keys: %s", err) + return nil, fmt.Errorf("could not load TLS keys: %w", err) } opts = append(opts, otlptracegrpc.WithTLSCredentials(creds)) } diff --git a/internal/tracing/nooptracer.go b/internal/tracing/nooptracer.go index 265c77c6..059cdf9b 100644 --- a/internal/tracing/nooptracer.go +++ b/internal/tracing/nooptracer.go @@ -7,6 +7,7 @@ package tracing import ( "context" + "go.opentelemetry.io/otel/trace" ) @@ -19,9 +20,11 @@ type noopTracer struct { func NewNoOpTracer() (Tracer, error) { return &noopTracer{tp: trace.NewNoopTracerProvider()}, nil } + func (n *noopTracer) Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { return n.tp.Tracer("").Start(ctx, spanName, opts...) } + func (n *noopTracer) Shutdown() error { return nil // nothing to do here } diff --git a/internal/tracing/tracer.go b/internal/tracing/tracer.go index 247913e9..b047533b 100644 --- a/internal/tracing/tracer.go +++ b/internal/tracing/tracer.go @@ -7,13 +7,14 @@ package tracing import ( "context" + "time" + "github.com/chain4travel/camino-messenger-bot/config" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.12.0" "go.opentelemetry.io/otel/trace" - "time" ) const tracerProviderShutdownTimeout = exportTimeout + 5*time.Second @@ -32,6 +33,7 @@ type tracer struct { func (t *tracer) Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { return t.tp.Tracer("").Start(ctx, spanName, opts...) } + func (t *tracer) Shutdown() error { ctx, cancel := context.WithTimeout(context.Background(), tracerProviderShutdownTimeout) defer cancel() diff --git a/scripts/mocks.mockgen.txt b/scripts/mocks.mockgen.txt index 733f461b..4f607a7a 100644 --- a/scripts/mocks.mockgen.txt +++ b/scripts/mocks.mockgen.txt @@ -3,4 +3,3 @@ github.com/chain4travel/camino-messenger-bot/internal/matrix=Client=internal/mat buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/activity/v1alpha/activityv1alphagrpc=ActivityProductListServiceClient=internal/messaging/mock_list_grpc.pb.go github.com/chain4travel/camino-messenger-bot/internal/messaging=ServiceRegistry=internal/messaging/mock_service_registry.go github.com/chain4travel/camino-messenger-bot/internal/messaging=Messenger=internal/messaging/mock_messenger.go -