Skip to content

Commit

Permalink
Sync with develop
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Ananev <[email protected]>
  • Loading branch information
artemananiev committed Nov 30, 2023
2 parents 5ce2bb3 + f2aac3a commit 2730acb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class OwnershipTrackerTest {
Expand All @@ -29,12 +28,6 @@ class OwnershipTrackerTest {
private final Id account = new Id(4, 5, 6);
private final Id token = new Id(0, 0, 1);

@BeforeEach
void setup() {
// setup the getter/setter test
subject = new OwnershipTracker();
}

@Test
void add() {
final var burn = OwnershipTracker.forRemoving(treasury, 1L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ void objectContractWorks() {
assertEquals(1, subj.getSerialNumber());
assertEquals(Id.DEFAULT, subj.getTokenId());

var metadata = new byte[] {107, 117, 114};
subj = new UniqueToken(Id.DEFAULT, 1, RichInstant.MISSING_INSTANT, new Id(1, 2, 3), new byte[] {111, 23, 85});
assertEquals(RichInstant.MISSING_INSTANT, subj.getCreationTime());
assertEquals(new Id(1, 2, 3), subj.getOwner());
subj.setSerialNumber(2);
assertEquals(2, subj.getSerialNumber());

metadata = new byte[] {1, 2, 3};
var metadata = new byte[] {1, 2, 3};
subj.setMetadata(metadata);
assertEquals(metadata, subj.getMetadata());
subj.setTokenId(Id.DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.swirlds.common.test.fixtures.RandomUtils.getRandomPrintSeed;
import static com.swirlds.common.test.fixtures.RandomUtils.randomHash;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
Expand Down Expand Up @@ -84,6 +85,10 @@ class OrphanBufferTests {
*/
private static final int MAX_GENERATION_STEP = 10;

private AtomicLong eventsExitedIntakePipeline;

private OrphanBuffer orphanBuffer;

/**
* Create a bootstrap event for a node. This is just a descriptor, and will never be received from intake.
*
Expand Down Expand Up @@ -223,7 +228,7 @@ private EventDescriptor chooseOtherParent(@NonNull final List<EventDescriptor> p
void setup() {
random = getRandomPrintSeed();

final ArrayList<EventDescriptor> parentCandidates = new ArrayList<>();
final List<EventDescriptor> parentCandidates = new ArrayList<>();
final Map<NodeId, EventDescriptor> tips = new HashMap<>();

intakeEvents = new ArrayList<>();
Expand All @@ -234,14 +239,10 @@ void setup() {
parentCandidates.add(newEvent.getDescriptor());
intakeEvents.add(newEvent);
}
}

@Test
@DisplayName("Test standard orphan buffer operation")
void standardOperation() {
final AtomicLong minimumGenerationNonAncient = new AtomicLong(0);
Collections.shuffle(intakeEvents, random);

final AtomicLong eventsExitedIntakePipeline = new AtomicLong(0);
eventsExitedIntakePipeline = new AtomicLong(0);
final IntakeEventCounter intakeEventCounter = mock(IntakeEventCounter.class);
doAnswer(invocation -> {
eventsExitedIntakePipeline.incrementAndGet();
Expand All @@ -250,8 +251,13 @@ void standardOperation() {
.when(intakeEventCounter)
.eventExitedIntakePipeline(any());

final OrphanBuffer orphanBuffer =
new OrphanBuffer(TestPlatformContextBuilder.create().build(), intakeEventCounter);
orphanBuffer = new OrphanBuffer(TestPlatformContextBuilder.create().build(), intakeEventCounter);
}

@Test
@DisplayName("Test standard orphan buffer operation")
void standardOperation() {
long minimumGenerationNonAncient = 0;

// increase minimum generation non-ancient at the approximate rate that event generations are increasing
// this means that roughly half of the events will be ancient before they are received from intake
Expand All @@ -260,7 +266,6 @@ void standardOperation() {
// events that have been emitted from the orphan buffer
final Collection<Hash> emittedEvents = new HashSet<>();

Collections.shuffle(intakeEvents, random);
for (final GossipEvent intakeEvent : intakeEvents) {
final List<GossipEvent> unorphanedEvents = new ArrayList<>();

Expand All @@ -269,12 +274,12 @@ void standardOperation() {
// add some randomness to step size, so minimumGenerationNonAncient doesn't always just increase by 1
final int stepRandomness = Math.round(random.nextFloat() * MAX_GENERATION_STEP);
if (random.nextFloat() < averageGenerationAdvancement / stepRandomness) {
minimumGenerationNonAncient.addAndGet(stepRandomness);
unorphanedEvents.addAll(orphanBuffer.setMinimumGenerationNonAncient(minimumGenerationNonAncient.get()));
minimumGenerationNonAncient += stepRandomness;
unorphanedEvents.addAll(orphanBuffer.setMinimumGenerationNonAncient(minimumGenerationNonAncient));
}

for (final GossipEvent unorphanedEvent : unorphanedEvents) {
assertValidParents(unorphanedEvent, minimumGenerationNonAncient.get(), emittedEvents);
assertValidParents(unorphanedEvent, minimumGenerationNonAncient, emittedEvents);
emittedEvents.add(unorphanedEvent.getHashedData().getHash());
}
}
Expand All @@ -284,4 +289,49 @@ void standardOperation() {
assertEquals(TEST_EVENT_COUNT, eventsExitedIntakePipeline.get() + emittedEvents.size());
assertEquals(0, orphanBuffer.getCurrentOrphanCount());
}

@Test
@DisplayName("Test pausing orphan buffer")
void pause() {
// cause the bootstrap events to become ancient
orphanBuffer.setMinimumGenerationNonAncient(1);

// events that have been emitted from the orphan buffer
final Collection<Hash> emittedEvents = new HashSet<>();

// handle half of the events. this will result in many events being in the orphan buffer
final int halfEventCount = intakeEvents.size() / 2;
for (int i = 0; i < halfEventCount; i++) {
final GossipEvent intakeEvent = intakeEvents.get(i);

final List<GossipEvent> unorphanedEvents = new ArrayList<>();

unorphanedEvents.addAll(orphanBuffer.handleEvent(intakeEvent));

for (final GossipEvent unorphanedEvent : unorphanedEvents) {
assertValidParents(unorphanedEvent, 1, emittedEvents);
emittedEvents.add(unorphanedEvent.getHashedData().getHash());
}
}

final long eventsExitedPipelineBeforePause = eventsExitedIntakePipeline.get();

orphanBuffer.setPaused(true);
final List<GossipEvent> noEvents = orphanBuffer.setMinimumGenerationNonAncient(maxGeneration);
assertEquals(0, noEvents.size(), "A paused orphan buffer shouldn't emit events upon generation change");
assertEquals(
eventsExitedPipelineBeforePause,
eventsExitedIntakePipeline.get(),
"A paused orphan buffer shouldn't cause any events to exit the intake pipeline upon generation change");
orphanBuffer.setPaused(false);

orphanBuffer.setMinimumGenerationNonAncient(maxGeneration + 1);
assertNotEquals(
eventsExitedPipelineBeforePause,
eventsExitedIntakePipeline.get(),
"An unpaused orphan buffer with large generation change should cause many events to exit the intake pipeline");

assertEquals(halfEventCount, eventsExitedIntakePipeline.get() + emittedEvents.size());
assertEquals(0, orphanBuffer.getCurrentOrphanCount());
}
}

0 comments on commit 2730acb

Please sign in to comment.