Skip to content

Commit 8ddf23a

Browse files
committed
[FAB-15267] Refactor goGen in handler v12
Refactoring go:generate in core/handler/validation/ buildin/v12/validation_logic.go, create local interfaces to generate mocks for tests. Change-Id: Ia03e4867b215f035c401a769c2a9ad119cf48617 Signed-off-by: Chongxin Luo <Chongxin.Luo@ibm.com>
1 parent 152a802 commit 8ddf23a

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

core/handlers/validation/builtin/v12/mocks/identity_deserializer.go

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

core/handlers/validation/builtin/v12/mocks/state_fetcher.go

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

core/handlers/validation/builtin/v12/validation_logic.go

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121
"github.com/hyperledger/fabric/core/chaincode/platforms/node"
2222
"github.com/hyperledger/fabric/core/common/ccprovider"
2323
"github.com/hyperledger/fabric/core/common/privdata"
24-
. "github.com/hyperledger/fabric/core/handlers/validation/api/capabilities"
25-
. "github.com/hyperledger/fabric/core/handlers/validation/api/identities"
26-
. "github.com/hyperledger/fabric/core/handlers/validation/api/policies"
27-
. "github.com/hyperledger/fabric/core/handlers/validation/api/state"
24+
vc "github.com/hyperledger/fabric/core/handlers/validation/api/capabilities"
25+
vi "github.com/hyperledger/fabric/core/handlers/validation/api/identities"
26+
vp "github.com/hyperledger/fabric/core/handlers/validation/api/policies"
27+
vs "github.com/hyperledger/fabric/core/handlers/validation/api/state"
2828
"github.com/hyperledger/fabric/core/handlers/validation/builtin/internal/car"
2929
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/rwsetutil"
3030
"github.com/hyperledger/fabric/core/scc/lscc"
@@ -44,14 +44,37 @@ const (
4444

4545
var validCollectionNameRegex = regexp.MustCompile(ccmetadata.AllowedCharsCollectionName)
4646

47-
//go:generate mockery -dir ../../api/capabilities/ -name Capabilities -case underscore -output mocks/
48-
//go:generate mockery -dir ../../api/state/ -name StateFetcher -case underscore -output mocks/
49-
//go:generate mockery -dir ../../api/identities/ -name IdentityDeserializer -case underscore -output mocks/
50-
//go:generate mockery -dir ../../api/policies/ -name PolicyEvaluator -case underscore -output mocks/
47+
//go:generate mockery -dir . -name Capabilities -case underscore -output mocks/
48+
49+
// Capabilities is the local interface that used to generate mocks for foreign interface.
50+
type Capabilities interface {
51+
vc.Capabilities
52+
}
53+
54+
//go:generate mockery -dir . -name StateFetcher -case underscore -output mocks/
55+
56+
// StateFetcher is the local interface that used to generate mocks for foreign interface.
57+
type StateFetcher interface {
58+
vs.StateFetcher
59+
}
60+
61+
//go:generate mockery -dir . -name IdentityDeserializer -case underscore -output mocks/
62+
63+
// IdentityDeserializer is the local interface that used to generate mocks for foreign interface.
64+
type IdentityDeserializer interface {
65+
vi.IdentityDeserializer
66+
}
67+
68+
//go:generate mockery -dir . -name PolicyEvaluator -case underscore -output mocks/
69+
70+
// PolicyEvaluator is the local interface that used to generate mocks for foreign interface.
71+
type PolicyEvaluator interface {
72+
vp.PolicyEvaluator
73+
}
5174

5275
// New creates a new instance of the default VSCC
53-
// Typically this will only be invoked once per peer
54-
func New(c Capabilities, s StateFetcher, d IdentityDeserializer, pe PolicyEvaluator) *Validator {
76+
// Typically this will only be invoked once per peer.
77+
func New(c vc.Capabilities, s vs.StateFetcher, d vi.IdentityDeserializer, pe vp.PolicyEvaluator) *Validator {
5578
return &Validator{
5679
capabilities: c,
5780
stateFetcher: s,
@@ -63,16 +86,16 @@ func New(c Capabilities, s StateFetcher, d IdentityDeserializer, pe PolicyEvalua
6386
// Validator implements the default transaction validation policy,
6487
// which is to check the correctness of the read-write set and the endorsement
6588
// signatures against an endorsement policy that is supplied as argument to
66-
// every invoke
89+
// every invoke.
6790
type Validator struct {
68-
deserializer IdentityDeserializer
69-
capabilities Capabilities
70-
stateFetcher StateFetcher
71-
policyEvaluator PolicyEvaluator
91+
deserializer vi.IdentityDeserializer
92+
capabilities vc.Capabilities
93+
stateFetcher vs.StateFetcher
94+
policyEvaluator vp.PolicyEvaluator
7295
}
7396

7497
// Validate validates the given envelope corresponding to a transaction with an endorsement
75-
// policy as given in its serialized form
98+
// policy as given in its serialized form.
7699
func (vscc *Validator) Validate(
77100
block *common.Block,
78101
namespace string,
@@ -147,7 +170,7 @@ func (vscc *Validator) Validate(
147170
return nil
148171
}
149172

150-
// checkInstantiationPolicy evaluates an instantiation policy against a signed proposal
173+
// checkInstantiationPolicy evaluates an instantiation policy against a signed proposal.
151174
func (vscc *Validator) checkInstantiationPolicy(chainName string, env *common.Envelope, instantiationPolicy []byte, payl *common.Payload) commonerrors.TxValidationError {
152175
// get the signature header
153176
shdr, err := protoutil.GetSignatureHeader(payl.Header.SignatureHeader)
@@ -214,7 +237,7 @@ func validateNewCollectionConfigs(newCollectionConfigs []*common.CollectionConfi
214237
return nil
215238
}
216239

217-
// validateSpOrConcat checks if the supplied signature policy is just an OR-concatenation of identities
240+
// validateSpOrConcat checks if the supplied signature policy is just an OR-concatenation of identities.
218241
func validateSpOrConcat(sp *common.SignaturePolicy) error {
219242
if sp.GetNOutOf() == nil {
220243
return nil
@@ -329,13 +352,13 @@ func validateCollectionName(collectionName string) error {
329352

330353
// validateRWSetAndCollection performs validation of the rwset
331354
// of an LSCC deploy operation and then it validates any collection
332-
// configuration
355+
// configuration.
333356
func (vscc *Validator) validateRWSetAndCollection(
334357
lsccrwset *kvrwset.KVRWSet,
335358
cdRWSet *ccprovider.ChaincodeData,
336359
lsccArgs [][]byte,
337360
lsccFunc string,
338-
ac Capabilities,
361+
ac vc.Capabilities,
339362
channelName string,
340363
) commonerrors.TxValidationError {
341364
/********************************************/
@@ -455,7 +478,7 @@ func (vscc *Validator) ValidateLSCCInvocation(
455478
env *common.Envelope,
456479
cap *pb.ChaincodeActionPayload,
457480
payl *common.Payload,
458-
ac Capabilities,
481+
ac vc.Capabilities,
459482
) commonerrors.TxValidationError {
460483
cpp, err := protoutil.GetChaincodeProposalPayload(cap.ChaincodeProposalPayload)
461484
if err != nil {
@@ -776,10 +799,10 @@ func (vscc *Validator) deduplicateIdentity(cap *pb.ChaincodeActionPayload) ([]*p
776799
}
777800

778801
type state struct {
779-
State
802+
vs.State
780803
}
781804

782-
// GetState retrieves the value for the given key in the given namespace
805+
// GetState retrieves the value for the given key in the given namespace.
783806
func (s *state) GetState(namespace string, key string) ([]byte, error) {
784807
values, err := s.GetStateMultipleKeys(namespace, []string{key})
785808
if err != nil {

0 commit comments

Comments
 (0)