44import os
55import json
66import subprocess
7+ import time
78from api .lib .peer .command import Command
89from api .config import FABRIC_TOOL , FABRIC_VERSION
910import logging
@@ -20,23 +21,39 @@ def __init__(self, version=FABRIC_VERSION, peer=FABRIC_TOOL, **kwargs):
2021
2122 def create (self , channel , orderer_admin_url , block_path , time_out = "90s" ):
2223 try :
23- res = 0x100
24- command = ""
24+ command = []
2525
2626 if os .getenv ("CORE_PEER_TLS_ENABLED" ) == "false" or os .getenv ("CORE_PEER_TLS_ENABLED" ) is None :
27- command = "{} channel join --channelID {} --config-block {} -o {}" .format (self .osnadmin , channel , block_path , orderer_admin_url )
27+ command = [
28+ self .osnadmin ,
29+ "channel" , "join" ,
30+ "--channelID" , channel ,
31+ "--config-block" , block_path ,
32+ "-o" , orderer_admin_url ,
33+ ]
2834 else :
2935 ORDERER_CA = os .getenv ("ORDERER_CA" )
3036 ORDERER_ADMIN_TLS_SIGN_CERT = os .getenv ("ORDERER_ADMIN_TLS_SIGN_CERT" )
3137 ORDERER_ADMIN_TLS_PRIVATE_KEY = os .getenv ("ORDERER_ADMIN_TLS_PRIVATE_KEY" )
32- command = "{} channel join --channelID {} --config-block {} -o {} --ca-file {} --client-cert {} --client-key {}" .format (self .osnadmin , channel , block_path , orderer_admin_url , ORDERER_CA , ORDERER_ADMIN_TLS_SIGN_CERT , ORDERER_ADMIN_TLS_PRIVATE_KEY )
33-
34- LOG .info (f"{ command } " )
35- res = os .system (command )
36-
37- # The return value of os.system is not the result of executing the program. It is a 16 bit number,
38- # and its high bit is the return code
39- res = res >> 8
38+ command = [
39+ self .osnadmin ,
40+ "channel" , "join" ,
41+ "--channelID" , channel ,
42+ "--config-block" , block_path ,
43+ "-o" , orderer_admin_url ,
44+ "--ca-file" , ORDERER_CA ,
45+ "--client-cert" , ORDERER_ADMIN_TLS_SIGN_CERT ,
46+ "--client-key" , ORDERER_ADMIN_TLS_PRIVATE_KEY
47+ ]
48+
49+ LOG .info (" " .join (command ))
50+
51+ res = subprocess .run (command , check = True )
52+
53+ except subprocess .CalledProcessError as e :
54+ err_msg = "create channel failed for {}!" .format (e )
55+ raise Exception (err_msg + str (e ))
56+
4057 except Exception as e :
4158 err_msg = "create channel failed for {}!" .format (e )
4259 raise Exception (err_msg )
@@ -78,30 +95,58 @@ def update(self, channel, channel_tx, orderer_url):
7895 res = res >> 8
7996 return res
8097
81- def fetch (self , block_path , channel , orderer_general_url ):
98+ def fetch (self , block_path , channel , orderer_general_url , max_retries = 5 , retry_interval = 1 ):
8299 """
83100 Fetch a specified block, writing it to a file e.g. <channelID>.block.
84101 params:
85102 option: block option newest|oldest|config|(block number).
86103 channel: channel id.
87104 """
88- try :
89- res = 0x100
90- command = ""
91- if os .getenv ("CORE_PEER_TLS_ENABLED" ) == "false" or os .getenv ("CORE_PEER_TLS_ENABLED" ) is None :
92- command = "{} channel fetch config {} -o {} -c {}" .format (self .peer , block_path , orderer_general_url , channel )
93- else :
94- ORDERER_CA = os .getenv ("ORDERER_CA" )
95- orderer_address = orderer_general_url .split (":" )[0 ]
96- command = "{} channel fetch config {} -o {} --ordererTLSHostnameOverride {} -c {} --tls --cafile {}" .format (self .peer , block_path , orderer_general_url , orderer_address , channel , ORDERER_CA )
105+ res = 0
106+ command = []
107+ if os .getenv ("CORE_PEER_TLS_ENABLED" ) == "false" or os .getenv ("CORE_PEER_TLS_ENABLED" ) is None :
108+ command = [
109+ self .peer ,
110+ "channel" , "fetch" ,
111+ "config" , block_path ,
112+ "-o" , orderer_general_url ,
113+ "-c" , channel
114+ ]
115+ else :
116+ ORDERER_CA = os .getenv ("ORDERER_CA" )
117+ orderer_address = orderer_general_url .split (":" )[0 ]
118+ command = [
119+ self .peer ,
120+ "channel" , "fetch" ,
121+ "config" , block_path ,
122+ "-o" , orderer_general_url ,
123+ "--ordererTLSHostnameOverride" , orderer_address ,
124+ "-c" , channel ,
125+ "--tls" ,
126+ "--cafile" , ORDERER_CA
127+ ]
128+
129+ LOG .info (" " .join (command ))
130+
131+ # Retry fetching the block up to max_retries times
132+ for attempt in range (1 , max_retries + 1 ):
133+ try :
134+ LOG .debug ("Attempt %d/%d to fetch block" , attempt , max_retries )
135+
136+ res = subprocess .run (command , check = True )
137+
138+ LOG .info ("Successfully fetched block" )
139+ break
140+
141+ except subprocess .CalledProcessError as e :
142+ LOG .debug (f"Attempt { attempt } /{ max_retries } failed" )
143+
144+ if attempt <= max_retries :
145+ time .sleep (retry_interval )
146+ else :
147+ LOG .error (f"Failed to fetch block after { max_retries } attempts" )
148+ raise e
97149
98- LOG .info (f"{ command } " )
99- res = os .system (command )
100-
101- res = res >> 8
102- except Exception as e :
103- err_msg = "fetch a specified block failed {}!" .format (e )
104- raise Exception (err_msg )
105150 return res
106151
107152 def signconfigtx (self , channel_tx ):
0 commit comments