Search in sources :

Example 1 with Wei

use of org.hyperledger.besu.datatypes.Wei in project hedera-services by hashgraph.

the class AbstractRecordingCreateOperation method execute.

@Override
public Operation.OperationResult execute(final MessageFrame frame, final EVM evm) {
    // We have a feature flag for CREATE2
    if (!isEnabled()) {
        return INVALID_RESPONSE;
    }
    // manual check because some reads won't come until the "complete" step.
    if (frame.stackSize() < getStackItemsConsumed()) {
        return UNDERFLOW_RESPONSE;
    }
    final Gas cost = cost(frame);
    final Optional<Gas> optionalCost = Optional.ofNullable(cost);
    if (cost != null) {
        if (frame.isStatic()) {
            return haltWith(optionalCost, ILLEGAL_STATE_CHANGE);
        } else if (frame.getRemainingGas().compareTo(cost) < 0) {
            return new Operation.OperationResult(optionalCost, Optional.of(ExceptionalHaltReason.INSUFFICIENT_GAS));
        }
        final Wei value = Wei.wrap(frame.getStackItem(0));
        final Address address = frame.getRecipientAddress();
        final MutableAccount account = frame.getWorldUpdater().getAccount(address).getMutable();
        frame.clearReturnData();
        if (value.compareTo(account.getBalance()) > 0 || frame.getMessageStackDepth() >= MAX_STACK_DEPTH) {
            fail(frame);
        } else {
            spawnChildMessage(frame);
        }
    }
    return new Operation.OperationResult(optionalCost, Optional.empty());
}
Also used : Address(org.hyperledger.besu.datatypes.Address) Gas(org.hyperledger.besu.evm.Gas) Wei(org.hyperledger.besu.datatypes.Wei) MutableAccount(org.hyperledger.besu.evm.account.MutableAccount) AbstractOperation(org.hyperledger.besu.evm.operation.AbstractOperation) Operation(org.hyperledger.besu.evm.operation.Operation)

Example 2 with Wei

use of org.hyperledger.besu.datatypes.Wei 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 3 with Wei

use of org.hyperledger.besu.datatypes.Wei in project hedera-services by hashgraph.

the class HederaSStoreOperation method execute.

@Override
public Operation.OperationResult execute(final MessageFrame frame, final EVM evm) {
    final UInt256 key = UInt256.fromBytes(frame.popStackItem());
    final UInt256 value = UInt256.fromBytes(frame.popStackItem());
    final MutableAccount account = frame.getWorldUpdater().getAccount(frame.getRecipientAddress()).getMutable();
    if (account == null) {
        return ILLEGAL_STATE_CHANGE;
    }
    UInt256 currentValue = account.getStorageValue(key);
    boolean currentZero = currentValue.isZero();
    boolean newZero = value.isZero();
    boolean checkCalculator = checkSuperCost;
    Gas gasCost = Gas.ZERO;
    if (currentZero && !newZero) {
        final var updater = (HederaWorldUpdater) frame.getWorldUpdater();
        HederaWorldState.WorldStateAccount hederaAccount = updater.getHederaAccount(frame.getRecipientAddress());
        long durationInSeconds = Math.max(0, (hederaAccount != null ? hederaAccount.getExpiry() : HederaOperationUtil.newContractExpiryIn(frame)) - frame.getBlockValues().getTimestamp());
        long sbh = frame.getMessageFrameStack().getLast().getContextVariable("sbh");
        Wei gasPrice = frame.getGasPrice();
        gasCost = Gas.of(calculateStorageGasNeeded(64, /*two 256-bit words*/
        durationInSeconds, sbh, gasPrice.toLong()));
        ((HederaWorldUpdater) frame.getWorldUpdater()).addSbhRefund(gasCost);
    } else {
        checkCalculator = true;
    }
    if (checkCalculator) {
        Address address = account.getAddress();
        boolean slotIsWarm = frame.warmUpStorage(address, key);
        gasCost = gasCost.max(gasCalculator().calculateStorageCost(account, key, value).plus(slotIsWarm ? Gas.ZERO : this.gasCalculator().getColdSloadCost()));
        frame.incrementGasRefund(gasCalculator().calculateStorageRefundAmount(account, key, value));
    }
    Optional<Gas> optionalCost = Optional.of(gasCost);
    final Gas remainingGas = frame.getRemainingGas();
    if (frame.isStatic()) {
        return new OperationResult(optionalCost, Optional.of(ExceptionalHaltReason.ILLEGAL_STATE_CHANGE));
    } else if (remainingGas.compareTo(gasCost) < 0) {
        return new OperationResult(optionalCost, Optional.of(ExceptionalHaltReason.INSUFFICIENT_GAS));
    }
    if (dynamicProperties.shouldEnableTraceability()) {
        HederaOperationUtil.cacheExistingValue(frame, account.getAddress(), key, currentValue);
    }
    account.setStorageValue(key, value);
    frame.storageWasUpdated(key, value);
    return new Operation.OperationResult(optionalCost, Optional.empty());
}
Also used : Address(org.hyperledger.besu.datatypes.Address) HederaWorldState(com.hedera.services.store.contracts.HederaWorldState) HederaWorldUpdater(com.hedera.services.store.contracts.HederaWorldUpdater) Gas(org.hyperledger.besu.evm.Gas) MutableAccount(org.hyperledger.besu.evm.account.MutableAccount) Wei(org.hyperledger.besu.datatypes.Wei) UInt256(org.apache.tuweni.units.bigints.UInt256)

