Skip to content

Commit 8d653af

Browse files
authored
Merge pull request #199 from harmony-dev/fix/fork-choice-issues
Fix/fork choice issues
2 parents 07d2003 + cf24e15 commit 8d653af

File tree

4 files changed

+70
-16
lines changed

4 files changed

+70
-16
lines changed

chain/src/main/java/org/ethereum/beacon/chain/DefaultBeaconChain.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public synchronized ImportResult insert(BeaconBlock block) {
126126

127127
BeaconTuple newTuple = BeaconTuple.of(block, postBlockState);
128128
tupleStorage.put(newTuple);
129-
updateFinality(parentState, postBlockState);
129+
updateFinality(postBlockState);
130130

131131
chainStorage.commit();
132132

@@ -153,21 +153,32 @@ public BeaconTuple getRecentlyProcessed() {
153153
return recentlyProcessed;
154154
}
155155

156-
private void updateFinality(BeaconState previous, BeaconState current) {
157-
if (!previous.getFinalizedCheckpoint().equals(current.getFinalizedCheckpoint())) {
156+
private void updateFinality(BeaconState current) {
157+
boolean finalizedStorageUpdated = false;
158+
boolean justifiedStorageUpdated = false;
159+
if (current
160+
.getFinalizedCheckpoint()
161+
.getEpoch()
162+
.greater(fetchFinalizedCheckpoint().getEpoch())) {
158163
chainStorage.getFinalizedStorage().set(current.getFinalizedCheckpoint());
164+
finalizedStorageUpdated = true;
165+
}
166+
if (current
167+
.getCurrentJustifiedCheckpoint()
168+
.getEpoch()
169+
.greater(fetchJustifiedCheckpoint().getEpoch())) {
170+
chainStorage.getJustifiedStorage().set(current.getCurrentJustifiedCheckpoint());
171+
justifiedStorageUpdated = true;
172+
}
173+
// publish updates after both storages have been updated
174+
// the order can be important if a finalizedCheckpointStream subscriber will look
175+
// into justified storage
176+
// in general, it may be important to publish after commit has succeeded
177+
if (finalizedStorageUpdated) {
159178
finalizedCheckpointStream.onNext(current.getFinalizedCheckpoint());
160179
}
161-
if (!previous.getCurrentJustifiedCheckpoint().equals(current.getCurrentJustifiedCheckpoint())) {
162-
// store new justified checkpoint if its epoch greater than previous one
163-
if (current
164-
.getCurrentJustifiedCheckpoint()
165-
.getEpoch()
166-
.greater(fetchJustifiedCheckpoint().getEpoch())) {
167-
chainStorage.getJustifiedStorage().set(current.getCurrentJustifiedCheckpoint());
168-
}
169-
170-
justifiedCheckpointStream.onNext(current.getFinalizedCheckpoint());
180+
if (justifiedStorageUpdated) {
181+
justifiedCheckpointStream.onNext(current.getCurrentJustifiedCheckpoint());
171182
}
172183
}
173184

consensus/src/main/java/org/ethereum/beacon/consensus/spec/EpochProcessing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ default Pair<Crosslink, List<ValidatorIndex>> get_winning_crosslink_and_attestin
131131
Gwei b2 = get_attesting_balance(state,
132132
attestations.stream().filter(a -> a.getData().getCrosslink().equals(c2)).collect(toList()));
133133
if (b1.equals(b2)) {
134-
return c1.getDataRoot().toString().compareTo(c2.getDataRoot().toString());
134+
return c1.getDataRoot().compareTo(c2.getDataRoot());
135135
} else {
136136
return b1.compareTo(b2);
137137
}

consensus/src/main/java/org/ethereum/beacon/consensus/spec/ForkChoice.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.ethereum.beacon.core.types.Gwei;
1111
import org.ethereum.beacon.core.types.SlotNumber;
1212
import org.ethereum.beacon.core.types.ValidatorIndex;
13+
import org.javatuples.Pair;
1314
import tech.pegasys.artemis.ethereum.core.Hash32;
1415

1516
/**
@@ -109,7 +110,7 @@ default Hash32 get_head(Store store) {
109110
}
110111

111112
head = children.stream()
112-
.max(Comparator.comparing(root -> get_latest_attesting_balance(store, root)))
113+
.max(Comparator.comparing(root -> Pair.with(get_latest_attesting_balance(store, root), root)))
113114
.get();
114115
}
115116
}

types/src/test/java/tech/pegasys/artemis/util/bytes/BytesValueTest.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import io.netty.buffer.ByteBuf;
1717
import io.netty.buffer.Unpooled;
1818
import io.vertx.core.buffer.Buffer;
19-
import net.consensys.cava.bytes.MutableBytes;
2019
import org.junit.Rule;
2120
import org.junit.Test;
2221
import org.junit.rules.ExpectedException;
@@ -857,6 +856,49 @@ public void testBytesValuesComparatorReturnsMatchUnsignedValueByteValue() {
857856
assertThat(small.compareTo(otherSmall)).isEqualTo(0);
858857
}
859858

859+
@Test
860+
public void testBytesValueComparator_lexicographical_order() {
861+
checkOrder(h("0x00"), h("0x01"));
862+
checkOrder(h("0x01"), h("0x7f"));
863+
checkOrder(h("0x7f"), h("0x80"));
864+
checkOrder(h("0x80"), h("0xff"));
865+
866+
checkOrder(h("0x0000"), h("0x0001"));
867+
checkOrder(h("0x0001"), h("0x007f"));
868+
checkOrder(h("0x007f"), h("0x0080"));
869+
checkOrder(h("0x0080"), h("0x00ff"));
870+
871+
checkOrder(h("0x0000"), h("0x0100"));
872+
checkOrder(h("0x0100"), h("0x7f00"));
873+
checkOrder(h("0x7f00"), h("0x8000"));
874+
checkOrder(h("0x8000"), h("0xff00"));
875+
876+
checkOrder(h("0x00"), h("0x0100"));
877+
checkOrder(h("0x01"), h("0x7f00"));
878+
checkOrder(h("0x7f"), h("0x8000"));
879+
checkOrder(h("0x80"), h("0xff00"));
880+
881+
checkOrder(h("0x00"), h("0x01ff"));
882+
checkOrder(h("0x01"), h("0x7fff"));
883+
checkOrder(h("0x7f"), h("0x80ff"));
884+
checkOrder(h("0x80"), h("0xffff"));
885+
886+
checkOrder(h("0x0001"), h("0x0100"));
887+
checkOrder(h("0x007f"), h("0x7f00"));
888+
checkOrder(h("0x0080"), h("0x8000"));
889+
checkOrder(h("0x00ff"), h("0xff00"));
890+
891+
checkOrder(h("0x000001"), h("0x010000"));
892+
checkOrder(h("0x00007f"), h("0x7f0000"));
893+
checkOrder(h("0x000080"), h("0x800000"));
894+
checkOrder(h("0x0000ff"), h("0xff0000"));
895+
}
896+
897+
static void checkOrder(BytesValue lesser, BytesValue greater) {
898+
assertThat(lesser).isLessThan(greater);
899+
assertThat(greater).isGreaterThan(lesser);
900+
}
901+
860902
@Test
861903
public void testGetSetBit() {
862904
MutableBytesValue bytes = MutableBytesValue.create(4);

0 commit comments

Comments
 (0)