Java Code Examples for org.apache.tuweni.bytes.Bytes32#SIZE

The following examples show how to use org.apache.tuweni.bytes.Bytes32#SIZE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EthHashMiningCoordinatorTest.java    From besu with Apache License 2.0 6 votes vote down vote up
@Test
public void miningCoordinatorIsCreatedDisabledWithNoReportableMiningStatistics() {
  final EthHashMiningCoordinator miningCoordinator =
      new EthHashMiningCoordinator(
          executionContext.getBlockchain(),
          executor,
          syncState,
          DEFAULT_REMOTE_SEALERS_LIMIT,
          DEFAULT_REMOTE_SEALERS_TTL);
  final EthHashSolution solution = new EthHashSolution(1L, Hash.EMPTY, new byte[Bytes32.SIZE]);

  assertThat(miningCoordinator.isMining()).isFalse();
  assertThat(miningCoordinator.hashesPerSecond()).isEqualTo(Optional.empty());
  assertThat(miningCoordinator.getWorkDefinition()).isEqualTo(Optional.empty());
  assertThat(miningCoordinator.submitWork(solution)).isFalse();
}
 
Example 2
Source File: SECP256K1.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
/**
 * Create the private key from a {@link BigInteger}.
 *
 * @param key The integer describing the key.
 * @return The private key.
 * @throws IllegalArgumentException If the integer would overflow 32 bytes.
 */
public static SecretKey fromInteger(BigInteger key) {
  checkNotNull(key);
  byte[] bytes = key.toByteArray();
  int offset = 0;
  while (bytes[offset] == 0) {
    ++offset;
  }
  if ((bytes.length - offset) > Bytes32.SIZE) {
    throw new IllegalArgumentException("key integer is too large");
  }
  return fromBytes(Bytes32.leftPad(Bytes.wrap(bytes, offset, bytes.length - offset)));
}
 
Example 3
Source File: UInt256Value.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
/**
 * @return True if this value fits a java {@code int} (i.e. is less or equal to {@code Integer.MAX_VALUE}).
 */
default boolean fitsInt() {
  // Ints are 4 bytes, so anything but the 4 last bytes must be zeroes
  Bytes32 bytes = toBytes();
  for (int i = 0; i < Bytes32.SIZE - 4; i++) {
    if (bytes.get(i) != 0)
      return false;
  }
  // Lastly, the left-most byte of the int must not start with a 1.
  return bytes.get(Bytes32.SIZE - 4) >= 0;
}
 
Example 4
Source File: UInt256Value.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
/**
 * @return True if this value fits a java {@code long} (i.e. is less or equal to {@code Long.MAX_VALUE}).
 */
default boolean fitsLong() {
  // Longs are 8 bytes, so anything but the 8 last bytes must be zeroes
  for (int i = 0; i < Bytes32.SIZE - 8; i++) {
    if (toBytes().get(i) != 0)
      return false;
  }
  // Lastly, the left-most byte of the long must not start with a 1.
  return toBytes().get(Bytes32.SIZE - 8) >= 0;
}
 
Example 5
Source File: Memory.java    From besu with Apache License 2.0 5 votes vote down vote up
/**
 * Expands the active words to accommodate the specified byte position.
 *
 * @param address The location in memory to start with.
 * @param numBytes The number of bytes to get.
 */
void ensureCapacityForBytes(final int address, final int numBytes) {
  // Do not increase the memory capacity if no bytes are being written
  // regardless of what the address may be.
  if (numBytes == 0) {
    return;
  }
  final int lastByteIndex = Math.addExact(address, numBytes);
  final int lastWordRequired = ((lastByteIndex - 1) / Bytes32.SIZE);
  maybeExpandCapacity(lastWordRequired + 1);
}
 
Example 6
Source File: Memory.java    From besu with Apache License 2.0 5 votes vote down vote up
/**
 * Expands the memory to the specified number of active words.
 *
 * @param newActiveWords The new number of active words to expand to.
 */
private void maybeExpandCapacity(final int newActiveWords) {
  if (dataSize256 >= newActiveWords) return;

  // Require full capacity to guarantee we don't resize more than once.
  final byte[] newData = new byte[newActiveWords * Bytes32.SIZE];
  System.arraycopy(data, 0, newData, 0, data.length);
  data = newData;
  updateSize();
}
 
Example 7
Source File: BeaconBlocksByRootRequestMessageEncoder.java    From teku with Apache License 2.0 5 votes vote down vote up
@Override
public BeaconBlocksByRootRequestMessage decode(final Bytes message) throws RpcException {
  if (message.size() % Bytes32.SIZE != 0) {
    LOG.trace("Cannot split message into Bytes32 chunks {}", message);
    throw RpcException.DESERIALIZATION_FAILED;
  }
  final List<Bytes32> blockRoots = new ArrayList<>();
  for (int i = 0; i < message.size(); i += Bytes32.SIZE) {
    blockRoots.add(Bytes32.wrap(message.slice(i, Bytes32.SIZE)));
  }
  return new BeaconBlocksByRootRequestMessage(blockRoots);
}
 
Example 8
Source File: VersionProvider.java    From teku with Apache License 2.0 5 votes vote down vote up
public static Bytes32 getDefaultGraffiti() {
  final String graffitiVersionString = CLIENT_IDENTITY + "/" + IMPLEMENTATION_VERSION;
  final Bytes versionBytes = Bytes.wrap(graffitiVersionString.getBytes(StandardCharsets.UTF_8));
  if (versionBytes.size() <= Bytes32.SIZE) {
    return Bytes32.rightPad(versionBytes);
  } else {
    return Bytes32.wrap(versionBytes.slice(0, Bytes32.SIZE));
  }
}
 
Example 9
Source File: HandshakeSecrets.java    From besu with Apache License 2.0 4 votes vote down vote up
private static byte[] snapshot(final KeccakDigest digest) {
  final byte[] out = new byte[Bytes32.SIZE];
  new KeccakDigest(digest).doFinal(out, 0);
  return out;
}
 
Example 10
Source File: Memory.java    From besu with Apache License 2.0 4 votes vote down vote up
private void updateSize() {
  dataSize256 = data.length / Bytes32.SIZE;
  activeWords = UInt256.valueOf(dataSize256);
}
 
Example 11
Source File: Words.java    From besu with Apache License 2.0 2 votes vote down vote up
/**
 * The number of words corresponding to the provided input.
 *
 * <p>In other words, this compute {@code input.size() / 32} but rounded up.
 *
 * @param input the input to check.
 * @return the number of (32 bytes) words that {@code input} spans.
 */
public static int numWords(final Bytes input) {
  // m/n round up == (m + n - 1)/n: http://www.cs.nott.ac.uk/~psarb2/G51MPC/slides/NumberLogic.pdf
  return (input.size() + Bytes32.SIZE - 1) / Bytes32.SIZE;
}