use of tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64 in project teku by ConsenSys.
the class SszBasicListTest method simpleUInt64ListTest.
@Test
public void simpleUInt64ListTest() {
SszListSchema<SszUInt64, ?> listType = SszListSchema.create(SszPrimitiveSchemas.UINT64_SCHEMA, 7);
SszMutableList<SszUInt64> listView = listType.getDefault().createWritableCopy();
TreeNode n0 = listView.commitChanges().getBackingNode();
listView.append(SszUInt64.of(UInt64.valueOf(0x111)));
TreeNode n1 = listView.commitChanges().getBackingNode();
listView.append(SszUInt64.of(UInt64.valueOf(0x222)));
listView.append(SszUInt64.of(UInt64.valueOf(0x333)));
listView.append(SszUInt64.of(UInt64.valueOf(0x444)));
TreeNode n2 = listView.commitChanges().getBackingNode();
listView.append(SszUInt64.of(UInt64.valueOf(0x555)));
TreeNode n3 = listView.commitChanges().getBackingNode();
listView.append(SszUInt64.of(UInt64.valueOf(0x666)));
listView.append(SszUInt64.of(UInt64.valueOf(0x777)));
TreeNode n4 = listView.commitChanges().getBackingNode();
listView.set(0, SszUInt64.of(UInt64.valueOf(0x800)));
TreeNode n5 = listView.commitChanges().getBackingNode();
listView.set(1, SszUInt64.of(UInt64.valueOf(0x801)));
listView.set(2, SszUInt64.of(UInt64.valueOf(0x802)));
listView.set(3, SszUInt64.of(UInt64.valueOf(0x803)));
TreeNode n6 = listView.commitChanges().getBackingNode();
Assertions.assertThat(listType.createFromBackingNode(n0).size()).isEqualTo(0);
Assertions.assertThat(listType.createFromBackingNode(n1).size()).isEqualTo(1);
Assertions.assertThat(listType.createFromBackingNode(n1).get(0).longValue()).isEqualTo(0x111);
Assertions.assertThat(listType.createFromBackingNode(n2).size()).isEqualTo(4);
Assertions.assertThat(listType.createFromBackingNode(n2).get(0).longValue()).isEqualTo(0x111);
Assertions.assertThat(listType.createFromBackingNode(n2).get(1).longValue()).isEqualTo(0x222);
Assertions.assertThat(listType.createFromBackingNode(n2).get(2).longValue()).isEqualTo(0x333);
Assertions.assertThat(listType.createFromBackingNode(n2).get(3).longValue()).isEqualTo(0x444);
Assertions.assertThat(listType.createFromBackingNode(n3).size()).isEqualTo(5);
Assertions.assertThat(listType.createFromBackingNode(n3).get(0).longValue()).isEqualTo(0x111);
Assertions.assertThat(listType.createFromBackingNode(n3).get(1).longValue()).isEqualTo(0x222);
Assertions.assertThat(listType.createFromBackingNode(n3).get(2).longValue()).isEqualTo(0x333);
Assertions.assertThat(listType.createFromBackingNode(n3).get(3).longValue()).isEqualTo(0x444);
Assertions.assertThat(listType.createFromBackingNode(n3).get(4).longValue()).isEqualTo(0x555);
Assertions.assertThat(listType.createFromBackingNode(n4).size()).isEqualTo(7);
Assertions.assertThat(listType.createFromBackingNode(n4).get(5).longValue()).isEqualTo(0x666);
Assertions.assertThat(listType.createFromBackingNode(n4).get(6).longValue()).isEqualTo(0x777);
Assertions.assertThat(listType.createFromBackingNode(n5).get(0).longValue()).isEqualTo(0x800);
Assertions.assertThat(listType.createFromBackingNode(n5).get(1).longValue()).isEqualTo(0x222);
Assertions.assertThat(listType.createFromBackingNode(n6).size()).isEqualTo(7);
Assertions.assertThat(listType.createFromBackingNode(n6).get(0).longValue()).isEqualTo(0x800);
Assertions.assertThat(listType.createFromBackingNode(n6).get(1).longValue()).isEqualTo(0x801);
Assertions.assertThat(listType.createFromBackingNode(n6).get(2).longValue()).isEqualTo(0x802);
Assertions.assertThat(listType.createFromBackingNode(n6).get(3).longValue()).isEqualTo(0x803);
Assertions.assertThat(listType.createFromBackingNode(n6).get(4).longValue()).isEqualTo(0x555);
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> listType.createFromBackingNode(n3).createWritableCopy().set(7, SszUInt64.of(UInt64.valueOf(0xaaa))));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> listType.createFromBackingNode(n3).get(7));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> listType.createFromBackingNode(n3).get(8));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> listView.set(7, SszUInt64.of(UInt64.valueOf(0xaaa))));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> listView.append(SszUInt64.of(UInt64.valueOf(0xaaa))));
listView.clear();
assertThat(listView.commitChanges().hashTreeRoot()).isEqualTo(n0.hashTreeRoot());
}
use of tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64 in project teku by ConsenSys.
the class AbstractGossipManagerTest method shouldNotPublishMessageWhenUnsubscribed.
@Test
void shouldNotPublishMessageWhenUnsubscribed() {
gossipManager.subscribe();
gossipManager.unsubscribe();
final SszUInt64 message = wrap(1);
gossipManager.publishMessage(message);
verify(topicChannel1, never()).gossip(any());
}
use of tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64 in project teku by ConsenSys.
the class BlockProcessorAltair method processAttestation.
@Override
protected void processAttestation(final MutableBeaconState genericState, final Attestation attestation, final IndexedAttestationProvider indexedAttestationProvider) {
final MutableBeaconStateAltair state = MutableBeaconStateAltair.required(genericState);
final AttestationData data = attestation.getData();
final List<Integer> participationFlagIndices = beaconStateAccessorsAltair.getAttestationParticipationFlagIndices(state, data, state.getSlot().minus(data.getSlot()));
// Update epoch participation flags
final SszMutableList<SszByte> epochParticipation;
if (data.getTarget().getEpoch().equals(beaconStateAccessors.getCurrentEpoch(state))) {
epochParticipation = state.getCurrentEpochParticipation();
} else {
epochParticipation = state.getPreviousEpochParticipation();
}
UInt64 proposerRewardNumerator = UInt64.ZERO;
final SszUInt64List attestingIndices = indexedAttestationProvider.getIndexedAttestation(attestation).getAttesting_indices();
for (SszUInt64 attestingIndex : attestingIndices) {
final int index = attestingIndex.get().intValue();
byte participationFlags = epochParticipation.get(index).get();
final UInt64 baseReward = beaconStateAccessorsAltair.getBaseReward(state, index);
boolean shouldUpdate = false;
for (int flagIndex = 0; flagIndex < PARTICIPATION_FLAG_WEIGHTS.size(); flagIndex++) {
final UInt64 weight = PARTICIPATION_FLAG_WEIGHTS.get(flagIndex);
if (participationFlagIndices.contains(flagIndex) && !miscHelpersAltair.hasFlag(participationFlags, flagIndex)) {
shouldUpdate = true;
participationFlags = miscHelpersAltair.addFlag(participationFlags, flagIndex);
proposerRewardNumerator = proposerRewardNumerator.plus(baseReward.times(weight));
}
}
if (shouldUpdate) {
epochParticipation.set(index, SszByte.of(participationFlags));
}
}
if (!proposerRewardNumerator.isZero()) {
final int proposerIndex = beaconStateAccessors.getBeaconProposerIndex(state);
final UInt64 proposerRewardDenominator = WEIGHT_DENOMINATOR.minus(PROPOSER_WEIGHT).times(WEIGHT_DENOMINATOR).dividedBy(PROPOSER_WEIGHT);
final UInt64 proposerReward = proposerRewardNumerator.dividedBy(proposerRewardDenominator);
beaconStateMutators.increaseBalance(state, proposerIndex, proposerReward);
}
}
use of tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64 in project teku by ConsenSys.
the class SszContainerTest method testThreadSafety.
// The threading test is probabilistic and may have false positives
// (i.e. pass on incorrect implementation)
@Test
public void testThreadSafety() throws InterruptedException {
WritableMutableContainer c1w = WritableContainer.createDefault().createWritableCopy();
c1w.setLong1(UInt64.valueOf(0x1));
c1w.setLong2(UInt64.valueOf(0x2));
c1w.getSub1().setLong1(UInt64.valueOf(0x111));
c1w.getSub1().setLong2(UInt64.valueOf(0x222));
c1w.getList1().append(SszUInt64.of(UInt64.fromLongBits(0x333)));
c1w.getList1().append(SszUInt64.of(UInt64.fromLongBits(0x444)));
c1w.getList2().append(sc -> {
sc.setLong1(UInt64.valueOf(0x555));
sc.setLong2(UInt64.valueOf(0x666));
});
c1w.getVector1().set(0, new ImmutableSubContainerImpl(UInt64.valueOf(0x999), Bytes32.leftPad(Bytes.fromHexString("0xa999"))));
c1w.getVector1().set(1, new ImmutableSubContainerImpl(UInt64.valueOf(0xaaa), Bytes32.leftPad(Bytes.fromHexString("0xaaaa"))));
WritableContainer c1r = c1w.commitChanges();
// sanity check of equalsByGetters
SszDataAssert.assertThatSszData(c1r).isEqualTo(c1w).isEqualByGettersTo(c1w).isEqualByHashTreeRootTo(c1w).isEqualBySszTo(c1w);
WritableMutableContainer c2w = c1r.createWritableCopy();
c2w.getList2().getByRef(0).setLong1(UInt64.valueOf(293874));
SszDataAssert.assertThatSszData(c1r).isEqualTo(c1w).isEqualByGettersTo(c1w).isEqualByHashTreeRootTo(c1w).isEqualBySszTo(c1w);
SszDataAssert.assertThatSszData(c1r).isNotEqualByAllMeansTo(c2w);
SszDataAssert.assertThatSszData(c1r).isNotEqualByAllMeansTo(c2w.commitChanges());
// new container from backing tree without any cached views
WritableContainer c2r = WritableContainer.SSZ_SCHEMA.createFromBackingNode(c1r.getBackingNode());
// concurrently traversing children of the the same view instance to make sure the internal
// cache is thread safe
List<Future<Boolean>> futures = TestUtil.executeParallel(() -> SszDataAssert.isEqualByGetters(c2r, c1r), 512);
assertThat(TestUtil.waitAll(futures)).containsOnly(true);
Consumer<WritableMutableContainer> containerMutator = w -> {
w.setLong2(UInt64.valueOf(0x11111));
w.getSub1().setLong2(UInt64.valueOf(0x22222));
w.getList1().append(SszUInt64.of(UInt64.fromLongBits(0x44444)));
w.getList1().set(0, SszUInt64.of(UInt64.fromLongBits(0x11111)));
WritableMutableSubContainer sc = w.getList2().append();
sc.setLong1(UInt64.valueOf(0x77777));
sc.setLong2(UInt64.valueOf(0x88888));
w.getList2().getByRef(0).setLong2(UInt64.valueOf(0x44444));
w.getVector1().set(0, new ImmutableSubContainerImpl(UInt64.valueOf(0x99999), Bytes32.leftPad(Bytes.fromHexString("0xa99999"))));
};
WritableMutableContainer c3w = c1r.createWritableCopy();
containerMutator.accept(c3w);
WritableContainer c3r = c3w.commitChanges();
WritableContainer c4r = WritableContainer.SSZ_SCHEMA.createFromBackingNode(c1r.getBackingNode());
SszDataAssert.assertThatSszData(c4r).isEqualByAllMeansTo(c1r);
// make updated view from the source view in parallel
// this tests that mutable view caches are merged and transferred
// in a thread safe way
List<Future<WritableContainer>> modifiedFuts = TestUtil.executeParallel(() -> {
WritableMutableContainer w = c4r.createWritableCopy();
containerMutator.accept(w);
return w.commitChanges();
}, 512);
List<WritableContainer> modified = TestUtil.waitAll(modifiedFuts);
SszDataAssert.assertThatSszData(c4r).isEqualByAllMeansTo(c1r);
assertThat(modified).allSatisfy(c -> SszDataAssert.assertThatSszData(c).isEqualByAllMeansTo(c3r));
}
use of tech.pegasys.teku.infrastructure.ssz.primitive.SszUInt64 in project teku by ConsenSys.
the class SszPrimitiveSchemaTest method SszUInt64_shouldBeStoredInLE.
@Test
void SszUInt64_shouldBeStoredInLE() {
Bytes wrappedInput = Bytes.fromHexString("0x0102030405060708", 8);
UInt64 inputUint64 = UInt64.fromLongBits(wrappedInput.toLong());
SszUInt64 sszUInt64 = SszUInt64.of(inputUint64);
assertThat(sszUInt64.getBackingNode().hashTreeRoot()).isEqualByComparingTo(Bytes32.fromHexString("0x0807060504030201000000000000000000000000000000000000000000000000"));
}
Aggregations