github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/committer/txvalidator/v20/testdata/test_plugin.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package testdata
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/golang/protobuf/proto"
    13  	validation "github.com/hechain20/hechain/core/handlers/validation/api"
    14  	. "github.com/hechain20/hechain/core/handlers/validation/api/capabilities"
    15  	. "github.com/hechain20/hechain/core/handlers/validation/api/identities"
    16  	. "github.com/hechain20/hechain/core/handlers/validation/api/policies"
    17  	. "github.com/hechain20/hechain/core/handlers/validation/api/state"
    18  	"github.com/hechain20/hechain/protoutil"
    19  	"github.com/hyperledger/fabric-protos-go/common"
    20  	"github.com/pkg/errors"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  // SampleValidationPlugin is an example for a validation plugin,
    25  // and is used to exercise the dependencies that the plugin validator provides
    26  type SampleValidationPlugin struct {
    27  	t  *testing.T
    28  	d  IdentityDeserializer
    29  	c  Capabilities
    30  	sf StateFetcher
    31  	pe PolicyEvaluator
    32  }
    33  
    34  // NewSampleValidationPlugin returns an instance of a validation plugin setup
    35  // for assertions.
    36  func NewSampleValidationPlugin(t *testing.T) *SampleValidationPlugin {
    37  	return &SampleValidationPlugin{t: t}
    38  }
    39  
    40  type MarshaledSignedData struct {
    41  	Data      []byte `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
    42  	Signature []byte `protobuf:"bytes,2,opt,name=signature,proto3" json:"signature,omitempty"`
    43  	Identity  []byte `protobuf:"bytes,2,opt,name=identity,proto3" json:"identity,omitempty"`
    44  }
    45  
    46  func (sd *MarshaledSignedData) Reset() {
    47  	*sd = MarshaledSignedData{}
    48  }
    49  
    50  func (*MarshaledSignedData) String() string {
    51  	panic("implement me")
    52  }
    53  
    54  func (*MarshaledSignedData) ProtoMessage() {
    55  	panic("implement me")
    56  }
    57  
    58  func (p *SampleValidationPlugin) Validate(block *common.Block, namespace string, txPosition int, actionPosition int, contextData ...validation.ContextDatum) error {
    59  	txData := block.Data.Data[0]
    60  	txn := &MarshaledSignedData{}
    61  	err := proto.Unmarshal(txData, txn)
    62  	require.NoError(p.t, err)
    63  
    64  	// Check if the chaincode is instantiated
    65  	state, err := p.sf.FetchState()
    66  	if err != nil {
    67  		return err
    68  	}
    69  	defer state.Done()
    70  
    71  	results, err := state.GetStateMultipleKeys("lscc", []string{namespace})
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	_ = p.c.PrivateChannelData()
    77  
    78  	if len(results) == 0 {
    79  		return errors.New("not instantiated")
    80  	}
    81  
    82  	// Check the identity can be properly deserialized
    83  	identity, err := p.d.DeserializeIdentity(txn.Identity)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	identifier := identity.GetIdentityIdentifier()
    89  	require.Equal(p.t, "SampleOrg", identifier.Mspid)
    90  	require.Equal(p.t, "foo", identifier.Id)
    91  
    92  	sd := &protoutil.SignedData{
    93  		Signature: txn.Signature,
    94  		Data:      txn.Data,
    95  		Identity:  txn.Identity,
    96  	}
    97  	// Validate the policy
    98  	pol := contextData[0].(SerializedPolicy).Bytes()
    99  	err = p.pe.Evaluate(pol, []*protoutil.SignedData{sd})
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	return nil
   105  }
   106  
   107  func (p *SampleValidationPlugin) Init(dependencies ...validation.Dependency) error {
   108  	for _, dep := range dependencies {
   109  		if deserializer, isIdentityDeserializer := dep.(IdentityDeserializer); isIdentityDeserializer {
   110  			p.d = deserializer
   111  		}
   112  		if capabilities, isCapabilities := dep.(Capabilities); isCapabilities {
   113  			p.c = capabilities
   114  		}
   115  		if stateFetcher, isStateFetcher := dep.(StateFetcher); isStateFetcher {
   116  			p.sf = stateFetcher
   117  		}
   118  		if policyEvaluator, isPolicyFetcher := dep.(PolicyEvaluator); isPolicyFetcher {
   119  			p.pe = policyEvaluator
   120  		}
   121  	}
   122  	if p.sf == nil {
   123  		p.t.Fatal("stateFetcher not passed in init")
   124  	}
   125  	if p.d == nil {
   126  		p.t.Fatal("identityDeserializer not passed in init")
   127  	}
   128  	if p.c == nil {
   129  		p.t.Fatal("capabilities not passed in init")
   130  	}
   131  	if p.pe == nil {
   132  		p.t.Fatal("policy fetcher not passed in init")
   133  	}
   134  	return nil
   135  }