package Ġio . nova foundation . nova . runtime . multi Network . chain Ċ Ċ import Ġcom . google . gson . Gson Ċ import Ġio . nova foundation . nova . common . utils . Collection Differ Ċ import Ġio . nova foundation . nova . common . utils . Ident ifiable Ċ import Ġio . nova foundation . nova . common . utils . map Ċ import Ġio . nova foundation . nova . common . utils . retry Until Done Ċ import Ġio . nova foundation . nova . core _ db . dao . Chain Dao Ċ import Ġio . nova foundation . nova . runtime . multi Network . chain . model . Chain Ċ import Ġio . nova foundation . nova . runtime . multi Network . chain . remote . Chain Fetcher Ċ import Ġkotlinx . coroutines . Dispatch ers Ċ import Ġkotlinx . coroutines . with Context Ċ Ċ class ĠChain Sync Service ( ĊĠĠĠ Ġprivate Ġval Ġdao : ĠChain Dao , ĊĠĠĠ Ġprivate Ġval Ġchain Fetcher : ĠChain Fetcher , ĊĠĠĠ Ġprivate Ġval Ġgson : ĠGson Ċ ) Ġ{ ĊĊĠĠĠ Ġsuspend Ġfun Ġsync Up () Ġ= Ġwith Context ( Dispatch ers . Default ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġlocal Ch ains Joined Info Ġ= Ġdao . get Join Chain Info () ĊĊĠĠĠĠĠĠĠ Ġval Ġremote Ch ains Ġ= Ġretry Until Done Ġ{ Ġchain Fetcher . get Ch ains () Ġ}. map (:: map Chain Remote To Chain ) ĊĠĠĠĠĠĠĠ Ġval Ġlocal Ch ains Ġ= Ġlocal Ch ains Joined Info . map Ġ{ Ġmap Chain Local To Chain ( it , Ġgson ) Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġchains Diff Ġ= ĠCollection Differ . find Diff ( new Items Ġ= Ġremote Ch ains , Ġold Items Ġ= Ġlocal Ch ains , Ġforce Use New Items Ġ= Ġfalse ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ Ġmap Chain To Chain Local ( it , Ġgson ) Ġ} ĊĊĠĠĠĠĠĠĠ Ġdao . apply Diff ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġchain Diff Ġ= Ġchains Diff , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġassets Diff Ġ= Ġnested Collection Diff ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġnew Ch ains Ġ= Ġremote Ch ains , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġold Ch ains Ġ= Ġlocal Ch ains , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcollection Ġ= ĠChain :: assets , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġdomain ToLocal Mapper Ġ= Ġ{ Ġmap Chain Asset ToLocal ( it , Ġgson ) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġnodes Diff Ġ= Ġnested Collection Diff ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġnew Ch ains Ġ= Ġremote Ch ains , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġold Ch ains Ġ= Ġlocal Ch ains , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcollection Ġ= ĠChain :: nodes , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġdomain ToLocal Mapper Ġ= Ġ:: map Chain Node ToLocal ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġexplor ers Diff Ġ= Ġnested Collection Diff ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġnew Ch ains Ġ= Ġremote Ch ains , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġold Ch ains Ġ= Ġlocal Ch ains , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcollection Ġ= ĠChain :: ex plor ers , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġdomain ToLocal Mapper Ġ= Ġ:: map Chain Ex plor ers ToLocal ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ), ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġ< T Ġ: ĠIdent ifiable , ĠR > Ġnested Collection Diff ( ĊĠĠĠĠĠĠĠ Ġnew Ch ains : ĠList < Chain >, ĊĠĠĠĠĠĠĠ Ġold Ch ains : ĠList < Chain >, ĊĠĠĠĠĠĠĠ Ġcollection : Ġ( Chain ) Ġ-> ĠList < T >, ĊĠĠĠĠĠĠĠ Ġdomain ToLocal Mapper : Ġ( T ) Ġ-> ĠR ĊĠĠĠ Ġ): ĠCollection Differ . Diff < R > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġold Ġ= Ġold Ch ains . flatMap ( collection ) ĊĠĠĠĠĠĠĠ Ġval Ġnew Ġ= Ġnew Ch ains . flatMap ( collection ) ĊĊĠĠĠĠĠĠĠ Ġval Ġdiff ed Ġ= ĠCollection Differ . find Diff ( new Items Ġ= Ġnew , Ġold Items Ġ= Ġold , Ġforce Use New Items Ġ= Ġfalse ) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġdiff ed . map ( domain ToLocal Mapper ) ĊĠĠĠ Ġ} Ċ } Ċ