Search in sources :

Example 1 with RichInstant

use of com.hedera.services.state.submerkle.RichInstant in project hedera-services by hashgraph.

the class TypedTokenStoreTest method savesTokenAsExpected.

/* --- Token saving --- */
@Test
void savesTokenAsExpected() {
    // setup:
    final var mintedSerialNo = 33L;
    final var wipedSerialNo = 33L;
    final var mintedSerialNo2 = 44;
    final var burnedSerialNo = 44;
    final var nftMeta = "abcdefgh".getBytes();
    final var treasuryId = new EntityId(0, 0, treasuryAccountNum);
    final var tokenEntityId = new EntityId(0, 0, tokenNum);
    final var creationTime = new RichInstant(1_234_567L, 8);
    final var modelTreasuryId = new Id(0, 0, treasuryAccountNum);
    final var mintedToken = new UniqueToken(tokenId, mintedSerialNo, creationTime, Id.DEFAULT, nftMeta);
    final var wipedToken = new UniqueToken(tokenId, wipedSerialNo, creationTime, modelTreasuryId, nftMeta);
    final var mintedToken2 = new UniqueToken(tokenId, mintedSerialNo2, creationTime, Id.DEFAULT, nftMeta);
    final var burnedToken = new UniqueToken(tokenId, burnedSerialNo, creationTime, modelTreasuryId, nftMeta);
    // and:
    final var expectedReplacementToken = new MerkleToken(expiry, tokenSupply * 2, 0, symbol, name, freezeDefault, true, new EntityId(0, 0, autoRenewAccountNum));
    expectedReplacementToken.setAutoRenewAccount(treasuryId);
    expectedReplacementToken.setSupplyKey(supplyKey);
    expectedReplacementToken.setFreezeKey(freezeKey);
    expectedReplacementToken.setKycKey(kycKey);
    expectedReplacementToken.setPauseKey(pauseKey);
    expectedReplacementToken.setAccountsFrozenByDefault(!freezeDefault);
    expectedReplacementToken.setMemo(memo);
    expectedReplacementToken.setAutoRenewPeriod(autoRenewPeriod);
    // and:
    final var expectedReplacementToken2 = new MerkleToken(expiry, tokenSupply * 4, 0, symbol, name, freezeDefault, true, new EntityId(0, 0, treasuryAccountNum));
    expectedReplacementToken2.setAutoRenewAccount(treasuryId);
    expectedReplacementToken2.setSupplyKey(supplyKey);
    expectedReplacementToken2.setFreezeKey(freezeKey);
    expectedReplacementToken2.setKycKey(kycKey);
    expectedReplacementToken2.setPauseKey(pauseKey);
    expectedReplacementToken2.setAccountsFrozenByDefault(!freezeDefault);
    expectedReplacementToken2.setMemo(memo);
    expectedReplacementToken2.setAutoRenewPeriod(autoRenewPeriod);
    // and:
    final var expectedNewUniqTokenId = NftId.withDefaultShardRealm(tokenEntityId.num(), mintedSerialNo);
    final var expectedNewUniqTokenId2 = NftId.withDefaultShardRealm(tokenEntityId.num(), mintedSerialNo2);
    final var expectedNewUniqToken = new MerkleUniqueToken(MISSING_ENTITY_ID, nftMeta, creationTime);
    final var expectedPastUniqTokenId = NftId.withDefaultShardRealm(tokenEntityId.num(), wipedSerialNo);
    final var expectedPastUniqTokenId2 = NftId.withDefaultShardRealm(tokenEntityId.num(), burnedSerialNo);
    givenModifiableToken(merkleTokenId, merkleToken);
    givenToken(merkleTokenId, merkleToken);
    // when:
    var modelToken = subject.loadToken(tokenId);
    // and:
    modelToken.setTotalSupply(tokenSupply * 2);
    modelToken.setAutoRenewAccount(treasuryAccount);
    modelToken.setTreasury(autoRenewAccount);
    modelToken.setFrozenByDefault(!freezeDefault);
    modelToken.mintedUniqueTokens().add(mintedToken);
    modelToken.setIsDeleted(false);
    modelToken.setExpiry(expiry);
    modelToken.setAutoRenewPeriod(autoRenewPeriod);
    modelToken.removedUniqueTokens().add(wipedToken);
    modelToken.setAutoRenewPeriod(autoRenewPeriod);
    modelToken.setCustomFees(List.of());
    modelToken.setMemo(memo);
    // and:
    subject.commitToken(modelToken);
    // then:
    assertEquals(expectedReplacementToken, merkleToken);
    // and:
    verify(sideEffectsTracker).trackTokenChanges(modelToken);
    verify(uniqueTokens).put(expectedNewUniqTokenId, expectedNewUniqToken);
    verify(uniqueTokens).put(NftId.withDefaultShardRealm(tokenEntityId.num(), mintedSerialNo), expectedNewUniqToken);
    verify(uniqueTokens).remove(expectedPastUniqTokenId);
    // when:
    modelToken = subject.loadToken(tokenId);
    // and:
    modelToken.setTotalSupply(tokenSupply * 4);
    modelToken.setAutoRenewAccount(treasuryAccount);
    modelToken.setTreasury(treasuryAccount);
    modelToken.setFrozenByDefault(!freezeDefault);
    modelToken.mintedUniqueTokens().add(mintedToken2);
    modelToken.setIsDeleted(false);
    modelToken.setExpiry(expiry);
    modelToken.removedUniqueTokens().add(burnedToken);
    modelToken.setCustomFees(List.of());
    // and:
    subject.commitToken(modelToken);
    // then:
    assertEquals(expectedReplacementToken2, merkleToken);
    // and:
    verify(sideEffectsTracker).trackTokenChanges(modelToken);
    verify(uniqueTokens).put(expectedNewUniqTokenId2, expectedNewUniqToken);
    verify(uniqueTokens).remove(expectedPastUniqTokenId2);
}
Also used : EntityId(com.hedera.services.state.submerkle.EntityId) UniqueToken(com.hedera.services.store.models.UniqueToken) MerkleUniqueToken(com.hedera.services.state.merkle.MerkleUniqueToken) MerkleToken(com.hedera.services.state.merkle.MerkleToken) MerkleUniqueToken(com.hedera.services.state.merkle.MerkleUniqueToken) EntityId(com.hedera.services.state.submerkle.EntityId) Id(com.hedera.services.store.models.Id) NftId(com.hedera.services.store.models.NftId) RichInstant(com.hedera.services.state.submerkle.RichInstant) Test(org.junit.jupiter.api.Test)

