Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inject clock for usage in isDateInTolerance #3037

Merged
merged 1 commit into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/src/main/java/bisq/core/CoreModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/bisq/core/account/sign/SignedWitness.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.util.Utilities;


import com.google.protobuf.ByteString;

import org.bitcoinj.core.Coin;

import java.time.Clock;

import java.util.Date;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -122,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

import com.google.protobuf.ByteString;

import java.time.Clock;

import java.util.Date;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -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;
Expand Down
11 changes: 8 additions & 3 deletions p2p/src/main/java/bisq/network/p2p/storage/P2PDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -118,7 +120,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers

private final Set<AppendOnlyDataStoreListener> appendOnlyDataStoreListeners = new CopyOnWriteArraySet<>();
private final Set<ProtectedDataStoreListener> protectedDataStoreListeners = new CopyOnWriteArraySet<>();

private final Clock clock;

///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
Expand All @@ -130,11 +132,14 @@ public P2PDataStorage(NetworkNode networkNode,
AppendOnlyDataStoreService appendOnlyDataStoreService,
ProtectedDataStoreService protectedDataStoreService,
ResourceDataStoreService resourceDataStoreService,
Storage<SequenceNumberMap> sequenceNumberMapStorage) {
Storage<SequenceNumberMap> sequenceNumberMapStorage,
Clock clock) {
this.broadcaster = broadcaster;
this.appendOnlyDataStoreService = appendOnlyDataStoreService;
this.protectedDataStoreService = protectedDataStoreService;
this.resourceDataStoreService = resourceDataStoreService;
this.clock = clock;


networkNode.addMessageListener(this);
networkNode.addConnectionListener(this);
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}