|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package server |
| 8 | + |
| 9 | +import ( |
| 10 | + "runtime/debug" |
| 11 | + "time" |
| 12 | + |
| 13 | + cb "github.com/hyperledger/fabric-protos-go/common" |
| 14 | + ab "github.com/hyperledger/fabric-protos-go/orderer" |
| 15 | + "github.com/hyperledger/fabric/common/deliver" |
| 16 | + "github.com/hyperledger/fabric/common/metrics" |
| 17 | + "github.com/hyperledger/fabric/common/policies" |
| 18 | + localconfig "github.com/hyperledger/fabric/orderer/common/localconfig" |
| 19 | + "github.com/hyperledger/fabric/orderer/common/msgprocessor" |
| 20 | + "github.com/hyperledger/fabric/orderer/common/multichannel" |
| 21 | + "github.com/hyperledger/fabric/protoutil" |
| 22 | + "github.com/pkg/errors" |
| 23 | +) |
| 24 | + |
| 25 | +type attestationserver struct { |
| 26 | + dh *deliver.Handler |
| 27 | + debug *localconfig.Debug |
| 28 | + *multichannel.Registrar |
| 29 | +} |
| 30 | + |
| 31 | +// NewServer creates an ab.AtomicBroadcastServer based on the broadcast target and ledger Reader |
| 32 | +func NewAttestationService( |
| 33 | + r *multichannel.Registrar, |
| 34 | + metricsProvider metrics.Provider, |
| 35 | + debug *localconfig.Debug, |
| 36 | + timeWindow time.Duration, |
| 37 | + mutualTLS bool, |
| 38 | + expirationCheckDisabled bool, |
| 39 | +) ab.BlockAttestationsServer { |
| 40 | + s := &attestationserver{ |
| 41 | + dh: deliver.NewHandler(deliverSupport{Registrar: r}, timeWindow, mutualTLS, deliver.NewMetrics(metricsProvider), expirationCheckDisabled), |
| 42 | + debug: debug, |
| 43 | + Registrar: r, |
| 44 | + } |
| 45 | + return s |
| 46 | +} |
| 47 | + |
| 48 | +func (s *attestationserver) BlockAttestations(env *cb.Envelope, strm ab.BlockAttestations_BlockAttestationsServer) error { |
| 49 | + logger.Debugf("Starting new handler for block attestation") |
| 50 | + defer func() { |
| 51 | + if r := recover(); r != nil { |
| 52 | + logger.Criticalf("block attestation client triggered panic: %s\n%s", r, debug.Stack()) |
| 53 | + } |
| 54 | + logger.Debugf("Closing attestation server stream") |
| 55 | + }() |
| 56 | + |
| 57 | + policyChecker := func(env *cb.Envelope, channelID string) error { |
| 58 | + chain := s.GetChain(channelID) |
| 59 | + if chain == nil { |
| 60 | + return errors.Errorf("channel %s not found", channelID) |
| 61 | + } |
| 62 | + // In maintenance mode, we typically require the signature of /Channel/Orderer/Readers. |
| 63 | + // This will block Deliver requests from peers (which normally satisfy /Channel/Readers). |
| 64 | + sf := msgprocessor.NewSigFilter(policies.ChannelReaders, policies.ChannelOrdererReaders, chain) |
| 65 | + return sf.Apply(env) |
| 66 | + } |
| 67 | + attestationServer := &deliver.Server{ |
| 68 | + PolicyChecker: deliver.PolicyCheckerFunc(policyChecker), |
| 69 | + ResponseSender: &attestationSender{ |
| 70 | + BlockAttestations_BlockAttestationsServer: strm, |
| 71 | + }, |
| 72 | + } |
| 73 | + return s.dh.HandleAttestation(strm.Context(), attestationServer, env) |
| 74 | +} |
| 75 | + |
| 76 | +type attestationSender struct { |
| 77 | + ab.BlockAttestations_BlockAttestationsServer |
| 78 | +} |
| 79 | + |
| 80 | +func (rs *attestationSender) SendStatusResponse(status cb.Status) error { |
| 81 | + reply := &ab.BlockAttestationResponse{ |
| 82 | + Type: &ab.BlockAttestationResponse_Status{Status: status}, |
| 83 | + } |
| 84 | + return rs.Send(reply) |
| 85 | +} |
| 86 | + |
| 87 | +func (rs *attestationSender) SendBlockResponse( |
| 88 | + block *cb.Block, |
| 89 | + channelID string, |
| 90 | + chain deliver.Chain, |
| 91 | + signedData *protoutil.SignedData, |
| 92 | +) error { |
| 93 | + blockAttestation := ab.BlockAttestation{Header: block.Header, Metadata: block.Metadata} |
| 94 | + response := &ab.BlockAttestationResponse{ |
| 95 | + Type: &ab.BlockAttestationResponse_BlockAttestation{BlockAttestation: &blockAttestation}, |
| 96 | + } |
| 97 | + return rs.Send(response) |
| 98 | +} |
| 99 | + |
| 100 | +func (rs *attestationSender) DataType() string { |
| 101 | + return "block_attestation" |
| 102 | +} |
0 commit comments