mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-23 15:00:30 +01:00
Merge pull request #3037 from christophsturm/inject-clock
inject clock for usage in isDateInTolerance
This commit is contained in:
commit
360dc0b4e8
5 changed files with 20 additions and 6 deletions
|
@ -68,6 +68,8 @@ import org.springframework.core.env.Environment;
|
|||
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 @@ public class CoreModule extends AppModule {
|
|||
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);
|
||||
|
||||
|
|
|
@ -33,6 +33,8 @@ import com.google.protobuf.ByteString;
|
|||
|
||||
import org.bitcoinj.core.Coin;
|
||||
|
||||
import java.time.Clock;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
@ -121,7 +123,7 @@ public class SignedWitness implements LazyProcessedPayload, PersistableNetworkPa
|
|||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@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;
|
||||
|
|
|
@ -30,6 +30,8 @@ import bisq.common.util.Utilities;
|
|||
|
||||
import com.google.protobuf.ByteString;
|
||||
|
||||
import java.time.Clock;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
@ -87,7 +89,7 @@ public class AccountAgeWitness implements LazyProcessedPayload, PersistableNetwo
|
|||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@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;
|
||||
|
|
|
@ -73,6 +73,8 @@ import org.bouncycastle.util.encoders.Hex;
|
|||
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<AppendOnlyDataStoreListener> appendOnlyDataStoreListeners = new CopyOnWriteArraySet<>();
|
||||
private final Set<ProtectedDataStoreListener> protectedDataStoreListeners = new CopyOnWriteArraySet<>();
|
||||
|
||||
private final Clock clock;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Constructor
|
||||
|
@ -130,11 +132,14 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
|
|||
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);
|
||||
|
@ -309,7 +314,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
|
|||
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));
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue