Skip to content

Commit bc7b5d5

Browse files
committed
Refactored sync method.
1 parent 033141f commit bc7b5d5

File tree

4 files changed

+78
-50
lines changed

4 files changed

+78
-50
lines changed

src/main/java/co/rsk/federate/btcreleaseclient/BtcPegoutClientStorageAccessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class BtcPegoutClientStorageAccessor {
2828
private final int delayInMs;
2929
// Delay writing to avoid slowing down operations
3030
private final ScheduledExecutorService writeTimer;
31-
private ScheduledFuture task;
31+
private ScheduledFuture<?> task;
3232
private int delays;
3333

3434
public BtcPegoutClientStorageAccessor(FedNodeSystemProperties systemProperties) throws InvalidStorageFileException {
@@ -63,7 +63,7 @@ public BtcPegoutClientStorageAccessor(
6363
throw new InvalidStorageFileException(message, e);
6464
}
6565
}
66-
if (!readResult.getSuccess()) {
66+
if (Boolean.FALSE.equals(readResult.getSuccess())) {
6767
String message = "Error reading storage file for BtcPegoutClient";
6868
logger.error(message);
6969
throw new InvalidStorageFileException(message);

src/main/java/co/rsk/federate/btcreleaseclient/BtcPegoutClientStorageSynchronizer.java

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/main/java/co/rsk/federate/io/btcreleaseclientstorage/BtcPegoutClientFileStorageImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
import java.util.HashMap;
1010
import java.util.Map;
1111
import java.util.Optional;
12+
1213
import org.apache.commons.io.FileUtils;
1314
import org.ethereum.util.RLP;
1415
import org.ethereum.util.RLPElement;
1516
import org.ethereum.util.RLPList;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
1619

1720
public class BtcPegoutClientFileStorageImpl implements BtcPegoutClientFileStorage {
1821

22+
private static final Logger logger = LoggerFactory.getLogger(BtcPegoutClientFileStorageImpl.class);
1923
private final FileStorageInfo storageInfo;
2024

2125
public BtcPegoutClientFileStorageImpl(FileStorageInfo storageInfo) {
@@ -87,6 +91,7 @@ private BtcPegoutClientFileReadResult readFromRlp(byte[] fileData) {
8791
}
8892
}
8993
} catch (Exception e) {
94+
logger.error("[readFromRlp] error trying to file data.", e);
9095
return new BtcPegoutClientFileReadResult(Boolean.FALSE, null);
9196
}
9297

src/main/java/co/rsk/federate/signing/hsm/requirements/PegoutSigningRequirementsEnforcer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private void enforcePegoutRequirements(PegoutCreationInformation pegoutCreationI
3030
ancestorBlockUpdater.ensureAncestorBlockInPosition(pegoutCreationInformation.getPegoutCreationRskBlock());
3131
} catch (Exception e) {
3232
String message = "error trying to enforce ancestor";
33-
logger.error("[enforce]" + message, e);
33+
logger.error("[enforce] " + message, e);
3434
throw new PegoutSigningRequirementsEnforcerException(message, e);
3535
}
3636
}

0 commit comments

Comments
 (0)