From c50536e3a61ed853334a37fb11fcb9d132d0c40f Mon Sep 17 00:00:00 2001 From: David Echelberger Date: Mon, 14 Feb 2022 14:42:11 -0500 Subject: [PATCH] [metrics-types] using fftypes in metrics manager Signed-off-by: David Echelberger --- internal/assets/token_transfer.go | 6 +-- internal/broadcast/message.go | 2 +- internal/events/operation_update.go | 3 +- internal/events/tokens_transferred.go | 2 +- internal/metrics/metrics.go | 19 ++++---- internal/metrics/metrics_test.go | 64 ++++++++++----------------- internal/privatemessaging/message.go | 2 +- mocks/metricsmocks/manager.go | 12 +++-- 8 files changed, 45 insertions(+), 65 deletions(-) diff --git a/internal/assets/token_transfer.go b/internal/assets/token_transfer.go index 7d51d0b4b3..75a21cca06 100644 --- a/internal/assets/token_transfer.go +++ b/internal/assets/token_transfer.go @@ -125,7 +125,7 @@ func (am *assetManager) MintTokens(ctx context.Context, ns string, transfer *fft sender := am.NewTransfer(ns, transfer) if am.metrics.IsMetricsEnabled() { - am.metrics.TransferSubmitted(transfer) + am.metrics.TransferSubmitted(&transfer.TokenTransfer) } if waitConfirm { err = sender.SendAndWait(ctx) @@ -143,7 +143,7 @@ func (am *assetManager) BurnTokens(ctx context.Context, ns string, transfer *fft sender := am.NewTransfer(ns, transfer) if am.metrics.IsMetricsEnabled() { - am.metrics.TransferSubmitted(transfer) + am.metrics.TransferSubmitted(&transfer.TokenTransfer) } if waitConfirm { err = sender.SendAndWait(ctx) @@ -164,7 +164,7 @@ func (am *assetManager) TransferTokens(ctx context.Context, ns string, transfer sender := am.NewTransfer(ns, transfer) if am.metrics.IsMetricsEnabled() { - am.metrics.TransferSubmitted(transfer) + am.metrics.TransferSubmitted(&transfer.TokenTransfer) } if waitConfirm { err = sender.SendAndWait(ctx) diff --git a/internal/broadcast/message.go b/internal/broadcast/message.go index 6ade0c91b2..a4c3b31f6b 100644 --- a/internal/broadcast/message.go +++ b/internal/broadcast/message.go @@ -40,7 +40,7 @@ func (bm *broadcastManager) NewBroadcast(ns string, in *fftypes.MessageInOut) sy func (bm *broadcastManager) BroadcastMessage(ctx context.Context, ns string, in *fftypes.MessageInOut, waitConfirm bool) (out *fftypes.Message, err error) { broadcast := bm.NewBroadcast(ns, in) if bm.metrics.IsMetricsEnabled() { - bm.metrics.MessageSubmitted(in) + bm.metrics.MessageSubmitted(&in.Message) } if waitConfirm { err = broadcast.SendAndWait(ctx) diff --git a/internal/events/operation_update.go b/internal/events/operation_update.go index eeb4198297..0f1ee8356a 100644 --- a/internal/events/operation_update.go +++ b/internal/events/operation_update.go @@ -22,7 +22,6 @@ import ( "github.com/hyperledger/firefly/internal/log" "github.com/hyperledger/firefly/internal/txcommon" "github.com/hyperledger/firefly/pkg/fftypes" - "github.com/hyperledger/firefly/pkg/tokens" ) func (em *eventManager) operationUpdateCtx(ctx context.Context, operationID *fftypes.UUID, txState fftypes.OpStatus, blockchainTXID, errorMessage string, opOutput fftypes.JSONObject) error { @@ -45,7 +44,7 @@ func (em *eventManager) operationUpdateCtx(ctx context.Context, operationID *fft if err != nil { log.L(em.ctx).Warnf("Could not determine token transfer type: %s", err) } - em.metrics.TransferConfirmed(&tokens.TokenTransfer{TokenTransfer: tokenTransfer}) + em.metrics.TransferConfirmed(&tokenTransfer) } if err := em.database.InsertEvent(ctx, event); err != nil { return err diff --git a/internal/events/tokens_transferred.go b/internal/events/tokens_transferred.go index 6830a66be5..9d8c7e02d8 100644 --- a/internal/events/tokens_transferred.go +++ b/internal/events/tokens_transferred.go @@ -111,7 +111,7 @@ func (em *eventManager) persistTokenTransfer(ctx context.Context, transfer *toke } log.L(ctx).Infof("Token transfer recorded id=%s author=%s", transfer.ProtocolID, transfer.Key) if em.metrics.IsMetricsEnabled() && countMetric { - em.metrics.TransferConfirmed(transfer) + em.metrics.TransferConfirmed(&transfer.TokenTransfer) } return true, nil diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index 241a22f8c6..906519a662 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -23,17 +23,16 @@ import ( "github.com/hyperledger/firefly/internal/config" "github.com/hyperledger/firefly/pkg/fftypes" - "github.com/hyperledger/firefly/pkg/tokens" ) var mutex = &sync.Mutex{} type Manager interface { CountBatchPin() - MessageSubmitted(in *fftypes.MessageInOut) + MessageSubmitted(msg *fftypes.Message) MessageConfirmed(msg *fftypes.Message, eventType fftypes.FFEnum) - TransferSubmitted(transfer *fftypes.TokenTransferInput) - TransferConfirmed(transfer *tokens.TokenTransfer) + TransferSubmitted(transfer *fftypes.TokenTransfer) + TransferConfirmed(transfer *fftypes.TokenTransfer) AddTime(id string) GetTime(id string) time.Time DeleteTime(id string) @@ -65,15 +64,15 @@ func (mm *metricsManager) CountBatchPin() { BatchPinCounter.Inc() } -func (mm *metricsManager) MessageSubmitted(in *fftypes.MessageInOut) { - if len(in.Header.ID.String()) > 0 { - switch in.Message.Header.Type { +func (mm *metricsManager) MessageSubmitted(msg *fftypes.Message) { + if len(msg.Header.ID.String()) > 0 { + switch msg.Header.Type { case fftypes.MessageTypeBroadcast: BroadcastSubmittedCounter.Inc() case fftypes.MessageTypePrivate: PrivateMsgSubmittedCounter.Inc() } - mm.AddTime(in.Header.ID.String()) + mm.AddTime(msg.Header.ID.String()) } } @@ -99,7 +98,7 @@ func (mm *metricsManager) MessageConfirmed(msg *fftypes.Message, eventType fftyp } } -func (mm *metricsManager) TransferSubmitted(transfer *fftypes.TokenTransferInput) { +func (mm *metricsManager) TransferSubmitted(transfer *fftypes.TokenTransfer) { if len(transfer.LocalID.String()) > 0 { switch transfer.Type { case fftypes.TokenTransferTypeMint: // Mint submitted @@ -113,7 +112,7 @@ func (mm *metricsManager) TransferSubmitted(transfer *fftypes.TokenTransferInput } } -func (mm *metricsManager) TransferConfirmed(transfer *tokens.TokenTransfer) { +func (mm *metricsManager) TransferConfirmed(transfer *fftypes.TokenTransfer) { timeElapsed := time.Since(mm.GetTime(transfer.LocalID.String())).Seconds() mm.DeleteTime(transfer.LocalID.String()) diff --git a/internal/metrics/metrics_test.go b/internal/metrics/metrics_test.go index 26d5fb9e20..27d3d2f070 100644 --- a/internal/metrics/metrics_test.go +++ b/internal/metrics/metrics_test.go @@ -23,13 +23,11 @@ import ( "github.com/hyperledger/firefly/internal/config" "github.com/hyperledger/firefly/pkg/fftypes" - "github.com/hyperledger/firefly/pkg/tokens" "github.com/stretchr/testify/assert" ) var msgID = fftypes.NewUUID() -var MessageInOut = &fftypes.MessageInOut{ - Message: fftypes.Message{ +var Message = &fftypes.Message{ Header: fftypes.MessageHeader{ ID: msgID, Identity: fftypes.Identity{ @@ -38,30 +36,16 @@ var MessageInOut = &fftypes.MessageInOut{ }, Type: "", }, - }, - InlineData: fftypes.InlineData{ - {Value: fftypes.JSONAnyPtr(`{"hello": "world"}`)}, - }, -} + } -var tokenLocalID = fftypes.NewUUID() -var TokenTransferInput = &fftypes.TokenTransferInput{ - TokenTransfer: fftypes.TokenTransfer{ - Amount: *fftypes.NewFFBigInt(5), - LocalID: tokenLocalID, - Type: "", - }, - Pool: "pool1", -} -var TokenTransfer = &tokens.TokenTransfer{ - PoolProtocolID: "F1", - TokenTransfer: fftypes.TokenTransfer{ +var tokenLocalID = fftypes.NewUUID() +var TokenTransfer = &fftypes.TokenTransfer{ Amount: *fftypes.NewFFBigInt(1), LocalID: tokenLocalID, Type: "", - }, -} + } + func newTestMetricsManager(t *testing.T) (*metricsManager, func()) { config.Reset() @@ -89,8 +73,8 @@ func TestCountBatchPin(t *testing.T) { func TestMessageSubmittedBroadcast(t *testing.T) { mm, cancel := newTestMetricsManager(t) defer cancel() - MessageInOut.Header.Type = fftypes.MessageTypeBroadcast - mm.MessageSubmitted(MessageInOut) + Message.Header.Type = fftypes.MessageTypeBroadcast + mm.MessageSubmitted(Message) assert.Equal(t, len(mm.timeMap), 1) assert.NotNil(t, mm.timeMap[msgID.String()]) } @@ -98,8 +82,8 @@ func TestMessageSubmittedBroadcast(t *testing.T) { func TestMessageSubmittedPrivate(t *testing.T) { mm, cancel := newTestMetricsManager(t) defer cancel() - MessageInOut.Header.Type = fftypes.MessageTypePrivate - mm.MessageSubmitted(MessageInOut) + Message.Header.Type = fftypes.MessageTypePrivate + mm.MessageSubmitted(Message) assert.Equal(t, len(mm.timeMap), 1) assert.NotNil(t, mm.timeMap[msgID.String()]) } @@ -107,44 +91,44 @@ func TestMessageSubmittedPrivate(t *testing.T) { func TestMessageConfirmedBroadcastSuccess(t *testing.T) { mm, cancel := newTestMetricsManager(t) defer cancel() - MessageInOut.Header.Type = fftypes.MessageTypeBroadcast + Message.Header.Type = fftypes.MessageTypeBroadcast mm.timeMap[msgID.String()] = time.Now() - mm.MessageConfirmed(&MessageInOut.Message, fftypes.EventTypeMessageConfirmed) + mm.MessageConfirmed(Message, fftypes.EventTypeMessageConfirmed) assert.Equal(t, len(mm.timeMap), 0) } func TestMessageConfirmedBroadcastRejected(t *testing.T) { mm, cancel := newTestMetricsManager(t) defer cancel() - MessageInOut.Header.Type = fftypes.MessageTypeBroadcast + Message.Header.Type = fftypes.MessageTypeBroadcast mm.timeMap[msgID.String()] = time.Now() - mm.MessageConfirmed(&MessageInOut.Message, fftypes.EventTypeMessageRejected) + mm.MessageConfirmed(Message, fftypes.EventTypeMessageRejected) assert.Equal(t, len(mm.timeMap), 0) } func TestMessageConfirmedPrivateSuccess(t *testing.T) { mm, cancel := newTestMetricsManager(t) defer cancel() - MessageInOut.Header.Type = fftypes.MessageTypePrivate + Message.Header.Type = fftypes.MessageTypePrivate mm.timeMap[msgID.String()] = time.Now() - mm.MessageConfirmed(&MessageInOut.Message, fftypes.EventTypeMessageConfirmed) + mm.MessageConfirmed(Message, fftypes.EventTypeMessageConfirmed) assert.Equal(t, len(mm.timeMap), 0) } func TestMessageConfirmedPrivateRejected(t *testing.T) { mm, cancel := newTestMetricsManager(t) defer cancel() - MessageInOut.Header.Type = fftypes.MessageTypePrivate + Message.Header.Type = fftypes.MessageTypePrivate mm.timeMap[msgID.String()] = time.Now() - mm.MessageConfirmed(&MessageInOut.Message, fftypes.EventTypeMessageRejected) + mm.MessageConfirmed(Message, fftypes.EventTypeMessageRejected) assert.Equal(t, len(mm.timeMap), 0) } func TestTokenSubmittedMint(t *testing.T) { mm, cancel := newTestMetricsManager(t) defer cancel() - TokenTransferInput.Type = fftypes.TokenTransferTypeMint - mm.TransferSubmitted(TokenTransferInput) + TokenTransfer.Type = fftypes.TokenTransferTypeMint + mm.TransferSubmitted(TokenTransfer) assert.Equal(t, len(mm.timeMap), 1) assert.NotNil(t, mm.timeMap[tokenLocalID.String()]) } @@ -152,8 +136,8 @@ func TestTokenSubmittedMint(t *testing.T) { func TestTransferSubmittedTransfer(t *testing.T) { mm, cancel := newTestMetricsManager(t) defer cancel() - TokenTransferInput.Type = fftypes.TokenTransferTypeTransfer - mm.TransferSubmitted(TokenTransferInput) + TokenTransfer.Type = fftypes.TokenTransferTypeTransfer + mm.TransferSubmitted(TokenTransfer) assert.Equal(t, len(mm.timeMap), 1) assert.NotNil(t, mm.timeMap[tokenLocalID.String()]) } @@ -161,8 +145,8 @@ func TestTransferSubmittedTransfer(t *testing.T) { func TestTransferSubmittedBurn(t *testing.T) { mm, cancel := newTestMetricsManager(t) defer cancel() - TokenTransferInput.Type = fftypes.TokenTransferTypeBurn - mm.TransferSubmitted(TokenTransferInput) + TokenTransfer.Type = fftypes.TokenTransferTypeBurn + mm.TransferSubmitted(TokenTransfer) assert.Equal(t, len(mm.timeMap), 1) assert.NotNil(t, mm.timeMap[tokenLocalID.String()]) } diff --git a/internal/privatemessaging/message.go b/internal/privatemessaging/message.go index 288433f530..7f7d15372a 100644 --- a/internal/privatemessaging/message.go +++ b/internal/privatemessaging/message.go @@ -39,7 +39,7 @@ func (pm *privateMessaging) NewMessage(ns string, in *fftypes.MessageInOut) sysm func (pm *privateMessaging) SendMessage(ctx context.Context, ns string, in *fftypes.MessageInOut, waitConfirm bool) (out *fftypes.Message, err error) { message := pm.NewMessage(ns, in) if pm.metrics.IsMetricsEnabled() { - pm.metrics.MessageSubmitted(in) + pm.metrics.MessageSubmitted(&in.Message) } if waitConfirm { err = message.SendAndWait(ctx) diff --git a/mocks/metricsmocks/manager.go b/mocks/metricsmocks/manager.go index 23959bc07a..4a6fcb2ad5 100644 --- a/mocks/metricsmocks/manager.go +++ b/mocks/metricsmocks/manager.go @@ -8,8 +8,6 @@ import ( mock "github.com/stretchr/testify/mock" time "time" - - tokens "github.com/hyperledger/firefly/pkg/tokens" ) // Manager is an autogenerated mock type for the Manager type @@ -65,9 +63,9 @@ func (_m *Manager) MessageConfirmed(msg *fftypes.Message, eventType fftypes.FFEn _m.Called(msg, eventType) } -// MessageSubmitted provides a mock function with given fields: in -func (_m *Manager) MessageSubmitted(in *fftypes.MessageInOut) { - _m.Called(in) +// MessageSubmitted provides a mock function with given fields: msg +func (_m *Manager) MessageSubmitted(msg *fftypes.Message) { + _m.Called(msg) } // Start provides a mock function with given fields: @@ -85,11 +83,11 @@ func (_m *Manager) Start() error { } // TransferConfirmed provides a mock function with given fields: transfer -func (_m *Manager) TransferConfirmed(transfer *tokens.TokenTransfer) { +func (_m *Manager) TransferConfirmed(transfer *fftypes.TokenTransfer) { _m.Called(transfer) } // TransferSubmitted provides a mock function with given fields: transfer -func (_m *Manager) TransferSubmitted(transfer *fftypes.TokenTransferInput) { +func (_m *Manager) TransferSubmitted(transfer *fftypes.TokenTransfer) { _m.Called(transfer) }