Skip to content

Commit

Permalink
user token.ServiceProvider rather than view.ServiceProvider. (hyperle…
Browse files Browse the repository at this point in the history
…dger-labs#626)

Remove ServiceProvider as a field where possible.

Signed-off-by: Angelo De Caro <[email protected]>
  • Loading branch information
adecaro authored May 10, 2024
1 parent f71f8b5 commit 3acdd38
Show file tree
Hide file tree
Showing 24 changed files with 75 additions and 90 deletions.
5 changes: 2 additions & 3 deletions token/services/auditdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"sync"
"time"

"github.com/hyperledger-labs/fabric-smart-client/platform/view"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/flogging"
"github.com/hyperledger-labs/fabric-token-sdk/token"
"github.com/hyperledger-labs/fabric-token-sdk/token/core"
Expand Down Expand Up @@ -368,7 +367,7 @@ var (

// GetByTMSId returns the DB for the given TMS id.
// Nil might be returned if the wallet is not found or an error occurred.
func GetByTMSId(sp view.ServiceProvider, tmsID token.TMSID) (*DB, error) {
func GetByTMSId(sp token.ServiceProvider, tmsID token.TMSID) (*DB, error) {
s, err := GetProvider(sp)
if err != nil {
return nil, errors.Wrapf(err, "failed to get manager service")
Expand All @@ -380,7 +379,7 @@ func GetByTMSId(sp view.ServiceProvider, tmsID token.TMSID) (*DB, error) {
return c, nil
}

func GetProvider(sp view.ServiceProvider) (*Manager, error) {
func GetProvider(sp token.ServiceProvider) (*Manager, error) {
s, err := sp.GetService(managerType)
if err != nil {
return nil, errors.Wrapf(err, "failed to get manager service")
Expand Down
7 changes: 3 additions & 4 deletions token/services/auditor/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"reflect"
"sync"

"github.com/hyperledger-labs/fabric-smart-client/platform/view"
"github.com/hyperledger-labs/fabric-token-sdk/token"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/auditdb"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/network/common"
Expand Down Expand Up @@ -166,7 +165,7 @@ var (
)

// Get returns the Auditor instance for the passed auditor wallet
func Get(sp view.ServiceProvider, w *token.AuditorWallet) *Auditor {
func Get(sp token.ServiceProvider, w *token.AuditorWallet) *Auditor {
if w == nil {
logger.Debugf("no wallet provided")
return nil
Expand All @@ -175,7 +174,7 @@ func Get(sp view.ServiceProvider, w *token.AuditorWallet) *Auditor {
}

// GetByTMSID returns the Auditor instance for the passed auditor wallet
func GetByTMSID(sp view.ServiceProvider, tmsID token.TMSID) *Auditor {
func GetByTMSID(sp token.ServiceProvider, tmsID token.TMSID) *Auditor {
s, err := sp.GetService(managerType)
if err != nil {
logger.Errorf("failed to get manager service: [%s]", err)
Expand All @@ -190,6 +189,6 @@ func GetByTMSID(sp view.ServiceProvider, tmsID token.TMSID) *Auditor {
}

// New returns the Auditor instance for the passed auditor wallet
func New(sp view.ServiceProvider, w *token.AuditorWallet) *Auditor {
func New(sp token.ServiceProvider, w *token.AuditorWallet) *Auditor {
return Get(sp, w)
}
3 changes: 1 addition & 2 deletions token/services/identity/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package identity
import (
"reflect"

"github.com/hyperledger-labs/fabric-smart-client/platform/view"
"github.com/hyperledger-labs/fabric-token-sdk/token"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/db/driver"
"github.com/pkg/errors"
Expand All @@ -31,7 +30,7 @@ var (
)

// GetStorageProvider returns the registered instance of StorageProvider from the passed service provider
func GetStorageProvider(sp view.ServiceProvider) (StorageProvider, error) {
func GetStorageProvider(sp token.ServiceProvider) (StorageProvider, error) {
s, err := sp.GetService(storageProviderType)
if err != nil {
return nil, errors.Wrapf(err, "failed to get token vault provider")
Expand Down
5 changes: 2 additions & 3 deletions token/services/interop/htlc/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package htlc
import (
"encoding/json"

view2 "github.com/hyperledger-labs/fabric-smart-client/platform/view"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
"github.com/hyperledger-labs/fabric-token-sdk/token"
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
Expand Down Expand Up @@ -309,12 +308,12 @@ func (w *OwnerWallet) filterIterator(tokenType string, sender bool, selector Sel
}

// GetWallet returns the wallet whose id is the passed id
func GetWallet(sp view2.ServiceProvider, id string, opts ...token.ServiceOption) *token.OwnerWallet {
func GetWallet(sp token.ServiceProvider, id string, opts ...token.ServiceOption) *token.OwnerWallet {
return ttx.GetWallet(sp, id, opts...)
}

// Wallet returns an OwnerWallet which contains a wallet and a query service
func Wallet(sp view2.ServiceProvider, wallet *token.OwnerWallet) *OwnerWallet {
func Wallet(sp token.ServiceProvider, wallet *token.OwnerWallet) *OwnerWallet {
if wallet == nil {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions token/services/network/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ SPDX-License-Identifier: Apache-2.0

package driver

import "github.com/hyperledger-labs/fabric-smart-client/platform/view"
import "github.com/hyperledger-labs/fabric-token-sdk/token"

// Driver models the network driver factory
type Driver interface {
// New returns a new network instance for the passed network and channel (if applicable)
New(sp view.ServiceProvider, network, channel string) (Network, error)
New(sp token.ServiceProvider, network, channel string) (Network, error)
}
4 changes: 2 additions & 2 deletions token/services/network/fabric/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package fabric

import (
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric"
"github.com/hyperledger-labs/fabric-smart-client/platform/view"
"github.com/hyperledger-labs/fabric-token-sdk/token"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/network"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/network/driver"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/vault"
Expand All @@ -17,7 +17,7 @@ import (

type Driver struct{}

func (d *Driver) New(sp view.ServiceProvider, network, channel string) (driver.Network, error) {
func (d *Driver) New(sp token.ServiceProvider, network, channel string) (driver.Network, error) {
n, err := fabric.GetFabricNetworkService(sp, network)
if err != nil {
return nil, errors.WithMessagef(err, "fabric network [%s] not found", network)
Expand Down
2 changes: 1 addition & 1 deletion token/services/network/fabric/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ type Network struct {
subscribers *events.Subscribers
}

func NewNetwork(sp view2.ServiceProvider, n *fabric.NetworkService, ch *fabric.Channel, newVault NewVaultFunc) *Network {
func NewNetwork(sp token2.ServiceProvider, n *fabric.NetworkService, ch *fabric.Channel, newVault NewVaultFunc) *Network {
return &Network{
n: n,
ch: ch,
Expand Down
12 changes: 5 additions & 7 deletions token/services/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ import (
"sync"
"time"

"github.com/hyperledger-labs/fabric-token-sdk/token/services/vault"

view2 "github.com/hyperledger-labs/fabric-smart-client/platform/view"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/flogging"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
"github.com/hyperledger-labs/fabric-token-sdk/token"
api2 "github.com/hyperledger-labs/fabric-token-sdk/token/driver"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/network/driver"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/vault"
token2 "github.com/hyperledger-labs/fabric-token-sdk/token/token"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -391,14 +389,14 @@ func (n *Network) ProcessNamespace(namespace string) error {

// Provider returns an instance of network provider
type Provider struct {
sp view2.ServiceProvider
sp token.ServiceProvider

lock sync.Mutex
networks map[string]*Network
}

// NewProvider returns a new instance of network provider
func NewProvider(sp view2.ServiceProvider) *Provider {
func NewProvider(sp token.ServiceProvider) *Provider {
ms := &Provider{
sp: sp,
networks: map[string]*Network{},
Expand Down Expand Up @@ -445,7 +443,7 @@ func (np *Provider) newNetwork(network string, channel string) (*Network, error)
}

// GetInstance returns a network instance for the given network and channel
func GetInstance(sp view2.ServiceProvider, network, channel string) *Network {
func GetInstance(sp token.ServiceProvider, network, channel string) *Network {
n, err := GetProvider(sp).GetNetwork(network, channel)
if err != nil {
logger.Errorf("Failed to get network [%s:%s]: %s", network, channel, err)
Expand All @@ -454,7 +452,7 @@ func GetInstance(sp view2.ServiceProvider, network, channel string) *Network {
return n
}

func GetProvider(sp view2.ServiceProvider) *Provider {
func GetProvider(sp token.ServiceProvider) *Provider {
s, err := sp.GetService(&Provider{})
if err != nil {
panic(fmt.Sprintf("Failed to get service: %s", err))
Expand Down
3 changes: 2 additions & 1 deletion token/services/network/orion/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package orion
import (
"github.com/hyperledger-labs/fabric-smart-client/platform/orion"
"github.com/hyperledger-labs/fabric-smart-client/platform/view"
"github.com/hyperledger-labs/fabric-token-sdk/token"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/network"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/network/driver"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/vault"
Expand All @@ -17,7 +18,7 @@ import (

type Driver struct{}

func (d *Driver) New(sp view.ServiceProvider, network, channel string) (driver.Network, error) {
func (d *Driver) New(sp token.ServiceProvider, network, channel string) (driver.Network, error) {
n, err := orion.GetOrionNetworkService(sp, network)
if err != nil {
return nil, errors.WithMessagef(err, "network [%s] not found", network)
Expand Down
4 changes: 2 additions & 2 deletions token/services/network/orion/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type IdentityProvider interface {
}

type Network struct {
sp view2.ServiceProvider
sp token2.ServiceProvider
viewManager *view2.Manager
tmsProvider *token2.ManagementServiceProvider
n *orion.NetworkService
Expand All @@ -46,7 +46,7 @@ type Network struct {
subscribers *events.Subscribers
}

func NewNetwork(sp view2.ServiceProvider, ip IdentityProvider, n *orion.NetworkService, newVault NewVaultFunc) *Network {
func NewNetwork(sp token2.ServiceProvider, ip IdentityProvider, n *orion.NetworkService, newVault NewVaultFunc) *Network {
net := &Network{
sp: sp,
ip: ip,
Expand Down
6 changes: 3 additions & 3 deletions token/services/network/orion/publicparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ package orion
import (
"time"

"github.com/hyperledger-labs/fabric-token-sdk/token/services/network/common/rws/translator"

"github.com/hyperledger-labs/fabric-smart-client/platform/orion"
view2 "github.com/hyperledger-labs/fabric-smart-client/platform/view"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/flogging"
session2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/session"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
"github.com/hyperledger-labs/fabric-token-sdk/token"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/network/common/rws/translator"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -115,7 +115,7 @@ func (v *PublicParamsRequestResponderView) Call(context view.Context) (interface
return nil, nil
}

func ReadPublicParameters(context view2.ServiceProvider, network, namespace string) ([]byte, error) {
func ReadPublicParameters(context token.ServiceProvider, network, namespace string) ([]byte, error) {
ons, err := orion.GetOrionNetworkService(context, network)
if err != nil {
return nil, errors.Wrapf(err, "failed to get orion network service for network [%s]", network)
Expand Down
3 changes: 1 addition & 2 deletions token/services/nfttx/qe.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package nfttx
import (
"encoding/base64"

"github.com/hyperledger-labs/fabric-smart-client/platform/view"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/tracing"
"github.com/hyperledger-labs/fabric-token-sdk/token"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/nfttx/marshaller"
Expand Down Expand Up @@ -37,7 +36,7 @@ type QueryExecutor struct {
precision uint64
}

func NewQueryExecutor(sp view.ServiceProvider, wallet string, precision uint64, opts ...token.ServiceOption) (*QueryExecutor, error) {
func NewQueryExecutor(sp token.ServiceProvider, wallet string, precision uint64, opts ...token.ServiceOption) (*QueryExecutor, error) {
tms := token.GetManagementService(sp, opts...)
qe := tms.Vault().NewQueryEngine()
return &QueryExecutor{
Expand Down
21 changes: 13 additions & 8 deletions token/services/nfttx/uniqueness/uniqueness.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ import (
"encoding/base64"
"sync"

"github.com/hyperledger-labs/fabric-smart-client/platform/view"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/kvs"
"github.com/hyperledger-labs/fabric-token-sdk/token"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/nfttx/marshaller"
"github.com/pkg/errors"
)

type KVS interface {
Exists(k string) bool
Get(k string, v interface{}) error
Put(k string, key interface{}) error
}

// Service is a uniqueness service.
// The service computes a unique id for a given object by hashing the object's json representation together with a random salt.
// The random salt is used to avoid dictionary attacks.
// The random salt is stored in the KVS and is generated on the first call to the service.
type Service struct {
mutex sync.Mutex
sp view.ServiceProvider
kvs KVS
}

// ComputeID computes the unique ID of the given object.
Expand All @@ -36,11 +42,10 @@ func (s *Service) ComputeID(state interface{}) (string, error) {
s.mutex.Lock()
defer s.mutex.Unlock()

kvs := kvs.GetService(s.sp)
k := "github.com/hyperledger-labs/fabric-token-sdk/token/services/nfttx/uniqueness/key"
var key []byte
if kvs.Exists(k) {
if err := kvs.Get(k, &key); err != nil {
if s.kvs.Exists(k) {
if err := s.kvs.Get(k, &key); err != nil {
return "", errors.WithMessagef(err, "failed to get key %s", k)
}
} else {
Expand All @@ -54,7 +59,7 @@ func (s *Service) ComputeID(state interface{}) (string, error) {
if n != size {
return "", errors.New("error getting random bytes")
}
if err := kvs.Put(k, key); err != nil {
if err := s.kvs.Put(k, key); err != nil {
return "", errors.WithMessagef(err, "failed to put key %s", k)
}
}
Expand All @@ -78,8 +83,8 @@ func (s *Service) ComputeID(state interface{}) (string, error) {
}

// GetService returns the uniqueness service.
func GetService(sp view.ServiceProvider) *Service {
func GetService(sp token.ServiceProvider) *Service {
return &Service{
sp: sp,
kvs: kvs.GetService(sp),
}
}
Loading

0 comments on commit 3acdd38

Please sign in to comment.