Search in sources :

Example 1 with SszBytes32Vector

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());
}
Also used : SszBytes32Vector(tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector) SszList(tech.pegasys.teku.infrastructure.ssz.SszList) MinGenesisTimeBlockEvent(tech.pegasys.teku.ethereum.pow.api.MinGenesisTimeBlockEvent) ONE(tech.pegasys.teku.infrastructure.unsigned.UInt64.ONE) AtomicReference(java.util.concurrent.atomic.AtomicReference) Eth1Data(tech.pegasys.teku.spec.datastructures.blocks.Eth1Data) SszListSchema(tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) MerkleTree(tech.pegasys.teku.spec.datastructures.util.MerkleTree) DepositUtil(tech.pegasys.teku.spec.datastructures.util.DepositUtil) DepositsFromBlockEvent(tech.pegasys.teku.ethereum.pow.api.DepositsFromBlockEvent) Spec(tech.pegasys.teku.spec.Spec) FinalizedCheckpointChannel(tech.pegasys.teku.storage.api.FinalizedCheckpointChannel) Bytes32(org.apache.tuweni.bytes.Bytes32) NavigableMap(java.util.NavigableMap) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) Logger(org.apache.logging.log4j.Logger) TekuMetricCategory(tech.pegasys.teku.infrastructure.metrics.TekuMetricCategory) TreeMap(java.util.TreeMap) RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) LogManager(org.apache.logging.log4j.LogManager) Eth1EventsChannel(tech.pegasys.teku.pow.api.Eth1EventsChannel) Checkpoint(tech.pegasys.teku.spec.datastructures.state.Checkpoint) OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) Counter(org.hyperledger.besu.plugin.services.metrics.Counter) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) SszBytes32Vector(tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) AtomicReference(java.util.concurrent.atomic.AtomicReference) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Example 2 with SszBytes32Vector

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));
}
Also used : BouncyCastleExtension(org.apache.tuweni.junit.BouncyCastleExtension) SszBytes32Vector(tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SszList(tech.pegasys.teku.infrastructure.ssz.SszList) Bytes(org.apache.tuweni.bytes.Bytes) Fork(tech.pegasys.teku.spec.datastructures.state.Fork) SpecVersion(tech.pegasys.teku.spec.SpecVersion) ArrayList(java.util.ArrayList) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Eth1Data(tech.pegasys.teku.spec.datastructures.blocks.Eth1Data) SszListSchema(tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) MerkleTree(tech.pegasys.teku.spec.datastructures.util.MerkleTree) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Spec(tech.pegasys.teku.spec.Spec) Bytes32(org.apache.tuweni.bytes.Bytes32) BlockProcessingException(tech.pegasys.teku.spec.logic.common.statetransition.exceptions.BlockProcessingException) Validator(tech.pegasys.teku.spec.datastructures.state.Validator) BLSSignature(tech.pegasys.teku.bls.BLSSignature) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) SpecConfig(tech.pegasys.teku.spec.config.SpecConfig) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) Test(org.junit.jupiter.api.Test) List(java.util.List) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil) Bytes48(org.apache.tuweni.bytes.Bytes48) DepositMessage(tech.pegasys.teku.spec.datastructures.operations.DepositMessage) DepositData(tech.pegasys.teku.spec.datastructures.operations.DepositData) OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) Deposit(tech.pegasys.teku.spec.datastructures.operations.Deposit) MerkleTree(tech.pegasys.teku.spec.datastructures.util.MerkleTree) OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) SszBytes32Vector(tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector) DepositWithIndex(tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex) Eth1Data(tech.pegasys.teku.spec.datastructures.blocks.Eth1Data)

Example 3 with SszBytes32Vector

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);
    }
}
Also used : SlotAndBlockRoot(tech.pegasys.teku.spec.datastructures.blocks.SlotAndBlockRoot) SszBytes32Vector(tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 4 with SszBytes32Vector

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);
}
Also used : SszMutableBytes32Vector(tech.pegasys.teku.infrastructure.ssz.collections.SszMutableBytes32Vector) SszBytes32Vector(tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector) Test(org.junit.jupiter.api.Test)

Aggregations

SszBytes32Vector (tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector)4 Bytes32 (org.apache.tuweni.bytes.Bytes32)3 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)3 Test (org.junit.jupiter.api.Test)2 SszList (tech.pegasys.teku.infrastructure.ssz.SszList)2 SszListSchema (tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema)2 Spec (tech.pegasys.teku.spec.Spec)2 Eth1Data (tech.pegasys.teku.spec.datastructures.blocks.Eth1Data)2 Deposit (tech.pegasys.teku.spec.datastructures.operations.Deposit)2 DepositWithIndex (tech.pegasys.teku.spec.datastructures.operations.DepositWithIndex)2 BeaconState (tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState)2 MerkleTree (tech.pegasys.teku.spec.datastructures.util.MerkleTree)2 OptimizedMerkleTree (tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NavigableMap (java.util.NavigableMap)1 TreeMap (java.util.TreeMap)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1