Search in sources :

Example 1 with KeyedMerkleLong

use of com.swirlds.common.merkle.utility.KeyedMerkleLong in project hedera-services by hashgraph.

the class BackingAccountsTest method twoPutsChangesG4M.

@Test
void twoPutsChangesG4M() throws ConstructableRegistryException {
    // setup:
    ConstructableRegistry.registerConstructable(new ClassConstructorPair(KeyedMerkleLong.class, KeyedMerkleLong::new));
    final var oneGrandKey = new FcLong(1000L);
    final var twoGrandKey = new FcLong(2000L);
    final var evilKey = new FcLong(666L);
    final var nonEvilKey = new FcLong(667L);
    /* Case 1: g4m a leaf; then put ONE new leaf; then change the mutable leaf and re-get to verify new value */
    final MerkleMap<FcLong, KeyedMerkleLong<FcLong>> firstMm = new MerkleMap<>();
    final var oneGrandEntry = new KeyedMerkleLong<>(oneGrandKey, 1000L);
    firstMm.put(oneGrandKey, oneGrandEntry);
    final var mutableOne = firstMm.getForModify(oneGrandKey);
    /* Putting just one new leaf */
    final var evilEntry = new KeyedMerkleLong<>(evilKey, 666L);
    firstMm.put(evilKey, evilEntry);
    /* Then the mutable value is retained */
    assertSame(mutableOne, firstMm.get(oneGrandKey));
    /* Case 2: g4m a leaf; then put TWO new leaves; then change the mutable leaf and re-get to verify new value */
    final var secondFcm = new MerkleMap<FcLong, KeyedMerkleLong<FcLong>>();
    final var twoGrandEntry = new KeyedMerkleLong<>(twoGrandKey, 2000L);
    final var evilEntry2 = new KeyedMerkleLong<>(evilKey, 666L);
    final var nonEvilEntry2 = new KeyedMerkleLong<>(nonEvilKey, 667L);
    secondFcm.put(twoGrandKey, twoGrandEntry);
    final var mutableTwo = secondFcm.getForModify(twoGrandEntry.getKey());
    /* Putting two new leaves now */
    secondFcm.put(evilEntry2.getKey(), evilEntry2);
    secondFcm.put(nonEvilEntry2.getKey(), nonEvilEntry2);
    /* And now changing the once-mutable value throws MutabilityException */
    assertThrows(MutabilityException.class, mutableTwo::increment);
}
Also used : FcLong(com.hedera.services.utils.FcLong) MerkleMap(com.swirlds.merkle.map.MerkleMap) KeyedMerkleLong(com.swirlds.common.merkle.utility.KeyedMerkleLong) ClassConstructorPair(com.swirlds.common.constructable.ClassConstructorPair) Test(org.junit.jupiter.api.Test)

Aggregations

FcLong (com.hedera.services.utils.FcLong)1 ClassConstructorPair (com.swirlds.common.constructable.ClassConstructorPair)1 KeyedMerkleLong (com.swirlds.common.merkle.utility.KeyedMerkleLong)1 MerkleMap (com.swirlds.merkle.map.MerkleMap)1 Test (org.junit.jupiter.api.Test)1