Skip to content

Commit

Permalink
Cap items if requesterVersion is newer in HistoricalDataStoreService.…
Browse files Browse the repository at this point in the history
…getMapSinceVersion(...)
  • Loading branch information
alvasw committed Dec 21, 2024
1 parent e15a34f commit 97330ba
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ void testRequesterVersionIsOlder() {
}

@Test
void testRequesterVersionIsNewer() {
void testRequesterHasHistoricalDataStores() {
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap = storageService.getStore().getMap();
DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessToMap(liveDataMap);
DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessesToMap(liveDataMap, 150);

AccountAgeWitnessStore firstVersionStore = DummyAccountAgeWitnessFactory.
createAccountAgeWitnessStore(1, 100);
AccountAgeWitnessStore secondVersionStore = DummyAccountAgeWitnessFactory.
createAccountAgeWitnessStore(1, 200);
AccountAgeWitnessStore secondVersionStore = DummyAccountAgeWitnessFactory.
createAccountAgeWitnessStore(1, 300);

Map<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> storeByVersion = Map.of(
"1.8.0", firstVersionStore,
Expand All @@ -180,6 +180,38 @@ void testRequesterVersionIsNewer() {
assertThat(mapSinceVersion, is(expected));
}

@Test
void testRequesterVersionIsNewerCapped() {
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap = storageService.getStore().getMap();
DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessesToMap(liveDataMap, 150);

AccountAgeWitnessStore firstVersionStore = DummyAccountAgeWitnessFactory
.createAccountAgeWitnessStore(1, 200);
AccountAgeWitnessStore secondVersionStore = DummyAccountAgeWitnessFactory
.createAccountAgeWitnessStore(1, 300);

Map<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> storeByVersion = Map.of(
"1.8.0", firstVersionStore,
"1.9.0", secondVersionStore);

storageService.setStoresByVersion(storeByVersion);

String version = Version.VERSION;
String higherVersion = Version.getMajorVersion(version) + "." +
Version.getMinorVersion(version) + "." + (Version.getPatchVersion(version) + 1);

Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> mapSinceVersion =
storageService.getMapSinceVersion(higherVersion, Collections.emptySet());

int mapSize = mapSinceVersion.size();
assertThat(mapSize, is(100));

for (Map.Entry<P2PDataStorage.ByteArray, PersistableNetworkPayload> entry : mapSinceVersion.entrySet()) {
var expected = liveDataMap.get(entry.getKey());
assertThat(entry.getValue(), is(expected));
}
}

@Test
void testKnownHashes() {
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap = storageService.getStore().getMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -72,6 +71,7 @@ public Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> getMapSinceVersi
Stream<Map.Entry<P2PDataStorage.ByteArray, PersistableNetworkPayload>> liveDataStream =
store.getMap().entrySet().stream();


if (requestersVersion == null) {
log.warn("The requester did not send a version but the field was added in v1.4.0. " +
"Returning capped live data (100 items).");
Expand All @@ -81,6 +81,15 @@ public Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> getMapSinceVersi
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

boolean isRequesterVersionNewer = Version.isNewVersion(requestersVersion, Version.VERSION);
if (isRequesterVersionNewer) {
log.warn("The requester's version is newer than ours. Returning capped live data (100 items).");

return liveDataStream
.limit(100)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

// If we have a store with a newer version than the requesters version we will add those as well.
Stream<Map.Entry<P2PDataStorage.ByteArray, PersistableNetworkPayload>> entryStream =
storesByVersion.entrySet().stream()
Expand Down

0 comments on commit 97330ba

Please sign in to comment.