Skip to content

Commit

Permalink
Merge pull request #3037 from christophsturm/inject-clock
Browse files Browse the repository at this point in the history
inject clock for usage in isDateInTolerance
  • Loading branch information
sqrrm authored Jul 30, 2019
2 parents 7acee4f + c38ef06 commit 360dc0b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
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
4 changes: 3 additions & 1 deletion core/src/main/java/bisq/core/account/sign/SignedWitness.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

import org.bitcoinj.core.Coin;

import java.time.Clock;

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

Expand Down Expand Up @@ -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;
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);
}

0 comments on commit 360dc0b

Please sign in to comment.