github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/pkg/gateway/peeradapter.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package gateway
     8  
     9  import (
    10  	commonledger "github.com/hechain20/hechain/common/ledger"
    11  	coreledger "github.com/hechain20/hechain/core/ledger"
    12  	"github.com/hechain20/hechain/core/peer"
    13  	peerproto "github.com/hyperledger/fabric-protos-go/peer"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  // peerAdapter presents a small piece of the Peer in a form that can be easily used (and mocked) by the gateway's
    18  // transaction status checking and eventing.
    19  type peerAdapter struct {
    20  	Peer *peer.Peer
    21  }
    22  
    23  func (adapter *peerAdapter) CommitNotifications(done <-chan struct{}, channelName string) (<-chan *coreledger.CommitNotification, error) {
    24  	channel, err := adapter.channel(channelName)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	return channel.Ledger().CommitNotificationsChannel(done)
    30  }
    31  
    32  func (adapter *peerAdapter) TransactionStatus(channelName string, transactionID string) (peerproto.TxValidationCode, uint64, error) {
    33  	channel, err := adapter.channel(channelName)
    34  	if err != nil {
    35  		return 0, 0, err
    36  	}
    37  
    38  	status, blockNumber, err := channel.Ledger().GetTxValidationCodeByTxID(transactionID)
    39  	if err != nil {
    40  		return 0, 0, err
    41  	}
    42  
    43  	return status, blockNumber, nil
    44  }
    45  
    46  func (adapter *peerAdapter) Ledger(channelName string) (commonledger.Ledger, error) {
    47  	channel, err := adapter.channel(channelName)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	return channel.Ledger(), nil
    53  }
    54  
    55  func (adapter *peerAdapter) channel(name string) (*peer.Channel, error) {
    56  	channel := adapter.Peer.Channel(name)
    57  	if channel == nil {
    58  		return nil, errors.Errorf("channel does not exist: %s", name)
    59  	}
    60  
    61  	return channel, nil
    62  }