@@ -320,7 +320,7 @@ def sync_level(self) -> Optional[int]:
320320 @property
321321 def block (self ) -> HeadBlockData :
322322 if self ._block is None :
323- raise RuntimeError ('No message from ` head` channel received ' )
323+ raise RuntimeError ('Attempt to access head block before the first message ' )
324324 return self ._block
325325
326326 async def get_similar_contracts (self , address : str , strict : bool = False ) -> List [str ]:
@@ -633,87 +633,57 @@ def _default_http_config(self) -> HTTPConfig:
633633 connection_limit = 25 ,
634634 )
635635
636- async def _on_operation_message (self , message : List [Dict [str , Any ]]) -> None :
637- """Parse and emit raw operations from WS"""
636+ async def _extract_message_data (self , channel : str , message : List [Any ]) -> Any :
638637 for item in message :
639- current_level = item ['state' ]
638+ head_level = item ['state' ]
640639 message_type = TzktMessageType (item ['type' ])
641- self ._logger .info ( 'Got operation message, %s, level %s' , message_type , current_level )
640+ self ._logger .debug ( '`%s` message: %s' , channel , message_type . name )
642641
643642 if message_type == TzktMessageType .STATE :
644- self ._sync_level = current_level
645- self ._level = current_level
643+ if self ._sync_level != head_level :
644+ self ._logger .info ('Datasource level set to %s' , head_level )
645+ self ._sync_level = head_level
646+ self ._level = head_level
646647
647648 elif message_type == TzktMessageType .DATA :
648- self ._level = current_level
649- operations = []
650- for operation_json in item ['data' ]:
651- operation = self .convert_operation (operation_json )
652- if operation .status != 'applied' :
653- continue
654- operations .append (operation )
655- if operations :
656- self .emit_operations (operations , self .block )
649+ self ._level = head_level
650+ yield item ['data' ]
657651
658652 elif message_type == TzktMessageType .REORG :
659653 if self .level is None :
660654 raise RuntimeError
661- self .emit_rollback (self .level , current_level )
655+ self .emit_rollback (self .level , head_level )
662656
663657 else :
664658 raise NotImplementedError
665659
666- async def _on_big_map_message (self , message : List [Dict [str , Any ]]) -> None :
667- """Parse and emit raw big map diffs from WS"""
668- for item in message :
669- current_level = item ['state' ]
670- message_type = TzktMessageType (item ['type' ])
671- self ._logger .info ('Got big map message, %s, level %s' , message_type , current_level )
672-
673- if message_type == TzktMessageType .STATE :
674- self ._sync_level = current_level
675- self ._level = current_level
676-
677- elif message_type == TzktMessageType .DATA :
678- self ._level = current_level
679- big_maps = []
680- for big_map_json in item ['data' ]:
681- big_map = self .convert_big_map (big_map_json )
682- big_maps .append (big_map )
683- self .emit_big_maps (big_maps )
684660
685- elif message_type == TzktMessageType .REORG :
686- if self .level is None :
687- raise RuntimeError
688- self .emit_rollback (self .level , current_level )
661+ async def _on_operation_message (self , message : List [Dict [str , Any ]]) -> None :
662+ """Parse and emit raw operations from WS"""
663+ async for data in self ._extract_message_data ('operation' , message ):
664+ operations = []
665+ for operation_json in data :
666+ operation = self .convert_operation (operation_json )
667+ if operation .status != 'applied' :
668+ continue
669+ operations .append (operation )
670+ if operations :
671+ self .emit_operations (operations , self .block )
689672
690- else :
691- raise NotImplementedError
673+ async def _on_big_map_message (self , message : List [Dict [str , Any ]]) -> None :
674+ """Parse and emit raw big map diffs from WS"""
675+ async for data in self ._extract_message_data ('big_map' , message ):
676+ big_maps = []
677+ for big_map_json in data :
678+ big_map = self .convert_big_map (big_map_json )
679+ big_maps .append (big_map )
680+ self .emit_big_maps (big_maps )
692681
693682 async def _on_head_message (self , message : List [Dict [str , Any ]]) -> None :
694- for item in message :
695- current_level = item ['state' ]
696- message_type = TzktMessageType (item ['type' ])
697- self ._logger .info ('Got block message, %s, level %s' , message_type , current_level )
698-
699- if message_type == TzktMessageType .STATE :
700- self ._sync_level = current_level
701- self ._level = current_level
702-
703- elif message_type == TzktMessageType .DATA :
704- self ._level = current_level
705- block_json = item ['data' ]
706- block = self .convert_head_block (block_json )
707- self ._block = block
708- self .emit_head (block )
709-
710- elif message_type == TzktMessageType .REORG :
711- if self .level is None :
712- raise RuntimeError
713- self .emit_rollback (self .level , current_level )
714-
715- else :
716- raise NotImplementedError
683+ async for data in self ._extract_message_data ('head' , message ):
684+ block = self .convert_head_block (data )
685+ self ._block = block
686+ self .emit_head (block )
717687
718688 @classmethod
719689 def convert_operation (cls , operation_json : Dict [str , Any ]) -> OperationData :
0 commit comments