github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/pkg/gateway/gateway.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 SPDX-License-Identifier: Apache-2.0 4 */ 5 6 package gateway 7 8 import ( 9 "context" 10 11 "github.com/hechain20/hechain/common/flogging" 12 "github.com/hechain20/hechain/common/ledger" 13 "github.com/hechain20/hechain/core/peer" 14 gdiscovery "github.com/hechain20/hechain/gossip/discovery" 15 "github.com/hechain20/hechain/internal/pkg/comm" 16 "github.com/hechain20/hechain/internal/pkg/gateway/commit" 17 "github.com/hechain20/hechain/internal/pkg/gateway/config" 18 peerproto "github.com/hyperledger/fabric-protos-go/peer" 19 "google.golang.org/grpc" 20 ) 21 22 var logger = flogging.MustGetLogger("gateway") 23 24 // Server represents the GRPC server for the Gateway. 25 type Server struct { 26 registry *registry 27 commitFinder CommitFinder 28 policy ACLChecker 29 options config.Options 30 logger *flogging.FabricLogger 31 ledgerProvider LedgerProvider 32 } 33 34 type EndorserServerAdapter struct { 35 Server peerproto.EndorserServer 36 } 37 38 func (e *EndorserServerAdapter) ProcessProposal(ctx context.Context, req *peerproto.SignedProposal, _ ...grpc.CallOption) (*peerproto.ProposalResponse, error) { 39 return e.Server.ProcessProposal(ctx, req) 40 } 41 42 type CommitFinder interface { 43 TransactionStatus(ctx context.Context, channelName string, transactionID string) (*commit.Status, error) 44 } 45 46 type ACLChecker interface { 47 CheckACL(policyName string, channelName string, data interface{}) error 48 } 49 50 type LedgerProvider interface { 51 Ledger(channelName string) (ledger.Ledger, error) 52 } 53 54 // CreateServer creates an embedded instance of the Gateway. 55 func CreateServer(localEndorser peerproto.EndorserServer, discovery Discovery, peerInstance *peer.Peer, secureOptions *comm.SecureOptions, policy ACLChecker, localMSPID string, options config.Options) *Server { 56 adapter := &peerAdapter{ 57 Peer: peerInstance, 58 } 59 notifier := commit.NewNotifier(adapter) 60 61 server := newServer( 62 &EndorserServerAdapter{ 63 Server: localEndorser, 64 }, 65 discovery, 66 commit.NewFinder(adapter, notifier), 67 policy, 68 adapter, 69 peerInstance.GossipService.SelfMembershipInfo(), 70 localMSPID, 71 secureOptions, 72 options, 73 ) 74 75 peerInstance.AddConfigCallbacks(server.registry.configUpdate) 76 77 return server 78 } 79 80 func newServer(localEndorser peerproto.EndorserClient, discovery Discovery, finder CommitFinder, policy ACLChecker, ledgerProvider LedgerProvider, localInfo gdiscovery.NetworkMember, localMSPID string, secureOptions *comm.SecureOptions, options config.Options) *Server { 81 return &Server{ 82 registry: ®istry{ 83 localEndorser: &endorser{client: localEndorser, endpointConfig: &endpointConfig{pkiid: localInfo.PKIid, address: localInfo.Endpoint, mspid: localMSPID}}, 84 discovery: discovery, 85 logger: logger, 86 endpointFactory: &endpointFactory{timeout: options.DialTimeout, clientCert: secureOptions.Certificate, clientKey: secureOptions.Key}, 87 remoteEndorsers: map[string]*endorser{}, 88 channelInitialized: map[string]bool{}, 89 }, 90 commitFinder: finder, 91 policy: policy, 92 options: options, 93 logger: logger, 94 ledgerProvider: ledgerProvider, 95 } 96 }