def format_event_register_request(from_block, to_block, addresses, topics, groupid="1", filterid=None): ''' { "fromBlock": "latest", "toBlock": "latest", "addresses": [ 0xca5ed56862869c25da0bdf186e634aac6c6361ee ], "topics": [ "0x91c95f04198617c60eaf2180fbca88fc192db379657df0e412a9f7dd4ebbe95d" ], "groupID": "1", "filterID": "bb31e4ec086c48e18f21cb994e2e5967" }''' request = dict() request["fromBlock"] = from_block request["toBlock"] = to_block request["addresses"] = addresses request["topics"] = topics request["groupID"] = groupid if filterid is None: seq = uuid.uuid1() filterid = seq.hex request["filterID"] = filterid requestJson = FriendlyJsonSerde().json_encode(request) return requestJson
def encode_rpc_request(self, method, params): rpc_dict = { "jsonrpc": "2.0", "method": method, "params": params or [], "id": next(self.request_counter), } encoded = FriendlyJsonSerde().json_encode(rpc_dict) return to_bytes(text=encoded)
def make_request(self, method, params, type=ChannelPack.TYPE_RPC): stat = StatTool.begin() rpc_data = self.encode_rpc_request(method,params) self.logger.debug("request rpc_data : {}".format(rpc_data) ) #print("request rpc_data", rpc_data) request_pack = ChannelPack(type,ChannelPack.make_seq32(), 0,rpc_data) res = self.send_pack(request_pack) starttime = time.time() responsematch = False while time.time() - starttime < 10: # spend max 10 sec to wait a correct response try: theQueue = self.recvThread.getQueue(ChannelPack.TYPE_RPC) responsepack = theQueue.get(block=True, timeout=3) # pop msg from queue except Empty as e: continue #print("got a pack from queue, detail:{}".format(responsepack.detail())) self.logger.debug("got a pack from queue, detail:{}".format(responsepack.detail())) if responsepack.type == ChannelPack.TYPE_RPC and responsepack.seq == request_pack.seq: responsematch = True break else: #print("*******SKIP!!!! pack ", responsepack.detail()) self.logger.debug("*******SKIP!!!! pack {}".format( responsepack.detail() )) responsepack = None continue if responsematch == False: raise BcosError(102,None,"timeout") result = responsepack.result data = responsepack.data.decode("utf-8") msg = "success" if(result!=0): if result in self.errorMsg: msg = "unknow error %d"%result msg = self.errorMsg[result] raise BcosError(result,msg) response = FriendlyJsonSerde().json_decode(data) stat.done() stat.debug("make_request:{}".format(method) ) self.logger.debug("GetResponse. %s, Response: %s", method, response) #print("response from server:",response) self.logger.debug("response from server: {}".format( response) ) if "result" not in response: tempresp =dict() tempresp["result"] = response response =tempresp return response
def onResponse(self, responsepack): """ obtain the response of given type """ result = responsepack.result data = responsepack.data.decode("utf-8") # get onResponse emitter onresponse_emitter = ChannelHandler.getEmitterStr(self.onResponsePrefix, responsepack.seq, responsepack.type) if onresponse_emitter in self.requests: self.requests.remove(onresponse_emitter) emitter_str = ChannelHandler.getEmitterStr(self.getResultPrefix, responsepack.seq, responsepack.type) self.logger.debug("onResponse, emitter: {}".format(emitter_str)) if result != 0: self.logger.error("response from server failed , seq: {}, type:{}, result: {}". format(responsepack.seq, responsepack.type, result)) self.callbackEmitter.emit(emitter_str, result, True) return try: # json packet if responsepack.type == ChannelPack.TYPE_RPC or \ responsepack.type == ChannelPack.TYPE_TX_COMMITTED: response = FriendlyJsonSerde().json_decode(data) response_item = None if "result" not in response.keys(): response_item = dict() response_item["result"] = response else: response_item = response self.callbackEmitter.emit(emitter_str, response_item, False) self.logger.debug("response from server , seq: {}, type:{}". format(responsepack.seq, responsepack.type)) # block notify elif responsepack.type == ChannelPack.TYPE_TX_BLOCKNUM: number = int(data.split(',')[1], 10) self.logger.debug("receive block notify: seq: {} type:{}". format(responsepack.seq, responsepack.type)) if self.blockNumber < number: self.blockNumber = number self.logger.debug("currentBlockNumber: {}".format(self.blockNumber)) except Exception as e: self.logger.error("decode response failed, seq:{}, type:{}, error info: {}" .format(responsepack.seq, responsepack.type, e)) error_msg = "decode response failed, seq:{}, type:{}, message: {}".format( responsepack.seq, responsepack.type, result) self.callbackEmitter.emit(emitter_str, error_msg, True)
def decode_rpc_response(self, response): text_response = to_text(response) return FriendlyJsonSerde().json_decode(text_response)