Search in sources :

Example 1 with MessageFrame

use of org.hyperledger.besu.evm.frame.MessageFrame in project hedera-services by hashgraph.

the class AbstractRecordingCreateOperation method spawnChildMessage.

private void spawnChildMessage(final MessageFrame frame) {
    // memory cost needs to be calculated prior to memory expansion
    final Gas cost = cost(frame);
    frame.decrementRemainingGas(cost);
    final Address address = frame.getRecipientAddress();
    final MutableAccount account = frame.getWorldUpdater().getAccount(address).getMutable();
    account.incrementNonce();
    final Wei value = Wei.wrap(frame.getStackItem(0));
    final long inputOffset = clampedToLong(frame.getStackItem(1));
    final long inputSize = clampedToLong(frame.getStackItem(2));
    final Bytes inputData = frame.readMemory(inputOffset, inputSize);
    final Address contractAddress = targetContractAddress(frame);
    final Gas childGasStipend = gasCalculator().gasAvailableForChildCreate(frame.getRemainingGas());
    frame.decrementRemainingGas(childGasStipend);
    final MessageFrame childFrame = MessageFrame.builder().type(MessageFrame.Type.CONTRACT_CREATION).messageFrameStack(frame.getMessageFrameStack()).worldUpdater(frame.getWorldUpdater().updater()).initialGas(childGasStipend).address(contractAddress).originator(frame.getOriginatorAddress()).contract(contractAddress).gasPrice(frame.getGasPrice()).inputData(Bytes.EMPTY).sender(frame.getRecipientAddress()).value(value).apparentValue(value).code(new Code(inputData, Hash.EMPTY)).blockValues(frame.getBlockValues()).depth(frame.getMessageStackDepth() + 1).completer(child -> complete(frame, child)).miningBeneficiary(frame.getMiningBeneficiary()).blockHashLookup(frame.getBlockHashLookup()).maxStackSize(frame.getMaxStackSize()).build();
    frame.incrementRemainingGas(cost);
    frame.getMessageFrameStack().addFirst(childFrame);
    frame.setState(MessageFrame.State.CODE_SUSPENDED);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Address(org.hyperledger.besu.datatypes.Address) MessageFrame(org.hyperledger.besu.evm.frame.MessageFrame) Gas(org.hyperledger.besu.evm.Gas) MutableAccount(org.hyperledger.besu.evm.account.MutableAccount) Wei(org.hyperledger.besu.datatypes.Wei) Code(org.hyperledger.besu.evm.Code)

Example 2 with MessageFrame

use of org.hyperledger.besu.evm.frame.MessageFrame in project hedera-services by hashgraph.

the class HederaOperationUtil method newContractExpiryIn.

/**
 * Returns the expiry to be used for a new contract. Climbs the {@link MessageFrame} and searches for the parent
 * {@link com.hedera.services.store.contracts.HederaWorldState.WorldStateAccount}. The expiry to be used is
 * the expiry of the first account found in the stack
 *
 * @param frame Current message frame
 * @return Expiry to be used for new contracts
 */
public static long newContractExpiryIn(MessageFrame frame) {
    long expiry = 0;
    HederaWorldState.WorldStateAccount hederaAccount;
    Iterator<MessageFrame> framesIterator = frame.getMessageFrameStack().iterator();
    MessageFrame messageFrame;
    while (framesIterator.hasNext()) {
        messageFrame = framesIterator.next();
        /* if this is the initial frame from the deque, check context vars first */
        if (!framesIterator.hasNext()) {
            OptionalLong expiryOptional = messageFrame.getContextVariable("expiry");
            if (expiryOptional.isPresent()) {
                expiry = expiryOptional.getAsLong();
                break;
            }
        }
        /* check if this messageFrame's sender account can be retrieved from state */
        final var updater = (HederaWorldUpdater) messageFrame.getWorldUpdater();
        hederaAccount = updater.getHederaAccount(frame.getSenderAddress());
        if (hederaAccount != null) {
            expiry = hederaAccount.getExpiry();
            break;
        }
    }
    return expiry;
}
Also used : HederaWorldUpdater(com.hedera.services.store.contracts.HederaWorldUpdater) MessageFrame(org.hyperledger.besu.evm.frame.MessageFrame) HederaWorldState(com.hedera.services.store.contracts.HederaWorldState) OptionalLong(java.util.OptionalLong)

Example 3 with MessageFrame

use of org.hyperledger.besu.evm.frame.MessageFrame in project hedera-services by hashgraph.

the class CallEvmTxProcessorTest method assertTransactionSenderAndValue.

@Test
void assertTransactionSenderAndValue() {
    // setup:
    doReturn(Optional.of(receiver.getId().asEvmAddress())).when(transaction).getTo();
    given(worldState.updater()).willReturn(mock(HederaWorldState.Updater.class));
    given(codeCache.getIfPresent(any())).willReturn(new Code());
    given(transaction.getSender()).willReturn(sender.getId().asEvmAddress());
    given(transaction.getValue()).willReturn(Wei.of(1L));
    final MessageFrame.Builder commonInitialFrame = MessageFrame.builder().messageFrameStack(mock(Deque.class)).maxStackSize(MAX_STACK_SIZE).worldUpdater(mock(WorldUpdater.class)).initialGas(mock(Gas.class)).originator(sender.getId().asEvmAddress()).gasPrice(mock(Wei.class)).sender(sender.getId().asEvmAddress()).value(Wei.of(transaction.getValue().getAsBigInteger())).apparentValue(Wei.of(transaction.getValue().getAsBigInteger())).blockValues(mock(BlockValues.class)).depth(0).completer(__ -> {
    }).miningBeneficiary(mock(Address.class)).blockHashLookup(h -> null);
    // when:
    MessageFrame buildMessageFrame = callEvmTxProcessor.buildInitialFrame(commonInitialFrame, worldState.updater(), (Address) transaction.getTo().get(), Bytes.EMPTY);
    // expect:
    assertEquals(transaction.getSender(), buildMessageFrame.getSenderAddress());
    assertEquals(transaction.getValue(), buildMessageFrame.getApparentValue());
}
Also used : WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) MessageFrame(org.hyperledger.besu.evm.frame.MessageFrame) Gas(org.hyperledger.besu.evm.Gas) BlockValues(org.hyperledger.besu.evm.frame.BlockValues) Wei(org.hyperledger.besu.datatypes.Wei) Code(org.hyperledger.besu.evm.Code) Test(org.junit.jupiter.api.Test)