Example 2 with RichInstant

use of com.hedera.services.state.submerkle.RichInstant in project hedera-services by hashgraph.

the class RichInstantTest method emptyConstructor.

@Test
void emptyConstructor() {
    RichInstant anotherSubject = new RichInstant();
    assertEquals(0, anotherSubject.getNanos());
    assertEquals(0, anotherSubject.getSeconds());
    assertEquals(MISSING_INSTANT, anotherSubject);
    assertEquals(MISSING_INSTANT, fromGrpc(Timestamp.getDefaultInstance()));
    assertEquals(Timestamp.getDefaultInstance(), anotherSubject.toGrpc());
}
Also used : RichInstant(com.hedera.services.state.submerkle.RichInstant) Test(org.junit.jupiter.api.Test)

Example 3 with RichInstant

use of com.hedera.services.state.submerkle.RichInstant in project hedera-services by hashgraph.

the class TxnAwareRecordsHistorianTest method incorporatesChildRecordsIfPresent.

@Test
void incorporatesChildRecordsIfPresent() {
    final var mockFollowingRecord = mock(ExpirableTxnRecord.class);
    final var followingChildNows = nows + 1;
    given(mockFollowingRecord.getConsensusSecond()).willReturn(followingChildNows);
    final var mockPrecedingRecord = mock(ExpirableTxnRecord.class);
    final var precedingChildNows = nows - 1;
    given(mockPrecedingRecord.getConsensusSecond()).willReturn(precedingChildNows);
    given(mockPrecedingRecord.getEnumStatus()).willReturn(INVALID_ACCOUNT_ID);
    given(mockFollowingRecord.getEnumStatus()).willReturn(INVALID_CHUNK_NUMBER);
    final var expPrecedeId = txnIdA.toBuilder().setNonce(1).build();
    final var expFollowId = txnIdA.toBuilder().setNonce(2).build();
    final var expectedPrecedingChildId = new TxnId(aEntity, new RichInstant(nows, nanos), false, 1);
    final var expectedFollowingChildId = new TxnId(aEntity, new RichInstant(nows, nanos), false, 2);
    final var mockTopLevelRecord = mock(ExpirableTxnRecord.class);
    given(mockTopLevelRecord.getEnumStatus()).willReturn(SUCCESS);
    final var topLevelRecord = mock(ExpirableTxnRecord.Builder.class);
    given(topLevelRecord.getTxnId()).willReturn(TxnId.fromGrpc(txnIdA));
    final var followingBuilder = mock(ExpirableTxnRecord.Builder.class);
    given(followingBuilder.getTxnId()).willReturn(expectedFollowingChildId);
    given(mockFollowingRecord.getTxnId()).willReturn(expectedFollowingChildId);
    final var precedingBuilder = mock(ExpirableTxnRecord.Builder.class);
    given(precedingBuilder.getTxnId()).willReturn(expectedPrecedingChildId);
    given(mockPrecedingRecord.getTxnId()).willReturn(expectedPrecedingChildId);
    final var expectedFollowTime = topLevelNow.plusNanos(1);
    final var expectedPrecedingTime = topLevelNow.minusNanos(1);
    givenTopLevelContext();
    given(topLevelRecord.setNumChildRecords(anyShort())).willReturn(topLevelRecord);
    given(topLevelRecord.build()).willReturn(mockTopLevelRecord);
    given(followingBuilder.build()).willReturn(mockFollowingRecord);
    given(precedingBuilder.build()).willReturn(mockPrecedingRecord);
    given(txnCtx.recordSoFar()).willReturn(topLevelRecord);
    given(creator.saveExpiringRecord(effPayer, mockTopLevelRecord, nows, submittingMember)).willReturn(mockTopLevelRecord);
    given(creator.saveExpiringRecord(effPayer, mockFollowingRecord, followingChildNows, submittingMember)).willReturn(mockFollowingRecord);
    given(creator.saveExpiringRecord(effPayer, mockPrecedingRecord, precedingChildNows, submittingMember)).willReturn(mockPrecedingRecord);
    final var followSynthBody = aBuilderWith("FOLLOW");
    final var precedeSynthBody = aBuilderWith("PRECEDE");
    assertEquals(topLevelNow.plusNanos(1), subject.nextFollowingChildConsensusTime());
    subject.trackFollowingChildRecord(1, followSynthBody, followingBuilder);
    assertEquals(topLevelNow.plusNanos(2), subject.nextFollowingChildConsensusTime());
    subject.trackPrecedingChildRecord(1, precedeSynthBody, precedingBuilder);
    subject.saveExpirableTransactionRecords();
    final var followingRsos = subject.getFollowingChildRecords();
    final var precedingRsos = subject.getPrecedingChildRecords();
    verify(topLevelRecord).excludeHbarChangesFrom(followingBuilder);
    verify(topLevelRecord).excludeHbarChangesFrom(precedingBuilder);
    verify(topLevelRecord).setNumChildRecords((short) 2);
    verify(followingBuilder).setConsensusTime(RichInstant.fromJava(expectedFollowTime));
    verify(followingBuilder).setParentConsensusTime(topLevelNow);
    verify(precedingBuilder).setConsensusTime(RichInstant.fromJava(expectedPrecedingTime));
    verify(precedingBuilder).setTxnId(expectedPrecedingChildId);
    verify(followingBuilder).setTxnId(expectedFollowingChildId);
    assertEquals(1, followingRsos.size());
    assertEquals(1, precedingRsos.size());
    final var precedeRso = precedingRsos.get(0);
    assertEquals(expectedPrecedingTime, precedeRso.getTimestamp());
    final var precedeSynth = precedeRso.getTransaction();
    final var expectedPrecedeSynth = synthFromBody(precedeSynthBody.setTransactionID(expPrecedeId).build());
    assertEquals(expectedPrecedeSynth, precedeSynth);
    final var followRso = followingRsos.get(0);
    assertEquals(expectedFollowTime, followRso.getTimestamp());
    final var followSynth = followRso.getTransaction();
    final var expectedFollowSynth = synthFromBody(followSynthBody.setTransactionID(expFollowId).build());
    assertEquals(expectedFollowSynth, followSynth);
    verify(creator).saveExpiringRecord(effPayer, mockPrecedingRecord, precedingChildNows, submittingMember);
    verify(creator).saveExpiringRecord(effPayer, mockTopLevelRecord, nows, submittingMember);
    verify(creator).saveExpiringRecord(effPayer, mockFollowingRecord, followingChildNows, submittingMember);
    verifyBuilderUse(precedingBuilder, expectedPrecedingChildId, noThrowSha384HashOf(expectedPrecedeSynth.getSignedTransactionBytes().toByteArray()));
    verifyBuilderUse(followingBuilder, expectedFollowingChildId, noThrowSha384HashOf(expectedFollowSynth.getSignedTransactionBytes().toByteArray()));
    verify(recordCache).setPostConsensus(expPrecedeId, INVALID_ACCOUNT_ID, mockPrecedingRecord);
    verify(recordCache).setPostConsensus(expFollowId, INVALID_CHUNK_NUMBER, mockFollowingRecord);
}
Also used : ExpirableTxnRecord(com.hedera.services.state.submerkle.ExpirableTxnRecord) TxnId(com.hedera.services.state.submerkle.TxnId) RichInstant(com.hedera.services.state.submerkle.RichInstant) Test(org.junit.jupiter.api.Test)

