Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/fork choice issues #199

Merged
merged 8 commits into from
Sep 23, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public synchronized ImportResult insert(BeaconBlock block) {

BeaconTuple newTuple = BeaconTuple.of(block, postBlockState);
tupleStorage.put(newTuple);
updateFinality(parentState, postBlockState);
updateFinality(postBlockState);

chainStorage.commit();

Expand All @@ -153,21 +153,32 @@ public BeaconTuple getRecentlyProcessed() {
return recentlyProcessed;
}

private void updateFinality(BeaconState previous, BeaconState current) {
if (!previous.getFinalizedCheckpoint().equals(current.getFinalizedCheckpoint())) {
private void updateFinality(BeaconState current) {
boolean finalizedStorageUpdated = false;
boolean justifiedStorageUpdated = false;
if (current
.getFinalizedCheckpoint()
.getEpoch()
.greater(fetchFinalizedCheckpoint().getEpoch())) {
chainStorage.getFinalizedStorage().set(current.getFinalizedCheckpoint());
finalizedStorageUpdated = true;
}
if (current
.getCurrentJustifiedCheckpoint()
.getEpoch()
.greater(fetchJustifiedCheckpoint().getEpoch())) {
chainStorage.getJustifiedStorage().set(current.getCurrentJustifiedCheckpoint());
justifiedStorageUpdated = true;
}
// publish updates after both storages have been updated
// the order can be important if a finalizedCheckpointStream subscriber will look
// into justified storage
// in general, it may be important to publish after commit has succeeded
if (finalizedStorageUpdated) {
finalizedCheckpointStream.onNext(current.getFinalizedCheckpoint());
}
if (!previous.getCurrentJustifiedCheckpoint().equals(current.getCurrentJustifiedCheckpoint())) {
// store new justified checkpoint if its epoch greater than previous one
if (current
.getCurrentJustifiedCheckpoint()
.getEpoch()
.greater(fetchJustifiedCheckpoint().getEpoch())) {
chainStorage.getJustifiedStorage().set(current.getCurrentJustifiedCheckpoint());
}

justifiedCheckpointStream.onNext(current.getFinalizedCheckpoint());
if (justifiedStorageUpdated) {
justifiedCheckpointStream.onNext(current.getCurrentJustifiedCheckpoint());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ default Pair<Crosslink, List<ValidatorIndex>> get_winning_crosslink_and_attestin
Gwei b2 = get_attesting_balance(state,
attestations.stream().filter(a -> a.getData().getCrosslink().equals(c2)).collect(toList()));
if (b1.equals(b2)) {
return c1.getDataRoot().toString().compareTo(c2.getDataRoot().toString());
return c1.getDataRoot().compareTo(c2.getDataRoot());
} else {
return b1.compareTo(b2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.ethereum.beacon.core.types.Gwei;
import org.ethereum.beacon.core.types.SlotNumber;
import org.ethereum.beacon.core.types.ValidatorIndex;
import org.javatuples.Pair;
import tech.pegasys.artemis.ethereum.core.Hash32;

/**
Expand Down Expand Up @@ -109,7 +110,7 @@ default Hash32 get_head(Store store) {
}

head = children.stream()
.max(Comparator.comparing(root -> get_latest_attesting_balance(store, root)))
.max(Comparator.comparing(root -> Pair.with(get_latest_attesting_balance(store, root), root)))
ericsson49 marked this conversation as resolved.
Show resolved Hide resolved
.get();
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/src/test/resources/eth2.0-spec-tests
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.vertx.core.buffer.Buffer;
import net.consensys.cava.bytes.MutableBytes;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -857,6 +856,49 @@ public void testBytesValuesComparatorReturnsMatchUnsignedValueByteValue() {
assertThat(small.compareTo(otherSmall)).isEqualTo(0);
}

@Test
public void testBytesValueComparator_lexicographical_order() {
checkOrder(h("0x00"), h("0x01"));
checkOrder(h("0x01"), h("0x7f"));
checkOrder(h("0x7f"), h("0x80"));
checkOrder(h("0x80"), h("0xff"));

checkOrder(h("0x0000"), h("0x0001"));
checkOrder(h("0x0001"), h("0x007f"));
checkOrder(h("0x007f"), h("0x0080"));
checkOrder(h("0x0080"), h("0x00ff"));

checkOrder(h("0x0000"), h("0x0100"));
checkOrder(h("0x0100"), h("0x7f00"));
checkOrder(h("0x7f00"), h("0x8000"));
checkOrder(h("0x8000"), h("0xff00"));

checkOrder(h("0x00"), h("0x0100"));
checkOrder(h("0x01"), h("0x7f00"));
checkOrder(h("0x7f"), h("0x8000"));
checkOrder(h("0x80"), h("0xff00"));

checkOrder(h("0x00"), h("0x01ff"));
checkOrder(h("0x01"), h("0x7fff"));
checkOrder(h("0x7f"), h("0x80ff"));
checkOrder(h("0x80"), h("0xffff"));

checkOrder(h("0x0001"), h("0x0100"));
checkOrder(h("0x007f"), h("0x7f00"));
checkOrder(h("0x0080"), h("0x8000"));
checkOrder(h("0x00ff"), h("0xff00"));

checkOrder(h("0x000001"), h("0x010000"));
checkOrder(h("0x00007f"), h("0x7f0000"));
checkOrder(h("0x000080"), h("0x800000"));
checkOrder(h("0x0000ff"), h("0xff0000"));
}

static void checkOrder(BytesValue lesser, BytesValue greater) {
assertThat(lesser).isLessThan(greater);
assertThat(greater).isGreaterThan(lesser);
}

@Test
public void testGetSetBit() {
MutableBytesValue bytes = MutableBytesValue.create(4);
Expand Down