Example 4 with MessageFrame

use of org.hyperledger.besu.evm.frame.MessageFrame in project hedera-services by hashgraph.

the class CreateEvmTxProcessorTest method assertTransactionSenderAndValue.

@Test
void assertTransactionSenderAndValue() {
    // setup:
    doReturn(Optional.of(receiver.getId().asEvmAddress())).when(transaction).getTo();
    given(transaction.getSender()).willReturn(sender.getId().asEvmAddress());
    given(transaction.getValue()).willReturn(Wei.of(1L));
    final MessageFrame.Builder commonInitialFrame = MessageFrame.builder().messageFrameStack(mock(Deque.class)).maxStackSize(MAX_STACK_SIZE).worldUpdater(mock(WorldUpdater.class)).initialGas(mock(Gas.class)).originator(sender.getId().asEvmAddress()).gasPrice(mock(Wei.class)).sender(sender.getId().asEvmAddress()).value(Wei.of(transaction.getValue().getAsBigInteger())).apparentValue(Wei.of(transaction.getValue().getAsBigInteger())).blockValues(mock(BlockValues.class)).depth(0).completer(__ -> {
    }).miningBeneficiary(mock(Address.class)).blockHashLookup(h -> null);
    // when:
    MessageFrame buildMessageFrame = createEvmTxProcessor.buildInitialFrame(commonInitialFrame, worldState.updater(), (Address) transaction.getTo().get(), Bytes.EMPTY);
    // expect:
    assertEquals(transaction.getSender(), buildMessageFrame.getSenderAddress());
    assertEquals(transaction.getValue(), buildMessageFrame.getApparentValue());
}
Also used : MessageFrame(org.hyperledger.besu.evm.frame.MessageFrame) Gas(org.hyperledger.besu.evm.Gas) BlockValues(org.hyperledger.besu.evm.frame.BlockValues) Wei(org.hyperledger.besu.datatypes.Wei) Test(org.junit.jupiter.api.Test)