Example 4 with Wei

use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.

the class GraphQLHttpServiceTest method query_jsonPost.

@Test
public void query_jsonPost() throws Exception {
    final RequestBody body = RequestBody.create("{\"query\":\"{gasPrice}\"}", JSON);
    final Wei price = Wei.of(16);
    Mockito.when(miningCoordinatorMock.getMinTransactionGasPrice()).thenReturn(price);
    try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
        // Check general format of result
        Assertions.assertThat(resp.code()).isEqualTo(200);
        final JsonObject json = new JsonObject(resp.body().string());
        testHelper.assertValidGraphQLResult(json);
        final String result = json.getJsonObject("data").getString("gasPrice");
        Assertions.assertThat(result).isEqualTo("0x10");
    }
}
Also used : Response(okhttp3.Response) JsonObject(io.vertx.core.json.JsonObject) Wei(org.hyperledger.besu.datatypes.Wei) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 5 with Wei

use of org.hyperledger.besu.datatypes.Wei in project besu by hyperledger.

the class GraphQLHttpServiceTest method query_get.

@Test
public void query_get() throws Exception {
    final Wei price = Wei.of(16);
    Mockito.when(blockchainQueries.gasPrice()).thenReturn(Optional.of(price.toLong()));
    Mockito.when(miningCoordinatorMock.getMinTransactionGasPrice()).thenReturn(price);
    try (final Response resp = client.newCall(buildGetRequest("?query={gasPrice}")).execute()) {
        Assertions.assertThat(resp.code()).isEqualTo(200);
        final JsonObject json = new JsonObject(resp.body().string());
        testHelper.assertValidGraphQLResult(json);
        final String result = json.getJsonObject("data").getString("gasPrice");
        Assertions.assertThat(result).isEqualTo("0x10");
    }
}
Also used : Response(okhttp3.Response) JsonObject(io.vertx.core.json.JsonObject) Wei(org.hyperledger.besu.datatypes.Wei) Test(org.junit.Test)

Aggregations

Wei (org.hyperledger.besu.datatypes.Wei)69 Address (org.hyperledger.besu.datatypes.Address)24 Test (org.junit.Test)20 Transaction (org.hyperledger.besu.ethereum.core.Transaction)18 Bytes (org.apache.tuweni.bytes.Bytes)16 Hash (org.hyperledger.besu.datatypes.Hash)14 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)13 MutableAccount (org.hyperledger.besu.evm.account.MutableAccount)12 Account (org.hyperledger.besu.evm.account.Account)9 WorldUpdater (org.hyperledger.besu.evm.worldstate.WorldUpdater)9 Optional (java.util.Optional)8 TransactionInvalidReason (org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason)7 ArrayList (java.util.ArrayList)6 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)6 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)6 JsonObject (io.vertx.core.json.JsonObject)5 Response (okhttp3.Response)5 Bytes32 (org.apache.tuweni.bytes.Bytes32)5 MessageFrame (org.hyperledger.besu.evm.frame.MessageFrame)5 List (java.util.List)4