@@ -93,63 +93,29 @@ private void sync() {
9393
9494 try {
9595 Optional <Keccak256 > storageBestBlockHash = this .storageAccessor .getBestBlockHash ();
96+
9697 Block storageBestBlock = null ;
9798 if (storageBestBlockHash .isPresent ()) {
98- storageBestBlock = blockStore .getBlockByHash (storageBestBlockHash .get ().getBytes ());
99- if (storageBestBlock == null ) {
100- logger .warn (
101- "BtcPegoutClientStorage best block hash doesn't exist in blockchain. {}" ,
102- storageBestBlockHash .get ()
103- );
104- } else {
105- Block blockInMainchain = blockStore .getChainBlockByNumber (storageBestBlock .getNumber ());
106- if (blockInMainchain == null ||
107- !blockInMainchain .getHash ().equals (storageBestBlockHash .get ())
108- ) {
109- logger .warn (
110- "BtcPegoutClientStorage best block hash doesn't belong to mainchain. ({})" ,
111- storageBestBlockHash
112- );
113- storageBestBlock = null ;
114- logger .info ("refreshing file" );
115- }
116- }
99+ storageBestBlock = getStorageBestBlock (storageBestBlockHash .get ());
117100 } else {
118- logger .info ("no data in file" );
101+ logger .info ("[sync] no data in file" );
119102 }
120103
121- Block blockToSearch = storageBestBlock ;
122- // If there is no data in the file, set a limit to avoid looking up all the blockchain
123- if (storageBestBlock == null ) {
124- long lastBlockNumberToSearch = blockStore .getBestBlock ().getNumber () - this .maxInitializationDepth ;
125- blockToSearch = blockStore .getChainBlockByNumber (Math .max (lastBlockNumberToSearch , 0 ));
126- } else {
127- if (blockToSearch .getNumber () == blockStore .getBestBlock ().getNumber ()) {
128- logger .info ("[sync] Storage already on sync" );
129- this .isSynced = true ;
130- this .syncTimer .shutdown ();
131- return ;
132- }
133- blockToSearch = blockStore .getChainBlockByNumber (blockToSearch .getNumber () + 1 );
104+ if (isStorageSync (storageBestBlock )) {
105+ logger .info ("[sync] Storage already on sync" );
106+ return ;
134107 }
135108
109+ Block fromBlock = findBlockToSearch (storageBestBlock );
110+
136111 logger .info (
137- "going to sync from block {} ({})" ,
138- blockToSearch .getNumber (),
139- blockToSearch .getHash ()
112+ "[sync] going to sync from block {} ({})" ,
113+ fromBlock .getNumber (),
114+ fromBlock .getHash ()
140115 );
141116
142- while (blockToSearch != null && blockStore .getBestBlock ().getNumber () >= blockToSearch .getNumber ()) {
143- logger .trace ("[sync] going to fetch block {}({})" , blockToSearch .getNumber (), blockToSearch .getHash ());
144- List <TransactionReceipt > receipts = new ArrayList <>();
145- for (Transaction transaction : blockToSearch .getTransactionsList ()) {
146- TransactionReceipt receipt = receiptStore .getInMainChain (transaction .getHash ().getBytes (), blockStore ).orElseThrow (NullPointerException ::new ).getReceipt ();
147- receipt .setTransaction (transaction );
148- receipts .add (receipt );
149- }
150- checkLogsForReleaseRequested (blockToSearch , receipts );
151- blockToSearch = blockStore .getChainBlockByNumber (blockToSearch .getNumber () + 1 );
152- }
117+ fetchNewBlocks (fromBlock );
118+
153119 logger .info (
154120 "[sync] Finished sync, storage has {} elements, and its best block is {}" ,
155121 storageAccessor .getMapSize (),
@@ -162,6 +128,63 @@ private void sync() {
162128 }
163129 }
164130
131+ private void fetchNewBlocks (Block blockToSearch ) {
132+ Block blockToFetch = blockToSearch ;
133+ while (blockToFetch != null && blockStore .getBestBlock ().getNumber () >= blockToFetch .getNumber ()) {
134+ logger .trace ("[sync] going to fetch block {}({})" , blockToFetch .getNumber (), blockToFetch .getHash ());
135+ List <TransactionReceipt > receipts = new ArrayList <>();
136+ for (Transaction transaction : blockToFetch .getTransactionsList ()) {
137+ TransactionReceipt receipt = receiptStore .getInMainChain (transaction .getHash ().getBytes (), blockStore ).orElseThrow (NullPointerException ::new ).getReceipt ();
138+ receipt .setTransaction (transaction );
139+ receipts .add (receipt );
140+ }
141+ checkLogsForReleaseRequested (blockToFetch , receipts );
142+ blockToFetch = blockStore .getChainBlockByNumber (blockToFetch .getNumber () + 1 );
143+ }
144+ }
145+
146+ private boolean isStorageSync (Block storageBestBlock ) {
147+ if (storageBestBlock != null && storageBestBlock .getNumber () == blockStore .getBestBlock ().getNumber ()) {
148+ this .isSynced = true ;
149+ this .syncTimer .shutdown ();
150+ return true ;
151+ }
152+ return false ;
153+ }
154+
155+ private Block findBlockToSearch (Block storageBestBlock ) {
156+ /* If there is no data in the file, set a limit to avoid looking up all the blockchain */
157+ if (storageBestBlock == null ) {
158+ long lastBlockNumberToSearch = blockStore .getBestBlock ().getNumber () - this .maxInitializationDepth ;
159+ return blockStore .getChainBlockByNumber (Math .max (lastBlockNumberToSearch , 0 ));
160+ } else {
161+ return blockStore .getChainBlockByNumber (storageBestBlock .getNumber () + 1 );
162+ }
163+ }
164+
165+ private Block getStorageBestBlock (Keccak256 storageBestBlockHash ) {
166+ Block storageBestBlock = blockStore .getBlockByHash (storageBestBlockHash .getBytes ());
167+ if (storageBestBlock == null ) {
168+ logger .warn (
169+ "[sync] BtcPegoutClientStorage best block hash doesn't exist in blockchain. {}" ,
170+ storageBestBlockHash
171+ );
172+ } else {
173+ Block blockInMainchain = blockStore .getChainBlockByNumber (storageBestBlock .getNumber ());
174+ if (blockInMainchain == null ||
175+ !blockInMainchain .getHash ().equals (storageBestBlockHash )
176+ ) {
177+ logger .warn (
178+ "[sync] BtcPegoutClientStorage best block hash doesn't belong to mainchain. ({})" ,
179+ storageBestBlockHash
180+ );
181+ storageBestBlock = null ;
182+ logger .info ("refreshing file" );
183+ }
184+ }
185+ return storageBestBlock ;
186+ }
187+
165188 private void checkLogsForReleaseRequested (Block block , List <TransactionReceipt > receipts ) {
166189 for (TransactionReceipt receipt : receipts ) {
167190 List <LogInfo > matches = receipt
0 commit comments