def request_header(self, interface, data, height): interface.print_error("requesting header %d" % height) interface.send_request({"method": "blockchain.block.get_header", "params": [height]}) data["header_height"] = height data["req_time"] = time.time() if not "chain" in data: data["chain"] = []
def request_header(self, interface, data, height): interface.print_error("requesting header %d" % height) interface.send_request({'method':'blockchain.block.get_header', 'params':[height]}) data['header_height'] = height data['req_time'] = time.time() if not 'chain' in data: data['chain'] = []
def on_get_chunk(self, interface, response): '''Handle receiving a chunk of block headers''' if self.bc_requests: req_if, data = self.bc_requests[0] req_idx = data.get('chunk_idx') # Ignore unsolicited chunks if req_if == interface and req_idx == response['params'][0]: idx = self.blockchain.connect_chunk(req_idx, response['result']) # If not finished, get the next chunk if idx < 0 or not idx or self.get_local_height() >= data['if_height']: self.bc_requests.popleft() if not idx: interface.print_error("header didn't match checkpoint, dismissing interface") interface.stop() else: self.request_chunk(interface, data, idx)
def on_get_header(self, interface, response): '''Handle receiving a single block header''' if self.bc_requests: req_if, data = self.bc_requests[0] req_height = data.get('header_height', -1) # Ignore unsolicited headers if req_if == interface and req_height == response['params'][0]: next_height = self.blockchain.connect_header(data['chain'], response['result']) # If not finished, get the next header if next_height in [True, False]: self.bc_requests.popleft() if next_height: self.notify('updated') else: interface.print_error("header didn't connect, dismissing interface") interface.stop() else: self.request_header(interface, data, next_height)
def on_get_chunk(self, interface, response): '''Handle receiving a chunk of block headers''' if self.bc_requests: req_if, data = self.bc_requests[0] req_idx = data.get('chunk_idx') # Ignore unsolicited chunks if req_if == interface and req_idx == response['params'][0]: idx = self.blockchain.connect_chunk(req_idx, response['result']) # If not finished, get the next chunk if idx < 0 or not idx or self.get_local_height( ) >= data['if_height']: self.bc_requests.popleft() if not idx: interface.print_error( "header didn't match checkpoint, dismissing interface" ) interface.stop() else: self.request_chunk(interface, data, idx)
def handle_bc_requests(self): '''Work through each interface that has notified us of a new header. Send it requests if it is ahead of our blockchain object''' while self.bc_requests: interface, data = self.bc_requests.popleft() # If the connection was lost move on if not interface.is_connected(): continue req_time = data.get('req_time') if not req_time: # No requests sent yet. This interface has a new height. # Request headers if it is ahead of our blockchain if not self.bc_request_headers(interface, data): continue elif time.time() - req_time > 10: interface.print_error("blockchain request timed out") interface.stop() continue # Put updated request state back at head of deque self.bc_requests.appendleft((interface, data)) break
def request_chunk(self, interface, data, idx): interface.print_error("requesting chunk %d" % idx) interface.send_request({'method':'blockchain.block.get_chunk', 'params':[idx]}) data['chunk_idx'] = idx data['req_time'] = time.time()
def request_chunk(self, interface, data, idx): interface.print_error("requesting chunk %d" % idx) interface.send_request({"method": "blockchain.block.get_chunk", "params": [idx]}) data["chunk_idx"] = idx data["req_time"] = time.time()