mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-24 15:10:44 +01:00
inject clock for usage in isDateInTolerance
This commit is contained in:
parent
3f5b0ca72d
commit
c38ef06d04
5 changed files with 20 additions and 7 deletions
|
@ -68,6 +68,8 @@ import org.springframework.core.env.Environment;
|
||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
import com.google.inject.name.Names;
|
import com.google.inject.name.Names;
|
||||||
|
|
||||||
|
import java.time.Clock;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import static com.google.inject.name.Names.named;
|
import static com.google.inject.name.Names.named;
|
||||||
|
@ -97,6 +99,7 @@ public class CoreModule extends AppModule {
|
||||||
bind(BridgeAddressProvider.class).to(Preferences.class).in(Singleton.class);
|
bind(BridgeAddressProvider.class).to(Preferences.class).in(Singleton.class);
|
||||||
bind(CorruptedDatabaseFilesHandler.class).in(Singleton.class);
|
bind(CorruptedDatabaseFilesHandler.class).in(Singleton.class);
|
||||||
bind(AvoidStandbyModeService.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);
|
bind(SeedNodeRepository.class).to(DefaultSeedNodeRepository.class).in(Singleton.class);
|
||||||
|
|
||||||
|
|
|
@ -29,11 +29,12 @@ import bisq.common.crypto.Hash;
|
||||||
import bisq.common.proto.persistable.PersistableEnvelope;
|
import bisq.common.proto.persistable.PersistableEnvelope;
|
||||||
import bisq.common.util.Utilities;
|
import bisq.common.util.Utilities;
|
||||||
|
|
||||||
|
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
|
|
||||||
import org.bitcoinj.core.Coin;
|
import org.bitcoinj.core.Coin;
|
||||||
|
|
||||||
|
import java.time.Clock;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@ -122,7 +123,7 @@ public class SignedWitness implements LazyProcessedPayload, PersistableNetworkPa
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isDateInTolerance() {
|
public boolean isDateInTolerance(Clock clock) {
|
||||||
// We don't allow older or newer then 1 day.
|
// We don't allow older or newer then 1 day.
|
||||||
// Preventing forward dating is also important to protect against a sophisticated attack
|
// Preventing forward dating is also important to protect against a sophisticated attack
|
||||||
return Math.abs(new Date().getTime() - date) <= TOLERANCE;
|
return Math.abs(new Date().getTime() - date) <= TOLERANCE;
|
||||||
|
|
|
@ -30,6 +30,8 @@ import bisq.common.util.Utilities;
|
||||||
|
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
|
|
||||||
|
import java.time.Clock;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@ -87,7 +89,7 @@ public class AccountAgeWitness implements LazyProcessedPayload, PersistableNetwo
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isDateInTolerance() {
|
public boolean isDateInTolerance(Clock clock) {
|
||||||
// We don't allow older or newer then 1 day.
|
// We don't allow older or newer then 1 day.
|
||||||
// Preventing forward dating is also important to protect against a sophisticated attack
|
// Preventing forward dating is also important to protect against a sophisticated attack
|
||||||
return Math.abs(new Date().getTime() - date) <= TOLERANCE;
|
return Math.abs(new Date().getTime() - date) <= TOLERANCE;
|
||||||
|
|
|
@ -73,6 +73,8 @@ import org.bouncycastle.util.encoders.Hex;
|
||||||
import java.security.KeyPair;
|
import java.security.KeyPair;
|
||||||
import java.security.PublicKey;
|
import java.security.PublicKey;
|
||||||
|
|
||||||
|
import java.time.Clock;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
@ -118,7 +120,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
|
||||||
|
|
||||||
private final Set<AppendOnlyDataStoreListener> appendOnlyDataStoreListeners = new CopyOnWriteArraySet<>();
|
private final Set<AppendOnlyDataStoreListener> appendOnlyDataStoreListeners = new CopyOnWriteArraySet<>();
|
||||||
private final Set<ProtectedDataStoreListener> protectedDataStoreListeners = new CopyOnWriteArraySet<>();
|
private final Set<ProtectedDataStoreListener> protectedDataStoreListeners = new CopyOnWriteArraySet<>();
|
||||||
|
private final Clock clock;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
|
@ -130,11 +132,14 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
|
||||||
AppendOnlyDataStoreService appendOnlyDataStoreService,
|
AppendOnlyDataStoreService appendOnlyDataStoreService,
|
||||||
ProtectedDataStoreService protectedDataStoreService,
|
ProtectedDataStoreService protectedDataStoreService,
|
||||||
ResourceDataStoreService resourceDataStoreService,
|
ResourceDataStoreService resourceDataStoreService,
|
||||||
Storage<SequenceNumberMap> sequenceNumberMapStorage) {
|
Storage<SequenceNumberMap> sequenceNumberMapStorage,
|
||||||
|
Clock clock) {
|
||||||
this.broadcaster = broadcaster;
|
this.broadcaster = broadcaster;
|
||||||
this.appendOnlyDataStoreService = appendOnlyDataStoreService;
|
this.appendOnlyDataStoreService = appendOnlyDataStoreService;
|
||||||
this.protectedDataStoreService = protectedDataStoreService;
|
this.protectedDataStoreService = protectedDataStoreService;
|
||||||
this.resourceDataStoreService = resourceDataStoreService;
|
this.resourceDataStoreService = resourceDataStoreService;
|
||||||
|
this.clock = clock;
|
||||||
|
|
||||||
|
|
||||||
networkNode.addMessageListener(this);
|
networkNode.addMessageListener(this);
|
||||||
networkNode.addConnectionListener(this);
|
networkNode.addConnectionListener(this);
|
||||||
|
@ -309,7 +314,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
|
||||||
final ByteArray hashAsByteArray = new ByteArray(hash);
|
final ByteArray hashAsByteArray = new ByteArray(hash);
|
||||||
boolean containsKey = getAppendOnlyDataStoreMap().containsKey(hashAsByteArray);
|
boolean containsKey = getAppendOnlyDataStoreMap().containsKey(hashAsByteArray);
|
||||||
if (!containsKey || reBroadcast) {
|
if (!containsKey || reBroadcast) {
|
||||||
if (!(payload instanceof DateTolerantPayload) || !checkDate || ((DateTolerantPayload) payload).isDateInTolerance()) {
|
if (!(payload instanceof DateTolerantPayload) || !checkDate || ((DateTolerantPayload) payload).isDateInTolerance(clock)) {
|
||||||
if (!containsKey) {
|
if (!containsKey) {
|
||||||
appendOnlyDataStoreService.put(hashAsByteArray, payload);
|
appendOnlyDataStoreService.put(hashAsByteArray, payload);
|
||||||
appendOnlyDataStoreListeners.forEach(e -> e.onAdded(payload));
|
appendOnlyDataStoreListeners.forEach(e -> e.onAdded(payload));
|
||||||
|
|
|
@ -17,10 +17,12 @@
|
||||||
|
|
||||||
package bisq.network.p2p.storage.payload;
|
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.
|
* Interface for PersistableNetworkPayload which only get added if the date is inside a tolerance range.
|
||||||
* Used for AccountAgeWitness.
|
* Used for AccountAgeWitness.
|
||||||
*/
|
*/
|
||||||
public interface DateTolerantPayload extends PersistableNetworkPayload {
|
public interface DateTolerantPayload extends PersistableNetworkPayload {
|
||||||
boolean isDateInTolerance();
|
boolean isDateInTolerance(Clock clock);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue