Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2E test for tokens only namespaces #919

Merged
merged 7 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ nav_order: 2

|Key|Description|Type|Default Value|
|---|-----------|----|-------------|
|keyNormalization|Mechanism to normalize keys before using them. Valid options are `blockchain_plugin` - use blockchain plugin (default) or `none` - do not attempt normalization|`string`|`<nil>`
|keynormalization|Mechanism to normalize keys before using them. Valid options are `blockchain_plugin` - use blockchain plugin (default) or `none` - do not attempt normalization|`string`|`<nil>`

## batch.cache

Expand Down Expand Up @@ -622,6 +622,12 @@ nav_order: 2
|plugins|The list of plugins for this namespace|`string`|`<nil>`
|remotename|The namespace name to be sent in plugin calls, if it differs from namespace name|`string`|`<nil>`

## namespaces.predefined[].assets.manager

|Key|Description|Type|Default Value|
|---|-----------|----|-------------|
|keynormalization|Mechanism to normalize keys before using them. Valid options are `blockchain_plugin` - use blockchain plugin (default) or `none` - do not attempt normalization|`string`|`<nil>`

## namespaces.predefined[].multiparty

|Key|Description|Type|Default Value|
Expand Down
6 changes: 2 additions & 4 deletions internal/assets/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ package assets
import (
"context"

"github.com/hyperledger/firefly-common/pkg/config"
"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly-common/pkg/i18n"
"github.com/hyperledger/firefly/internal/broadcast"
"github.com/hyperledger/firefly/internal/coreconfig"
"github.com/hyperledger/firefly/internal/coremsgs"
"github.com/hyperledger/firefly/internal/identity"
"github.com/hyperledger/firefly/internal/metrics"
Expand Down Expand Up @@ -84,7 +82,7 @@ type assetManager struct {
keyNormalization int
}

func NewAssetManager(ctx context.Context, ns string, di database.Plugin, ti map[string]tokens.Plugin, im identity.Manager, sa syncasync.Bridge, bm broadcast.Manager, pm privatemessaging.Manager, mm metrics.Manager, om operations.Manager, txHelper txcommon.Helper) (Manager, error) {
func NewAssetManager(ctx context.Context, ns, keyNormalization string, di database.Plugin, ti map[string]tokens.Plugin, im identity.Manager, sa syncasync.Bridge, bm broadcast.Manager, pm privatemessaging.Manager, mm metrics.Manager, om operations.Manager, txHelper txcommon.Helper) (Manager, error) {
if di == nil || im == nil || sa == nil || ti == nil || mm == nil || om == nil {
return nil, i18n.NewError(ctx, coremsgs.MsgInitializationNilDepError, "AssetManager")
}
Expand All @@ -98,7 +96,7 @@ func NewAssetManager(ctx context.Context, ns string, di database.Plugin, ti map[
broadcast: bm,
messaging: pm,
tokens: ti,
keyNormalization: identity.ParseKeyNormalizationConfig(config.GetString(coreconfig.AssetManagerKeyNormalization)),
keyNormalization: identity.ParseKeyNormalizationConfig(keyNormalization),
metrics: mm,
operations: om,
}
Expand Down
4 changes: 2 additions & 2 deletions internal/assets/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func newTestAssetsCommon(t *testing.T, metrics bool) (*assetManager, func()) {
mom.On("RegisterHandler", mock.Anything, mock.Anything, mock.Anything)
mti.On("Name").Return("ut").Maybe()
ctx, cancel := context.WithCancel(context.Background())
a, err := NewAssetManager(ctx, "ns1", mdi, map[string]tokens.Plugin{"magic-tokens": mti}, mim, msa, mbm, mpm, mm, mom, txHelper)
a, err := NewAssetManager(ctx, "ns1", "blockchain_plugin", mdi, map[string]tokens.Plugin{"magic-tokens": mti}, mim, msa, mbm, mpm, mm, mom, txHelper)
rag := mdi.On("RunAsGroup", mock.Anything, mock.Anything).Maybe()
rag.RunFn = func(a mock.Arguments) {
rag.ReturnArguments = mock.Arguments{a[1].(func(context.Context) error)(a[0].(context.Context))}
Expand All @@ -75,7 +75,7 @@ func newTestAssetsCommon(t *testing.T, metrics bool) (*assetManager, func()) {
}

func TestInitFail(t *testing.T) {
_, err := NewAssetManager(context.Background(), "", nil, nil, nil, nil, nil, nil, nil, nil, nil)
_, err := NewAssetManager(context.Background(), "", "", nil, nil, nil, nil, nil, nil, nil, nil, nil)
assert.Regexp(t, "FF10128", err)
}

Expand Down
4 changes: 3 additions & 1 deletion internal/coreconfig/coreconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const (
NamespacePlugins = "plugins"
// NamespaceDefaultKey is the default signing key for blockchain transactions within this namespace
NamespaceDefaultKey = "defaultkey"
// NamespaceAssetKeyNormalization mechanism to normalize keys before using them. Valid options: "blockchain_plugin" - use blockchain plugin (default), "none" - do not attempt normalization
NamespaceAssetKeyNormalization = "assets.manager.keynormalization"
shorsher marked this conversation as resolved.
Show resolved Hide resolved
// NamespaceMultiparty contains the multiparty configuration for a namespace
NamespaceMultiparty = "multiparty"
// NamespaceMultipartyEnabled specifies if multi-party mode is enabled for a namespace
Expand Down Expand Up @@ -287,7 +289,7 @@ var (
// TransactionCacheTTL
TransactionCacheTTL = ffc("transaction.cache.ttl")
// AssetManagerKeyNormalization mechanism to normalize keys before using them. Valid options: "blockchain_plugin" - use blockchain plugin (default), "none" - do not attempt normalization
AssetManagerKeyNormalization = ffc("asset.manager.keyNormalization")
AssetManagerKeyNormalization = ffc("asset.manager.keynormalization")
// UIEnabled set to false to disable the UI (default is true, so UI will be enabled if ui.path is valid)
UIEnabled = ffc("ui.enabled")
// UIPath the path on which to serve the UI
Expand Down
3 changes: 2 additions & 1 deletion internal/coremsgs/en_config_descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
ConfigAPIDefaultFilterLimit = ffc("config.api.defaultFilterLimit", "The maximum number of rows to return if no limit is specified on an API request", i18n.IntType)
ConfigAPIMaxFilterLimit = ffc("config.api.maxFilterLimit", "The largest value of `limit` that an HTTP client can specify in a request", i18n.IntType)
ConfigAPIRequestMaxTimeout = ffc("config.api.requestMaxTimeout", "The maximum amount of time that an HTTP client can specify in a `Request-Timeout` header to keep a specific request open", i18n.TimeDurationType)
ConfigAssetManagerKeyNormalization = ffc("config.asset.manager.keyNormalization", "Mechanism to normalize keys before using them. Valid options are `blockchain_plugin` - use blockchain plugin (default) or `none` - do not attempt normalization", i18n.StringType)
ConfigAssetManagerKeyNormalization = ffc("config.asset.manager.keynormalization", "Mechanism to normalize keys before using them. Valid options are `blockchain_plugin` - use blockchain plugin (default) or `none` - do not attempt normalization (deprecated - namespace.predefined[].assets.manager.keynormalization)", i18n.StringType)

ConfigBatchManagerMinimumPollDelay = ffc("config.batch.manager.minimumPollDelay", "The minimum time the batch manager waits between polls on the DB - to prevent thrashing", i18n.TimeDurationType)
ConfigBatchManagerPollTimeout = ffc("config.batch.manager.pollTimeout", "How long to wait without any notifications of new messages before doing a page query", i18n.TimeDurationType)
Expand Down Expand Up @@ -257,6 +257,7 @@ var (
ConfigNamespacesPredefinedPlugins = ffc("config.namespaces.predefined[].plugins", "The list of plugins for this namespace", i18n.StringType)
ConfigNamespacesPredefinedRemoteName = ffc("config.namespaces.predefined[].remotename", "The namespace name to be sent in plugin calls, if it differs from namespace name", i18n.StringType)
ConfigNamespacesPredefinedDefaultKey = ffc("config.namespaces.predefined[].defaultkey", "A default signing key for blockchain transactions within this namespace", i18n.StringType)
ConfigNamespacesPredefinedKeyNormalization = ffc("config.namespaces.predefined[].assets.manager.keynormalization", "Mechanism to normalize keys before using them. Valid options are `blockchain_plugin` - use blockchain plugin (default) or `none` - do not attempt normalization", i18n.StringType)
ConfigNamespacesMultipartyEnabled = ffc("config.namespaces.predefined[].multiparty.enabled", "Enables multi-party mode for this namespace (defaults to true if an org name or key is configured, either here or at the root level)", i18n.BooleanType)
ConfigNamespacesMultipartyOrgName = ffc("config.namespaces.predefined[].multiparty.org.name", "A short name for the local root organization within this namespace", i18n.StringType)
ConfigNamespacesMultipartyOrgDesc = ffc("config.namespaces.predefined[].multiparty.org.description", "A description for the local root organization within this namespace", i18n.StringType)
Expand Down
2 changes: 2 additions & 0 deletions internal/namespace/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func InitConfig(withDefaults bool) {
namespacePredefined.AddKnownKey(coreconfig.NamespaceRemoteName)
namespacePredefined.AddKnownKey(coreconfig.NamespacePlugins)
namespacePredefined.AddKnownKey(coreconfig.NamespaceDefaultKey)
namespacePredefined.AddKnownKey(coreconfig.NamespaceAssetKeyNormalization)

multipartyConf := namespacePredefined.SubSection(coreconfig.NamespaceMultiparty)
multipartyConf.AddKnownKey(coreconfig.NamespaceMultipartyEnabled)
Expand All @@ -55,5 +56,6 @@ func InitConfig(withDefaults bool) {
if withDefaults {
namespaceConfig.AddKnownKey(NamespacePredefined+".0."+coreconfig.NamespaceName, "default")
namespaceConfig.AddKnownKey(NamespacePredefined+".0."+coreconfig.NamespaceDescription, "Default predefined namespace")
namespaceConfig.AddKnownKey(NamespacePredefined+".0."+coreconfig.NamespaceAssetKeyNormalization, "blockchain_plugin")
}
}
6 changes: 6 additions & 0 deletions internal/namespace/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,11 @@ func (nm *namespaceManager) loadNamespace(ctx context.Context, name string, inde
remoteName = name
}

keyNormalization := conf.GetString(coreconfig.NamespaceAssetKeyNormalization)
if keyNormalization == "" {
keyNormalization = config.GetString(coreconfig.AssetManagerKeyNormalization)
}

multipartyConf := conf.SubSection(coreconfig.NamespaceMultiparty)
// If any multiparty org information is configured (here or at the root), assume multiparty mode by default
orgName := multipartyConf.GetString(coreconfig.NamespaceMultipartyOrgName)
Expand Down Expand Up @@ -859,6 +864,7 @@ func (nm *namespaceManager) loadNamespace(ctx context.Context, name string, inde
config := orchestrator.Config{
DefaultKey: conf.GetString(coreconfig.NamespaceDefaultKey),
TokenRemoteNames: nm.tokenRemoteNames,
KeyNormalization: keyNormalization,
}
if multipartyEnabled.(bool) {
contractsConf := multipartyConf.SubArray(coreconfig.NamespaceMultipartyContract)
Expand Down
3 changes: 2 additions & 1 deletion internal/orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ type Plugins struct {

type Config struct {
DefaultKey string
KeyNormalization string
Multiparty multiparty.Config
TokenRemoteNames map[string]string
}
Expand Down Expand Up @@ -449,7 +450,7 @@ func (or *orchestrator) initManagers(ctx context.Context) (err error) {
}

if or.assets == nil {
or.assets, err = assets.NewAssetManager(ctx, or.namespace.LocalName, or.database(), or.tokens(), or.identity, or.syncasync, or.broadcast, or.messaging, or.metrics, or.operations, or.txHelper)
or.assets, err = assets.NewAssetManager(ctx, or.namespace.LocalName, or.config.KeyNormalization, or.database(), or.tokens(), or.identity, or.syncasync, or.broadcast, or.messaging, or.metrics, or.operations, or.txHelper)
if err != nil {
return err
}
Expand Down
70 changes: 70 additions & 0 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package e2e

import (
"crypto/rand"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/go-resty/resty/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)

func ReadConfig(t *testing.T, configFile string) map[string]interface{} {
yfile, err := ioutil.ReadFile(configFile)
assert.NoError(t, err)
data := make(map[string]interface{})
err = yaml.Unmarshal(yfile, &data)
assert.NoError(t, err)
return data
}

func WriteConfig(t *testing.T, configFile string, data map[string]interface{}) {
out, err := yaml.Marshal(data)
assert.NoError(t, err)
f, err := os.Create(configFile)
assert.NoError(t, err)
_, err = f.Write(out)
assert.NoError(t, err)
f.Close()
}

func AddNamespace(data map[string]interface{}, ns map[string]interface{}) {
namespaces := data["namespaces"].(map[interface{}]interface{})
predefined := namespaces["predefined"].([]interface{})
namespaces["predefined"] = append(predefined, ns)
}

func ResetFireFly(t *testing.T, client *resty.Client) {
resp, err := client.R().
SetBody(map[string]interface{}{}).
Post("/reset")
require.NoError(t, err)
assert.Equal(t, 204, resp.StatusCode())
}

func RandomName(t *testing.T) string {
b := make([]byte, 5)
_, err := rand.Read(b)
assert.NoError(t, err)
return fmt.Sprintf("e2e_%x", b)
}
9 changes: 0 additions & 9 deletions test/e2e/gateway/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package gateway

import (
"crypto/rand"
"encoding/base64"
"fmt"
"net/http"
Expand All @@ -28,7 +27,6 @@ import (
"github.com/gorilla/websocket"
"github.com/hyperledger/firefly/test/e2e"
"github.com/hyperledger/firefly/test/e2e/client"
"github.com/stretchr/testify/assert"
)

const (
Expand Down Expand Up @@ -117,10 +115,3 @@ func beforeE2ETest(t *testing.T) *testState {
}
return ts
}

func randomName(t *testing.T) string {
b := make([]byte, 5)
_, err := rand.Read(b)
assert.NoError(t, err)
return fmt.Sprintf("e2e_%x", b)
}
2 changes: 1 addition & 1 deletion test/e2e/gateway/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (suite *TokensTestSuite) TestE2EFungibleTokensAsync() {

received1 := e2e.WsReader(suite.testState.ws1)

poolName := fmt.Sprintf("pool_%s", randomName(suite.T()))
poolName := fmt.Sprintf("pool_%s", e2e.RandomName(suite.T()))
suite.T().Logf("Pool name: %s", poolName)

pool := &core.TokenPool{
Expand Down
Loading