Example 4 with RichInstant

use of com.hedera.services.state.submerkle.RichInstant in project hedera-services by hashgraph.

the class GetMerkleTopicInfoResourceUsageTest method feeDataAsExpected.

@ParameterizedTest
@CsvSource({ ", , , , 236, 112", // bpr += memo size(8)
"abcdefgh, , , , 236, 120", // bpr += 32
"abcdefgh, 0000000000000000000000000000000000000000000000000000000000000000, , , 236, 152", // for admin key
"abcdefgh, 0000000000000000000000000000000000000000000000000000000000000000, " + // bpr += 32 for
"1111111111111111111111111111111111111111111111111111111111111111, , 236, 184", // submit key
"abcdefgh, 0000000000000000000000000000000000000000000000000000000000000000, " + // bpr += 24
"1111111111111111111111111111111111111111111111111111111111111111, 0.1.2, 236, 208" // for auto renew account
})
void feeDataAsExpected(final String memo, @ConvertWith(JEd25519KeyConverter.class) final JEd25519Key adminKey, @ConvertWith(JEd25519KeyConverter.class) final JEd25519Key submitKey, @ConvertWith(EntityIdConverter.class) final EntityId autoRenewAccountId, // query header + topic id size
final int expectedBpt, // query response header + topic id size + topic info size
final int expectedBpr) {
    final var merkleTopic = new MerkleTopic(memo, adminKey, submitKey, 0, autoRenewAccountId, new RichInstant(1, 0));
    final var expectedFeeData = FeeData.newBuilder().setNodedata(FeeComponents.newBuilder().setConstant(1).setBpt(expectedBpt).setBpr(expectedBpr).build()).setNetworkdata(FeeComponents.getDefaultInstance()).setServicedata(FeeComponents.getDefaultInstance()).build();
    given(topics.get(EntityNum.fromTopicId(topicId))).willReturn(merkleTopic);
    final var costAnswerEstimate = subject.usageGiven(topicInfoQuery(topicId, COST_ANSWER), view);
    final var answerOnlyEstimate = subject.usageGiven(topicInfoQuery(topicId, ANSWER_ONLY), view);
    assertEquals(expectedFeeData, costAnswerEstimate);
    assertEquals(expectedFeeData, answerOnlyEstimate);
}
Also used : MerkleTopic(com.hedera.services.state.merkle.MerkleTopic) RichInstant(com.hedera.services.state.submerkle.RichInstant) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with RichInstant

