Reduce UInt256 in EVMOperations (#5331)

Performance improvements
- Reduce the use of UInt256 in operations, replacing them with Bytes as
   appropriate.
- Use Java17 expression switch

Signed-off-by: Danno Ferrin <danno.ferrin@swirldslabs.com>
pull/5374/head
Danno Ferrin 2 years ago committed by GitHub
parent 9eec16eed4
commit b3d3f54fff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 268
      evm/src/main/java/org/hyperledger/besu/evm/EVM.java
  3. 29
      evm/src/main/java/org/hyperledger/besu/evm/internal/Words.java
  4. 7
      evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCallOperation.java
  5. 7
      evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCreateOperation.java
  6. 6
      evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractOperation.java
  7. 3
      evm/src/main/java/org/hyperledger/besu/evm/operation/AddModOperation.java
  8. 8
      evm/src/main/java/org/hyperledger/besu/evm/operation/AndOperation.java
  9. 4
      evm/src/main/java/org/hyperledger/besu/evm/operation/BalanceOperation.java
  10. 10
      evm/src/main/java/org/hyperledger/besu/evm/operation/BlockHashOperation.java
  11. 32
      evm/src/main/java/org/hyperledger/besu/evm/operation/ByteOperation.java
  12. 20
      evm/src/main/java/org/hyperledger/besu/evm/operation/CallDataLoadOperation.java
  13. 4
      evm/src/main/java/org/hyperledger/besu/evm/operation/CallDataSizeOperation.java
  14. 2
      evm/src/main/java/org/hyperledger/besu/evm/operation/CallValueOperation.java
  15. 5
      evm/src/main/java/org/hyperledger/besu/evm/operation/ChainIdOperation.java
  16. 5
      evm/src/main/java/org/hyperledger/besu/evm/operation/CodeSizeOperation.java
  17. 3
      evm/src/main/java/org/hyperledger/besu/evm/operation/Create2Operation.java
  18. 3
      evm/src/main/java/org/hyperledger/besu/evm/operation/DivOperation.java
  19. 7
      evm/src/main/java/org/hyperledger/besu/evm/operation/EqOperation.java
  20. 39
      evm/src/main/java/org/hyperledger/besu/evm/operation/ExpOperation.java
  21. 6
      evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeHashOperation.java
  22. 4
      evm/src/main/java/org/hyperledger/besu/evm/operation/ExtCodeSizeOperation.java
  23. 5
      evm/src/main/java/org/hyperledger/besu/evm/operation/GasLimitOperation.java
  24. 3
      evm/src/main/java/org/hyperledger/besu/evm/operation/GasOperation.java
  25. 2
      evm/src/main/java/org/hyperledger/besu/evm/operation/GasPriceOperation.java
  26. 8
      evm/src/main/java/org/hyperledger/besu/evm/operation/GtOperation.java
  27. 6
      evm/src/main/java/org/hyperledger/besu/evm/operation/IsZeroOperation.java
  28. 3
      evm/src/main/java/org/hyperledger/besu/evm/operation/JumpDestOperation.java
  29. 18
      evm/src/main/java/org/hyperledger/besu/evm/operation/JumpOperation.java
  30. 27
      evm/src/main/java/org/hyperledger/besu/evm/operation/JumpiOperation.java
  31. 3
      evm/src/main/java/org/hyperledger/besu/evm/operation/Keccak256Operation.java
  32. 8
      evm/src/main/java/org/hyperledger/besu/evm/operation/LtOperation.java
  33. 5
      evm/src/main/java/org/hyperledger/besu/evm/operation/MSizeOperation.java
  34. 3
      evm/src/main/java/org/hyperledger/besu/evm/operation/MulModOperation.java
  35. 7
      evm/src/main/java/org/hyperledger/besu/evm/operation/NotOperation.java
  36. 5
      evm/src/main/java/org/hyperledger/besu/evm/operation/NumberOperation.java
  37. 8
      evm/src/main/java/org/hyperledger/besu/evm/operation/OrOperation.java
  38. 5
      evm/src/main/java/org/hyperledger/besu/evm/operation/PCOperation.java
  39. 4
      evm/src/main/java/org/hyperledger/besu/evm/operation/ReturnDataSizeOperation.java
  40. 10
      evm/src/main/java/org/hyperledger/besu/evm/operation/SDivOperation.java
  41. 3
      evm/src/main/java/org/hyperledger/besu/evm/operation/SGtOperation.java
  42. 3
      evm/src/main/java/org/hyperledger/besu/evm/operation/SLtOperation.java
  43. 3
      evm/src/main/java/org/hyperledger/besu/evm/operation/SModOperation.java
  44. 11
      evm/src/main/java/org/hyperledger/besu/evm/operation/SarOperation.java
  45. 4
      evm/src/main/java/org/hyperledger/besu/evm/operation/SelfBalanceOperation.java
  46. 5
      evm/src/main/java/org/hyperledger/besu/evm/operation/ShlOperation.java
  47. 5
      evm/src/main/java/org/hyperledger/besu/evm/operation/ShrOperation.java
  48. 30
      evm/src/main/java/org/hyperledger/besu/evm/operation/SignExtendOperation.java
  49. 21
      evm/src/main/java/org/hyperledger/besu/evm/operation/SubOperation.java
  50. 5
      evm/src/main/java/org/hyperledger/besu/evm/operation/TimestampOperation.java
  51. 8
      evm/src/main/java/org/hyperledger/besu/evm/operation/XorOperation.java
  52. 3
      evm/src/main/java/org/hyperledger/besu/evm/precompile/KZGPointEvalPrecompiledContract.java
  53. 5
      evm/src/main/java/org/hyperledger/besu/evm/tracing/StandardJsonTracer.java
  54. 4
      evm/src/test/java/org/hyperledger/besu/evm/operations/BaseFeeOperationTest.java
  55. 3
      evm/src/test/java/org/hyperledger/besu/evm/operations/ChainIdOperationTest.java
  56. 7
      evm/src/test/java/org/hyperledger/besu/evm/operations/Create2OperationTest.java
  57. 13
      evm/src/test/java/org/hyperledger/besu/evm/operations/CreateOperationTest.java
  58. 7
      evm/src/test/java/org/hyperledger/besu/evm/operations/ExtCodeHashOperationTest.java
  59. 3
      evm/src/test/java/org/hyperledger/besu/evm/operations/RelativeJumpOperationTest.java
  60. 32
      evm/src/test/java/org/hyperledger/besu/evm/operations/SarOperationTest.java
  61. 27
      evm/src/test/java/org/hyperledger/besu/evm/operations/ShlOperationTest.java
  62. 39
      evm/src/test/java/org/hyperledger/besu/evm/operations/ShrOperationTest.java
  63. 4
      evm/src/test/java/org/hyperledger/besu/evm/testutils/TestMessageFrameBuilder.java

@ -17,6 +17,7 @@
- Add new sepolia bootnodes, which should improve peering in the testnet. [#5352](https://github.com/hyperledger/besu/pull/5352)
- Renamed --bonsai-maximum-back-layers-to-load option to --bonsai-historical-block-limit for clarity. Removed --Xbonsai-use-snapshots option as it is no longer functional [#5337](https://github.com/hyperledger/besu/pull/5337)
- Change Forest to use TransactionDB instead of OptimisticTransactionDB [#5328](https://github.com/hyperledger/besu/pull/5328)
- Performance: Reduced usage of UInt256 in EVM operations [#5331](https://github.com/hyperledger/besu/pull/5331)
### Bug Fixes

@ -27,12 +27,22 @@ import org.hyperledger.besu.evm.internal.CodeCache;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.evm.internal.FixedStack.OverflowException;
import org.hyperledger.besu.evm.internal.FixedStack.UnderflowException;
import org.hyperledger.besu.evm.operation.AddModOperation;
import org.hyperledger.besu.evm.operation.AddOperation;
import org.hyperledger.besu.evm.operation.AndOperation;
import org.hyperledger.besu.evm.operation.ByteOperation;
import org.hyperledger.besu.evm.operation.DivOperation;
import org.hyperledger.besu.evm.operation.DupOperation;
import org.hyperledger.besu.evm.operation.ExpOperation;
import org.hyperledger.besu.evm.operation.GtOperation;
import org.hyperledger.besu.evm.operation.InvalidOperation;
import org.hyperledger.besu.evm.operation.IsZeroOperation;
import org.hyperledger.besu.evm.operation.JumpDestOperation;
import org.hyperledger.besu.evm.operation.JumpOperation;
import org.hyperledger.besu.evm.operation.JumpiOperation;
import org.hyperledger.besu.evm.operation.LtOperation;
import org.hyperledger.besu.evm.operation.ModOperation;
import org.hyperledger.besu.evm.operation.MulModOperation;
import org.hyperledger.besu.evm.operation.MulOperation;
import org.hyperledger.besu.evm.operation.NotOperation;
import org.hyperledger.besu.evm.operation.Operation;
@ -42,11 +52,13 @@ import org.hyperledger.besu.evm.operation.OrOperation;
import org.hyperledger.besu.evm.operation.PopOperation;
import org.hyperledger.besu.evm.operation.Push0Operation;
import org.hyperledger.besu.evm.operation.PushOperation;
import org.hyperledger.besu.evm.operation.SDivOperation;
import org.hyperledger.besu.evm.operation.SGtOperation;
import org.hyperledger.besu.evm.operation.SLtOperation;
import org.hyperledger.besu.evm.operation.SModOperation;
import org.hyperledger.besu.evm.operation.SignExtendOperation;
import org.hyperledger.besu.evm.operation.StopOperation;
import org.hyperledger.besu.evm.operation.SubOperation;
import org.hyperledger.besu.evm.operation.SwapOperation;
import org.hyperledger.besu.evm.operation.VirtualOperation;
import org.hyperledger.besu.evm.operation.XorOperation;
@ -152,161 +164,107 @@ public class EVM {
OperationResult result;
try {
switch (opcode) {
// case 0x00: // STOP
// result = StopOperation.staticOperation(frame);
// break;
case 0x01: // ADD
result = AddOperation.staticOperation(frame);
break;
case 0x02: // MUL
result = MulOperation.staticOperation(frame);
break;
// case 0x03: // SUB
// result = SubOperation.staticOperation(frame);
// break;
// case 0x04: // DIV
// result = DivOperation.staticOperation(frame);
// break;
// case 0x05: // SDIV
// result = SDivOperation.staticOperation(frame);
// break;
// case 0x06: // MOD
// result = ModOperation.staticOperation(frame);
// break;
case 0x07: // SMOD
result = SModOperation.staticOperation(frame);
break;
// case 0x08: // ADDMOD
// result = AddModOperation.staticOperation(frame);
// break;
// case 0x09: // MULMOD
// result = MulModOperation.staticOperation(frame);
// break;
// case 0x0a: //EXP requires gasCalculator access, so it is skipped
case 0x0b: // SIGNEXTEND
result = SignExtendOperation.staticOperation(frame);
break;
case 0x0c:
case 0x0d:
case 0x0e:
case 0x0f:
result = InvalidOperation.INVALID_RESULT;
break;
// case 0x10: // LT
// result = LtOperation.staticOperation(frame);
// break;
// case 0x11: // GT
// result = GtOperation.staticOperation(frame);
// break;
case 0x12: // SLT
result = SLtOperation.staticOperation(frame);
break;
case 0x13: // SGT
result = SGtOperation.staticOperation(frame);
break;
case 0x15: // ISZERO
result = IsZeroOperation.staticOperation(frame);
break;
case 0x16: // AND
result = AndOperation.staticOperation(frame);
break;
case 0x17: // OR
result = OrOperation.staticOperation(frame);
break;
case 0x18: // XOR
result = XorOperation.staticOperation(frame);
break;
case 0x19: // NOT
result = NotOperation.staticOperation(frame);
break;
case 0x1a: // BYTE
result = ByteOperation.staticOperation(frame);
break;
case 0x50: // POP
result = PopOperation.staticOperation(frame);
break;
case 0x5f: // PUSH0
result =
enableShanghai
? Push0Operation.staticOperation(frame)
: InvalidOperation.INVALID_RESULT;
break;
case 0x60: // PUSH1-32
case 0x61:
case 0x62:
case 0x63:
case 0x64:
case 0x65:
case 0x66:
case 0x67:
case 0x68:
case 0x69:
case 0x6a:
case 0x6b:
case 0x6c:
case 0x6d:
case 0x6e:
case 0x6f:
case 0x70:
case 0x71:
case 0x72:
case 0x73:
case 0x74:
case 0x75:
case 0x76:
case 0x77:
case 0x78:
case 0x79:
case 0x7a:
case 0x7b:
case 0x7c:
case 0x7d:
case 0x7e:
case 0x7f:
result = PushOperation.staticOperation(frame, code, pc, opcode - PUSH_BASE);
break;
case 0x80: // DUP1-16
case 0x81:
case 0x82:
case 0x83:
case 0x84:
case 0x85:
case 0x86:
case 0x87:
case 0x88:
case 0x89:
case 0x8a:
case 0x8b:
case 0x8c:
case 0x8d:
case 0x8e:
case 0x8f:
result = DupOperation.staticOperation(frame, opcode - DupOperation.DUP_BASE);
break;
case 0x90: // SWAP1-16
case 0x91:
case 0x92:
case 0x93:
case 0x94:
case 0x95:
case 0x96:
case 0x97:
case 0x98:
case 0x99:
case 0x9a:
case 0x9b:
case 0x9c:
case 0x9d:
case 0x9e:
case 0x9f:
result = SwapOperation.staticOperation(frame, opcode - SWAP_BASE);
break;
default: // unoptimized operations
frame.setCurrentOperation(currentOperation);
result = currentOperation.execute(frame, this);
break;
}
result =
switch (opcode) {
case 0x00 -> StopOperation.staticOperation(frame);
case 0x01 -> AddOperation.staticOperation(frame);
case 0x02 -> MulOperation.staticOperation(frame);
case 0x03 -> SubOperation.staticOperation(frame);
case 0x04 -> DivOperation.staticOperation(frame);
case 0x05 -> SDivOperation.staticOperation(frame);
case 0x06 -> ModOperation.staticOperation(frame);
case 0x07 -> SModOperation.staticOperation(frame);
case 0x08 -> AddModOperation.staticOperation(frame);
case 0x09 -> MulModOperation.staticOperation(frame);
case 0x0a -> ExpOperation.staticOperation(frame, gasCalculator);
case 0x0b -> SignExtendOperation.staticOperation(frame);
case 0x0c, 0x0d, 0x0e, 0x0f -> InvalidOperation.INVALID_RESULT;
case 0x10 -> LtOperation.staticOperation(frame);
case 0x11 -> GtOperation.staticOperation(frame);
case 0x12 -> SLtOperation.staticOperation(frame);
case 0x13 -> SGtOperation.staticOperation(frame);
case 0x15 -> IsZeroOperation.staticOperation(frame);
case 0x16 -> AndOperation.staticOperation(frame);
case 0x17 -> OrOperation.staticOperation(frame);
case 0x18 -> XorOperation.staticOperation(frame);
case 0x19 -> NotOperation.staticOperation(frame);
case 0x1a -> ByteOperation.staticOperation(frame);
case 0x50 -> PopOperation.staticOperation(frame);
case 0x56 -> JumpOperation.staticOperation(frame);
case 0x57 -> JumpiOperation.staticOperation(frame);
case 0x5b -> JumpDestOperation.JUMPDEST_SUCCESS;
case 0x5f -> enableShanghai
? Push0Operation.staticOperation(frame)
: InvalidOperation.INVALID_RESULT;
case 0x60, // PUSH1-32
0x61,
0x62,
0x63,
0x64,
0x65,
0x66,
0x67,
0x68,
0x69,
0x6a,
0x6b,
0x6c,
0x6d,
0x6e,
0x6f,
0x70,
0x71,
0x72,
0x73,
0x74,
0x75,
0x76,
0x77,
0x78,
0x79,
0x7a,
0x7b,
0x7c,
0x7d,
0x7e,
0x7f -> PushOperation.staticOperation(frame, code, pc, opcode - PUSH_BASE);
case 0x80, // DUP1-16
0x81,
0x82,
0x83,
0x84,
0x85,
0x86,
0x87,
0x88,
0x89,
0x8a,
0x8b,
0x8c,
0x8d,
0x8e,
0x8f -> DupOperation.staticOperation(frame, opcode - DupOperation.DUP_BASE);
case 0x90, // SWAP1-16
0x91,
0x92,
0x93,
0x94,
0x95,
0x96,
0x97,
0x98,
0x99,
0x9a,
0x9b,
0x9c,
0x9d,
0x9e,
0x9f -> SwapOperation.staticOperation(frame, opcode - SWAP_BASE);
default -> { // unoptimized operations
frame.setCurrentOperation(currentOperation);
yield currentOperation.execute(frame, this);
}
};
} catch (final OverflowException oe) {
result = OVERFLOW_RESPONSE;
} catch (final UnderflowException ue) {

@ -193,4 +193,33 @@ public interface Words {
}
return (array[index] << 8) | (array[index + 1] & 0xff);
}
/**
* Get the big-endian Bytes representation of an unsigned int, including leading zeros
*
* @param value the int value
* @return a Bytes object of the value, Big Endian order
*/
static Bytes intBytes(final int value) {
return Bytes.of(
(byte) (value >>> 24), (byte) (value >>> 16), (byte) (value >>> 8), (byte) value);
}
/**
* Get the big-endian Bytes representation of an unsigned int, including leading zeros
*
* @param value the long value
* @return a Bytes object of the value, Big Endian order
*/
static Bytes longBytes(final long value) {
return Bytes.of(
(byte) (value >>> 56),
(byte) (value >>> 48),
(byte) (value >>> 40),
(byte) (value >>> 32),
(byte) (value >>> 24),
(byte) (value >>> 16),
(byte) (value >>> 8),
(byte) value);
}
}

@ -27,7 +27,6 @@ import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/**
* A skeleton class for implementing call operations.
@ -184,7 +183,7 @@ public abstract class AbstractCallOperation extends AbstractOperation {
frame.expandMemory(outputDataOffset(frame), outputDataLength(frame));
frame.incrementRemainingGas(gasAvailableForChildCall(frame) + cost);
frame.popStackItems(getStackItemsConsumed());
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(FAILURE_STACK_ITEM);
return new OperationResult(cost, null);
}
@ -268,9 +267,9 @@ public abstract class AbstractCallOperation extends AbstractOperation {
frame.popStackItems(getStackItemsConsumed());
if (childFrame.getState() == MessageFrame.State.COMPLETED_SUCCESS) {
frame.mergeWarmedUpFields(childFrame);
frame.pushStackItem(UInt256.ONE);
frame.pushStackItem(SUCCESS_STACK_ITEM);
} else {
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(FAILURE_STACK_ITEM);
}
final int currentPC = frame.getPC();

@ -28,7 +28,6 @@ import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.internal.Words;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The Abstract create operation. */
public abstract class AbstractCreateOperation extends AbstractOperation {
@ -132,7 +131,7 @@ public abstract class AbstractCreateOperation extends AbstractOperation {
final long inputSize = clampedToLong(frame.getStackItem(2));
frame.readMutableMemory(inputOffset, inputSize);
frame.popStackItems(getStackItemsConsumed());
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(FAILURE_STACK_ITEM);
}
private void spawnChildMessage(final MessageFrame frame, final Code code, final EVM evm) {
@ -189,12 +188,12 @@ public abstract class AbstractCreateOperation extends AbstractOperation {
frame.pushStackItem(Words.fromAddress(childFrame.getContractAddress()));
} else {
frame.setReturnData(childFrame.getOutputData());
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(FAILURE_STACK_ITEM);
}
} else {
frame.getWorldUpdater().deleteAccount(childFrame.getRecipientAddress());
frame.setReturnData(childFrame.getOutputData());
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(FAILURE_STACK_ITEM);
}
final int currentPC = frame.getPC();

@ -16,12 +16,18 @@ package org.hyperledger.besu.evm.operation;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes;
/**
* All {@link Operation} implementations should inherit from this class to get the setting of some
* members for free.
*/
public abstract class AbstractOperation implements Operation {
static final Bytes BYTES_ONE = Bytes.of(1);
static final Bytes SUCCESS_STACK_ITEM = BYTES_ONE;
static final Bytes FAILURE_STACK_ITEM = Bytes.EMPTY;
private final int opcode;
private final String name;
private final int stackItemsConsumed;

@ -22,7 +22,6 @@ import java.math.BigInteger;
import java.util.Arrays;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The Add mod operation. */
public class AddModOperation extends AbstractFixedCostOperation {
@ -57,7 +56,7 @@ public class AddModOperation extends AbstractFixedCostOperation {
final Bytes value2 = frame.popStackItem();
if (value2.isZero()) {
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(FAILURE_STACK_ITEM);
} else {
BigInteger b0 = new BigInteger(1, value0.toArrayUnsafe());
BigInteger b1 = new BigInteger(1, value1.toArrayUnsafe());

@ -18,7 +18,7 @@ import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.bytes.Bytes;
/** The And operation. */
public class AndOperation extends AbstractFixedCostOperation {
@ -48,10 +48,10 @@ public class AndOperation extends AbstractFixedCostOperation {
* @return the operation result
*/
public static OperationResult staticOperation(final MessageFrame frame) {
final UInt256 value0 = UInt256.fromBytes(frame.popStackItem());
final UInt256 value1 = UInt256.fromBytes(frame.popStackItem());
final Bytes value0 = frame.popStackItem();
final Bytes value1 = frame.popStackItem();
final UInt256 result = value0.and(value1);
final Bytes result = value0.and(value1);
frame.pushStackItem(result);
return andSuccess;

@ -24,7 +24,7 @@ import org.hyperledger.besu.evm.internal.FixedStack.OverflowException;
import org.hyperledger.besu.evm.internal.FixedStack.UnderflowException;
import org.hyperledger.besu.evm.internal.Words;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.bytes.Bytes;
/** The Balance operation. */
public class BalanceOperation extends AbstractOperation {
@ -62,7 +62,7 @@ public class BalanceOperation extends AbstractOperation {
return new OperationResult(cost, ExceptionalHaltReason.INSUFFICIENT_GAS);
} else {
final Account account = frame.getWorldUpdater().get(address);
frame.pushStackItem(account == null ? UInt256.ZERO : account.getBalance());
frame.pushStackItem(account == null ? Bytes.EMPTY : account.getBalance());
return new OperationResult(cost, null);
}
} catch (final UnderflowException ufe) {

@ -22,6 +22,8 @@ import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import java.util.function.Function;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
/** The Block hash operation. */
@ -41,10 +43,10 @@ public class BlockHashOperation extends AbstractFixedCostOperation {
@Override
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
final UInt256 blockArg = UInt256.fromBytes(frame.popStackItem());
final Bytes blockArg = frame.popStackItem().trimLeadingZeros();
// Short-circuit if value is unreasonably large
if (!blockArg.fitsLong()) {
if (blockArg.size() > 8) {
frame.pushStackItem(UInt256.ZERO);
return successResponse;
}
@ -59,11 +61,11 @@ public class BlockHashOperation extends AbstractFixedCostOperation {
if (currentBlockNumber == 0
|| soughtBlock < (mostRecentBlockNumber - MAX_RELATIVE_BLOCK)
|| soughtBlock > mostRecentBlockNumber) {
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(Bytes32.ZERO);
} else {
final Function<Long, Hash> blockHashLookup = frame.getBlockHashLookup();
final Hash blockHash = blockHashLookup.apply(soughtBlock);
frame.pushStackItem(UInt256.fromBytes(blockHash));
frame.pushStackItem(blockHash);
}
return successResponse;

@ -18,8 +18,7 @@ import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.MutableBytes32;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.bytes.Bytes;
/** The Byte operation. */
public class ByteOperation extends AbstractFixedCostOperation {
@ -36,20 +35,21 @@ public class ByteOperation extends AbstractFixedCostOperation {
super(0x1A, "BYTE", 2, 1, gasCalculator, gasCalculator.getVeryLowTierGasCost());
}
private static UInt256 getByte(final UInt256 seq, final UInt256 offset) {
if (!offset.fitsInt()) {
return UInt256.ZERO;
private static Bytes getByte(final Bytes seq, final Bytes offset) {
Bytes trimmedOffset = offset.trimLeadingZeros();
if (trimmedOffset.size() > 1) {
return Bytes.EMPTY;
}
final int index = trimmedOffset.toInt();
final int index = offset.intValue();
if (index >= 32) {
return UInt256.ZERO;
int size = seq.size();
int pos = index - 32 + size;
if (pos >= size || pos < 0) {
return Bytes.EMPTY;
} else {
final byte b = seq.get(pos);
return Bytes.of(b);
}
final byte b = seq.get(index);
final MutableBytes32 res = MutableBytes32.create();
res.set(31, b);
return UInt256.fromBytes(res);
}
@Override
@ -65,11 +65,11 @@ public class ByteOperation extends AbstractFixedCostOperation {
* @return the operation result
*/
public static OperationResult staticOperation(final MessageFrame frame) {
final UInt256 value0 = UInt256.fromBytes(frame.popStackItem());
final UInt256 value1 = UInt256.fromBytes(frame.popStackItem());
final Bytes value0 = frame.popStackItem();
final Bytes value1 = frame.popStackItem();
// Stack items are reversed for the BYTE operation.
final UInt256 result = getByte(value1, value0);
final Bytes result = getByte(value1, value0);
frame.pushStackItem(result);

@ -21,7 +21,6 @@ import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.bytes.MutableBytes32;
import org.apache.tuweni.units.bigints.UInt256;
/** The Call data load operation. */
public class CallDataLoadOperation extends AbstractFixedCostOperation {
@ -38,23 +37,30 @@ public class CallDataLoadOperation extends AbstractFixedCostOperation {
@Override
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
final UInt256 startWord = UInt256.fromBytes(frame.popStackItem());
final Bytes startWord = frame.popStackItem().trimLeadingZeros();
// If the start index doesn't fit a int, it comes after anything in data, and so the returned
// If the start index doesn't fit in an int, it comes after anything in data, and so the
// returned
// word should be zero.
if (!startWord.fitsInt()) {
frame.pushStackItem(UInt256.ZERO);
if (startWord.size() > 4) {
frame.pushStackItem(Bytes.EMPTY);
return successResponse;
}
final int offset = startWord.intValue();
final int offset = startWord.toInt();
if (offset < 0) {
frame.pushStackItem(Bytes.EMPTY);
return successResponse;
}
final Bytes data = frame.getInputData();
final MutableBytes32 res = MutableBytes32.create();
if (offset < data.size()) {
final Bytes toCopy = data.slice(offset, Math.min(Bytes32.SIZE, data.size() - offset));
toCopy.copyTo(res, 0);
frame.pushStackItem(res.copy());
} else {
frame.pushStackItem(Bytes.EMPTY);
}
frame.pushStackItem(UInt256.fromBytes(res));
return successResponse;
}

@ -17,9 +17,9 @@ package org.hyperledger.besu.evm.operation;
import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.internal.Words;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The Call data size operation. */
public class CallDataSizeOperation extends AbstractFixedCostOperation {
@ -37,7 +37,7 @@ public class CallDataSizeOperation extends AbstractFixedCostOperation {
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
final Bytes callData = frame.getInputData();
frame.pushStackItem(UInt256.valueOf(callData.size()));
frame.pushStackItem(Words.intBytes(callData.size()));
return successResponse;
}

@ -35,7 +35,7 @@ public class CallValueOperation extends AbstractFixedCostOperation {
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
final Wei value = frame.getApparentValue();
frame.pushStackItem(value.toUInt256());
frame.pushStackItem(value.toBytes());
return successResponse;
}

@ -19,12 +19,11 @@ import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
/** The Chain id operation. */
public class ChainIdOperation extends AbstractFixedCostOperation {
private final UInt256 chainId;
private final Bytes32 chainId;
/**
* Instantiates a new Chain id operation.
@ -34,7 +33,7 @@ public class ChainIdOperation extends AbstractFixedCostOperation {
*/
public ChainIdOperation(final GasCalculator gasCalculator, final Bytes32 chainId) {
super(0x46, "CHAINID", 0, 1, gasCalculator, gasCalculator.getBaseTierGasCost());
this.chainId = UInt256.fromBytes(chainId);
this.chainId = chainId;
}
@Override

@ -18,8 +18,7 @@ import org.hyperledger.besu.evm.Code;
import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import org.hyperledger.besu.evm.internal.Words;
/** The Code size operation. */
public class CodeSizeOperation extends AbstractFixedCostOperation {
@ -37,7 +36,7 @@ public class CodeSizeOperation extends AbstractFixedCostOperation {
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
final Code code = frame.getCode();
frame.pushStackItem(UInt256.valueOf(code.getSize()));
frame.pushStackItem(Words.intBytes(code.getSize()));
return successResponse;
}

@ -23,7 +23,6 @@ import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
/** The Create2 operation. */
public class Create2Operation extends AbstractCreateOperation {
@ -45,7 +44,7 @@ public class Create2Operation extends AbstractCreateOperation {
final Address sender = frame.getRecipientAddress();
final long offset = clampedToLong(frame.getStackItem(1));
final long length = clampedToLong(frame.getStackItem(2));
final Bytes32 salt = UInt256.fromBytes(frame.getStackItem(3));
final Bytes32 salt = Bytes32.leftPad(frame.getStackItem(3));
final Bytes initCode = frame.readMutableMemory(offset, length);
final Bytes32 hash = keccak256(Bytes.concatenate(PREFIX, sender, salt, keccak256(initCode)));
final Address address = Address.extract(hash);

@ -21,7 +21,6 @@ import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import java.math.BigInteger;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The Div operation. */
public class DivOperation extends AbstractFixedCostOperation {
@ -56,7 +55,7 @@ public class DivOperation extends AbstractFixedCostOperation {
final Bytes value1 = frame.popStackItem();
if (value1.isZero()) {
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(Bytes.EMPTY);
} else {
BigInteger b1 = new BigInteger(1, value0.toArrayUnsafe());
BigInteger b2 = new BigInteger(1, value1.toArrayUnsafe());

@ -18,6 +18,7 @@ import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The Eq operation. */
@ -48,10 +49,10 @@ public class EqOperation extends AbstractFixedCostOperation {
* @return the operation result
*/
public static OperationResult staticOperation(final MessageFrame frame) {
final UInt256 value0 = UInt256.fromBytes(frame.popStackItem());
final UInt256 value1 = UInt256.fromBytes(frame.popStackItem());
final Bytes value0 = frame.popStackItem().trimLeadingZeros();
final Bytes value1 = frame.popStackItem().trimLeadingZeros();
final UInt256 result = (value0.equals(value1) ? UInt256.ONE : UInt256.ZERO);
final Bytes result = (value0.equals(value1) ? UInt256.ONE : UInt256.ZERO);
frame.pushStackItem(result);

@ -19,11 +19,15 @@ import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import java.math.BigInteger;
import org.apache.tuweni.bytes.Bytes;
/** The Exp operation. */
public class ExpOperation extends AbstractOperation {
static final BigInteger MOD_BASE = BigInteger.TWO.pow(256);
/**
* Instantiates a new Exp operation.
*
@ -35,19 +39,42 @@ public class ExpOperation extends AbstractOperation {
@Override
public OperationResult execute(final MessageFrame frame, final EVM evm) {
final UInt256 number = UInt256.fromBytes(frame.popStackItem());
final UInt256 power = UInt256.fromBytes(frame.popStackItem());
return staticOperation(frame, gasCalculator());
}
/**
* Performs exp operation.
*
* @param frame the frame
* @param gasCalculator the gas calculator
* @return the operation result
*/
public static OperationResult staticOperation(
final MessageFrame frame, final GasCalculator gasCalculator) {
final Bytes number = frame.popStackItem();
final Bytes power = frame.popStackItem();
final int numBytes = (power.bitLength() + 7) / 8;
final long cost = gasCalculator().expOperationGasCost(numBytes);
final long cost = gasCalculator.expOperationGasCost(numBytes);
if (frame.getRemainingGas() < cost) {
return new OperationResult(cost, ExceptionalHaltReason.INSUFFICIENT_GAS);
}
final UInt256 result = number.pow(power);
byte[] numberBytes = number.toArrayUnsafe();
BigInteger numBI = numberBytes.length > 0 ? new BigInteger(1, numberBytes) : BigInteger.ZERO;
byte[] powBytes = power.toArrayUnsafe();
BigInteger powBI = powBytes.length > 0 ? new BigInteger(1, powBytes) : BigInteger.ZERO;
frame.pushStackItem(result);
final BigInteger result = numBI.modPow(powBI, MOD_BASE);
byte[] resultArray = result.toByteArray();
int length = resultArray.length;
if (length > 32) {
frame.pushStackItem(Bytes.wrap(resultArray, length - 32, 32));
} else {
frame.pushStackItem(Bytes.wrap(resultArray));
}
return new OperationResult(cost, null);
}
}

@ -24,7 +24,7 @@ import org.hyperledger.besu.evm.internal.FixedStack.OverflowException;
import org.hyperledger.besu.evm.internal.FixedStack.UnderflowException;
import org.hyperledger.besu.evm.internal.Words;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.bytes.Bytes;
/** The Ext code hash operation. */
public class ExtCodeHashOperation extends AbstractOperation {
@ -63,9 +63,9 @@ public class ExtCodeHashOperation extends AbstractOperation {
} else {
final Account account = frame.getWorldUpdater().get(address);
if (account == null || account.isEmpty()) {
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(Bytes.EMPTY);
} else {
frame.pushStackItem(UInt256.fromBytes(account.getCodeHash()));
frame.pushStackItem(account.getCodeHash());
}
return new OperationResult(cost, null);
}

@ -24,7 +24,7 @@ import org.hyperledger.besu.evm.internal.FixedStack.OverflowException;
import org.hyperledger.besu.evm.internal.FixedStack.UnderflowException;
import org.hyperledger.besu.evm.internal.Words;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.bytes.Bytes;
/** The Ext code size operation. */
public class ExtCodeSizeOperation extends AbstractOperation {
@ -63,7 +63,7 @@ public class ExtCodeSizeOperation extends AbstractOperation {
} else {
final Account account = frame.getWorldUpdater().get(address);
frame.pushStackItem(
account == null ? UInt256.ZERO : UInt256.valueOf(account.getCode().size()));
account == null ? Bytes.EMPTY : Words.intBytes(account.getCode().size()));
return new OperationResult(cost, null);
}
} catch (final UnderflowException ufe) {

@ -17,8 +17,7 @@ package org.hyperledger.besu.evm.operation;
import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.evm.internal.Words;
/** The Gas limit operation. */
public class GasLimitOperation extends AbstractFixedCostOperation {
@ -35,7 +34,7 @@ public class GasLimitOperation extends AbstractFixedCostOperation {
@Override
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
frame.pushStackItem(Bytes.ofUnsignedLong(frame.getBlockValues().getGasLimit()));
frame.pushStackItem(Words.longBytes(frame.getBlockValues().getGasLimit()));
return successResponse;
}

@ -17,6 +17,7 @@ package org.hyperledger.besu.evm.operation;
import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.internal.Words;
import org.apache.tuweni.bytes.Bytes;
@ -36,7 +37,7 @@ public class GasOperation extends AbstractFixedCostOperation {
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
final long gasRemaining = frame.getRemainingGas() - gasCost;
final Bytes value = Bytes.ofUnsignedLong(gasRemaining);
final Bytes value = Words.longBytes(gasRemaining);
frame.pushStackItem(value);
return successResponse;

@ -35,7 +35,7 @@ public class GasPriceOperation extends AbstractFixedCostOperation {
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
final Wei gasPrice = frame.getGasPrice();
frame.pushStackItem(gasPrice.toUInt256());
frame.pushStackItem(gasPrice.toBytes());
return successResponse;
}

@ -18,7 +18,7 @@ import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.bytes.Bytes;
/** The GT operation. */
public class GtOperation extends AbstractFixedCostOperation {
@ -48,10 +48,10 @@ public class GtOperation extends AbstractFixedCostOperation {
* @return the operation result
*/
public static OperationResult staticOperation(final MessageFrame frame) {
final UInt256 value0 = UInt256.fromBytes(frame.popStackItem());
final UInt256 value1 = UInt256.fromBytes(frame.popStackItem());
final Bytes value0 = frame.popStackItem().trimLeadingZeros();
final Bytes value1 = frame.popStackItem().trimLeadingZeros();
final UInt256 result = (value0.compareTo(value1) > 0 ? UInt256.ONE : UInt256.ZERO);
final Bytes result = (value0.compareTo(value1) > 0 ? BYTES_ONE : Bytes.EMPTY);
frame.pushStackItem(result);

@ -18,7 +18,7 @@ import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.bytes.Bytes;
/** The Is zero operation. */
public class IsZeroOperation extends AbstractFixedCostOperation {
@ -48,9 +48,9 @@ public class IsZeroOperation extends AbstractFixedCostOperation {
* @return the operation result
*/
public static OperationResult staticOperation(final MessageFrame frame) {
final UInt256 value = UInt256.fromBytes(frame.popStackItem());
final Bytes value = frame.popStackItem().trimLeadingZeros();
frame.pushStackItem(value.isZero() ? UInt256.ONE : UInt256.ZERO);
frame.pushStackItem((value.size() == 0) ? BYTES_ONE : Bytes.EMPTY);
return isZeroSuccess;
}

@ -21,6 +21,9 @@ import org.hyperledger.besu.evm.gascalculator.GasCalculator;
/** The Jump dest operation. */
public class JumpDestOperation extends AbstractFixedCostOperation {
/** constant for a successful jumpdest * */
public static final OperationResult JUMPDEST_SUCCESS = new OperationResult(1L, null);
/** The constant OPCODE. */
public static final int OPCODE = 0x5B;

@ -25,8 +25,9 @@ import org.apache.tuweni.bytes.Bytes;
/** The Jump operation. */
public class JumpOperation extends AbstractFixedCostOperation {
private final Operation.OperationResult invalidJumpResponse;
private final OperationResult jumpResponse;
private static final Operation.OperationResult invalidJumpResponse =
new Operation.OperationResult(8L, ExceptionalHaltReason.INVALID_JUMP_DESTINATION);
private static final OperationResult jumpResponse = new OperationResult(8L, null, 0);
/**
* Instantiates a new Jump operation.
@ -35,14 +36,21 @@ public class JumpOperation extends AbstractFixedCostOperation {
*/
public JumpOperation(final GasCalculator gasCalculator) {
super(0x56, "JUMP", 2, 0, gasCalculator, gasCalculator.getMidTierGasCost());
invalidJumpResponse =
new Operation.OperationResult(gasCost, ExceptionalHaltReason.INVALID_JUMP_DESTINATION);
jumpResponse = new OperationResult(gasCost, null, 0);
}
@Override
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
return staticOperation(frame);
}
/**
* Performs Jump operation.
*
* @param frame the frame
* @return the operation result
*/
public static OperationResult staticOperation(final MessageFrame frame) {
final int jumpDestination;
final Bytes bytes = frame.popStackItem().trimLeadingZeros();
try {

@ -25,8 +25,10 @@ import org.apache.tuweni.bytes.Bytes;
/** The JUMPI operation. */
public class JumpiOperation extends AbstractFixedCostOperation {
private final OperationResult invalidJumpResponse;
private final OperationResult jumpResponse;
private static final OperationResult invalidJumpResponse =
new Operation.OperationResult(10L, ExceptionalHaltReason.INVALID_JUMP_DESTINATION);
private static final OperationResult jumpiResponse = new OperationResult(10L, null, 0);
private static final OperationResult nojumpResponse = new OperationResult(10L, null);
/**
* Instantiates a new JUMPI operation.
@ -35,19 +37,26 @@ public class JumpiOperation extends AbstractFixedCostOperation {
*/
public JumpiOperation(final GasCalculator gasCalculator) {
super(0x57, "JUMPI", 2, 0, gasCalculator, gasCalculator.getHighTierGasCost());
invalidJumpResponse =
new Operation.OperationResult(gasCost, ExceptionalHaltReason.INVALID_JUMP_DESTINATION);
jumpResponse = new OperationResult(gasCost, null, 0);
}
@Override
public OperationResult executeFixedCostOperation(final MessageFrame frame, final EVM evm) {
return staticOperation(frame);
}
/**
* Performs Jump operation.
*
* @param frame the frame
* @return the operation result
*/
public static OperationResult staticOperation(final MessageFrame frame) {
final Bytes dest = frame.popStackItem().trimLeadingZeros();
final Bytes condition = frame.popStackItem();
final Bytes condition = frame.popStackItem().trimLeadingZeros();
// If condition is zero (false), no jump is will be performed. Therefore, skip the test.
if (condition.isZero()) {
return successResponse;
if (condition.size() == 0) {
return nojumpResponse;
} else {
final int jumpDestination;
try {
@ -60,7 +69,7 @@ public class JumpiOperation extends AbstractFixedCostOperation {
return invalidJumpResponse;
}
frame.setPC(jumpDestination);
return jumpResponse;
return jumpiResponse;
}
}
}

@ -23,7 +23,6 @@ import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The Keccak256 operation. */
public class Keccak256Operation extends AbstractOperation {
@ -48,7 +47,7 @@ public class Keccak256Operation extends AbstractOperation {
}
final Bytes bytes = frame.readMutableMemory(from, length);
frame.pushStackItem(UInt256.fromBytes(keccak256(bytes)));
frame.pushStackItem(keccak256(bytes));
return new OperationResult(cost, null);
}
}

@ -18,7 +18,7 @@ import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.bytes.Bytes;
/** The LT operation. */
public class LtOperation extends AbstractFixedCostOperation {
@ -48,10 +48,10 @@ public class LtOperation extends AbstractFixedCostOperation {
* @return the operation result
*/
public static OperationResult staticOperation(final MessageFrame frame) {
final UInt256 value0 = UInt256.fromBytes(frame.popStackItem());
final UInt256 value1 = UInt256.fromBytes(frame.popStackItem());
final Bytes value0 = frame.popStackItem().trimLeadingZeros();
final Bytes value1 = frame.popStackItem().trimLeadingZeros();
final UInt256 result = value0.compareTo(value1) < 0 ? UInt256.ONE : UInt256.ZERO;
final Bytes result = value0.compareTo(value1) < 0 ? BYTES_ONE : Bytes.EMPTY;
frame.pushStackItem(result);

@ -17,8 +17,7 @@ package org.hyperledger.besu.evm.operation;
import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import org.hyperledger.besu.evm.internal.Words;
/** The M size operation. */
public class MSizeOperation extends AbstractFixedCostOperation {
@ -35,7 +34,7 @@ public class MSizeOperation extends AbstractFixedCostOperation {
@Override
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
frame.pushStackItem(UInt256.valueOf(frame.memoryByteSize()));
frame.pushStackItem(Words.longBytes(frame.memoryByteSize()));
return successResponse;
}

@ -22,7 +22,6 @@ import java.math.BigInteger;
import java.util.Arrays;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The Mul mod operation. */
public class MulModOperation extends AbstractFixedCostOperation {
@ -56,7 +55,7 @@ public class MulModOperation extends AbstractFixedCostOperation {
final Bytes value2 = frame.popStackItem();
if (value2.isZero()) {
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(Bytes.EMPTY);
} else {
BigInteger b0 = new BigInteger(1, value0.toArrayUnsafe());
BigInteger b1 = new BigInteger(1, value1.toArrayUnsafe());

@ -18,7 +18,8 @@ import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
/** The Not operation. */
public class NotOperation extends AbstractFixedCostOperation {
@ -48,9 +49,9 @@ public class NotOperation extends AbstractFixedCostOperation {
* @return the operation result
*/
public static OperationResult staticOperation(final MessageFrame frame) {
final UInt256 value = UInt256.fromBytes(frame.popStackItem());
final Bytes value = Bytes32.leftPad(frame.popStackItem());
final UInt256 result = value.not();
final Bytes result = value.not();
frame.pushStackItem(result);

@ -17,8 +17,7 @@ package org.hyperledger.besu.evm.operation;
import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.evm.internal.Words;
/** The Number operation. */
public class NumberOperation extends AbstractFixedCostOperation {
@ -36,7 +35,7 @@ public class NumberOperation extends AbstractFixedCostOperation {
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
final long number = frame.getBlockValues().getNumber();
frame.pushStackItem(Bytes.ofUnsignedLong(number));
frame.pushStackItem(Words.longBytes(number));
return successResponse;
}

@ -18,7 +18,7 @@ import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.bytes.Bytes;
/** The Or operation. */
public class OrOperation extends AbstractFixedCostOperation {
@ -48,10 +48,10 @@ public class OrOperation extends AbstractFixedCostOperation {
* @return the operation result
*/
public static OperationResult staticOperation(final MessageFrame frame) {
final UInt256 value0 = UInt256.fromBytes(frame.popStackItem());
final UInt256 value1 = UInt256.fromBytes(frame.popStackItem());
final Bytes value0 = frame.popStackItem();
final Bytes value1 = frame.popStackItem();
final UInt256 result = value0.or(value1);
final Bytes result = value0.or(value1);
frame.pushStackItem(result);

@ -17,8 +17,7 @@ package org.hyperledger.besu.evm.operation;
import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import org.hyperledger.besu.evm.internal.Words;
/** The PC operation. */
public class PCOperation extends AbstractFixedCostOperation {
@ -35,7 +34,7 @@ public class PCOperation extends AbstractFixedCostOperation {
@Override
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
frame.pushStackItem(UInt256.valueOf(frame.getPC()));
frame.pushStackItem(Words.intBytes(frame.getPC()));
return successResponse;
}

@ -17,9 +17,9 @@ package org.hyperledger.besu.evm.operation;
import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.internal.Words;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The Return data size operation. */
public class ReturnDataSizeOperation extends AbstractFixedCostOperation {
@ -37,7 +37,7 @@ public class ReturnDataSizeOperation extends AbstractFixedCostOperation {
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
final Bytes returnData = frame.getReturnData();
frame.pushStackItem(UInt256.valueOf(returnData.size()));
frame.pushStackItem(Words.longBytes(returnData.size()));
return successResponse;
}

@ -19,10 +19,9 @@ import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import java.math.BigInteger;
import java.util.Arrays;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.bytes.Bytes32;
/** The SDiv operation. */
public class SDivOperation extends AbstractFixedCostOperation {
@ -55,7 +54,7 @@ public class SDivOperation extends AbstractFixedCostOperation {
final Bytes value1 = frame.popStackItem();
if (value1.isZero()) {
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(Bytes.EMPTY);
} else {
final BigInteger b1 =
value0.size() < 32
@ -71,10 +70,7 @@ public class SDivOperation extends AbstractFixedCostOperation {
resultBytes = resultBytes.slice(resultBytes.size() - 32, 32);
}
final byte[] padding = new byte[32 - resultBytes.size()];
Arrays.fill(padding, result.signum() < 0 ? (byte) 0xFF : 0x00);
frame.pushStackItem(UInt256.fromBytes(Bytes.concatenate(Bytes.wrap(padding), resultBytes)));
frame.pushStackItem(Bytes32.leftPad(resultBytes, result.signum() < 0 ? (byte) 0xFF : 0x00));
}
return sdivSuccess;

@ -21,7 +21,6 @@ import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import java.math.BigInteger;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The SGt operation. */
public class SGtOperation extends AbstractFixedCostOperation {
@ -63,7 +62,7 @@ public class SGtOperation extends AbstractFixedCostOperation {
? new BigInteger(1, value1.toArrayUnsafe())
: new BigInteger(value1.toArrayUnsafe());
final UInt256 result = b0.compareTo(b1) > 0 ? UInt256.ONE : UInt256.ZERO;
final Bytes result = b0.compareTo(b1) > 0 ? BYTES_ONE : Bytes.EMPTY;
frame.pushStackItem(result);

@ -21,7 +21,6 @@ import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import java.math.BigInteger;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The SLT operation. */
public class SLtOperation extends AbstractFixedCostOperation {
@ -63,7 +62,7 @@ public class SLtOperation extends AbstractFixedCostOperation {
? new BigInteger(1, value1.toArrayUnsafe())
: new BigInteger(value1.toArrayUnsafe());
final UInt256 result = b0.compareTo(b1) < 0 ? UInt256.ONE : UInt256.ZERO;
final Bytes result = b0.compareTo(b1) < 0 ? BYTES_ONE : Bytes.EMPTY;
frame.pushStackItem(result);

@ -22,7 +22,6 @@ import java.math.BigInteger;
import java.util.Arrays;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The SMod operation. */
public class SModOperation extends AbstractFixedCostOperation {
@ -55,7 +54,7 @@ public class SModOperation extends AbstractFixedCostOperation {
final Bytes value1 = frame.popStackItem();
if (value1.isZero()) {
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(Bytes.EMPTY);
} else {
final BigInteger b1 =
value0.size() < 32

@ -21,8 +21,6 @@ import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
/** The Sar operation. */
public class SarOperation extends AbstractFixedCostOperation {
@ -30,7 +28,8 @@ public class SarOperation extends AbstractFixedCostOperation {
/** The Sar operation success result. */
static final OperationResult sarSuccess = new OperationResult(3, null);
private static final UInt256 ALL_BITS = UInt256.MAX_VALUE;
private static final Bytes ALL_BITS =
Bytes.fromHexString("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
/**
* Instantiates a new Sar operation.
@ -58,19 +57,19 @@ public class SarOperation extends AbstractFixedCostOperation {
final Bytes value = leftPad(frame.popStackItem());
final boolean negativeNumber = value.get(0) < 0;
if (shiftAmount.size() > 4 && (shiftAmount = shiftAmount.trimLeadingZeros()).size() > 4) {
frame.pushStackItem(negativeNumber ? ALL_BITS : UInt256.ZERO);
frame.pushStackItem(negativeNumber ? ALL_BITS : Bytes.EMPTY);
} else {
final int shiftAmountInt = shiftAmount.toInt();
if (shiftAmountInt >= 256 || shiftAmountInt < 0) {
frame.pushStackItem(negativeNumber ? ALL_BITS : UInt256.ZERO);
frame.pushStackItem(negativeNumber ? ALL_BITS : Bytes.EMPTY);
} else {
// first perform standard shift right.
Bytes result = value.shiftRight(shiftAmountInt);
// if a negative number, carry through the sign.
if (negativeNumber) {
final Bytes32 significantBits = ALL_BITS.shiftLeft(256 - shiftAmountInt);
final Bytes significantBits = ALL_BITS.shiftLeft(256 - shiftAmountInt);
result = result.or(significantBits);
}
frame.pushStackItem(result);

@ -20,7 +20,7 @@ import org.hyperledger.besu.evm.account.Account;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.bytes.Bytes;
/** The Self balance operation. */
public class SelfBalanceOperation extends AbstractFixedCostOperation {
@ -39,7 +39,7 @@ public class SelfBalanceOperation extends AbstractFixedCostOperation {
final MessageFrame frame, final EVM evm) {
final Address accountAddress = frame.getRecipientAddress();
final Account account = frame.getWorldUpdater().get(accountAddress);
frame.pushStackItem(account == null ? UInt256.ZERO : account.getBalance());
frame.pushStackItem(account == null ? Bytes.EMPTY : account.getBalance());
return successResponse;
}

@ -21,7 +21,6 @@ import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The Shl (Shift Left) operation. */
public class ShlOperation extends AbstractFixedCostOperation {
@ -54,13 +53,13 @@ public class ShlOperation extends AbstractFixedCostOperation {
Bytes shiftAmount = frame.popStackItem();
if (shiftAmount.size() > 4 && (shiftAmount = shiftAmount.trimLeadingZeros()).size() > 4) {
frame.popStackItem();
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(Bytes.EMPTY);
} else {
final int shiftAmountInt = shiftAmount.toInt();
final Bytes value = leftPad(frame.popStackItem());
if (shiftAmountInt >= 256 || shiftAmountInt < 0) {
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(Bytes.EMPTY);
} else {
frame.pushStackItem(value.shiftLeft(shiftAmountInt));
}

@ -21,7 +21,6 @@ import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.units.bigints.UInt256;
/** The Shr (Shift Right) operation. */
public class ShrOperation extends AbstractFixedCostOperation {
@ -54,13 +53,13 @@ public class ShrOperation extends AbstractFixedCostOperation {
Bytes shiftAmount = frame.popStackItem();
if (shiftAmount.size() > 4 && (shiftAmount = shiftAmount.trimLeadingZeros()).size() > 4) {
frame.popStackItem();
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(Bytes.EMPTY);
} else {
final int shiftAmountInt = shiftAmount.toInt();
final Bytes value = leftPad(frame.popStackItem());
if (shiftAmountInt >= 256 || shiftAmountInt < 0) {
frame.pushStackItem(UInt256.ZERO);
frame.pushStackItem(Bytes.EMPTY);
} else {
frame.pushStackItem(value.shiftRight(shiftAmountInt));
}

@ -18,8 +18,9 @@ import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.bytes.MutableBytes32;
import org.apache.tuweni.units.bigints.UInt256;
/** The Sign extend operation. */
public class SignExtendOperation extends AbstractFixedCostOperation {
@ -48,24 +49,31 @@ public class SignExtendOperation extends AbstractFixedCostOperation {
* @return the operation result
*/
public static OperationResult staticOperation(final MessageFrame frame) {
final UInt256 value0 = UInt256.fromBytes(frame.popStackItem());
final UInt256 value1 = UInt256.fromBytes(frame.popStackItem());
final Bytes value0 = frame.popStackItem().trimLeadingZeros();
final Bytes value1 = Bytes32.leftPad(frame.popStackItem());
final MutableBytes32 result = MutableBytes32.create();
// Any value >= 31 imply an index <= 0, so no work to do (note that 0 itself is a valid index,
// but copying the 0th byte to itself is only so useful).
if (!value0.fitsInt() || value0.intValue() >= 31) {
int value0size = value0.size();
if (value0size > 1) {
frame.pushStackItem(value1);
} else {
// This is safe, since other < 31.
final int byteIndex = 32 - 1 - value0.getInt(32 - 4);
final byte toSet = value1.get(byteIndex) < 0 ? (byte) 0xFF : 0x00;
result.mutableSlice(0, byteIndex).fill(toSet);
value1.slice(byteIndex).copyTo(result, byteIndex);
frame.pushStackItem(UInt256.fromBytes(result));
return signExtendSuccess;
}
int value0Value = value0.toInt();
if (value0Value >= 31) {
frame.pushStackItem(value1);
return signExtendSuccess;
}
final int byteIndex = 31 - value0.toInt();
final byte toSet = value1.get(byteIndex) < 0 ? (byte) 0xFF : 0x00;
result.mutableSlice(0, byteIndex).fill(toSet);
value1.slice(byteIndex).copyTo(result, byteIndex);
frame.pushStackItem(result);
return signExtendSuccess;
}
}

@ -18,7 +18,10 @@ import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import java.math.BigInteger;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
/** The Sub (Subtract) operation. */
public class SubOperation extends AbstractFixedCostOperation {
@ -48,12 +51,20 @@ public class SubOperation extends AbstractFixedCostOperation {
* @return the operation result
*/
public static OperationResult staticOperation(final MessageFrame frame) {
final UInt256 value0 = UInt256.fromBytes(frame.popStackItem());
final UInt256 value1 = UInt256.fromBytes(frame.popStackItem());
final BigInteger value0 = new BigInteger(1, frame.popStackItem().toArrayUnsafe());
final BigInteger value1 = new BigInteger(1, frame.popStackItem().toArrayUnsafe());
final UInt256 result = value0.subtract(value1);
final BigInteger result = value0.subtract(value1);
frame.pushStackItem(result);
byte[] resultArray = result.toByteArray();
int length = resultArray.length;
if (length >= 32) {
frame.pushStackItem(Bytes.wrap(resultArray, length - 32, 32));
} else if (result.signum() < 0) {
frame.pushStackItem(Bytes32.leftPad(Bytes.wrap(resultArray), (byte) -1));
} else {
frame.pushStackItem(Bytes.wrap(resultArray));
}
return subSuccess;
}

@ -17,8 +17,7 @@ package org.hyperledger.besu.evm.operation;
import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import org.hyperledger.besu.evm.internal.Words;
/** The Timestamp operation. */
public class TimestampOperation extends AbstractFixedCostOperation {
@ -36,7 +35,7 @@ public class TimestampOperation extends AbstractFixedCostOperation {
public Operation.OperationResult executeFixedCostOperation(
final MessageFrame frame, final EVM evm) {
final long timestamp = frame.getBlockValues().getTimestamp();
frame.pushStackItem(UInt256.valueOf(timestamp));
frame.pushStackItem(Words.longBytes(timestamp));
return successResponse;
}

@ -18,7 +18,7 @@ import org.hyperledger.besu.evm.EVM;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.bytes.Bytes;
/** The XOR operation. */
public class XorOperation extends AbstractFixedCostOperation {
@ -48,10 +48,10 @@ public class XorOperation extends AbstractFixedCostOperation {
* @return the operation result
*/
public static OperationResult staticOperation(final MessageFrame frame) {
final UInt256 value0 = UInt256.fromBytes(frame.popStackItem());
final UInt256 value1 = UInt256.fromBytes(frame.popStackItem());
final Bytes value0 = frame.popStackItem();
final Bytes value1 = frame.popStackItem();
final UInt256 result = value0.xor(value1);
final Bytes result = value0.xor(value1);
frame.pushStackItem(result);

@ -16,6 +16,7 @@ package org.hyperledger.besu.evm.precompile;
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.internal.Words;
import java.nio.file.Path;
import java.util.Optional;
@ -36,7 +37,7 @@ public class KZGPointEvalPrecompiledContract implements PrecompiledContract {
private static void init() {
CKZG4844JNI.loadNativeLibrary(CKZG4844JNI.Preset.MAINNET);
Bytes fieldElementsPerBlob =
Bytes32.wrap(Bytes.ofUnsignedInt(CKZG4844JNI.getFieldElementsPerBlob()).xor(Bytes32.ZERO));
Bytes32.wrap(Words.intBytes(CKZG4844JNI.getFieldElementsPerBlob()).xor(Bytes32.ZERO));
Bytes blsModulus =
Bytes32.wrap(Bytes.of(CKZG4844JNI.BLS_MODULUS.toByteArray()).xor(Bytes32.ZERO));

@ -20,6 +20,7 @@ import static com.google.common.base.Strings.padStart;
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.internal.Words;
import org.hyperledger.besu.evm.operation.Operation;
import java.io.PrintStream;
@ -222,9 +223,7 @@ public class StandardJsonTracer implements OperationTracer {
} else {
sb.append("\"output\":\"\",");
}
sb.append("\"gasUsed\":\"")
.append(Bytes.ofUnsignedLong(gasUsed).toShortHexString())
.append("\",");
sb.append("\"gasUsed\":\"").append(Words.longBytes(gasUsed).toShortHexString()).append("\",");
sb.append("\"time\":").append(timeNs).append("}");
out.println(sb);
}

@ -25,13 +25,13 @@ import org.hyperledger.besu.evm.frame.ExceptionalHaltReason;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.BerlinGasCalculator;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.internal.Words;
import org.hyperledger.besu.evm.operation.BaseFeeOperation;
import org.hyperledger.besu.evm.operation.Operation;
import org.hyperledger.besu.evm.operation.Operation.OperationResult;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
import org.junit.Test;
@ -53,7 +53,7 @@ public class BaseFeeOperationTest {
final MessageFrame frame = createMessageFrame(100, Optional.of(Wei.of(5L)));
final Operation operation = new BaseFeeOperation(gasCalculator);
final OperationResult result = operation.execute(frame, null);
verify(frame).pushStackItem(UInt256.fromBytes(Bytes32.leftPad(Bytes.ofUnsignedLong(5L))));
verify(frame).pushStackItem(UInt256.fromBytes(Bytes32.leftPad(Words.longBytes(5L))));
assertSuccessResult(result);
}

@ -23,6 +23,7 @@ import org.hyperledger.besu.evm.gascalculator.ConstantinopleGasCalculator;
import org.hyperledger.besu.evm.operation.ChainIdOperation;
import org.hyperledger.besu.evm.operation.Operation.OperationResult;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
import org.junit.Test;
@ -60,7 +61,7 @@ public class ChainIdOperationTest {
@SuppressWarnings("ResultOfMethodCallIgnored")
@Test
public void shouldReturnChainId() {
final ArgumentCaptor<UInt256> arg = ArgumentCaptor.forClass(UInt256.class);
final ArgumentCaptor<Bytes> arg = ArgumentCaptor.forClass(UInt256.class);
when(messageFrame.getRemainingGas()).thenReturn(100L);
operation.execute(messageFrame, null);
Mockito.verify(messageFrame).getRemainingGas();

@ -32,6 +32,7 @@ import org.hyperledger.besu.evm.frame.BlockValues;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.ConstantinopleGasCalculator;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.evm.internal.Words;
import org.hyperledger.besu.evm.log.Log;
import org.hyperledger.besu.evm.operation.Create2Operation;
import org.hyperledger.besu.evm.operation.Operation.OperationResult;
@ -159,7 +160,7 @@ public class Create2OperationTest {
.depth(1)
.completer(__ -> {})
.address(Address.fromHexString(sender))
.blockHashLookup(n -> Hash.hash(Bytes.ofUnsignedLong(n)))
.blockHashLookup(n -> Hash.hash(Words.longBytes(n)))
.blockValues(mock(BlockValues.class))
.gasPrice(Wei.ZERO)
.messageFrameStack(new ArrayDeque<>())
@ -284,7 +285,7 @@ public class Create2OperationTest {
.depth(depth)
.completer(__ -> {})
.address(Address.fromHexString(SENDER))
.blockHashLookup(n -> Hash.hash(Bytes.ofUnsignedLong(n)))
.blockHashLookup(n -> Hash.hash(Words.longBytes(n)))
.blockValues(mock(BlockValues.class))
.gasPrice(Wei.ZERO)
.messageFrameStack(messageFrameStack)
@ -325,7 +326,7 @@ public class Create2OperationTest {
final EVM evm = MainnetEVMs.cancun(DEV_NET_CHAIN_ID, EvmConfiguration.DEFAULT);
var result = operation.execute(messageFrame, evm);
assertThat(result.getHaltReason()).isNull();
assertThat(messageFrame.getStackItem(0)).isEqualTo(UInt256.ZERO);
assertThat(messageFrame.getStackItem(0).trimLeadingZeros()).isEqualTo(Bytes.EMPTY);
}
@Test

@ -33,6 +33,7 @@ import org.hyperledger.besu.evm.frame.BlockValues;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.ConstantinopleGasCalculator;
import org.hyperledger.besu.evm.internal.EvmConfiguration;
import org.hyperledger.besu.evm.internal.Words;
import org.hyperledger.besu.evm.log.Log;
import org.hyperledger.besu.evm.operation.CreateOperation;
import org.hyperledger.besu.evm.processor.ContractCreationProcessor;
@ -141,7 +142,7 @@ class CreateOperationTest {
final EVM evm = MainnetEVMs.london(EvmConfiguration.DEFAULT);
operation.execute(messageFrame, evm);
assertThat(messageFrame.getStackItem(0)).isEqualTo(UInt256.ZERO);
assertThat(messageFrame.getStackItem(0).trimLeadingZeros()).isEqualTo(Bytes.EMPTY);
}
@Test
@ -160,7 +161,7 @@ class CreateOperationTest {
final EVM evm = MainnetEVMs.london(EvmConfiguration.DEFAULT);
operation.execute(messageFrame, evm);
assertThat(messageFrame.getStackItem(0)).isEqualTo(UInt256.ZERO);
assertThat(messageFrame.getStackItem(0).trimLeadingZeros()).isEqualTo(Bytes.EMPTY);
}
@Test
@ -182,7 +183,7 @@ class CreateOperationTest {
final EVM evm = MainnetEVMs.london(EvmConfiguration.DEFAULT);
operation.execute(messageFrame, evm);
assertThat(messageFrame.getStackItem(0)).isEqualTo(UInt256.ZERO);
assertThat(messageFrame.getStackItem(0).trimLeadingZeros()).isEqualTo(Bytes.EMPTY);
}
@Test
@ -262,7 +263,7 @@ class CreateOperationTest {
final EVM evm = MainnetEVMs.cancun(DEV_NET_CHAIN_ID, EvmConfiguration.DEFAULT);
var result = operation.execute(messageFrame, evm);
assertThat(result.getHaltReason()).isNull();
assertThat(messageFrame.getStackItem(0)).isEqualTo(UInt256.ZERO);
assertThat(messageFrame.getStackItem(0).trimLeadingZeros()).isEqualTo(Bytes.EMPTY);
}
@Test
@ -290,7 +291,7 @@ class CreateOperationTest {
final EVM evm = MainnetEVMs.cancun(DEV_NET_CHAIN_ID, EvmConfiguration.DEFAULT);
var result = operation.execute(messageFrame, evm);
assertThat(result.getHaltReason()).isNull();
assertThat(messageFrame.getStackItem(0)).isNotEqualTo(UInt256.ZERO);
assertThat(messageFrame.getState()).isEqualTo(MessageFrame.State.CODE_SUSPENDED);
}
@NotNull
@ -312,7 +313,7 @@ class CreateOperationTest {
.depth(depth)
.completer(__ -> {})
.address(Address.fromHexString(SENDER))
.blockHashLookup(n -> Hash.hash(Bytes.ofUnsignedLong(n)))
.blockHashLookup(n -> Hash.hash(Words.longBytes(n)))
.blockValues(mock(BlockValues.class))
.gasPrice(Wei.ZERO)
.messageFrameStack(messageFrameStack)

@ -33,7 +33,6 @@ import org.hyperledger.besu.evm.toy.ToyWorld;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
import org.junit.Test;
@ -65,7 +64,7 @@ public class ExtCodeHashOperationTest {
@Test
public void shouldReturnZeroWhenAccountDoesNotExist() {
final Bytes result = executeOperation(REQUESTED_ADDRESS);
assertThat(result).isEqualTo(Bytes32.ZERO);
assertThat(result.trimLeadingZeros()).isEqualTo(Bytes.EMPTY);
}
@Test
@ -77,12 +76,12 @@ public class ExtCodeHashOperationTest {
@Test
public void shouldReturnZeroWhenAccountExistsButIsEmpty() {
worldStateUpdater.getOrCreate(REQUESTED_ADDRESS);
assertThat(executeOperation(REQUESTED_ADDRESS)).isEqualTo(Bytes32.ZERO);
assertThat(executeOperation(REQUESTED_ADDRESS).trimLeadingZeros()).isEqualTo(Bytes.EMPTY);
}
@Test
public void shouldReturnZeroWhenPrecompiledContractHasNoBalance() {
assertThat(executeOperation(Address.ECREC)).isEqualTo(Bytes32.ZERO);
assertThat(executeOperation(Address.ECREC).trimLeadingZeros()).isEqualTo(Bytes.EMPTY);
}
@Test

@ -22,6 +22,7 @@ import static org.mockito.Mockito.when;
import org.hyperledger.besu.evm.Code;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.internal.Words;
import org.hyperledger.besu.evm.operation.Operation;
import org.hyperledger.besu.evm.operation.RelativeJumpIfOperation;
import org.hyperledger.besu.evm.operation.RelativeJumpOperation;
@ -92,7 +93,7 @@ class RelativeJumpOperationTest {
.code(mockCode)
.pc(rjumpOperationIndex)
.initialGas(5L)
.pushStackItem(Bytes.ofUnsignedInt(1))
.pushStackItem(Words.intBytes(1))
.build();
when(mockCode.getBytes()).thenReturn(code);

@ -25,8 +25,8 @@ import org.hyperledger.besu.evm.operation.SarOperation;
import java.util.Arrays;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@ -137,40 +137,28 @@ public class SarOperationTest {
"0xff",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{
"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"0x100",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0x100", "0x"},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x80",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x8000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x80000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{"0x0000000000000000000000000000000000000000000000000000000000000400", "0x8000", "0x"},
{"0x0000000000000000000000000000000000000000000000000000000000000400", "0x80000000", "0x"},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x8000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
"0x"
},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x80000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
"0x"
},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x8000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
"0x"
},
{
"0x8000000000000000000000000000000000000000000000000000000000000400",
@ -221,9 +209,9 @@ public class SarOperationTest {
when(frame.stackSize()).thenReturn(2);
when(frame.getRemainingGas()).thenReturn(100L);
when(frame.popStackItem())
.thenReturn(UInt256.fromBytes(Bytes32.fromHexStringLenient(shift)))
.thenReturn(UInt256.fromHexString(number));
.thenReturn(Bytes32.fromHexStringLenient(shift))
.thenReturn(Bytes.fromHexString(number));
operation.execute(frame, null);
verify(frame).pushStackItem(UInt256.fromHexString(expectedResult));
verify(frame).pushStackItem(Bytes.fromHexString(expectedResult));
}
}

@ -25,6 +25,7 @@ import org.hyperledger.besu.evm.operation.ShlOperation;
import java.util.Arrays;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
import org.junit.Test;
@ -72,11 +73,7 @@ public class ShlOperationTest {
"0x01",
"0x0000000000000000000000000000000000000000000000000000000000000010"
},
{
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x100",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{"0x0000000000000000000000000000000000000000000000000000000000000001", "0x100", "0x"},
{
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"0x01",
@ -97,30 +94,22 @@ public class ShlOperationTest {
"0x80",
"0x0000000000000000000000000000040000000000000000000000000000000000"
},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x8000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x80000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{"0x0000000000000000000000000000000000000000000000000000000000000400", "0x8000", "0x"},
{"0x0000000000000000000000000000000000000000000000000000000000000400", "0x80000000", "0x"},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x8000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
"0x"
},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x80000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
"0x"
},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x8000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
"0x"
}
};
@ -144,6 +133,6 @@ public class ShlOperationTest {
.thenReturn(UInt256.fromBytes(Bytes32.fromHexStringLenient(shift)))
.thenReturn(UInt256.fromHexString(number));
operation.execute(frame, null);
verify(frame).pushStackItem(UInt256.fromHexString(expectedResult));
verify(frame).pushStackItem(Bytes.fromHexString(expectedResult));
}
}

@ -25,6 +25,7 @@ import org.hyperledger.besu.evm.operation.ShrOperation;
import java.util.Arrays;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
import org.junit.Test;
@ -76,16 +77,8 @@ public class ShrOperationTest {
"0xff",
"0x0000000000000000000000000000000000000000000000000000000000000001"
},
{
"0x8000000000000000000000000000000000000000000000000000000000000000",
"0x100",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{
"0x8000000000000000000000000000000000000000000000000000000000000000",
"0x101",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{"0x8000000000000000000000000000000000000000000000000000000000000000", "0x100", "0x"},
{"0x8000000000000000000000000000000000000000000000000000000000000000", "0x101", "0x"},
{
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"0x0",
@ -101,11 +94,7 @@ public class ShrOperationTest {
"0xff",
"0x0000000000000000000000000000000000000000000000000000000000000001"
},
{
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"0x100",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0x100", "0x"},
{
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x01",
@ -116,30 +105,22 @@ public class ShrOperationTest {
"0x80",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x8000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x80000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
},
{"0x0000000000000000000000000000000000000000000000000000000000000400", "0x8000", "0x"},
{"0x0000000000000000000000000000000000000000000000000000000000000400", "0x80000000", "0x"},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x8000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
"0x"
},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x80000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
"0x"
},
{
"0x0000000000000000000000000000000000000000000000000000000000000400",
"0x8000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
"0x"
}
};
@ -163,6 +144,6 @@ public class ShrOperationTest {
.thenReturn(UInt256.fromBytes(Bytes32.fromHexStringLenient(shift)))
.thenReturn(UInt256.fromHexString(number));
operation.execute(frame, null);
verify(frame).pushStackItem(UInt256.fromHexString(expectedResult));
verify(frame).pushStackItem(Bytes.fromHexString(expectedResult));
}
}

@ -23,6 +23,7 @@ import org.hyperledger.besu.evm.Code;
import org.hyperledger.besu.evm.code.CodeV0;
import org.hyperledger.besu.evm.frame.BlockValues;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.internal.Words;
import org.hyperledger.besu.evm.toy.ToyWorld;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
@ -163,8 +164,7 @@ public class TestMessageFrameBuilder {
.depth(depth)
.completer(c -> {})
.miningBeneficiary(Address.ZERO)
.blockHashLookup(
blockHashLookup.orElse(number -> Hash.hash(Bytes.ofUnsignedLong(number))))
.blockHashLookup(blockHashLookup.orElse(number -> Hash.hash(Words.longBytes(number))))
.maxStackSize(maxStackSize)
.build();
frame.setPC(pc);

Loading…
Cancel
Save