From 22c43769bd7f68aa251a8aedcee59be0d713137f Mon Sep 17 00:00:00 2001 From: HenrikJannsen Date: Mon, 21 Nov 2022 19:12:28 -0500 Subject: [PATCH] Add BurningManAccountingService to CorePersistedDataHost Signed-off-by: HenrikJannsen --- .../BurningManAccountingStoreService.java | 92 +++++++++++++++++++ .../core/setup/CorePersistedDataHost.java | 2 + 2 files changed, 94 insertions(+) create mode 100644 core/src/main/java/bisq/core/dao/burningman/accounting/storage/BurningManAccountingStoreService.java diff --git a/core/src/main/java/bisq/core/dao/burningman/accounting/storage/BurningManAccountingStoreService.java b/core/src/main/java/bisq/core/dao/burningman/accounting/storage/BurningManAccountingStoreService.java new file mode 100644 index 0000000000..ec650d9774 --- /dev/null +++ b/core/src/main/java/bisq/core/dao/burningman/accounting/storage/BurningManAccountingStoreService.java @@ -0,0 +1,92 @@ +/* + * 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 . + */ + +package bisq.core.dao.burningman.accounting.storage; + +import bisq.core.dao.burningman.accounting.blockchain.AccountingBlock; + +import bisq.common.persistence.PersistenceManager; +import bisq.common.proto.persistable.PersistedDataHost; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Singleton +public class BurningManAccountingStoreService implements PersistedDataHost { + private final PersistenceManager persistenceManager; + private final BurningManAccountingStore burningManAccountingStore = new BurningManAccountingStore(new ArrayList<>()); + + @Inject + public BurningManAccountingStoreService(PersistenceManager persistenceManager) { + this.persistenceManager = persistenceManager; + + this.persistenceManager.initialize(burningManAccountingStore, PersistenceManager.Source.PRIVATE_LOW_PRIO); + } + + + /////////////////////////////////////////////////////////////////////////////////////////// + // PersistedDataHost implementation + /////////////////////////////////////////////////////////////////////////////////////////// + + @Override + public void readPersisted(Runnable completeHandler) { + persistenceManager.readPersisted(persisted -> { + burningManAccountingStore.getBlocks().addAll(persisted.getBlocks()); + completeHandler.run(); + }, + completeHandler); + } + + + /////////////////////////////////////////////////////////////////////////////////////////// + // API + /////////////////////////////////////////////////////////////////////////////////////////// + + public void requestPersistence() { + persistenceManager.requestPersistence(); + } + + public List getBlocks() { + return Collections.unmodifiableList(burningManAccountingStore.getBlocks()); + } + + public void addBlock(AccountingBlock block) { + burningManAccountingStore.getBlocks().add(block); + requestPersistence(); + } + + public void purgeLastTenBlocks() { + List blocks = burningManAccountingStore.getBlocks(); + if (blocks.size() <= 10) { + blocks.clear(); + requestPersistence(); + return; + } + + List purged = new ArrayList<>(blocks.subList(0, blocks.size() - 10)); + blocks.clear(); + blocks.addAll(purged); + requestPersistence(); + } +} diff --git a/core/src/main/java/bisq/core/setup/CorePersistedDataHost.java b/core/src/main/java/bisq/core/setup/CorePersistedDataHost.java index 98e171b426..ee145e3edb 100644 --- a/core/src/main/java/bisq/core/setup/CorePersistedDataHost.java +++ b/core/src/main/java/bisq/core/setup/CorePersistedDataHost.java @@ -18,6 +18,7 @@ package bisq.core.setup; import bisq.core.btc.model.AddressEntryList; +import bisq.core.dao.burningman.accounting.storage.BurningManAccountingStoreService; import bisq.core.dao.governance.ballot.BallotListService; import bisq.core.dao.governance.blindvote.MyBlindVoteListService; import bisq.core.dao.governance.bond.reputation.MyReputationListService; @@ -80,6 +81,7 @@ public class CorePersistedDataHost { persistedDataHosts.add(injector.getInstance(MyReputationListService.class)); persistedDataHosts.add(injector.getInstance(MyProofOfBurnListService.class)); persistedDataHosts.add(injector.getInstance(UnconfirmedBsqChangeOutputListService.class)); + persistedDataHosts.add(injector.getInstance(BurningManAccountingStoreService.class)); return persistedDataHosts; } }