improve trade state reliability

update trade state atomically on UserThread
nullify error handling on deposits confirmed message
set trade state before deposit request and relay
add checks before deleting wallet
UserThread.await() detects if on UserThread
This commit is contained in:
woodser 2023-12-24 12:09:53 -05:00
parent 3de4264c4b
commit 6c2f3ea154
7 changed files with 65 additions and 28 deletions

View file

@ -45,6 +45,7 @@ public class UserThread {
@Getter
@Setter
private static Executor executor;
private static final String USER_THREAD_NAME = "UserThread";
public static void setTimerClass(Class<? extends Timer> timerClass) {
UserThread.timerClass = timerClass;
@ -57,22 +58,31 @@ public class UserThread {
}
public static void execute(Runnable command) {
UserThread.executor.execute(command);
UserThread.executor.execute(() -> {
Thread.currentThread().setName(USER_THREAD_NAME);
command.run();
});
}
public static void await(Runnable command) {
CountDownLatch latch = new CountDownLatch(1);
executor.execute(() -> {
if (isUserThread(Thread.currentThread())) {
command.run();
latch.countDown();
});
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} else {
CountDownLatch latch = new CountDownLatch(1);
execute(command); // run task
execute(() -> latch.countDown()); // await next tick
try {
latch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public static boolean isUserThread(Thread thread) {
return USER_THREAD_NAME.equals(thread.getName());
}
// Prefer FxTimer if a delay is needed in a JavaFx class (gui module)
public static Timer runAfterRandomDelay(Runnable runnable, long minDelayInSec, long maxDelayInSec) {
return UserThread.runAfterRandomDelay(runnable, minDelayInSec, maxDelayInSec, TimeUnit.SECONDS);