use of tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector in project teku by ConsenSys.
the class DepositProvider method getDepositsWithProof.
/**
* @param fromDepositIndex inclusive
* @param toDepositIndex exclusive
* @param eth1DepositCount number of deposits in the merkle tree according to Eth1Data in state
* @return
*/
private SszList<Deposit> getDepositsWithProof(UInt64 fromDepositIndex, UInt64 toDepositIndex, UInt64 eth1DepositCount, long maxDeposits) {
final AtomicReference<UInt64> expectedDepositIndex = new AtomicReference<>(fromDepositIndex);
SszListSchema<Deposit, ?> depositsSchema = depositsSchemaCache.get(maxDeposits);
return depositNavigableMap.subMap(fromDepositIndex, true, toDepositIndex, false).values().stream().map(deposit -> {
if (!deposit.getIndex().equals(expectedDepositIndex.get())) {
throw MissingDepositsException.missingRange(expectedDepositIndex.get(), deposit.getIndex());
}
expectedDepositIndex.set(deposit.getIndex().plus(ONE));
SszBytes32Vector proof = Deposit.SSZ_SCHEMA.getProofSchema().of(depositMerkleTree.getProofWithViewBoundary(deposit.getIndex().intValue(), eth1DepositCount.intValue()));
return new DepositWithIndex(proof, deposit.getData(), deposit.getIndex());
}).collect(depositsSchema.collector());
}
use of tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector in project teku by ConsenSys.
the class BlockProcessorTest method processDepositHelper.
private BeaconState processDepositHelper(BeaconState beaconState, DepositData depositData) throws BlockProcessingException {
// Add the deposit to a Merkle tree so that we can get the root to put into the state Eth1 data
MerkleTree depositMerkleTree = new OptimizedMerkleTree(specConfig.getDepositContractTreeDepth());
depositMerkleTree.add(depositData.hashTreeRoot());
beaconState = beaconState.updated(state -> state.setEth1_data(new Eth1Data(depositMerkleTree.getRoot(), UInt64.valueOf(1), Bytes32.ZERO)));
SszListSchema<Deposit, ?> schema = SszListSchema.create(DepositWithIndex.SSZ_SCHEMA, specConfig.getMaxDeposits());
SszBytes32Vector proof = Deposit.SSZ_SCHEMA.getProofSchema().of(depositMerkleTree.getProof(0));
SszList<Deposit> deposits = schema.of(new DepositWithIndex(proof, depositData, UInt64.valueOf(0)));
// Attempt to process deposit with above data.
return beaconState.updated(state -> blockProcessor.processDeposits(state, deposits));
}
use of tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector in project teku by ConsenSys.
the class StateRootCollector method addParentStateRoots.
public static void addParentStateRoots(final Spec spec, final BeaconState blockSlotState, final StoreTransaction transaction) {
final UInt64 newBlockSlot = blockSlotState.getSlot();
final int slotsPerHistoricalRoot = spec.getSpecConfig(newBlockSlot).getSlotsPerHistoricalRoot();
final SszBytes32Vector blockRoots = blockSlotState.getBlock_roots();
final SszBytes32Vector stateRoots = blockSlotState.getState_roots();
final UInt64 minimumSlot = newBlockSlot.minusMinZero(slotsPerHistoricalRoot);
// Get the parent block root from the state as the genesis block root is recorded as 0
final Bytes32 parentBlockRoot = blockRoots.getElement(newBlockSlot.minusMinZero(1).mod(slotsPerHistoricalRoot).intValue());
UInt64 slot = newBlockSlot.minusMinZero(1);
while (slot.isGreaterThanOrEqualTo(minimumSlot) && !slot.isZero()) {
final Bytes32 previousBlockRoot = getValue(blockRoots, slot.minus(1), slotsPerHistoricalRoot);
if (!previousBlockRoot.equals(parentBlockRoot)) {
// Reached the first slot of the parent block - its state root is already be imported
return;
}
transaction.putStateRoot(getValue(stateRoots, slot, slotsPerHistoricalRoot), new SlotAndBlockRoot(slot, parentBlockRoot));
slot = slot.decrement();
}
if (!slot.isZero()) {
LOG.warn("Missing some state root mappings prior to slot {}", minimumSlot);
}
}
use of tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector in project teku by ConsenSys.
the class AbstractSszMutableCompositeTest method setChildAfterAcquiringItByRefShouldWork.
@Test
@SuppressWarnings("unchecked")
void setChildAfterAcquiringItByRefShouldWork() {
SszBytes32VectorSchema<SszBytes32Vector> childSchema = SszBytes32VectorSchema.create(3);
SszVectorSchema<SszBytes32Vector, ?> compositeSchema = SszVectorSchema.create(childSchema, 2);
SszMutableRefVector<SszBytes32Vector, SszMutableBytes32Vector> mutableComposite = (SszMutableRefVector<SszBytes32Vector, SszMutableBytes32Vector>) compositeSchema.getDefault().createWritableCopy();
SszMutableBytes32Vector mutableChild = mutableComposite.getByRef(0);
mutableChild.setElement(0, Bytes32.fromHexStringLenient("0x1111"));
SszBytes32Vector newChild = childSchema.of(Bytes32.fromHexStringLenient("0x2222"), Bytes32.fromHexStringLenient("0x3333"), Bytes32.fromHexStringLenient("0x4444"));
mutableComposite.set(0, newChild);
SszVector<SszBytes32Vector> changedComposite = mutableComposite.commitChanges();
assertThat(changedComposite.get(0).getElement(0)).isEqualTo(Bytes32.fromHexStringLenient("0x2222"));
assertThat(changedComposite.get(0).getElement(1)).isEqualTo(Bytes32.fromHexStringLenient("0x3333"));
assertThat(changedComposite.get(0).getElement(2)).isEqualTo(Bytes32.fromHexStringLenient("0x4444"));
assertThat(changedComposite.get(1).getElement(0)).isEqualTo(Bytes32.ZERO);
assertThat(changedComposite.get(1).getElement(1)).isEqualTo(Bytes32.ZERO);
assertThat(changedComposite.get(1).getElement(2)).isEqualTo(Bytes32.ZERO);
}
Aggregations