Skip to content

Commit 072e73c

Browse files
committed
more refactoring
Signed-off-by: Angelo De Caro <[email protected]>
1 parent cb9fc3d commit 072e73c

File tree

8 files changed

+368
-78
lines changed

8 files changed

+368
-78
lines changed

token/services/network/fabric/endorsement/fsc/deps.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,33 @@ package fsc
88

99
import (
1010
"context"
11+
"time"
1112

13+
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric"
1214
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/driver"
15+
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/services/endorser"
1316
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
1417
"github.com/hyperledger-labs/fabric-token-sdk/token"
1518
)
1619

20+
type Translator interface {
21+
AddPublicParamsDependency() error
22+
CommitTokenRequest(raw []byte, storeHash bool) ([]byte, error)
23+
Write(ctx context.Context, action any) error
24+
}
25+
26+
type TranslatorProviderFunc = func(txID string, namespace string, rws *fabric.RWSet) (Translator, error)
27+
28+
// EndorserService defines the behaviors of the FSC's fabric endorser service that are needed by this package
29+
//
30+
//go:generate counterfeiter -o mock/endorser_service.go -fake-name EndorserService . EndorserService
31+
type EndorserService interface {
32+
NewTransaction(context view.Context, opts ...fabric.TransactionOption) (*endorser.Transaction, error)
33+
ReceiveTx(ctx view.Context) (*endorser.Transaction, error)
34+
CollectEndorsements(ctx view.Context, tx *endorser.Transaction, timeOut time.Duration, parties ...view.Identity) error
35+
Endorse(tx *endorser.Transaction, identities ...view.Identity) (any, error)
36+
}
37+
1738
// Context is an alias for view.Context
1839
//
1940
//go:generate counterfeiter -o mock/ctx.go -fake-name Context . Context

token/services/network/fabric/endorsement/fsc/initiator.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,52 @@ import (
1010
"time"
1111

1212
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
13-
fabric2 "github.com/hyperledger-labs/fabric-smart-client/platform/fabric"
14-
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/services/endorser"
13+
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric"
1514
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
16-
token2 "github.com/hyperledger-labs/fabric-token-sdk/token"
15+
"github.com/hyperledger-labs/fabric-token-sdk/token"
1716
"github.com/hyperledger-labs/fabric-token-sdk/token/services/network/driver"
1817
)
1918

19+
// RequestApprovalView is the initiator of the request approval protocol
2020
type RequestApprovalView struct {
21-
TMSID token2.TMSID
21+
TMSID token.TMSID
2222
TxID driver.TxID
2323
RequestRaw []byte
2424
// Nonce, if not nil it will be appended to the messages to sign.
2525
// This is to be used only for testing.
2626
Nonce []byte
2727
// Endorsers are the identities of the FSC node that play the role of endorser
2828
Endorsers []view.Identity
29+
30+
// EndorserService is the endorser service
31+
EndorserService EndorserService
32+
}
33+
34+
// NewRequestApprovalView returns a new instance of RequestApprovalView
35+
func NewRequestApprovalView(
36+
TMSID token.TMSID,
37+
txID driver.TxID,
38+
requestRaw []byte,
39+
nonce []byte,
40+
endorsers []view.Identity,
41+
endorserService EndorserService,
42+
) *RequestApprovalView {
43+
return &RequestApprovalView{TMSID: TMSID, TxID: txID, RequestRaw: requestRaw, Nonce: nonce, Endorsers: endorsers, EndorserService: endorserService}
2944
}
3045

31-
func (r *RequestApprovalView) Call(context view.Context) (interface{}, error) {
32-
logger.DebugfContext(context.Context(), "request approval...")
46+
func (r *RequestApprovalView) Call(ctx view.Context) (any, error) {
47+
logger.DebugfContext(ctx.Context(), "request approval...")
3348

34-
_, tx, err := endorser.NewTransaction(
35-
context,
36-
fabric2.WithCreator(r.TxID.Creator),
37-
fabric2.WithNonce(r.TxID.Nonce),
49+
tx, err := r.EndorserService.NewTransaction(
50+
ctx,
51+
fabric.WithCreator(r.TxID.Creator),
52+
fabric.WithNonce(r.TxID.Nonce),
3853
)
3954
if err != nil {
4055
return nil, errors.WithMessagef(err, "failed to create endorser transaction")
4156
}
4257

43-
tms, err := token2.GetManagementService(context, token2.WithTMSID(r.TMSID))
58+
tms, err := token.GetManagementService(ctx, token.WithTMSID(r.TMSID))
4459
if err != nil {
4560
return nil, errors.WithMessagef(err, "no token management service for [%s]", r.TMSID)
4661
}
@@ -57,22 +72,19 @@ func (r *RequestApprovalView) Call(context view.Context) (interface{}, error) {
5772
return nil, errors.WithMessagef(err, "failed to set token request transient")
5873
}
5974

60-
logger.DebugfContext(context.Context(), "request endorsement on tx [%s] to [%v]...", tx.ID(), r.Endorsers)
61-
_, err = context.RunView(endorser.NewParallelCollectEndorsementsOnProposalView(
62-
tx,
63-
r.Endorsers...,
64-
).WithTimeout(2 * time.Minute))
75+
logger.DebugfContext(ctx.Context(), "request endorsement on tx [%s] to [%v]...", tx.ID(), r.Endorsers)
76+
err = r.EndorserService.CollectEndorsements(ctx, tx, 2*time.Minute, r.Endorsers...)
6577
if err != nil {
6678
return nil, errors.WithMessagef(err, "failed to collect endorsements")
6779
}
68-
logger.DebugfContext(context.Context(), "request endorsement done")
80+
logger.DebugfContext(ctx.Context(), "request endorsement done")
6981

7082
// Return envelope
7183
env, err := tx.Envelope()
7284
if err != nil {
7385
return nil, errors.WithMessagef(err, "failed to retrieve envelope for endorsement")
7486
}
75-
logger.DebugfContext(context.Context(), "envelope ready")
87+
logger.DebugfContext(ctx.Context(), "envelope ready")
7688

7789
return env, nil
7890
}

token/services/network/fabric/endorsement/fsc/mock/endorser_service.go

Lines changed: 163 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)