def on_post(self, req, resp): block_hash = None if 'block_id' in req.media: block = Block.query(self.session).filter( Block.id == req.media.get('block_id')).first() elif req.media.get('block_hash'): block_hash = req.media.get('block_hash') block = Block.query( self.session).filter(Block.hash == block_hash).first() else: block = None resp.status = falcon.HTTP_BAD_REQUEST resp.media = { 'errors': ['Either block_hash or block_id should be supplied'] } if block: print('Sequencing #{} ...'.format(block.id)) harvester = PolkascanHarvesterService(self.session, type_registry=TYPE_REGISTRY) if block.id == 1: # Add genesis block parent_block = harvester.add_block(block.parent_hash) block_total = BlockTotal.query( self.session).filter_by(id=block.id).first() parent_block = Block.query( self.session).filter(Block.id == block.id - 1).first() parent_block_total = BlockTotal.query( self.session).filter_by(id=block.id - 1).first() if block_total: resp.status = falcon.HTTP_200 resp.media = {'result': 'already exists', 'blockId': block.id} else: if parent_block_total: parent_block_total = parent_block_total.asdict() if parent_block: parent_block = parent_block.asdict() harvester.sequence_block(block, parent_block, parent_block_total) self.session.commit() resp.status = falcon.HTTP_201 resp.media = { 'result': 'added', 'parentHash': block.parent_hash } else: resp.status = falcon.HTTP_404 resp.media = {'result': 'Block not found'}
def get_item(self, item_id): if item_id.isnumeric(): return BlockTotal.query(self.session).get(item_id) else: block = Block.query(self.session).filter_by(hash=item_id).first() if block: return BlockTotal.query(self.session).get(block.id)
def on_get(self, req, resp, network_id=None): resp.status = falcon.HTTP_200 # TODO make caching more generic for custom resources cache_key = '{}-{}'.format(req.method, req.url) response = self.cache_region.get(cache_key, self.cache_expiration_time) if response is NO_VALUE: best_block = BlockTotal.query( self.session).filter_by(id=self.session.query( func.max(BlockTotal.id)).one()[0]).first() if best_block: response = self.get_jsonapi_response(data={ 'type': 'networkstats', 'id': network_id, 'attributes': { 'best_block': best_block.id, 'total_signed_extrinsics': int(best_block.total_extrinsics_signed), 'total_events': int(best_block.total_events), 'total_events_module': int(best_block.total_events_module), 'total_blocks': 'N/A', 'total_accounts': int(best_block.total_accounts), 'total_runtimes': Runtime.query(self.session).count() } }, ) else: response = self.get_jsonapi_response(data={ 'type': 'networkstats', 'id': network_id, 'attributes': { 'best_block': 0, 'total_signed_extrinsics': 0, 'total_events': 0, 'total_events_module': 0, 'total_blocks': 'N/A', 'total_accounts': 0, 'total_runtimes': 0 } }, ) self.cache_region.set(cache_key, response) resp.set_header('X-Cache', 'MISS') else: resp.set_header('X-Cache', 'HIT') resp.media = response
def start_sequencer(self): # Start sequencer max_sequenced_block_id = self.session.query(func.max( BlockTotal.id)).one()[0] if max_sequenced_block_id is not None: sequencer_parent_block = BlockTotal.query( self.session).filter_by(id=max_sequenced_block_id).first() parent_block = Block.query( self.session).filter_by(id=max_sequenced_block_id).first() sequence_block_recursive.delay( parent_block_data=parent_block.asdict(), parent_sequenced_block_data=sequencer_parent_block.asdict()) else: sequence_block_recursive.delay(parent_block_data=None)
def start_sequencer(self): integrity_status = self.integrity_checks() self.db_session.commit() block_nr = None integrity_head = Status.get_status(self.db_session, 'INTEGRITY_HEAD') if not integrity_head.value: integrity_head.value = 0 # 3. Check sequence head sequencer_head = self.db_session.query(func.max(BlockTotal.id)).one()[0] if sequencer_head is None: sequencer_head = -1 # Start sequencing process sequencer_parent_block = BlockTotal.query(self.db_session).filter_by(id=sequencer_head).first() parent_block = Block.query(self.db_session).filter_by(id=sequencer_head).first() for block_nr in range(sequencer_head + 1, int(integrity_head.value) + 1): if block_nr == 0: # No block ever sequenced, check if chain is at genesis state assert (not sequencer_parent_block) block = Block.query(self.db_session).order_by('id').first() if not block: self.db_session.commit() return {'error': 'Chain not at genesis'} if block.id == 1: # Add genesis block block = self.add_block(block.parent_hash) if block.id != 0: self.db_session.commit() return {'error': 'Chain not at genesis'} self.process_genesis(block) sequencer_parent_block_data = None parent_block_data = None else: block_id = sequencer_parent_block.id + 1 assert (block_id == block_nr) block = Block.query(self.db_session).get(block_nr) if not block: self.db_session.commit() return {'result': 'Finished at #{}'.format(sequencer_parent_block.id)} sequencer_parent_block_data = sequencer_parent_block.asdict() parent_block_data = parent_block.asdict() sequenced_block = self.sequence_block(block, parent_block_data, sequencer_parent_block_data) self.db_session.commit() parent_block = block sequencer_parent_block = sequenced_block if block_nr: return {'result': 'Finished at #{}'.format(block_nr)} else: return {'result': 'Nothing to sequence'}
def get_query(self): return BlockTotal.query(self.session).order_by(BlockTotal.id.desc())
def get_item(self, item_id): return BlockTotal.query(self.session).get(item_id)
def on_post(self, req, resp): blockHash = None if req.media.get('block_id'): substrate = SubstrateInterface(url=SUBSTRATE_RPC_URL, address_type=SUBSTRATE_ADDRESS_TYPE, type_registry_preset=TYPE_REGISTRY) blockHash = substrate.get_block_hash(req.media.get('block_id')) elif req.media.get('block_hash'): blockHash = req.media.get('block_hash') else: resp.status = falcon.HTTP_BAD_REQUEST resp.media = { 'errors': ['Either blockHash or block_id should be supplied'] } if blockHash: resp.status = falcon.HTTP_200 block = Block.query( self.session).filter(Block.hash == blockHash).first() blockTotal = BlockTotal.query( self.session).filter(BlockTotal.id == block.id).first() author = ss58_encode( blockTotal.author.replace('0x', '') ) if blockTotal is not None and blockTotal.author is not None else None if block: blockInfo = {} blockInfo["timestamp"] = block.datetime.strftime( "%Y-%m-%d %H:%M:%S") blockInfo["block_hash"] = block.hash blockInfo["block_id"] = block.id blockInfo["parent_id"] = block.id - 1 if block.id > 0 else 0 blockInfo["child_id"] = block.id + 1 blockInfo["parent_hash"] = block.parent_hash blockInfo["state_root"] = block.state_root blockInfo["extrinsic_root"] = block.extrinsics_root blockInfo["validator"] = author blockInfo["count_extrinsic"] = block.count_extrinsics blockInfo["count_event"] = block.count_events blockInfo["count_log"] = block.count_log # blockInfo["age"] = time.mktime(block.datetime.timetuple()) blockInfo["age"] = block.datetime.strftime("%Y-%m-%d %H:%M:%S") # 获取和区块相关的交易信息 extrinsics = Extrinsic.query( self.session).filter(Extrinsic.block_id == block.id).all() extrinsicsObj = [ { "extrinsic_id": '{}-{}'.format(block.id, extrinsic.extrinsic_idx), "hash": extrinsic.extrinsic_hash if extrinsic.extrinsic_hash else None, # "age": time.mktime(block.datetime.timetuple()), "age": block.datetime.strftime("%Y-%m-%d %H:%M:%S"), "result": extrinsic.success, # "address": extrinsic.address if extrinsic.address else None, # "module": extrinsic.module_id, # "fee": None, # "nonce": extrinsic.nonce if extrinsic.nonce else None, # "call": extrinsic.call_id, "operation": '{}({})'.format(extrinsic.module_id, extrinsic.call_id), "params": extrinsic.params # "signature": extrinsic.signature if extrinsic.signature else None } for extrinsic in extrinsics ] # 获取和区块相关的日志信息 logs = Log.query( self.session).filter(Log.block_id == block.id).all() logsObj = [{ "log_id": '{}-{}'.format(block.id, log.log_idx), "block_id": block.id, "type": log.type, "data": log.data['value'] } for log in logs] # 获取和区块相关的事件信息 events = Event.query( self.session).filter(Event.block_id == block.id).all() eventObj = [{ "id": '{}-{}'.format(block.id, event.event_idx), "block_id": block.id, "block_hash": block.hash, "module_id": event.module_id, "event_id": event.event_id, "attributes": event.attributes, "operation": '{}({})'.format(event.module_id, event.event_id), "desc": self.getEventDesc(event.module_id, event.event_id), "hash": self.getEventHash(block.id, event.extrinsic_idx) } for event in events] resp.media = { 'status': 'success', 'data': { "block_info": blockInfo, "extrinsics": extrinsicsObj, "logs": logsObj, "events": eventObj } } else: resp.status = falcon.HTTP_404 resp.media = {'result': 'Block not found'}