From 560580927faa485fe7c50252ec182056efd2026f Mon Sep 17 00:00:00 2001 From: Andrew Richardson Date: Thu, 10 Feb 2022 13:27:32 -0500 Subject: [PATCH] Rename data exchange plugin to "ffdx" Resolves #259 Signed-off-by: Andrew Richardson --- internal/dataexchange/dxfactory/factory.go | 10 +++- .../dataexchange/{dxhttps => ffdx}/config.go | 4 +- .../{dxhttps/dxhttps.go => ffdx/ffdx.go} | 34 +++++------ .../dxhttps_test.go => ffdx/ffdx_test.go} | 58 +++++++++---------- internal/orchestrator/orchestrator.go | 13 ++++- internal/orchestrator/orchestrator_test.go | 22 +++++-- 6 files changed, 84 insertions(+), 57 deletions(-) rename internal/dataexchange/{dxhttps => ffdx}/config.go (94%) rename internal/dataexchange/{dxhttps/dxhttps.go => ffdx/ffdx.go} (87%) rename internal/dataexchange/{dxhttps/dxhttps_test.go => ffdx/ffdx_test.go} (92%) diff --git a/internal/dataexchange/dxfactory/factory.go b/internal/dataexchange/dxfactory/factory.go index 8ba1fe09d5..8914505066 100644 --- a/internal/dataexchange/dxfactory/factory.go +++ b/internal/dataexchange/dxfactory/factory.go @@ -20,18 +20,24 @@ import ( "context" "github.com/hyperledger/firefly/internal/config" - "github.com/hyperledger/firefly/internal/dataexchange/dxhttps" + "github.com/hyperledger/firefly/internal/dataexchange/ffdx" "github.com/hyperledger/firefly/internal/i18n" "github.com/hyperledger/firefly/pkg/dataexchange" ) var pluginsByName = map[string]func() dataexchange.Plugin{ - (*dxhttps.HTTPS)(nil).Name(): func() dataexchange.Plugin { return &dxhttps.HTTPS{} }, + (*ffdx.FFDX)(nil).Name(): func() dataexchange.Plugin { return &ffdx.FFDX{} }, } func InitPrefix(prefix config.Prefix) { for name, plugin := range pluginsByName { plugin().InitPrefix(prefix.SubPrefix(name)) + + // Migration path for old plugin name + // TODO: remove this + if name == "ffdx" { + plugin().InitPrefix(prefix.SubPrefix("https")) + } } } diff --git a/internal/dataexchange/dxhttps/config.go b/internal/dataexchange/ffdx/config.go similarity index 94% rename from internal/dataexchange/dxhttps/config.go rename to internal/dataexchange/ffdx/config.go index 6d200122ea..a284c5746c 100644 --- a/internal/dataexchange/dxhttps/config.go +++ b/internal/dataexchange/ffdx/config.go @@ -14,7 +14,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package dxhttps +package ffdx import ( "github.com/hyperledger/firefly/internal/config" @@ -26,7 +26,7 @@ const ( DataExchangeManifestEnabled = "manifestEnabled" ) -func (h *HTTPS) InitPrefix(prefix config.Prefix) { +func (h *FFDX) InitPrefix(prefix config.Prefix) { wsconfig.InitPrefix(prefix) prefix.AddKnownKey(DataExchangeManifestEnabled, false) } diff --git a/internal/dataexchange/dxhttps/dxhttps.go b/internal/dataexchange/ffdx/ffdx.go similarity index 87% rename from internal/dataexchange/dxhttps/dxhttps.go rename to internal/dataexchange/ffdx/ffdx.go index 29b8190b51..dc3b696348 100644 --- a/internal/dataexchange/dxhttps/dxhttps.go +++ b/internal/dataexchange/ffdx/ffdx.go @@ -14,7 +14,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package dxhttps +package ffdx import ( "context" @@ -35,7 +35,7 @@ import ( "github.com/hyperledger/firefly/pkg/wsclient" ) -type HTTPS struct { +type FFDX struct { ctx context.Context capabilities *dataexchange.Capabilities callbacks dataexchange.Callbacks @@ -100,16 +100,16 @@ type wsAck struct { Manifest string `json:"manifest,omitempty"` // FireFly core determined that DX should propagate opaquely to TransferResult, if this DX supports delivery acknowledgements. } -func (h *HTTPS) Name() string { - return "https" +func (h *FFDX) Name() string { + return "ffdx" } -func (h *HTTPS) Init(ctx context.Context, prefix config.Prefix, callbacks dataexchange.Callbacks) (err error) { - h.ctx = log.WithLogField(ctx, "dx", "https") +func (h *FFDX) Init(ctx context.Context, prefix config.Prefix, callbacks dataexchange.Callbacks) (err error) { + h.ctx = log.WithLogField(ctx, "dx", "ffdx") h.callbacks = callbacks if prefix.GetString(restclient.HTTPConfigURL) == "" { - return i18n.NewError(ctx, i18n.MsgMissingPluginConfig, "url", "dataexchange.https") + return i18n.NewError(ctx, i18n.MsgMissingPluginConfig, "url", "dataexchange.ffdx") } h.client = restclient.New(h.ctx, prefix) @@ -127,15 +127,15 @@ func (h *HTTPS) Init(ctx context.Context, prefix config.Prefix, callbacks dataex return nil } -func (h *HTTPS) Start() error { +func (h *FFDX) Start() error { return h.wsconn.Connect() } -func (h *HTTPS) Capabilities() *dataexchange.Capabilities { +func (h *FFDX) Capabilities() *dataexchange.Capabilities { return h.capabilities } -func (h *HTTPS) GetEndpointInfo(ctx context.Context) (peerID string, endpoint fftypes.JSONObject, err error) { +func (h *FFDX) GetEndpointInfo(ctx context.Context) (peerID string, endpoint fftypes.JSONObject, err error) { res, err := h.client.R().SetContext(ctx). SetResult(&endpoint). Get("/api/v1/id") @@ -145,7 +145,7 @@ func (h *HTTPS) GetEndpointInfo(ctx context.Context) (peerID string, endpoint ff return endpoint.GetString("id"), endpoint, nil } -func (h *HTTPS) AddPeer(ctx context.Context, peerID string, endpoint fftypes.JSONObject) (err error) { +func (h *FFDX) AddPeer(ctx context.Context, peerID string, endpoint fftypes.JSONObject) (err error) { res, err := h.client.R().SetContext(ctx). SetBody(endpoint). Put(fmt.Sprintf("/api/v1/peers/%s", peerID)) @@ -155,7 +155,7 @@ func (h *HTTPS) AddPeer(ctx context.Context, peerID string, endpoint fftypes.JSO return nil } -func (h *HTTPS) UploadBLOB(ctx context.Context, ns string, id fftypes.UUID, content io.Reader) (payloadRef string, hash *fftypes.Bytes32, size int64, err error) { +func (h *FFDX) UploadBLOB(ctx context.Context, ns string, id fftypes.UUID, content io.Reader) (payloadRef string, hash *fftypes.Bytes32, size int64, err error) { payloadRef = fmt.Sprintf("%s/%s", ns, &id) var upload uploadBlob res, err := h.client.R().SetContext(ctx). @@ -172,7 +172,7 @@ func (h *HTTPS) UploadBLOB(ctx context.Context, ns string, id fftypes.UUID, cont return payloadRef, hash, upload.Size, nil } -func (h *HTTPS) DownloadBLOB(ctx context.Context, payloadRef string) (content io.ReadCloser, err error) { +func (h *FFDX) DownloadBLOB(ctx context.Context, payloadRef string) (content io.ReadCloser, err error) { res, err := h.client.R().SetContext(ctx). SetDoNotParseResponse(true). Get(fmt.Sprintf("/api/v1/blobs/%s", payloadRef)) @@ -185,7 +185,7 @@ func (h *HTTPS) DownloadBLOB(ctx context.Context, payloadRef string) (content io return res.RawBody(), nil } -func (h *HTTPS) SendMessage(ctx context.Context, opID *fftypes.UUID, peerID string, data []byte) (err error) { +func (h *FFDX) SendMessage(ctx context.Context, opID *fftypes.UUID, peerID string, data []byte) (err error) { var responseData responseWithRequestID res, err := h.client.R().SetContext(ctx). SetBody(&sendMessage{ @@ -201,7 +201,7 @@ func (h *HTTPS) SendMessage(ctx context.Context, opID *fftypes.UUID, peerID stri return nil } -func (h *HTTPS) TransferBLOB(ctx context.Context, opID *fftypes.UUID, peerID, payloadRef string) (err error) { +func (h *FFDX) TransferBLOB(ctx context.Context, opID *fftypes.UUID, peerID, payloadRef string) (err error) { var responseData responseWithRequestID res, err := h.client.R().SetContext(ctx). SetBody(&transferBlob{ @@ -217,7 +217,7 @@ func (h *HTTPS) TransferBLOB(ctx context.Context, opID *fftypes.UUID, peerID, pa return nil } -func (h *HTTPS) CheckBLOBReceived(ctx context.Context, peerID, ns string, id fftypes.UUID) (hash *fftypes.Bytes32, size int64, err error) { +func (h *FFDX) CheckBLOBReceived(ctx context.Context, peerID, ns string, id fftypes.UUID) (hash *fftypes.Bytes32, size int64, err error) { var responseData responseWithRequestID res, err := h.client.R().SetContext(ctx). SetResult(&responseData). @@ -241,7 +241,7 @@ func (h *HTTPS) CheckBLOBReceived(ctx context.Context, peerID, ns string, id fft return hash, size, nil } -func (h *HTTPS) eventLoop() { +func (h *FFDX) eventLoop() { defer h.wsconn.Close() l := log.L(h.ctx).WithField("role", "event-loop") ctx := log.WithLogger(h.ctx, l) diff --git a/internal/dataexchange/dxhttps/dxhttps_test.go b/internal/dataexchange/ffdx/ffdx_test.go similarity index 92% rename from internal/dataexchange/dxhttps/dxhttps_test.go rename to internal/dataexchange/ffdx/ffdx_test.go index a3b3d8a580..07b773fbb6 100644 --- a/internal/dataexchange/dxhttps/dxhttps_test.go +++ b/internal/dataexchange/ffdx/ffdx_test.go @@ -14,7 +14,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package dxhttps +package ffdx import ( "bytes" @@ -36,9 +36,9 @@ import ( "github.com/stretchr/testify/mock" ) -var utConfPrefix = config.NewPluginConfig("dxhttps_unit_tests") +var utConfPrefix = config.NewPluginConfig("ffdx_unit_tests") -func newTestHTTPS(t *testing.T) (h *HTTPS, toServer, fromServer chan string, httpURL string, done func()) { +func newTestFFDX(t *testing.T) (h *FFDX, toServer, fromServer chan string, httpURL string, done func()) { mockedClient := &http.Client{} httpmock.ActivateNonDefault(mockedClient) @@ -53,11 +53,11 @@ func newTestHTTPS(t *testing.T) (h *HTTPS, toServer, fromServer chan string, htt utConfPrefix.Set(restclient.HTTPConfigURL, httpURL) utConfPrefix.Set(restclient.HTTPCustomClient, mockedClient) - h = &HTTPS{} + h = &FFDX{} h.InitPrefix(utConfPrefix) err := h.Init(context.Background(), utConfPrefix, &dataexchangemocks.Callbacks{}) assert.NoError(t, err) - assert.Equal(t, "https", h.Name()) + assert.Equal(t, "ffdx", h.Name()) assert.NotNil(t, h.Capabilities()) return h, toServer, fromServer, httpURL, func() { cancel() @@ -67,7 +67,7 @@ func newTestHTTPS(t *testing.T) (h *HTTPS, toServer, fromServer chan string, htt func TestInitBadURL(t *testing.T) { config.Reset() - h := &HTTPS{} + h := &FFDX{} h.InitPrefix(utConfPrefix) utConfPrefix.Set(restclient.HTTPConfigURL, "::::////") err := h.Init(context.Background(), utConfPrefix, &dataexchangemocks.Callbacks{}) @@ -76,7 +76,7 @@ func TestInitBadURL(t *testing.T) { func TestInitMissingURL(t *testing.T) { config.Reset() - h := &HTTPS{} + h := &FFDX{} h.InitPrefix(utConfPrefix) err := h.Init(context.Background(), utConfPrefix, &dataexchangemocks.Callbacks{}) assert.Regexp(t, "FF10138", err) @@ -84,7 +84,7 @@ func TestInitMissingURL(t *testing.T) { func TestGetEndpointInfo(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() httpmock.RegisterResponder("GET", fmt.Sprintf("%s/api/v1/id", httpURL), @@ -105,7 +105,7 @@ func TestGetEndpointInfo(t *testing.T) { } func TestGetEndpointInfoError(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() httpmock.RegisterResponder("GET", fmt.Sprintf("%s/api/v1/id", httpURL), @@ -117,7 +117,7 @@ func TestGetEndpointInfoError(t *testing.T) { func TestAddPeer(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() httpmock.RegisterResponder("PUT", fmt.Sprintf("%s/api/v1/peers/peer1", httpURL), @@ -134,7 +134,7 @@ func TestAddPeer(t *testing.T) { } func TestAddPeerError(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() httpmock.RegisterResponder("PUT", fmt.Sprintf("%s/api/v1/peers/peer1", httpURL), @@ -146,7 +146,7 @@ func TestAddPeerError(t *testing.T) { func TestUploadBLOB(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() u := fftypes.NewUUID() @@ -176,7 +176,7 @@ func TestUploadBLOB(t *testing.T) { func TestUploadBLOBBadHash(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() u := fftypes.NewUUID() @@ -197,7 +197,7 @@ func TestUploadBLOBBadHash(t *testing.T) { } func TestUploadBLOBError(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() u := fftypes.NewUUID() @@ -210,7 +210,7 @@ func TestUploadBLOBError(t *testing.T) { func TestCheckBLOBReceivedOk(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() u := fftypes.NewUUID() @@ -235,7 +235,7 @@ func TestCheckBLOBReceivedOk(t *testing.T) { func TestCheckBLOBReceivedBadHash(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() u := fftypes.NewUUID() @@ -256,7 +256,7 @@ func TestCheckBLOBReceivedBadHash(t *testing.T) { func TestCheckBLOBReceivedBadSize(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() u := fftypes.NewUUID() @@ -279,7 +279,7 @@ func TestCheckBLOBReceivedBadSize(t *testing.T) { func TestCheckBLOBReceivedNotFound(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() u := fftypes.NewUUID() @@ -298,7 +298,7 @@ func TestCheckBLOBReceivedNotFound(t *testing.T) { func TestCheckBLOBReceivedError(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() u := fftypes.NewUUID() @@ -316,7 +316,7 @@ func TestCheckBLOBReceivedError(t *testing.T) { func TestDownloadBLOB(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() u := fftypes.NewUUID() @@ -331,7 +331,7 @@ func TestDownloadBLOB(t *testing.T) { } func TestDownloadBLOBError(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() httpmock.RegisterResponder("GET", fmt.Sprintf("%s/api/v1/blobs/bad", httpURL), @@ -343,7 +343,7 @@ func TestDownloadBLOBError(t *testing.T) { func TestSendMessage(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() httpmock.RegisterResponder("POST", fmt.Sprintf("%s/api/v1/messages", httpURL), @@ -354,7 +354,7 @@ func TestSendMessage(t *testing.T) { } func TestSendMessageError(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() httpmock.RegisterResponder("POST", fmt.Sprintf("%s/api/v1/message", httpURL), @@ -366,7 +366,7 @@ func TestSendMessageError(t *testing.T) { func TestTransferBLOB(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() httpmock.RegisterResponder("POST", fmt.Sprintf("%s/api/v1/transfers", httpURL), @@ -377,7 +377,7 @@ func TestTransferBLOB(t *testing.T) { } func TestTransferBLOBError(t *testing.T) { - h, _, _, httpURL, done := newTestHTTPS(t) + h, _, _, httpURL, done := newTestFFDX(t) defer done() httpmock.RegisterResponder("POST", fmt.Sprintf("%s/api/v1/transfers", httpURL), @@ -389,7 +389,7 @@ func TestTransferBLOBError(t *testing.T) { func TestEvents(t *testing.T) { - h, toServer, fromServer, _, done := newTestHTTPS(t) + h, toServer, fromServer, _, done := newTestFFDX(t) defer done() err := h.Start() @@ -456,7 +456,7 @@ func TestEvents(t *testing.T) { func TestEventLoopReceiveClosed(t *testing.T) { dxc := &dataexchangemocks.Callbacks{} wsm := &wsmocks.WSClient{} - h := &HTTPS{ + h := &FFDX{ ctx: context.Background(), callbacks: dxc, wsconn: wsm, @@ -471,7 +471,7 @@ func TestEventLoopReceiveClosed(t *testing.T) { func TestEventLoopSendClosed(t *testing.T) { dxc := &dataexchangemocks.Callbacks{} wsm := &wsmocks.WSClient{} - h := &HTTPS{ + h := &FFDX{ ctx: context.Background(), callbacks: dxc, wsconn: wsm, @@ -489,7 +489,7 @@ func TestEventLoopClosedContext(t *testing.T) { wsm := &wsmocks.WSClient{} ctx, cancel := context.WithCancel(context.Background()) cancel() - h := &HTTPS{ + h := &FFDX{ ctx: ctx, callbacks: dxc, wsconn: wsm, diff --git a/internal/orchestrator/orchestrator.go b/internal/orchestrator/orchestrator.go index e1d4da0f5e..43200e67ca 100644 --- a/internal/orchestrator/orchestrator.go +++ b/internal/orchestrator/orchestrator.go @@ -328,13 +328,20 @@ func (or *orchestrator) initPlugins(ctx context.Context) (err error) { return err } + dxPlugin := config.GetString(config.DataexchangeType) if or.dataexchange == nil { - dxType := config.GetString(config.DataexchangeType) - if or.dataexchange, err = dxfactory.GetPlugin(ctx, dxType); err != nil { + pluginName := dxPlugin + if pluginName == "https" { + // Migration path for old plugin name + // TODO: eventually make this fatal + log.L(ctx).Warnf("Your data exchange config uses the old plugin name 'https' - this plugin has been renamed to 'ffdx'") + pluginName = "ffdx" + } + if or.dataexchange, err = dxfactory.GetPlugin(ctx, pluginName); err != nil { return err } } - if err = or.dataexchange.Init(ctx, dataexchangeConfig.SubPrefix(or.dataexchange.Name()), &or.bc); err != nil { + if err = or.dataexchange.Init(ctx, dataexchangeConfig.SubPrefix(dxPlugin), &or.bc); err != nil { return err } diff --git a/internal/orchestrator/orchestrator_test.go b/internal/orchestrator/orchestrator_test.go index ea1cbd5fb3..1e830c37d1 100644 --- a/internal/orchestrator/orchestrator_test.go +++ b/internal/orchestrator/orchestrator_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/hyperledger/firefly/internal/config" + "github.com/hyperledger/firefly/internal/dataexchange/dxfactory" "github.com/hyperledger/firefly/internal/restclient" "github.com/hyperledger/firefly/internal/tokens/tifactory" "github.com/hyperledger/firefly/mocks/assetmocks" @@ -277,9 +278,25 @@ func TestBadDataExchangeInitFail(t *testing.T) { assert.EqualError(t, err, "pop") } +func TestDataExchangePluginOldName(t *testing.T) { + or := newTestOrchestrator() + dxfactory.InitPrefix(dataexchangeConfig) + config.Set(config.DataexchangeType, "https") + dataexchangeConfig.SubPrefix("https").Set(restclient.HTTPConfigURL, "http://test") + or.dataexchange = nil + or.mdi.On("GetConfigRecords", mock.Anything, mock.Anything, mock.Anything).Return([]*fftypes.ConfigRecord{}, nil, nil) + or.mdi.On("Init", mock.Anything, mock.Anything, mock.Anything).Return(nil) + or.mbi.On("Init", mock.Anything, mock.Anything, mock.Anything).Return(nil) + or.mii.On("Init", mock.Anything, mock.Anything, mock.Anything).Return(nil) + or.mps.On("Init", mock.Anything, mock.Anything, mock.Anything).Return(nil) + or.mdi.On("GetNamespace", mock.Anything, mock.Anything).Return(nil, fmt.Errorf("pop")) + ctx, cancelCtx := context.WithCancel(context.Background()) + err := or.Init(ctx, cancelCtx) + assert.EqualError(t, err, "pop") +} + func TestBadTokensPlugin(t *testing.T) { or := newTestOrchestrator() - tokensConfig = config.NewPluginConfig("tokens").Array() tifactory.InitPrefix(tokensConfig) tokensConfig.AddKnownKey(tokens.TokensConfigName, "text") tokensConfig.AddKnownKey(tokens.TokensConfigConnector, "wrong") @@ -321,7 +338,6 @@ func TestBadTokensPluginNoConnector(t *testing.T) { func TestBadTokensPluginNoName(t *testing.T) { or := newTestOrchestrator() - tokensConfig = config.NewPluginConfig("tokens").Array() tifactory.InitPrefix(tokensConfig) tokensConfig.AddKnownKey(tokens.TokensConfigName) tokensConfig.AddKnownKey(tokens.TokensConfigConnector, "wrong") @@ -342,7 +358,6 @@ func TestBadTokensPluginNoName(t *testing.T) { func TestBadTokensPluginInvalidName(t *testing.T) { or := newTestOrchestrator() - tokensConfig = config.NewPluginConfig("tokens").Array() tifactory.InitPrefix(tokensConfig) tokensConfig.AddKnownKey(tokens.TokensConfigName, "!wrong") tokensConfig.AddKnownKey(tokens.TokensConfigConnector, "text") @@ -363,7 +378,6 @@ func TestBadTokensPluginInvalidName(t *testing.T) { func TestBadTokensPluginNoType(t *testing.T) { or := newTestOrchestrator() - tokensConfig = config.NewPluginConfig("tokens").Array() tifactory.InitPrefix(tokensConfig) tokensConfig.AddKnownKey(tokens.TokensConfigName, "text") tokensConfig.AddKnownKey(tokens.TokensConfigConnector)