Skip to content

Commit

Permalink
Merge pull request #7047 from stejbac/fix-remaining-accounting-store-…
Browse files Browse the repository at this point in the history
…data-race

Fix remaining data race in BurningManAccountingStore
  • Loading branch information
alejandrogarcia83 authored Feb 25, 2024
2 parents 42d40e5 + 0214566 commit f941ef3
Showing 1 changed file with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package bisq.core.dao.burningman.accounting.storage;


import bisq.core.dao.burningman.accounting.blockchain.AccountingBlock;
import bisq.core.dao.burningman.accounting.exceptions.BlockHashNotConnectingException;
import bisq.core.dao.burningman.accounting.exceptions.BlockHeightNotConnectingException;
Expand Down Expand Up @@ -75,7 +74,9 @@ public void purgeLastTenBlocks() {
Lock writeLock = readWriteLock.writeLock();
writeLock.lock();
try {
purgeLast10Blocks();
for (int i = 0; i < 10 && !blocks.isEmpty(); i++) {
blocks.removeLast();
}
} finally {
writeLock.unlock();
}
Expand Down Expand Up @@ -148,21 +149,18 @@ private void tryToAddNewBlock(AccountingBlock newBlock) throws BlockHeightNotCon
}
}

private void purgeLast10Blocks() {
if (blocks.size() <= 10) {
blocks.clear();
return;
}

List<AccountingBlock> purged = new ArrayList<>(blocks.subList(0, blocks.size() - 10));
blocks.clear();
blocks.addAll(purged);
}

public Message toProtoMessage() {
List<AccountingBlock> blocksCopy;
Lock readLock = readWriteLock.readLock();
readLock.lock();
try {
blocksCopy = new ArrayList<>(blocks);
} finally {
readLock.unlock();
}
return protobuf.PersistableEnvelope.newBuilder()
.setBurningManAccountingStore(protobuf.BurningManAccountingStore.newBuilder()
.addAllBlocks(blocks.stream()
.addAllBlocks(blocksCopy.stream()
.map(AccountingBlock::toProtoMessage)
.collect(Collectors.toList())))
.build();
Expand Down

0 comments on commit f941ef3

Please sign in to comment.