101101import org .hyperledger .fabric .sdk .helper .Config ;
102102import org .hyperledger .fabric .sdk .helper .DiagnosticFileDumper ;
103103import org .hyperledger .fabric .sdk .helper .Utils ;
104+ import org .hyperledger .fabric .sdk .transaction .GetConfigBlockBuilder ;
104105import org .hyperledger .fabric .sdk .transaction .InstallProposalBuilder ;
105106import org .hyperledger .fabric .sdk .transaction .InstantiateProposalBuilder ;
106107import org .hyperledger .fabric .sdk .transaction .JoinPeerProposalBuilder ;
@@ -714,6 +715,52 @@ public Channel joinPeer(Peer peer, PeerOptions peerOptions) throws ProposalExcep
714715 return this ;
715716 }
716717
718+ private Block getConfigBlock (Peer peer ) throws ProposalException {
719+
720+ logger .debug (format ("getConfigBlock for channel %s with peer %s, url: %s" , name , peer .getName (), peer .getUrl ()));
721+
722+ if (shutdown ) {
723+ throw new ProposalException (format ("Channel %s has been shutdown." , name ));
724+ }
725+
726+ try {
727+
728+ final Channel systemChannel = newSystemChannel (client ); //needs to be invoked on system channel
729+
730+ TransactionContext transactionContext = systemChannel .getTransactionContext ();
731+
732+ FabricProposal .Proposal proposal = GetConfigBlockBuilder .newBuilder ()
733+ .context (transactionContext )
734+ .channelId (name )
735+ .build ();
736+
737+ logger .debug ("Getting signed proposal." );
738+ SignedProposal signedProposal = getSignedProposal (transactionContext , proposal );
739+ logger .debug ("Got signed proposal." );
740+
741+ Collection <ProposalResponse > resp = sendProposalToPeers (new ArrayList <>(Collections .singletonList (peer )),
742+ signedProposal , transactionContext );
743+
744+ ProposalResponse pro = resp .iterator ().next ();
745+
746+ if (pro .getStatus () == ProposalResponse .Status .SUCCESS ) {
747+ logger .trace (format ("getConfigBlock from peer %s on channel %s success" , peer .getName (), name ));
748+ return Block .parseFrom (pro .getProposalResponse ().getResponse ().getPayload ().toByteArray ());
749+ } else {
750+ throw new ProposalException (format ("getConfigBlock for channel %s failed with peer %s. Status %s, details: %s" ,
751+ name , peer .getName (), pro .getStatus ().toString (), pro .getMessage ()));
752+
753+ }
754+ } catch (ProposalException e ) {
755+ logger .error (format ("getConfigBlock for channel %s failed with peer %s." , name , peer .getName ()), e );
756+ throw e ;
757+ } catch (Exception e ) {
758+ logger .error (format ("getConfigBlock for channel %s failed with peer %s." , name , peer .getName ()), e );
759+ throw new ProposalException (e .getMessage (), e );
760+ }
761+
762+ }
763+
717764 /**
718765 * Removes the peer connection from the channel.
719766 * This does NOT unjoin the peer from from the channel.
@@ -1100,11 +1147,13 @@ protected void parseConfigBlock() throws TransactionException {
11001147
11011148 try {
11021149
1103- final Block configBlock = getConfigurationBlock ();
1150+ Block parseFrom = getConfigBlock (getRandomPeer ());
1151+
1152+ // final Block configBlock = getConfigurationBlock();
11041153
11051154 logger .debug (format ("Channel %s Got config block getting MSP data and anchorPeers data" , name ));
11061155
1107- Envelope envelope = Envelope .parseFrom (configBlock .getData ().getData (0 ));
1156+ Envelope envelope = Envelope .parseFrom (parseFrom .getData ().getData (0 ));
11081157 Payload payload = Payload .parseFrom (envelope .getPayload ());
11091158 ConfigEnvelope configEnvelope = ConfigEnvelope .parseFrom (payload .getData ());
11101159 ConfigGroup channelGroup = configEnvelope .getConfig ().getChannelGroup ();
@@ -1114,9 +1163,6 @@ protected void parseConfigBlock() throws TransactionException {
11141163
11151164// anchorPeers = Collections.unmodifiableSet(traverseConfigGroupsAnchors("", channelGroup, new HashSet<>()));
11161165
1117- } catch (TransactionException e ) {
1118- logger .error (e .getMessage (), e );
1119- throw e ;
11201166 } catch (Exception e ) {
11211167 logger .error (e .getMessage (), e );
11221168 throw new TransactionException (e );
@@ -1217,7 +1263,7 @@ private Block getConfigurationBlock() throws TransactionException {
12171263
12181264 public byte [] getChannelConfigurationBytes () throws TransactionException {
12191265 try {
1220- final Block configBlock = getConfigurationBlock ( );
1266+ final Block configBlock = getConfigBlock ( getRandomPeer () );
12211267
12221268 Envelope envelopeRet = Envelope .parseFrom (configBlock .getData ().getData (0 ));
12231269
@@ -1697,6 +1743,17 @@ private Peer getRandomLedgerQueryPeer() throws InvalidArgumentException {
16971743
16981744 }
16991745
1746+ private Peer getRandomPeer () throws InvalidArgumentException {
1747+
1748+ final ArrayList <Peer > randPicks = new ArrayList <>(getPeers ()); //copy to avoid unlikely changes
1749+
1750+ if (randPicks .isEmpty ()) {
1751+ throw new InvalidArgumentException ("Channel " + name + " does not have any peers associated with it." );
1752+ }
1753+
1754+ return randPicks .get (RANDOM .nextInt (randPicks .size ()));
1755+ }
1756+
17001757 private Orderer getRandomOrderer () throws InvalidArgumentException {
17011758
17021759 final ArrayList <Orderer > randPicks = new ArrayList <>(new HashSet <>(getOrderers ())); //copy to avoid unlikely changes
0 commit comments