Search in sources :

Example 11 with ClassConstructorPair

use of com.swirlds.common.constructable.ClassConstructorPair in project hedera-services by hashgraph.

the class FcAssessedCustomFeeTest method liveFireSerdeWorksForHbarFee0170Version.

@Test
void liveFireSerdeWorksForHbarFee0170Version() throws IOException, ConstructableRegistryException {
    // setup:
    final var account = new EntityId(1, 2, 3);
    final var amount = 345L;
    final var subject = new FcAssessedCustomFee(account, amount, FcAssessedCustomFee.UNKNOWN_EFFECTIVE_PAYER_ACCOUNT_NUMS);
    // and:
    ConstructableRegistry.registerConstructable(new ClassConstructorPair(EntityId.class, EntityId::new));
    // and:
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final var dos = new SerializableDataOutputStream(baos);
    // given:
    subject.serialize(dos);
    dos.flush();
    // and:
    final var bytes = baos.toByteArray();
    final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    final var din = new SerializableDataInputStream(bais);
    // when:
    final var newSubject = new FcAssessedCustomFee();
    newSubject.deserialize(din, FcAssessedCustomFee.RELEASE_0170_VERSION);
    // then:
    assertEquals(subject, newSubject);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SerializableDataOutputStream(com.swirlds.common.io.SerializableDataOutputStream) SerializableDataInputStream(com.swirlds.common.io.SerializableDataInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClassConstructorPair(com.swirlds.common.constructable.ClassConstructorPair) Test(org.junit.jupiter.api.Test)

Example 12 with ClassConstructorPair

use of com.swirlds.common.constructable.ClassConstructorPair in project hedera-services by hashgraph.

the class FcAssessedCustomFeeTest method liveFireSerdeWorksForHtsFee0170Version.

@Test
void liveFireSerdeWorksForHtsFee0170Version() throws IOException, ConstructableRegistryException {
    // setup:
    final var account = new EntityId(1, 2, 3);
    final var token = new EntityId(2, 3, 4);
    final var amount = 345L;
    final var subject = new FcAssessedCustomFee(account, token, amount, FcAssessedCustomFee.UNKNOWN_EFFECTIVE_PAYER_ACCOUNT_NUMS);
    // and:
    ConstructableRegistry.registerConstructable(new ClassConstructorPair(EntityId.class, EntityId::new));
    // and:
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final var dos = new SerializableDataOutputStream(baos);
    // given:
    subject.serialize(dos);
    dos.flush();
    // and:
    final var bytes = baos.toByteArray();
    final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    final var din = new SerializableDataInputStream(bais);
    // when:
    final var newSubject = new FcAssessedCustomFee();
    newSubject.deserialize(din, FcAssessedCustomFee.RELEASE_0170_VERSION);
    // then:
    assertEquals(subject, newSubject);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SerializableDataOutputStream(com.swirlds.common.io.SerializableDataOutputStream) SerializableDataInputStream(com.swirlds.common.io.SerializableDataInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClassConstructorPair(com.swirlds.common.constructable.ClassConstructorPair) Test(org.junit.jupiter.api.Test)

Example 13 with ClassConstructorPair

use of com.swirlds.common.constructable.ClassConstructorPair in project hedera-services by hashgraph.

the class FcTokenAssociationTest method serializationWorks.

@Test
void serializationWorks() throws IOException, ConstructableRegistryException {
    ConstructableRegistry.registerConstructable(new ClassConstructorPair(EntityId.class, EntityId::new));
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final var dos = new SerializableDataOutputStream(baos);
    subject.serialize(dos);
    dos.flush();
    final var bytes = baos.toByteArray();
    final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    final var din = new SerializableDataInputStream(bais);
    final var newSubject = new FcTokenAssociation();
    newSubject.deserialize(din, FcTokenAssociation.RELEASE_0180_VERSION);
    assertEquals(subject, newSubject);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SerializableDataOutputStream(com.swirlds.common.io.SerializableDataOutputStream) SerializableDataInputStream(com.swirlds.common.io.SerializableDataInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClassConstructorPair(com.swirlds.common.constructable.ClassConstructorPair) Test(org.junit.jupiter.api.Test)

Example 14 with ClassConstructorPair

use of com.swirlds.common.constructable.ClassConstructorPair 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)

Example 15 with ClassConstructorPair

use of com.swirlds.common.constructable.ClassConstructorPair in project hedera-services by hashgraph.

the class BackingAccountsTest method auxiliarySetIsRebuiltFromScratch.

@Test
void auxiliarySetIsRebuiltFromScratch() throws ConstructableRegistryException {
    ConstructableRegistry.registerConstructable(new ClassConstructorPair(MerkleAccount.class, MerkleAccount::new));
    final var idSet = subject.getExistingAccounts();
    subject.rebuildFromSources();
    assertTrue(idSet.contains(aKey.toGrpcAccountId()));
    assertTrue(idSet.contains(bKey.toGrpcAccountId()));
    delegate.remove(aKey);
    subject.rebuildFromSources();
    assertFalse(idSet.contains(aKey.toGrpcAccountId()));
    assertTrue(idSet.contains(bKey.toGrpcAccountId()));
}
Also used : MerkleAccount(com.hedera.services.state.merkle.MerkleAccount) ClassConstructorPair(com.swirlds.common.constructable.ClassConstructorPair) Test(org.junit.jupiter.api.Test)

Aggregations

ClassConstructorPair (com.swirlds.common.constructable.ClassConstructorPair)19 Test (org.junit.jupiter.api.Test)13 SerializableDataInputStream (com.swirlds.common.io.SerializableDataInputStream)9 SerializableDataOutputStream (com.swirlds.common.io.SerializableDataOutputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 EntityId (com.hedera.services.state.submerkle.EntityId)5 MerkleAccount (com.hedera.services.state.merkle.MerkleAccount)4 ExpirableTxnRecord (com.hedera.services.state.submerkle.ExpirableTxnRecord)3 MerkleLong (com.swirlds.common.merkle.utility.MerkleLong)3 FCQueue (com.swirlds.fcqueue.FCQueue)3 TxnReceipt (com.hedera.services.legacy.core.jproto.TxnReceipt)2 MerkleAccountState (com.hedera.services.state.merkle.MerkleAccountState)2 MerkleEntityId (com.hedera.services.state.merkle.MerkleEntityId)2 CurrencyAdjustments (com.hedera.services.state.submerkle.CurrencyAdjustments)2 EvmFnResult (com.hedera.services.state.submerkle.EvmFnResult)2 TxnId (com.hedera.services.state.submerkle.TxnId)2 MerkleMap (com.swirlds.merkle.map.MerkleMap)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 ServicesState (com.hedera.services.ServicesState)1