Search in sources :

Example 1 with OptimizedMerkleTree

use of tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree in project teku by ConsenSys.

the class DepositProviderTest method setup.

void setup(final int maxDeposits) {
    when(state.getSlot()).thenReturn(UInt64.valueOf(1234));
    SpecConfig specConfig = SpecConfigLoader.loadConfig("minimal", b -> b.maxDeposits(maxDeposits));
    spec = TestSpecFactory.createPhase0(specConfig);
    depositUtil = new DepositUtil(spec);
    dataStructureUtil = new DataStructureUtil(spec);
    depositProvider = new DepositProvider(new StubMetricsSystem(), recentChainData, eth1DataCache, spec);
    depositMerkleTree = new OptimizedMerkleTree(spec.getGenesisSpecConfig().getDepositContractTreeDepth());
    mockStateEth1DataVotes();
    createDepositEvents(40);
    randomEth1Data = dataStructureUtil.randomEth1Data();
}
Also used : DepositUtil(tech.pegasys.teku.spec.datastructures.util.DepositUtil) OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) StubMetricsSystem(tech.pegasys.teku.infrastructure.metrics.StubMetricsSystem) SpecConfig(tech.pegasys.teku.spec.config.SpecConfig) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil)

Example 2 with OptimizedMerkleTree

use of tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree 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 OptimizedMerkleTree

use of tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree in project teku by ConsenSys.

the class MerkleTreeTest method proofsWithViewBoundaryOptimizedTree_getProofForIndexAlwaysSmallerThanLimit.

@Test
void proofsWithViewBoundaryOptimizedTree_getProofForIndexAlwaysSmallerThanLimit() {
    merkleTree1 = new OptimizedMerkleTree(treeDepth);
    merkleTree2 = new OptimizedMerkleTree(treeDepth);
    for (int i = 0; i < 8; i++) {
        merkleTree2.add(leaves.get(i));
    }
    List<Boolean> results = new ArrayList<>();
    for (int index = 0; index < 8; index++) {
        Bytes32 leaf = leaves.get(index);
        merkleTree1.add(leaf);
        Bytes32 root = merkleTree1.getRoot();
        results.add(genesisSpec.predicates().isValidMerkleBranch(leaf, toSszBytes32Vector(merkleTree2.getProofWithViewBoundary(leaf, index + 1)), // Add 1 for the `List` length mix-in
        treeDepth + 1, index, root));
    }
    assertThat(results).allSatisfy(Assertions::assertTrue);
}
Also used : OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) ArrayList(java.util.ArrayList) Bytes32(org.apache.tuweni.bytes.Bytes32) Assertions(org.junit.jupiter.api.Assertions) Test(org.junit.jupiter.api.Test)

Example 4 with OptimizedMerkleTree

use of tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree in project teku by ConsenSys.

the class MerkleTreeTest method proofsWithViewBoundary_getProofForEachIndexInTheSmallTree.

@Test
void proofsWithViewBoundary_getProofForEachIndexInTheSmallTree() {
    merkleTree1 = new OptimizedMerkleTree(treeDepth);
    merkleTree2 = new OptimizedMerkleTree(treeDepth);
    for (int i = 0; i < 16; i++) {
        merkleTree2.add(leaves.get(i));
    }
    for (int i = 0; i < 10; i++) {
        merkleTree1.add(leaves.get(i));
    }
    Bytes32 root = merkleTree1.getRoot();
    List<Boolean> results = new ArrayList<>();
    for (int index = 0; index < 10; index++) {
        results.add(genesisSpec.predicates().isValidMerkleBranch(leaves.get(index), toSszBytes32Vector(merkleTree2.getProofWithViewBoundary(index, 10)), // Add 1 for the `List` length mix-in
        treeDepth + 1, index, root));
    }
    assertThat(results).allSatisfy(Assertions::assertTrue);
}
Also used : OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) ArrayList(java.util.ArrayList) Bytes32(org.apache.tuweni.bytes.Bytes32) Assertions(org.junit.jupiter.api.Assertions) Test(org.junit.jupiter.api.Test)

Example 5 with OptimizedMerkleTree

use of tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree in project teku by ConsenSys.

the class MerkleTreeTest method getProof.

@Test
void getProof() {
    merkleTree1 = new OptimizedMerkleTree(treeDepth);
    List<Boolean> results = new ArrayList<>();
    for (int index = 0; index < 7; index++) {
        Bytes32 leaf = leaves.get(index);
        merkleTree1.add(leaf);
        Bytes32 root = merkleTree1.getRoot();
        results.add(genesisSpec.predicates().isValidMerkleBranch(leaf, toSszBytes32Vector(merkleTree1.getProof(leaf)), // Add 1 for the `List` length mix-in
        treeDepth + 1, index, root));
    }
    assertThat(results).allSatisfy(Assertions::assertTrue);
}
Also used : OptimizedMerkleTree(tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree) ArrayList(java.util.ArrayList) Bytes32(org.apache.tuweni.bytes.Bytes32) Assertions(org.junit.jupiter.api.Assertions) Test(org.junit.jupiter.api.Test)

Aggregations

OptimizedMerkleTree (tech.pegasys.teku.spec.datastructures.util.OptimizedMerkleTree)5 ArrayList (java.util.ArrayList)4 Bytes32 (org.apache.tuweni.bytes.Bytes32)4 Test (org.junit.jupiter.api.Test)4 Assertions (org.junit.jupiter.api.Assertions)3 SpecConfig (tech.pegasys.teku.spec.config.SpecConfig)2 DataStructureUtil (tech.pegasys.teku.spec.util.DataStructureUtil)2 List (java.util.List)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Bytes48 (org.apache.tuweni.bytes.Bytes48)1 BouncyCastleExtension (org.apache.tuweni.junit.BouncyCastleExtension)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)1 BLSPublicKey (tech.pegasys.teku.bls.BLSPublicKey)1 BLSSignature (tech.pegasys.teku.bls.BLSSignature)1 StubMetricsSystem (tech.pegasys.teku.infrastructure.metrics.StubMetricsSystem)1 SszList (tech.pegasys.teku.infrastructure.ssz.SszList)1 SszBytes32Vector (tech.pegasys.teku.infrastructure.ssz.collections.SszBytes32Vector)1 SszListSchema (tech.pegasys.teku.infrastructure.ssz.schema.SszListSchema)1