forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix BurningManAccountingStore data races
Multiple threads read and write to the accounting blocks list causing data races. Luckily, the LinkedList threw a ConcurrentModificationException to limit damage. Now, a ReadWriteLock protects the LinkedList against data races. Multiple threads can read the list at the same time but only one thread can write to it. Other writing threads wait until it's their turn. Fixes bisq-network#6545
- Loading branch information
Showing
5 changed files
with
154 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
core/src/main/java/bisq/core/dao/burningman/accounting/storage/AccountingBlockIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* This file is part of Bisq. | ||
* | ||
* Bisq is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* Bisq is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bisq.core.dao.burningman.accounting.storage; | ||
|
||
import bisq.core.dao.burningman.accounting.blockchain.AccountingBlock; | ||
|
||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.locks.Lock; | ||
|
||
public class AccountingBlockIterator implements Iterator<Optional<AccountingBlock>> { | ||
|
||
private final Lock readLock; | ||
private final LinkedList<AccountingBlock> blocks; | ||
private AtomicInteger currentIndex = new AtomicInteger(0); | ||
|
||
public AccountingBlockIterator(Lock readLock, LinkedList<AccountingBlock> blocks) { | ||
this.readLock = readLock; | ||
this.blocks = blocks; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
readLock.lock(); | ||
try { | ||
return currentIndex.get() < blocks.size() - 1; | ||
} finally { | ||
readLock.unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<AccountingBlock> next() { | ||
readLock.lock(); | ||
try { | ||
if (currentIndex.get() < blocks.size() - 1) { | ||
int i = currentIndex.getAndIncrement(); | ||
AccountingBlock accountingBlock = blocks.get(i); | ||
return Optional.of(accountingBlock); | ||
|
||
} else { | ||
return Optional.empty(); | ||
} | ||
} finally { | ||
readLock.unlock(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters