Skip to content

Commit

Permalink
#191: add assetions
Browse files Browse the repository at this point in the history
  • Loading branch information
eustimenko committed Sep 20, 2019
1 parent 8578fe8 commit 55ba795
Showing 1 changed file with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.ethereum.beacon.core.BeaconState;
import org.ethereum.beacon.core.state.Eth1Data;
import org.ethereum.beacon.core.types.BLSSignature;
import org.ethereum.beacon.core.types.SlotNumber;
import org.ethereum.beacon.core.types.Time;
import org.ethereum.beacon.db.Database;
import org.ethereum.beacon.schedulers.ControlledSchedulers;
Expand All @@ -38,6 +39,8 @@
import java.util.Collections;
import java.util.stream.IntStream;

import static org.assertj.core.api.Assertions.assertThat;

public class DefaultBeaconChainTest {

@Test
Expand Down Expand Up @@ -126,6 +129,7 @@ public BeaconStateEx apply(BeaconStateEx stateEx) {
@Test
public void testRejectBlocks_future() {
ControlledSchedulers schedulers = Schedulers.createControlled();
long currentTime = schedulers.getCurrentTime();

BeaconChainSpec spec =
BeaconChainSpec.Builder.createWithDefaultParams()
Expand All @@ -140,18 +144,31 @@ public void testRejectBlocks_future() {
BeaconTuple initialTuple = beaconChain.getRecentlyProcessed();
Assert.assertEquals(spec.getConstants().getGenesisSlot(), initialTuple.getBlock().getSlot());

BeaconTuple recentlyProcessed = beaconChain.getRecentlyProcessed();
BeaconTuple parent = beaconChain.getRecentlyProcessed();

schedulers.addTime(Duration.of(1, ChronoUnit.DAYS));
BeaconBlock aBlock =
createBlock(recentlyProcessed, spec, schedulers.getCurrentTime(), perSlotTransition);
createBlock(parent, spec, schedulers.getCurrentTime(), perSlotTransition);

// assert block.slot <= get_current_slot(store.time) + 1
BeaconState state = perSlotTransition.apply(new BeaconStateExImpl(parent.getState()));
SlotNumber currentSlot = spec.get_current_slot(parent.getState(), schedulers.getCurrentTime());
SlotNumber nextToCurrentSlot = spec.get_current_slot(state, currentTime).increment();
final boolean actual = nextToCurrentSlot.greaterEqual(currentSlot);

// assert store.time >= pre_state.genesis_time + block.slot * SECONDS_PER_SLOT
final long expectedTime = parent.getState().getGenesisTime().longValue() + currentSlot.longValue() * spec.getConstants().getSecondsPerSlot().longValue();
final boolean expected = currentTime >= expectedTime;

assertThat(actual).isNotEqualTo(expected);

Assert.assertEquals(ImportResult.ExpiredBlock, beaconChain.insert(aBlock));
}

@Test
public void testRejectBlocks_past() {
ControlledSchedulers schedulers = Schedulers.createControlled();
long currentTime = schedulers.getCurrentTime();

BeaconChainSpec spec =
BeaconChainSpec.Builder.createWithDefaultParams()
Expand All @@ -166,10 +183,22 @@ public void testRejectBlocks_past() {
BeaconTuple initialTuple = beaconChain.getRecentlyProcessed();
Assert.assertEquals(spec.getConstants().getGenesisSlot(), initialTuple.getBlock().getSlot());

BeaconTuple recentlyProcessed = beaconChain.getRecentlyProcessed();
BeaconTuple parent = beaconChain.getRecentlyProcessed();

BeaconBlock aBlock =
createBlock(recentlyProcessed, spec, schedulers.getCurrentTime() - 100_000, perSlotTransition);
createBlock(parent, spec, schedulers.getCurrentTime() - 100_000, perSlotTransition);

// assert block.slot <= get_current_slot(store.time) + 1
BeaconState state = perSlotTransition.apply(new BeaconStateExImpl(parent.getState()));
SlotNumber currentSlot = spec.get_current_slot(parent.getState(), schedulers.getCurrentTime());
SlotNumber nextToCurrentSlot = spec.get_current_slot(state, currentTime).increment();
final boolean actual = nextToCurrentSlot.greaterEqual(currentSlot);

// assert store.time >= pre_state.genesis_time + block.slot * SECONDS_PER_SLOT
final long expectedTime = parent.getState().getGenesisTime().longValue() + currentSlot.longValue() * spec.getConstants().getSecondsPerSlot().longValue();
final boolean expected = currentTime >= expectedTime;

assertThat(actual).isNotEqualTo(expected);

Assert.assertEquals(ImportResult.ExpiredBlock, beaconChain.insert(aBlock));
}
Expand Down

0 comments on commit 55ba795

Please sign in to comment.