Example 5 with MessageFrame

use of org.hyperledger.besu.evm.frame.MessageFrame in project hedera-services by hashgraph.

the class HederaSStoreOperationTest method executesWithZero.

@Test
void executesWithZero() {
    final UInt256 keyBytes = UInt256.fromBytes(keyBytesMock);
    final UInt256 valueBytes = UInt256.fromBytes(Bytes.fromHexString("0x12345678"));
    givenValidContext(keyBytes, valueBytes);
    given(mutableAccount.getStorageValue(any())).willReturn(UInt256.ZERO);
    final var expectedExpiry = 20L;
    Deque<MessageFrame> frameDeque = new ArrayDeque<>();
    frameDeque.add(messageFrame);
    given(messageFrame.getMessageFrameStack()).willReturn(frameDeque);
    given(messageFrame.getContextVariable("expiry")).willReturn(OptionalLong.of(expectedExpiry));
    given(messageFrame.getContextVariable("sbh")).willReturn(5L);
    given(messageFrame.getBlockValues()).willReturn(hederaBlockValues);
    given(messageFrame.getGasPrice()).willReturn(Wei.of(50000L));
    given(hederaBlockValues.getTimestamp()).willReturn(10L);
    final var result = subject.execute(messageFrame, evm);
    final var expected = new Operation.OperationResult(Optional.of(Gas.of(10)), Optional.empty());
    assertEquals(expected.getGasCost(), result.getGasCost());
    assertEquals(expected.getHaltReason(), result.getHaltReason());
    verify(mutableAccount).setStorageValue(any(), any());
    verify(messageFrame).storageWasUpdated(any(), any());
}
Also used : MessageFrame(org.hyperledger.besu.evm.frame.MessageFrame) UInt256(org.apache.tuweni.units.bigints.UInt256) ArrayDeque(java.util.ArrayDeque) Test(org.junit.jupiter.api.Test)

Aggregations

MessageFrame (org.hyperledger.besu.evm.frame.MessageFrame)73 Test (org.junit.Test)39 Bytes (org.apache.tuweni.bytes.Bytes)28 WorldUpdater (org.hyperledger.besu.evm.worldstate.WorldUpdater)15 ArrayDeque (java.util.ArrayDeque)13 Wei (org.hyperledger.besu.datatypes.Wei)12 Code (org.hyperledger.besu.evm.Code)12 MessageFrameTestFixture (org.hyperledger.besu.ethereum.core.MessageFrameTestFixture)11 OperationResult (org.hyperledger.besu.evm.operation.Operation.OperationResult)11 UInt256 (org.apache.tuweni.units.bigints.UInt256)10 Address (org.hyperledger.besu.datatypes.Address)10 EVM (org.hyperledger.besu.evm.EVM)10 TraceFrame (org.hyperledger.besu.ethereum.debug.TraceFrame)9 Bytes32 (org.apache.tuweni.bytes.Bytes32)8 ContractCreationProcessor (org.hyperledger.besu.evm.processor.ContractCreationProcessor)8 BlockHashLookup (org.hyperledger.besu.ethereum.vm.BlockHashLookup)7 MutableAccount (org.hyperledger.besu.evm.account.MutableAccount)7 Hash (org.hyperledger.besu.datatypes.Hash)6 Block (org.hyperledger.besu.ethereum.core.Block)6 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)6