use of com.hedera.services.state.submerkle.RichInstant in project hedera-services by hashgraph.

the class MerkleTopicTest method assertDefaultTopicAccessors.

/**
 * Assert that all the accessors for topic return default values.
 *
 * @param merkleTopic
 */
private void assertDefaultTopicAccessors(final MerkleTopic merkleTopic) {
    assertFalse(merkleTopic.hasMemo());
    assertEquals("", merkleTopic.getMemo());
    assertFalse(merkleTopic.hasAdminKey());
    assertTrue(merkleTopic.getAdminKey().isEmpty());
    assertFalse(merkleTopic.hasSubmitKey());
    assertTrue(merkleTopic.getSubmitKey().isEmpty());
    assertEquals(0L, merkleTopic.getAutoRenewDurationSeconds());
    assertFalse(merkleTopic.hasAutoRenewAccountId());
    assertEquals(new EntityId(), merkleTopic.getAutoRenewAccountId());
    assertFalse(merkleTopic.hasExpirationTimestamp());
    assertEquals(new RichInstant(), merkleTopic.getExpirationTimestamp());
    assertFalse(merkleTopic.isDeleted());
    assertEquals(0L, merkleTopic.getSequenceNumber());
    assertFalse(merkleTopic.hasRunningHash());
    assertArrayEquals(new byte[48], merkleTopic.getRunningHash());
}
Also used : EntityId(com.hedera.services.state.submerkle.EntityId) RichInstant(com.hedera.services.state.submerkle.RichInstant)

Aggregations

RichInstant (com.hedera.services.state.submerkle.RichInstant)10 Test (org.junit.jupiter.api.Test)7 EntityId (com.hedera.services.state.submerkle.EntityId)5 MerkleTopic (com.hedera.services.state.merkle.MerkleTopic)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 JKeyList (com.hedera.services.legacy.core.jproto.JKeyList)1 MerkleToken (com.hedera.services.state.merkle.MerkleToken)1 MerkleUniqueToken (com.hedera.services.state.merkle.MerkleUniqueToken)1 ExpirableTxnRecord (com.hedera.services.state.submerkle.ExpirableTxnRecord)1 TxnId (com.hedera.services.state.submerkle.TxnId)1 Id (com.hedera.services.store.models.Id)1 NftId (com.hedera.services.store.models.NftId)1 UniqueToken (com.hedera.services.store.models.UniqueToken)1 AccountID (com.hederahashgraph.api.proto.java.AccountID)1 TopicID (com.hederahashgraph.api.proto.java.TopicID)1 DecoderException (org.apache.commons.codec.DecoderException)1 CsvSource (org.junit.jupiter.params.provider.CsvSource)1