diff --git a/core/src/main/java/bisq/core/CoreModule.java b/core/src/main/java/bisq/core/CoreModule.java index fa4d8795155..654cbdede34 100644 --- a/core/src/main/java/bisq/core/CoreModule.java +++ b/core/src/main/java/bisq/core/CoreModule.java @@ -68,6 +68,8 @@ import com.google.inject.Singleton; import com.google.inject.name.Names; +import java.time.Clock; + import java.io.File; import static com.google.inject.name.Names.named; @@ -97,6 +99,7 @@ protected void configure() { bind(BridgeAddressProvider.class).to(Preferences.class).in(Singleton.class); bind(CorruptedDatabaseFilesHandler.class).in(Singleton.class); bind(AvoidStandbyModeService.class).in(Singleton.class); + bind(Clock.class).toInstance(Clock.systemDefaultZone()); bind(SeedNodeRepository.class).to(DefaultSeedNodeRepository.class).in(Singleton.class); diff --git a/core/src/main/java/bisq/core/account/sign/SignedWitness.java b/core/src/main/java/bisq/core/account/sign/SignedWitness.java index 588e399e3f5..e397ea6f010 100644 --- a/core/src/main/java/bisq/core/account/sign/SignedWitness.java +++ b/core/src/main/java/bisq/core/account/sign/SignedWitness.java @@ -33,6 +33,8 @@ import org.bitcoinj.core.Coin; +import java.time.Clock; + import java.util.Date; import java.util.concurrent.TimeUnit; @@ -121,7 +123,7 @@ public static SignedWitness fromProto(protobuf.SignedWitness proto) { /////////////////////////////////////////////////////////////////////////////////////////// @Override - public boolean isDateInTolerance() { + public boolean isDateInTolerance(Clock clock) { // We don't allow older or newer then 1 day. // Preventing forward dating is also important to protect against a sophisticated attack return Math.abs(new Date().getTime() - date) <= TOLERANCE; diff --git a/core/src/main/java/bisq/core/account/witness/AccountAgeWitness.java b/core/src/main/java/bisq/core/account/witness/AccountAgeWitness.java index c4588bdebd7..d3a29a03680 100644 --- a/core/src/main/java/bisq/core/account/witness/AccountAgeWitness.java +++ b/core/src/main/java/bisq/core/account/witness/AccountAgeWitness.java @@ -30,6 +30,8 @@ import com.google.protobuf.ByteString; +import java.time.Clock; + import java.util.Date; import java.util.concurrent.TimeUnit; @@ -87,7 +89,7 @@ public static AccountAgeWitness fromProto(protobuf.AccountAgeWitness proto) { /////////////////////////////////////////////////////////////////////////////////////////// @Override - public boolean isDateInTolerance() { + public boolean isDateInTolerance(Clock clock) { // We don't allow older or newer then 1 day. // Preventing forward dating is also important to protect against a sophisticated attack return Math.abs(new Date().getTime() - date) <= TOLERANCE; diff --git a/p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java b/p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java index 2e64b11aa62..6be3ae8cb1b 100644 --- a/p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java +++ b/p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java @@ -73,6 +73,8 @@ import java.security.KeyPair; import java.security.PublicKey; +import java.time.Clock; + import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -118,7 +120,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers private final Set appendOnlyDataStoreListeners = new CopyOnWriteArraySet<>(); private final Set protectedDataStoreListeners = new CopyOnWriteArraySet<>(); - + private final Clock clock; /////////////////////////////////////////////////////////////////////////////////////////// // Constructor @@ -130,11 +132,14 @@ public P2PDataStorage(NetworkNode networkNode, AppendOnlyDataStoreService appendOnlyDataStoreService, ProtectedDataStoreService protectedDataStoreService, ResourceDataStoreService resourceDataStoreService, - Storage sequenceNumberMapStorage) { + Storage sequenceNumberMapStorage, + Clock clock) { this.broadcaster = broadcaster; this.appendOnlyDataStoreService = appendOnlyDataStoreService; this.protectedDataStoreService = protectedDataStoreService; this.resourceDataStoreService = resourceDataStoreService; + this.clock = clock; + networkNode.addMessageListener(this); networkNode.addConnectionListener(this); @@ -309,7 +314,7 @@ public boolean addPersistableNetworkPayload(PersistableNetworkPayload payload, final ByteArray hashAsByteArray = new ByteArray(hash); boolean containsKey = getAppendOnlyDataStoreMap().containsKey(hashAsByteArray); if (!containsKey || reBroadcast) { - if (!(payload instanceof DateTolerantPayload) || !checkDate || ((DateTolerantPayload) payload).isDateInTolerance()) { + if (!(payload instanceof DateTolerantPayload) || !checkDate || ((DateTolerantPayload) payload).isDateInTolerance(clock)) { if (!containsKey) { appendOnlyDataStoreService.put(hashAsByteArray, payload); appendOnlyDataStoreListeners.forEach(e -> e.onAdded(payload)); diff --git a/p2p/src/main/java/bisq/network/p2p/storage/payload/DateTolerantPayload.java b/p2p/src/main/java/bisq/network/p2p/storage/payload/DateTolerantPayload.java index e8b831eb67a..7a5c0f41a35 100644 --- a/p2p/src/main/java/bisq/network/p2p/storage/payload/DateTolerantPayload.java +++ b/p2p/src/main/java/bisq/network/p2p/storage/payload/DateTolerantPayload.java @@ -17,10 +17,12 @@ package bisq.network.p2p.storage.payload; +import java.time.Clock; + /** * Interface for PersistableNetworkPayload which only get added if the date is inside a tolerance range. * Used for AccountAgeWitness. */ public interface DateTolerantPayload extends PersistableNetworkPayload { - boolean isDateInTolerance(); + boolean isDateInTolerance(Clock clock); }