class FabricInitializer: """This provides the proper fabric client wrapper """ def __init__(self, net_profile=None, channel_name=None, cc_name=None, cc_version=None, org_name=None, user_name=None, peer_name=None): self.client = Client(net_profile=net_profile) assert self.client print("---client---") print(self.client.orderers) print(self.client.peers) print("---client---") self.channel_name = channel_name self.channel = self.client.new_channel(channel_name) print("---channel---") print("client channels: ", self.client._channels) print("channel: ", self.client.get_channel(channel_name)) print("channel name: ", self.client.get_channel(channel_name).name) print("channel peers: ", self.client.get_channel(channel_name).peers) print("channel orderers: ", self.client.get_channel(channel_name).orderers) print("---channel---") assert self.channel self.cc_name = cc_name self.cc_version = cc_version self.user = self.client.get_user(org_name, user_name) assert self.user self.peers = [self.client.get_peer(peer_name)] assert self.peers print("---parameters---") print("User: "******"Peers: ", self.peers) print("cc name:", self.cc_name) print("---parameters---") self.hub = None self.reg_nub = None async def get_height(self): info = await self.client.query_info(self.user, self.channel_name, self.peers) return info.height
async def main(): cli = Client(net_profile=os.path.join(dir_path, '../network.json')) admin_owkin = cli.get_user('owkin', 'admin') cli.new_channel('mychannel') peer = cli.get_peer('peer1-owkin') events = cli.get_events(admin_owkin, peer, 'mychannel', start=0, filtered=True) async for v in cli.getEvents(events): print(v)
)) print("Get channel config done.") print(response) # Channel event hub def getBlocks(blocks): # On event complition the block is appended to the list of blocks def onEvent(block): blocks.append(block) # Returns an instance of the onEvent function return onEvent blocks = [] # empty list channel = cli.get_channel('businesschannel') channel_event_hub = channel.newChannelEventHub(cli.get_peer('peer0.org1.example.com'), org1_admin) channel_event_hub.registerBlockEvent(start=0, onEvent=getBlocks(blocks)) stream = channel_event_hub.connect() print(blocks) # Query Channel response = cli.query_channels( requestor=org1_admin, peers=['peer0.org1.example.com', 'peer1.org1.example.com'] ) print(response) # Query Info response = loop.run_until_complete(cli.query_info( requestor=orgq_admin,
class FabricILStateManager(LocalILStateManger): def __init__(self, net_profile: Path, channel_name: str, cc_name: str, cc_version: str, org_name: str, user_name: str, peer_name: str) -> None: self.entries_ready = [] # to store list of json objects self.entries_responded = [] # to store list of json objects self.client = Client(net_profile=net_profile) if not self.client: raise ValueError("Invalid network profile.") self.channel_name = channel_name self.channel = self.client.new_channel(channel_name) print(f'HF state manager - channel: {self.channel}') self.cc_name = cc_name self.cc_version = cc_version self.user = self.client.get_user(org_name, user_name) print(f'HF state manager - orgs: {self.client.organizations}') print(self.client.organizations['Org1MSP']._users) print(f'HF state manager - user: {self.user}') self.peers = [self.client.get_peer(peer_name)] print(f'HF state manager - peers: {self.peers}') self.hub = None self.reg_num = None self.events = [] async def get_height(self): info = await self.client.query_info(self.user, self.channel_name, self.peers) return info.height async def create_entry(self, id: str, transfer: Transfer) -> bool: payload = transfer.payload nonce, data = payload.nonce, payload.data try: await self.client.chaincode_invoke( requestor=self.user, peers=self.peers, channel_name=self.channel_name, cc_name=self.cc_name, cc_version=self.cc_version, fcn="createTransferEntry", args=[nonce, data], wait_for_event=True) return True except Exception as e: print(e) return False async def signal_send_acceptance(self, id: str, signal_acceptance: bool = True) -> bool: try: await self.client.chaincode_invoke( requestor=self.user, peers=self.peers, channel_name=self.channel_name, cc_name=self.cc_name, cc_version=self.cc_version, fcn="updateTransferEntry", args=[id, TransferStatus.READY, signal_acceptance]) return True except Exception as e: print(e) return False async def update_entry(self, id: str, status: TransferStatus, transfer: Transfer = None) -> bool: signal_acceptance, result = transfer.send_accepted, transfer.result try: await self.client.chaincode_invoke( requestor=self.user, peers=self.peers, channel_name=self.channel_name, cc_name=self.cc_name, cc_version=self.cc_version, fcn="updateTransferEntry", args=[id, status, signal_acceptance, result]) return True except Exception as e: print(e) return False async def receive_entry_events(self, event: TransferStatus) -> None: self.hub = self.channel.newChannelEventHub(self.peers[0], self.user) self.reg_num = self.hub.registerBlockEvent( onEvent=self._event_handler) if not self.height: self.height = await self.get_height() stream = self.hub.connect(filtered=False, start=self.height) try: await asyncio.wait_for(stream, timeout=1) except asyncio.TimeoutError: pass self.height = await self.get_height() if event == TransferStatus.READY: self.transfers_ready = self._buffer_data(self.entries_ready) if event == TransferStatus.RESPONDED: self.transfers_responded = self._buffer_data(self.entries_responded) # clean up self.hub.disconnect() self.hub.unregisterBlockEvent(self.reg_num) def _event_handler(self, event_obj): d = event_obj['data']['data'] actions = d[0]['payload']['data']['actions'] action = actions[0] event = action['payload']['action']['proposal_response_payload']['extension']['events'] event_name = event['event_name'] if event_name == "transferReady": self.entries_ready.append(event) if event_name == "transferResponded": self.entries_responded.append(event) def _buffer_data(self, events): res = [] for event in events: transfer = Transfer() json_str = event['payload'] t_obj = json.loads(json_str) transfer.status, = t_obj['status'] transfer.payload = t_obj['payload'] if transfer.status == TransferStatus.RESPONDED: transfer.send_accepted = t_obj['sendAcceptance'] transfer.result = json.loads(t_obj['result']) res.append(transfer) return res
print(response) # Channel event hub def getBlocks(blocks): # On event complition the block is appended to the list of blocks def onEvent(block): blocks.append(block) # Returns an instance of the onEvent function return onEvent blocks = [] # empty list channel = cli.get_channel('businesschannel') channel_event_hub = channel.newChannelEventHub( cli.get_peer('peer0.org1.example.com'), org1_admin) channel_event_hub.registerBlockEvent(start=0, onEvent=getBlocks(blocks)) stream = channel_event_hub.connect() print(blocks) # Query Channel response = cli.query_channels( requestor=org1_admin, peers=['peer0.org1.example.com', 'peer1.org1.example.com']) print(response) # Query Info response = loop.run_until_complete( cli.query_info( requestor=orgq_admin,