Skip to content

Commit

Permalink
Fix time reject check during block processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsson49 committed Nov 4, 2019
1 parent 04fb85b commit 7b50b85
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,9 @@ private boolean hasParent(BeaconBlock block) {
* @return true if block should be rejected, false otherwise.
*/
private boolean rejectedByTime(BeaconBlock block) {
SlotNumber nextToCurrentSlot =
spec.get_current_slot(recentlyProcessed.getState(), schedulers.getCurrentTime()).increment();

return block.getSlot().greater(nextToCurrentSlot);
SlotNumber currentSlot =
spec.get_current_slot(recentlyProcessed.getState(), schedulers.getCurrentTime());
return block.getSlot().greater(currentSlot);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.ethereum.beacon.chain;

import java.util.Collections;
import java.util.stream.IntStream;
import org.ethereum.beacon.chain.MutableBeaconChain.ImportResult;
import org.ethereum.beacon.chain.storage.BeaconChainStorage;
import org.ethereum.beacon.chain.storage.impl.SSZBeaconChainStorageFactory;
Expand All @@ -26,14 +24,21 @@
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;
import org.ethereum.beacon.schedulers.Schedulers;
import org.junit.Assert;
import org.junit.Test;
import tech.pegasys.artemis.ethereum.core.Hash32;
import tech.pegasys.artemis.util.uint.UInt64;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.stream.IntStream;

public class DefaultBeaconChainTest {

@Test
Expand Down Expand Up @@ -118,4 +123,40 @@ public BeaconStateEx apply(BeaconStateEx stateEx) {
chainStorage,
schedulers);
}

@Test
public void testRejectBlocks() {
ControlledSchedulers schedulers = Schedulers.createControlled();
Instant genesisTime = Instant.now().plus(1, ChronoUnit.DAYS);
schedulers.setCurrentTime(genesisTime.toEpochMilli());

BeaconChainSpec spec =
BeaconChainSpec.Builder.createWithDefaultParams()
.withComputableGenesisTime(false)
.withVerifyDepositProof(false)
.build();
StateTransition<BeaconStateEx> perSlotTransition =
StateTransitionTestUtil.createNextSlotTransition();
MutableBeaconChain beaconChain = createBeaconChain(spec, perSlotTransition, schedulers);

beaconChain.init();
BeaconTuple initialTuple = beaconChain.getRecentlyProcessed();
Assert.assertEquals(spec.getConstants().getGenesisSlot(), initialTuple.getBlock().getSlot());

BeaconTuple recentlyProcessed = beaconChain.getRecentlyProcessed();

schedulers.setCurrentTime(
spec.get_slot_start_time(initialTuple.getState(), SlotNumber.of(2)).getValue() * 1000);

SlotNumber nextSlot =
spec.get_current_slot(recentlyProcessed.getState(), schedulers.getCurrentTime())
.plus(1);
long nextSlotTime =
spec.get_slot_start_time(recentlyProcessed.getState(), nextSlot).getMillis().getValue() + 1;

BeaconBlock aBlock =
createBlock(recentlyProcessed, spec, nextSlotTime, perSlotTransition);

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

0 comments on commit 7b50b85

Please sign in to comment.