Check accountWitness date only if we receive it from other peers in the P2P network

This commit is contained in:
Manfred Karrer 2017-10-21 23:05:52 -05:00
parent 7c78db5101
commit 14bb75ded5
No known key found for this signature in database
GPG Key ID: 401250966A6B2C46
6 changed files with 22 additions and 13 deletions

View File

@ -79,6 +79,8 @@ public class AccountAgeWitness implements LazyProcessedPayload, PersistableNetwo
@Override
public boolean isDateInTolerance() {
// 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;
}

View File

@ -280,7 +280,7 @@ public class AccountAgeWitnessService {
ErrorMessageHandler errorMessageHandler) {
// Check if trade date in witness is not older than the release date of that feature (was added in v0.6)
// TODO set date before releasing
if (!isTradeDateAfterReleaseDate(witness.getDate(), new GregorianCalendar(2017, GregorianCalendar.OCTOBER, 17).getTime(), errorMessageHandler))
if (!isDateAfterReleaseDate(witness.getDate(), new GregorianCalendar(2017, GregorianCalendar.OCTOBER, 17).getTime(), errorMessageHandler))
return false;
final byte[] peersAccountInputDataWithSalt = Utilities.concatenateByteArrays(peersPaymentAccountPayload.getAgeWitnessInputData(), peersPaymentAccountPayload.getSalt());
@ -307,7 +307,7 @@ public class AccountAgeWitnessService {
// Package scope verification subroutines
///////////////////////////////////////////////////////////////////////////////////////////
boolean isTradeDateAfterReleaseDate(long witnessDateAsLong, Date ageWitnessReleaseDate, ErrorMessageHandler errorMessageHandler) {
boolean isDateAfterReleaseDate(long witnessDateAsLong, Date ageWitnessReleaseDate, ErrorMessageHandler errorMessageHandler) {
// Release date minus 1 day as tolerance for not synced clocks
Date releaseDateWithTolerance = new Date(ageWitnessReleaseDate.getTime() - TimeUnit.DAYS.toMillis(1));
final Date witnessDate = new Date(witnessDateAsLong);
@ -334,6 +334,7 @@ public class AccountAgeWitnessService {
return result;
}
// TODO use peers local date and check tolerance
private boolean verifyPeersTradeLimit(Offer offer,
PaymentAccountPayload paymentAccountPayload,
ErrorMessageHandler errorMessageHandler) {

View File

@ -60,15 +60,20 @@ public class AccountAgeWitnessServiceTest {
public void testIsTradeDateAfterReleaseDate() throws CryptoException {
Date ageWitnessReleaseDate = new GregorianCalendar(2017, 9, 23).getTime();
Date tradeDate = new GregorianCalendar(2017, 10, 1).getTime();
assertTrue(service.isTradeDateAfterReleaseDate(tradeDate.getTime(), ageWitnessReleaseDate, errorMessage -> {}));
assertTrue(service.isDateAfterReleaseDate(tradeDate.getTime(), ageWitnessReleaseDate, errorMessage -> {
}));
tradeDate = new GregorianCalendar(2017, 9, 23).getTime();
assertTrue(service.isTradeDateAfterReleaseDate(tradeDate.getTime(), ageWitnessReleaseDate, errorMessage -> {}));
assertTrue(service.isDateAfterReleaseDate(tradeDate.getTime(), ageWitnessReleaseDate, errorMessage -> {
}));
tradeDate = new GregorianCalendar(2017, 9, 22, 0, 0, 1).getTime();
assertTrue(service.isTradeDateAfterReleaseDate(tradeDate.getTime(), ageWitnessReleaseDate, errorMessage -> {}));
assertTrue(service.isDateAfterReleaseDate(tradeDate.getTime(), ageWitnessReleaseDate, errorMessage -> {
}));
tradeDate = new GregorianCalendar(2017, 9, 22).getTime();
assertFalse(service.isTradeDateAfterReleaseDate(tradeDate.getTime(), ageWitnessReleaseDate, errorMessage -> {}));
assertFalse(service.isDateAfterReleaseDate(tradeDate.getTime(), ageWitnessReleaseDate, errorMessage -> {
}));
tradeDate = new GregorianCalendar(2017, 9, 21).getTime();
assertFalse(service.isTradeDateAfterReleaseDate(tradeDate.getTime(), ageWitnessReleaseDate, errorMessage -> {}));
assertFalse(service.isDateAfterReleaseDate(tradeDate.getTime(), ageWitnessReleaseDate, errorMessage -> {
}));
}
@Test

View File

@ -789,7 +789,7 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
///////////////////////////////////////////////////////////////////////////////////////////
public boolean addPersistableNetworkPayload(PersistableNetworkPayload payload, boolean reBroadcast) {
return p2PDataStorage.addPersistableNetworkPayload(payload, networkNode.getNodeAddress(), true, true, reBroadcast);
return p2PDataStorage.addPersistableNetworkPayload(payload, networkNode.getNodeAddress(), true, true, reBroadcast, false);
}
public boolean addData(ProtectedStoragePayload protectedStoragePayload, boolean isDataOwner) {

View File

@ -242,7 +242,7 @@ public class RequestDataHandler implements MessageListener {
processDelayedItems.add(e);
} else {
// We dont broadcast here as we are only connected to the seed node and would be pointless
dataStorage.addPersistableNetworkPayload(e, sender, false, false, false);
dataStorage.addPersistableNetworkPayload(e, sender, false, false, false, false);
}
});
}
@ -272,7 +272,7 @@ public class RequestDataHandler implements MessageListener {
if (item instanceof ProtectedStorageEntry)
dataStorage.add((ProtectedStorageEntry) item, sender, null, false, false);
else if (item instanceof PersistableNetworkPayload)
dataStorage.addPersistableNetworkPayload((PersistableNetworkPayload) item, sender, false, false, false);
dataStorage.addPersistableNetworkPayload((PersistableNetworkPayload) item, sender, false, false, false, false);
}), delay, TimeUnit.MILLISECONDS);
}

View File

@ -245,7 +245,7 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
refreshTTL((RefreshOfferMessage) networkEnvelop, peersNodeAddress, false);
} else if (networkEnvelop instanceof AddPersistableNetworkPayloadMessage) {
addPersistableNetworkPayload(((AddPersistableNetworkPayloadMessage) networkEnvelop).getPersistableNetworkPayload(),
peersNodeAddress, false, true, false);
peersNodeAddress, false, true, false, true);
}
});
}
@ -321,12 +321,13 @@ public class P2PDataStorage implements MessageListener, ConnectionListener, Pers
@Nullable NodeAddress sender,
boolean isDataOwner,
boolean allowBroadcast,
boolean reBroadcast) {
boolean reBroadcast,
boolean checkDate) {
log.debug("addPersistableNetworkPayload payload={}", payload);
final ByteArray hashAsByteArray = new ByteArray(payload.getHash());
boolean containsKey = persistableNetworkPayloadCollection.getMap().containsKey(hashAsByteArray);
if (!containsKey || reBroadcast) {
if (!(payload instanceof DateTolerantPayload) || ((DateTolerantPayload) payload).isDateInTolerance()) {
if (!(payload instanceof DateTolerantPayload) || !checkDate || ((DateTolerantPayload) payload).isDateInTolerance()) {
if (!containsKey) {
persistableNetworkPayloadCollection.getMap().put(hashAsByteArray, payload);
persistableNetworkPayloadMapStorage.queueUpForSave(persistableNetworkPayloadCollection, 2000);