Search in sources :

Example 6 with JContractIDKey

use of com.hedera.services.legacy.core.jproto.JContractIDKey in project hedera-services by hashgraph.

the class HederaWorldStateTest method usesContractKeyWhenSponsorDid.

@Test
void usesContractKeyWhenSponsorDid() {
    final var sponsorId = AccountID.newBuilder().setAccountNum(123L).build();
    final var sponsoredId = AccountID.newBuilder().setAccountNum(321L).build();
    final var sponsorAddress = EntityIdUtils.asEvmAddress(sponsorId);
    final var sponsoredAddress = EntityIdUtils.asEvmAddress(sponsoredId);
    givenNonNullWorldLedgers();
    given(entityAccess.isExtant(any())).willReturn(true);
    given(entityAccess.getKey(sponsorId)).willReturn(new JContractIDKey(0, 0, 123L));
    final var updater = subject.updater();
    updater.getSponsorMap().put(Address.fromHexString(Hex.encodeHexString(sponsoredAddress)), Address.fromHexString(Hex.encodeHexString(sponsorAddress)));
    final ArgumentCaptor<HederaAccountCustomizer> captor = forClass(HederaAccountCustomizer.class);
    updater.commit();
    subject.customizeSponsoredAccounts();
    verify(entityAccess).customize(eq(sponsoredId), captor.capture());
    final var customizer = captor.getValue();
    final var standin = new MerkleAccount();
    customizer.customizing(standin);
    final var key = standin.getAccountKey();
    assertInstanceOf(JContractIDKey.class, key);
    assertEquals(sponsoredId.getAccountNum(), ((JContractIDKey) key).getContractID().getContractNum());
}
Also used : MerkleAccount(com.hedera.services.state.merkle.MerkleAccount) JContractIDKey(com.hedera.services.legacy.core.jproto.JContractIDKey) HederaAccountCustomizer(com.hedera.services.ledger.accounts.HederaAccountCustomizer) Test(org.junit.jupiter.api.Test)

Example 7 with JContractIDKey

use of com.hedera.services.legacy.core.jproto.JContractIDKey in project hedera-services by hashgraph.

the class ContractCreateTransitionLogic method doStateTransition.

@Override
public void doStateTransition() {
    /* --- Translate from gRPC types --- */
    var contractCreateTxn = txnCtx.accessor().getTxn();
    var op = contractCreateTxn.getContractCreateInstance();
    final var senderId = Id.fromGrpcAccount(contractCreateTxn.getTransactionID().getAccountID());
    final var proxyAccount = op.hasProxyAccountID() ? Id.fromGrpcAccount(op.getProxyAccountID()) : Id.DEFAULT;
    var key = op.hasAdminKey() ? validator.attemptToDecodeOrThrow(op.getAdminKey(), SERIALIZATION_FAILED) : STANDIN_CONTRACT_ID_KEY;
    /* --- Load the model objects --- */
    final var sender = accountStore.loadAccount(senderId);
    final var codeWithConstructorArgs = prepareCodeWithConstructorArguments(op);
    long expiry = RequestBuilder.getExpirationTime(txnCtx.consensusTime(), op.getAutoRenewPeriod()).getSeconds();
    /* --- Do the business logic --- */
    final var newContractAddress = worldState.newContractAddress(sender.getId().asEvmAddress());
    final var result = evmTxProcessor.execute(sender, newContractAddress, op.getGas(), op.getInitialBalance(), codeWithConstructorArgs, txnCtx.consensusTime(), expiry);
    /* --- Persist changes into state --- */
    final var createdContracts = worldState.persistProvisionalContractCreations();
    result.setCreatedContracts(createdContracts);
    if (result.isSuccessful()) {
        /* --- Create customizer for the newly created contract --- */
        final var account = EntityIdUtils.accountIdFromEvmAddress(newContractAddress);
        if (key == STANDIN_CONTRACT_ID_KEY) {
            key = new JContractIDKey(account.getShardNum(), account.getRealmNum(), account.getAccountNum());
        }
        final var customizer = new HederaAccountCustomizer().key(key).memo(op.getMemo()).proxy(proxyAccount.asEntityId()).expiry(expiry).autoRenewPeriod(op.getAutoRenewPeriod().getSeconds()).isSmartContract(true);
        hederaLedger.customizePotentiallyDeleted(account, customizer);
    } else {
        worldState.reclaimContractId();
    }
    /* --- Customize sponsored Accounts */
    worldState.customizeSponsoredAccounts();
    /* --- Externalise changes --- */
    for (final var createdContract : createdContracts) {
        sigImpactHistorian.markEntityChanged(createdContract.getContractNum());
    }
    if (result.isSuccessful()) {
        final var newEvmAddress = newContractAddress.toArrayUnsafe();
        final var newContractId = contractIdFromEvmAddress(newEvmAddress);
        sigImpactHistorian.markEntityChanged(newContractId.getContractNum());
        txnCtx.setTargetedContract(newContractId);
        recordService.externalizeSuccessfulEvmCreate(result, newEvmAddress);
    } else {
        recordService.externalizeUnsuccessfulEvmCreate(result);
    }
}
Also used : JContractIDKey(com.hedera.services.legacy.core.jproto.JContractIDKey) HederaAccountCustomizer(com.hedera.services.ledger.accounts.HederaAccountCustomizer)

Aggregations

JContractIDKey (com.hedera.services.legacy.core.jproto.JContractIDKey)7 BeforeEach (org.junit.jupiter.api.BeforeEach)4 HFileMeta (com.hedera.services.files.HFileMeta)3 HederaAccountCustomizer (com.hedera.services.ledger.accounts.HederaAccountCustomizer)2 Consumer (java.util.function.Consumer)2 MockAccountNumbers (com.hedera.services.config.MockAccountNumbers)1 TransactionContext (com.hedera.services.context.TransactionContext)1 GlobalDynamicProperties (com.hedera.services.context.properties.GlobalDynamicProperties)1 PropertySource (com.hedera.services.context.properties.PropertySource)1 FeeCalculator (com.hedera.services.fees.FeeCalculator)1 JDelegatableContractIDKey (com.hedera.services.legacy.core.jproto.JDelegatableContractIDKey)1 TxnScopedPlatformSigFactory (com.hedera.services.sigs.factories.TxnScopedPlatformSigFactory)1 PubKeyToSigBytes (com.hedera.services.sigs.sourcing.PubKeyToSigBytes)1 MerkleAccount (com.hedera.services.state.merkle.MerkleAccount)1 ExchangeRates (com.hedera.services.state.submerkle.ExchangeRates)1 PlatformTxnAccessor (com.hedera.services.utils.PlatformTxnAccessor)1 BiPredicate (java.util.function.BiPredicate)1 IntFunction (java.util.function.IntFunction)1 Predicate (java.util.function.Predicate)1 Test (org.junit.jupiter.api.Test)1