Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions internal/orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,28 @@ func (or *orchestrator) initPlugins(ctx context.Context) (err error) {
for i := 0; i < tokensConfig.ArraySize(); i++ {
prefix := tokensConfig.ArrayEntry(i)
name := prefix.GetString(tokens.TokensConfigName)
connector := prefix.GetString(tokens.TokensConfigConnector)
if name == "" || connector == "" {
pluginName := prefix.GetString(tokens.TokensConfigPlugin)
if name == "" {
return i18n.NewError(ctx, i18n.MsgMissingTokensPluginConfig)
}
if pluginName == "" {
// Migration path for old config key
// TODO: eventually make this fatal
pluginName = prefix.GetString(tokens.TokensConfigConnector)
if pluginName == "" {
return i18n.NewError(ctx, i18n.MsgMissingTokensPluginConfig)
}
log.L(ctx).Warnf("Your tokens config uses the deprecated 'connector' key - please change to 'plugin' instead")
}
if pluginName == "https" {
// Migration path for old plugin name
// TODO: eventually make this fatal
log.L(ctx).Warnf("Your tokens config uses the old plugin name 'https' - this plugin has been renamed to 'fftokens'")
pluginName = "fftokens"
}

log.L(ctx).Infof("Loading tokens plugin name=%s connector=%s", name, connector)
plugin, err := tifactory.GetPlugin(ctx, connector)
log.L(ctx).Infof("Loading tokens plugin name=%s plugin=%s", name, pluginName)
plugin, err := tifactory.GetPlugin(ctx, pluginName)
if plugin != nil {
err = plugin.Init(ctx, name, prefix, &or.bc)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package https
package fftokens

import (
"github.com/hyperledger/firefly/internal/config"
"github.com/hyperledger/firefly/internal/wsclient"
)

func (h *HTTPS) InitPrefix(prefix config.PrefixArray) {
func (h *FFTokens) InitPrefix(prefix config.PrefixArray) {
wsclient.InitPrefix(prefix)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package https
package fftokens

import (
"context"
Expand All @@ -30,7 +30,7 @@ import (
"github.com/hyperledger/firefly/pkg/tokens"
)

type HTTPS struct {
type FFTokens struct {
ctx context.Context
capabilities *tokens.Capabilities
callbacks tokens.Callbacks
Expand Down Expand Up @@ -65,18 +65,17 @@ type createPoolData struct {
TransactionID *fftypes.UUID `json:"transactionId"`
}

func (h *HTTPS) Name() string {
return "https"
func (h *FFTokens) Name() string {
return "fftokens"
}

func (h *HTTPS) Init(ctx context.Context, name string, prefix config.Prefix, callbacks tokens.Callbacks) (err error) {

h.ctx = log.WithLogField(ctx, "proto", "https")
func (h *FFTokens) Init(ctx context.Context, name string, prefix config.Prefix, callbacks tokens.Callbacks) (err error) {
h.ctx = log.WithLogField(ctx, "proto", "fftokens")
h.callbacks = callbacks
h.configuredName = name

if prefix.GetString(restclient.HTTPConfigURL) == "" {
return i18n.NewError(ctx, i18n.MsgMissingPluginConfig, "url", "tokens.https")
return i18n.NewError(ctx, i18n.MsgMissingPluginConfig, "url", "tokens.fftokens")
}

h.client = restclient.New(h.ctx, prefix)
Expand All @@ -95,15 +94,15 @@ func (h *HTTPS) Init(ctx context.Context, name string, prefix config.Prefix, cal
return nil
}

func (h *HTTPS) Start() error {
func (h *FFTokens) Start() error {
return h.wsconn.Connect()
}

func (h *HTTPS) Capabilities() *tokens.Capabilities {
func (h *FFTokens) Capabilities() *tokens.Capabilities {
return h.capabilities
}

func (h *HTTPS) handleReceipt(ctx context.Context, data fftypes.JSONObject) error {
func (h *FFTokens) handleReceipt(ctx context.Context, data fftypes.JSONObject) error {
l := log.L(ctx)

requestID := data.GetString("id")
Expand All @@ -121,7 +120,7 @@ func (h *HTTPS) handleReceipt(ctx context.Context, data fftypes.JSONObject) erro
return h.callbacks.TokensTxUpdate(h, requestID, updateType, message, data)
}

func (h *HTTPS) handleTokenPoolCreate(ctx context.Context, data fftypes.JSONObject) (err error) {
func (h *FFTokens) handleTokenPoolCreate(ctx context.Context, data fftypes.JSONObject) (err error) {
packedData := data.GetString("data")
tokenType := data.GetString("type")
protocolID := data.GetString("poolId")
Expand Down Expand Up @@ -169,7 +168,7 @@ func (h *HTTPS) handleTokenPoolCreate(ctx context.Context, data fftypes.JSONObje
return h.callbacks.TokenPoolCreated(h, pool, operatorAddress, txHash, tx)
}

func (h *HTTPS) eventLoop() {
func (h *FFTokens) eventLoop() {
defer h.wsconn.Close()
l := log.L(h.ctx).WithField("role", "event-loop")
ctx := log.WithLogger(h.ctx, l)
Expand Down Expand Up @@ -219,7 +218,7 @@ func (h *HTTPS) eventLoop() {
}
}

func (h *HTTPS) CreateTokenPool(ctx context.Context, identity *fftypes.Identity, pool *fftypes.TokenPool) error {
func (h *FFTokens) CreateTokenPool(ctx context.Context, identity *fftypes.Identity, pool *fftypes.TokenPool) error {
data := createPoolData{
Namespace: pool.Namespace,
Name: pool.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package https
package fftokens

import (
"bytes"
Expand All @@ -40,7 +40,7 @@ import (

var utConfPrefix = config.NewPluginConfig("tokens").Array()

func newTestHTTPS(t *testing.T) (h *HTTPS, toServer, fromServer chan string, httpURL string, done func()) {
func newTestFFTokens(t *testing.T) (h *FFTokens, toServer, fromServer chan string, httpURL string, done func()) {
mockedClient := &http.Client{}
httpmock.ActivateNonDefault(mockedClient)

Expand All @@ -51,18 +51,18 @@ func newTestHTTPS(t *testing.T) (h *HTTPS, toServer, fromServer chan string, htt
httpURL = u.String()

config.Reset()
h = &HTTPS{}
h = &FFTokens{}
h.InitPrefix(utConfPrefix)

utConfPrefix.AddKnownKey(tokens.TokensConfigName, "test")
utConfPrefix.AddKnownKey(tokens.TokensConfigConnector, "https")
utConfPrefix.AddKnownKey(tokens.TokensConfigPlugin, "fftokens")
utConfPrefix.AddKnownKey(restclient.HTTPConfigURL, httpURL)
utConfPrefix.AddKnownKey(restclient.HTTPCustomClient, mockedClient)
config.Set("tokens", []fftypes.JSONObject{{}})

err := h.Init(context.Background(), "testtokens", utConfPrefix.ArrayEntry(0), &tokenmocks.Callbacks{})
assert.NoError(t, err)
assert.Equal(t, "https", h.Name())
assert.Equal(t, "fftokens", h.Name())
assert.Equal(t, "testtokens", h.configuredName)
assert.NotNil(t, h.Capabilities())
return h, toServer, fromServer, httpURL, func() {
Expand All @@ -73,30 +73,30 @@ func newTestHTTPS(t *testing.T) (h *HTTPS, toServer, fromServer chan string, htt

func TestInitBadURL(t *testing.T) {
config.Reset()
h := &HTTPS{}
h := &FFTokens{}
h.InitPrefix(utConfPrefix)

utConfPrefix.AddKnownKey(tokens.TokensConfigName, "test")
utConfPrefix.AddKnownKey(tokens.TokensConfigConnector, "https")
utConfPrefix.AddKnownKey(tokens.TokensConfigPlugin, "fftokens")
utConfPrefix.AddKnownKey(restclient.HTTPConfigURL, "::::////")
err := h.Init(context.Background(), "testtokens", utConfPrefix.ArrayEntry(0), &tokenmocks.Callbacks{})
assert.Regexp(t, "FF10162", err)
}

func TestInitMissingURL(t *testing.T) {
config.Reset()
h := &HTTPS{}
h := &FFTokens{}
h.InitPrefix(utConfPrefix)

utConfPrefix.AddKnownKey(tokens.TokensConfigName, "test")
utConfPrefix.AddKnownKey(tokens.TokensConfigConnector, "https")
utConfPrefix.AddKnownKey(tokens.TokensConfigPlugin, "fftokens")
utConfPrefix.AddKnownKey(restclient.HTTPConfigURL, "")
err := h.Init(context.Background(), "testtokens", utConfPrefix.ArrayEntry(0), &tokenmocks.Callbacks{})
assert.Regexp(t, "FF10138", err)
}

func TestCreateTokenPool(t *testing.T) {
h, _, _, httpURL, done := newTestHTTPS(t)
h, _, _, httpURL, done := newTestFFTokens(t)
defer done()

pool := &fftypes.TokenPool{
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestCreateTokenPool(t *testing.T) {
}

func TestCreateTokenPoolError(t *testing.T) {
h, _, _, httpURL, done := newTestHTTPS(t)
h, _, _, httpURL, done := newTestFFTokens(t)
defer done()

pool := &fftypes.TokenPool{
Expand All @@ -156,7 +156,7 @@ func TestCreateTokenPoolError(t *testing.T) {
}

func TestEvents(t *testing.T) {
h, toServer, fromServer, _, done := newTestHTTPS(t)
h, toServer, fromServer, _, done := newTestFFTokens(t)
defer done()

err := h.Start()
Expand Down Expand Up @@ -224,7 +224,7 @@ func TestEvents(t *testing.T) {
func TestEventLoopReceiveClosed(t *testing.T) {
dxc := &tokenmocks.Callbacks{}
wsm := &wsmocks.WSClient{}
h := &HTTPS{
h := &FFTokens{
ctx: context.Background(),
callbacks: dxc,
wsconn: wsm,
Expand All @@ -239,7 +239,7 @@ func TestEventLoopReceiveClosed(t *testing.T) {
func TestEventLoopSendClosed(t *testing.T) {
dxc := &tokenmocks.Callbacks{}
wsm := &wsmocks.WSClient{}
h := &HTTPS{
h := &FFTokens{
ctx: context.Background(),
callbacks: dxc,
wsconn: wsm,
Expand All @@ -257,7 +257,7 @@ func TestEventLoopClosedContext(t *testing.T) {
wsm := &wsmocks.WSClient{}
ctx, cancel := context.WithCancel(context.Background())
cancel()
h := &HTTPS{
h := &FFTokens{
ctx: ctx,
callbacks: dxc,
wsconn: wsm,
Expand Down
5 changes: 3 additions & 2 deletions internal/tokens/tifactory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (

"github.com/hyperledger/firefly/internal/config"
"github.com/hyperledger/firefly/internal/i18n"
"github.com/hyperledger/firefly/internal/tokens/https"
"github.com/hyperledger/firefly/internal/tokens/fftokens"
"github.com/hyperledger/firefly/pkg/tokens"
)

var plugins = []tokens.Plugin{
&https.HTTPS{},
&fftokens.FFTokens{},
}

var pluginsByName = make(map[string]tokens.Plugin)
Expand All @@ -39,6 +39,7 @@ func init() {

func InitPrefix(prefix config.PrefixArray) {
prefix.AddKnownKey(tokens.TokensConfigConnector)
prefix.AddKnownKey(tokens.TokensConfigPlugin)
prefix.AddKnownKey(tokens.TokensConfigName)
for _, plugin := range plugins {
// Accept a superset of configs allowed by all plugins
Expand Down
6 changes: 4 additions & 2 deletions pkg/tokens/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package tokens
const (
// TokensConfigName is the user-supplied name for this token type
TokensConfigName = "name"
// TokensConfigConnector is the connector plugin used for this token type
TokensConfigConnector = "connector"
// TokensConfigConnector is the connector plugin used for this token type (deprecated)
TokensConfigConnector = "connector" // TODO: remove
// TokensConfigPlugin is the connector plugin used for this token type
TokensConfigPlugin = "plugin"
)