Java Code Examples for org.apache.tuweni.bytes.Bytes#isZero()

The following examples show how to use org.apache.tuweni.bytes.Bytes#isZero() . 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: TrieIterator.java    From besu with Apache License 2.0 5 votes vote down vote up
private Bytes32 keyHash() {
  final Iterator<Bytes> iterator = paths.descendingIterator();
  Bytes fullPath = iterator.next();
  while (iterator.hasNext()) {
    fullPath = Bytes.wrap(fullPath, iterator.next());
  }
  return fullPath.isZero()
      ? Bytes32.ZERO
      : Bytes32.wrap(CompactEncoding.pathToBytes(fullPath), 0);
}
 
Example 2
Source File: Framer.java    From besu with Apache License 2.0 4 votes vote down vote up
/**
 * Parses, decrypts and performs MAC verification on a frame.
 *
 * <p>This method expects a well-formed frame, sized according to the length indicated in this
 * packet's header.
 *
 * @param f The buffer containing
 * @param frameSize The expected
 */
private MessageData processFrame(final ByteBuf f, final int frameSize) {
  final int pad = padding16(frameSize);
  final int expectedSize = frameSize + pad + LENGTH_MAC;
  if (f.readableBytes() != expectedSize) {
    throw error("Expected %s bytes in header, got %s", expectedSize, f.readableBytes());
  }

  final byte[] frameData = new byte[frameSize + pad];
  final byte[] fMac = new byte[LENGTH_MAC];
  f.readBytes(frameData).readBytes(fMac);

  // Validate the frame's MAC.
  final byte[] fMacSeed = secrets.updateIngress(frameData).getIngressMac();
  final byte[] fMacSeedEnc = new byte[16];
  macEncryptor.processBlock(fMacSeed, 0, fMacSeedEnc, 0);
  byte[] expectedMac = secrets.updateIngress(xor(fMacSeedEnc, fMacSeed)).getIngressMac();
  expectedMac = Arrays.copyOf(expectedMac, LENGTH_MAC);

  validateMac(fMac, expectedMac);

  // Decrypt frame data.
  decryptor.processBytes(frameData, 0, frameData.length, frameData, 0);

  // Read the id.
  final Bytes idbv = RLP.decodeOne(Bytes.of(frameData[0]));
  final int id = idbv.isZero() || idbv.size() == 0 ? 0 : idbv.get(0);

  // Write message data to ByteBuf, decompressing as necessary
  final Bytes data;
  if (compressionEnabled) {
    final byte[] compressedMessageData = Arrays.copyOfRange(frameData, 1, frameData.length - pad);
    final int uncompressedLength = compressor.uncompressedLength(compressedMessageData);
    if (uncompressedLength >= LENGTH_MAX_MESSAGE_FRAME) {
      throw error("Message size %s in excess of maximum length.", uncompressedLength);
    }
    final byte[] decompressedMessageData = compressor.decompress(compressedMessageData);
    data = Bytes.wrap(decompressedMessageData);
  } else {
    // Move data to a ByteBuf
    final int messageLength = frameSize - LENGTH_MESSAGE_ID;
    data = Bytes.wrap(frameData, 1, messageLength);
  }

  return new RawMessage(id, data);
}
 
Example 3
Source File: IbftExtraData.java    From besu with Apache License 2.0 4 votes vote down vote up
private static Signature parseProposerSeal(final RLPInput rlpInput) {
  final Bytes data = rlpInput.readBytes();
  return data.isZero() ? null : Signature.decode(data);
}
 
Example 4
Source File: CliqueExtraData.java    From besu with Apache License 2.0 4 votes vote down vote up
private static Signature parseProposerSeal(final Bytes proposerSealRaw) {
  return proposerSealRaw.isZero() ? null : Signature.decode(proposerSealRaw);
}