From a6b8723ebe4fe7d4b9409795dc82765055ca1d17 Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 19 Jan 2024 10:43:29 -0500 Subject: [PATCH] persist wallet creation date when created --- core/src/main/java/haveno/core/user/User.java | 9 ++++++++ .../java/haveno/core/user/UserPayload.java | 5 +++- .../core/xmr/wallet/XmrWalletService.java | 23 ++++++++++++++++++- proto/src/main/proto/pb.proto | 1 + 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/haveno/core/user/User.java b/core/src/main/java/haveno/core/user/User.java index 4a5e6cd56f..d73cad7936 100644 --- a/core/src/main/java/haveno/core/user/User.java +++ b/core/src/main/java/haveno/core/user/User.java @@ -374,6 +374,11 @@ public class User implements PersistedDataHost { requestPersistence(); } + public void setWalletCreationDate(long walletCreationDate) { + userPayload.setWalletCreationDate(walletCreationDate); + requestPersistence(); + } + /////////////////////////////////////////////////////////////////////////////////////////// // Getters /////////////////////////////////////////////////////////////////////////////////////////// @@ -521,4 +526,8 @@ public class User implements PersistedDataHost { public Cookie getCookie() { return userPayload.getCookie(); } + + public long getWalletCreationDate() { + return userPayload.getWalletCreationDate(); + } } diff --git a/core/src/main/java/haveno/core/user/UserPayload.java b/core/src/main/java/haveno/core/user/UserPayload.java index 539b2e5247..26f81579e8 100644 --- a/core/src/main/java/haveno/core/user/UserPayload.java +++ b/core/src/main/java/haveno/core/user/UserPayload.java @@ -82,6 +82,7 @@ public class UserPayload implements PersistableEnvelope { // Generic map for persisting various UI states. We keep values un-typed as string to // provide sufficient flexibility. private Cookie cookie = new Cookie(); + private long walletCreationDate; public UserPayload() { } @@ -122,6 +123,7 @@ public class UserPayload implements PersistableEnvelope { .ifPresent(e -> builder.addAllAcceptedRefundAgents(ProtoUtil.collectionToProto(acceptedRefundAgents, message -> ((protobuf.StoragePayload) message).getRefundAgent()))); Optional.ofNullable(cookie).ifPresent(e -> builder.putAllCookie(cookie.toProtoMessage())); + builder.setWalletCreationDate(walletCreationDate); return protobuf.PersistableEnvelope.newBuilder().setUserPayload(builder).build(); } @@ -153,7 +155,8 @@ public class UserPayload implements PersistableEnvelope { proto.getAcceptedRefundAgentsList().isEmpty() ? new ArrayList<>() : new ArrayList<>(proto.getAcceptedRefundAgentsList().stream() .map(RefundAgent::fromProto) .collect(Collectors.toList())), - Cookie.fromProto(proto.getCookieMap()) + Cookie.fromProto(proto.getCookieMap()), + proto.getWalletCreationDate() ); } } diff --git a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java index d8ea75e524..28091c4945 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -20,6 +20,7 @@ import haveno.core.trade.MakerTrade; import haveno.core.trade.Trade; import haveno.core.trade.TradeManager; import haveno.core.user.Preferences; +import haveno.core.user.User; import haveno.core.xmr.listeners.XmrBalanceListener; import haveno.core.xmr.model.XmrAddressEntry; import haveno.core.xmr.model.XmrAddressEntryList; @@ -60,6 +61,9 @@ import org.slf4j.LoggerFactory; import javax.inject.Inject; import java.io.File; import java.math.BigInteger; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -109,6 +113,7 @@ public class XmrWalletService { private static final boolean PRINT_STACK_TRACE = false; private static final String THREAD_ID = XmrWalletService.class.getSimpleName(); + private final User user; private final Preferences preferences; private final CoreAccountService accountService; private final XmrConnectionService xmrConnectionService; @@ -135,13 +140,15 @@ public class XmrWalletService { private TaskLooper syncLooper = null; @Inject - XmrWalletService(Preferences preferences, + XmrWalletService(User user, + Preferences preferences, CoreAccountService accountService, XmrConnectionService xmrConnectionService, WalletsSetup walletsSetup, XmrAddressEntryList xmrAddressEntryList, @Named(Config.WALLET_DIR) File walletDir, @Named(Config.WALLET_RPC_BIND_PORT) int rpcBindPort) { + this.user = user; this.preferences = preferences; this.accountService = accountService; this.xmrConnectionService = xmrConnectionService; @@ -203,6 +210,15 @@ public class XmrWalletService { return wallet; } + /** + * Get the wallet creation date in seconds since epoch. + * + * @return the wallet creation date in seconds since epoch + */ + public long getWalletCreationDate() { + return user.getWalletCreationDate(); + } + public void saveMainWallet() { saveMainWallet(true); } @@ -787,6 +803,11 @@ public class XmrWalletService { wallet = openWalletRpc(walletConfig, rpcBindPort, isProxyApplied(wasWalletSynced)); } else if (xmrConnectionService.getConnection() != null && Boolean.TRUE.equals(xmrConnectionService.getConnection().isConnected())) { wallet = createWalletRpc(walletConfig, rpcBindPort); + + // set wallet creation date to yesterday to guarantee complete restore + LocalDateTime localDateTime = LocalDate.now().atStartOfDay().minusDays(1); + long date = localDateTime.toEpochSecond(ZoneOffset.UTC); + user.setWalletCreationDate(date); } } diff --git a/proto/src/main/proto/pb.proto b/proto/src/main/proto/pb.proto index 5fdad9f283..558e40052d 100644 --- a/proto/src/main/proto/pb.proto +++ b/proto/src/main/proto/pb.proto @@ -1756,6 +1756,7 @@ message UserPayload { repeated RefundAgent accepted_refund_agents = 14; RefundAgent registered_refund_agent = 15; map cookie = 16; + int64 wallet_creation_date = 17; } message BlockChainExplorer {