github.com/s7techlab/cckit@v0.10.5/gateway/chaincode_old.go (about)

     1  package gateway
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/s7techlab/cckit/convert"
     7  )
     8  
     9  // All this components are deprecated and will be removed,  use ChaincodeInvoker
    10  // ====================================================================
    11  
    12  type Action string
    13  
    14  const (
    15  	Query  Action = `query`
    16  	Invoke Action = `invoke`
    17  )
    18  
    19  // Deprecated: use ChaincodeInvoker
    20  type Chaincode interface {
    21  	Query(ctx context.Context, fn string, args []interface{}, target interface{}) (interface{}, error)
    22  	Invoke(ctx context.Context, fn string, args []interface{}, target interface{}) (interface{}, error)
    23  	Events(ctx context.Context, r ...*ChaincodeInstanceEventsStreamRequest) (ChaincodeEventSub, error)
    24  }
    25  
    26  // Deprecated: use ChaincodeInstanceEventDelivery
    27  type ChaincodeEventSub interface {
    28  	Context() context.Context
    29  	Events() <-chan *ChaincodeEvent
    30  	Recv(*ChaincodeEvent) error
    31  	Close()
    32  }
    33  
    34  type chaincode struct {
    35  	Service   *ChaincodeService
    36  	Channel   string
    37  	Chaincode string
    38  }
    39  
    40  // Deprecated: use NewChaincodeInvoker
    41  func NewChaincode(service *ChaincodeService, channelName, chaincodeName string, opts ...Opt) *chaincode {
    42  	c := &chaincode{
    43  		Service:   service,
    44  		Channel:   channelName,
    45  		Chaincode: chaincodeName,
    46  	}
    47  
    48  	return c
    49  }
    50  
    51  func (c *chaincode) Locator() *ChaincodeLocator {
    52  	return &ChaincodeLocator{
    53  		Channel:   c.Channel,
    54  		Chaincode: c.Chaincode,
    55  	}
    56  }
    57  
    58  // Deprecated: use ChaincodeInstanceEventDelivery
    59  // ChaincodeInstance for converting to new model
    60  func (c *chaincode) ChaincodeInstance() *ChaincodeInstanceService {
    61  	return c.Service.InstanceService(c.Locator())
    62  }
    63  
    64  // Deprecated: use ChaincodeInstanceEventDelivery
    65  func (c *chaincode) Events(ctx context.Context, r ...*ChaincodeInstanceEventsStreamRequest) (_ chan<- *ChaincodeEvent, closer func() error, _ error) {
    66  	req := &ChaincodeInstanceEventsStreamRequest{}
    67  	if len(r) == 1 {
    68  		req = r[0]
    69  	}
    70  	return c.ChaincodeInstance().EventsChan(ctx, req)
    71  }
    72  
    73  func (c *chaincode) Query(ctx context.Context, fn string, args []interface{}, target interface{}) (interface{}, error) {
    74  	ccInput, err := ccInput(ctx, fn, args)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	response, err := c.Service.Query(ctx, &ChaincodeQueryRequest{
    80  		Locator: c.Locator(),
    81  		Input:   ccInput,
    82  	})
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	return convert.FromBytes(response.Payload, target)
    88  }
    89  
    90  func (c *chaincode) Invoke(ctx context.Context, fn string, args []interface{}, target interface{}) (interface{}, error) {
    91  	ccInput, err := ccInput(ctx, fn, args)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	response, err := c.Service.Invoke(ctx, &ChaincodeInvokeRequest{
    97  		Locator: c.Locator(),
    98  		Input:   ccInput,
    99  	})
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	return convert.FromBytes(response.Payload, target)
   105  }