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

The following examples show how to use org.apache.tuweni.bytes.Bytes#equals() . 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: NodeSmartContractPermissioningController.java    From besu with Apache License 2.0 6 votes vote down vote up
public static Boolean checkTransactionResult(final Bytes result) {
  // booleans are padded to 32 bytes
  if (result.size() != 32) {
    throw new IllegalArgumentException("Unexpected result size");
  }

  // 0 is false
  if (result.equals(FALSE_RESPONSE)) {
    return false;
    // 1 filled to 32 bytes is true
  } else if (result.equals(TRUE_RESPONSE)) {
    return true;
    // Anything else is wrong
  } else {
    throw new IllegalStateException("Unexpected result form");
  }
}
 
Example 2
Source File: TransactionSmartContractPermissioningController.java    From besu with Apache License 2.0 6 votes vote down vote up
public static Boolean checkTransactionResult(final Bytes result) {
  // booleans are padded to 32 bytes
  if (result.size() != 32) {
    throw new IllegalArgumentException("Unexpected result size");
  }

  // 0 is false
  if (result.equals(FALSE_RESPONSE)) {
    return false;
    // true is 1, padded to 32 bytes
  } else if (result.equals(TRUE_RESPONSE)) {
    return true;
    // Anything else is wrong
  } else {
    throw new IllegalStateException("Unexpected result form");
  }
}
 
Example 3
Source File: AccountSmartContractPermissioningIsAllowedTransaction.java    From besu with Apache License 2.0 6 votes vote down vote up
static Boolean checkTransactionResult(final Bytes result) {
  // booleans are padded to 32 bytes
  if (result.size() != 32) {
    throw new IllegalArgumentException("Unexpected result size");
  }

  // 0 is false
  if (result.equals(
      Bytes.fromHexString(
          "0x0000000000000000000000000000000000000000000000000000000000000000"))) {
    return false;
    // 1 filled to 32 bytes is true
  } else if (result.equals(
      Bytes.fromHexString(
          "0x0000000000000000000000000000000000000000000000000000000000000001"))) {
    return true;
    // Anything else is wrong
  } else {
    throw new IllegalStateException("Unexpected result form");
  }
}
 
Example 4
Source File: NodeSmartContractPermissioningIsAllowedTransaction.java    From besu with Apache License 2.0 6 votes vote down vote up
static Boolean checkTransactionResult(final Bytes result) {
  // booleans are padded to 32 bytes
  if (result.size() != 32) {
    throw new IllegalArgumentException("Unexpected result size");
  }

  // 0 is false
  if (result.equals(
      Bytes.fromHexString(
          "0x0000000000000000000000000000000000000000000000000000000000000000"))) {
    return false;
    // 1 filled to 32 bytes is true
  } else if (result.equals(
      Bytes.fromHexString(
          "0x0000000000000000000000000000000000000000000000000000000000000001"))) {
    return true;
    // Anything else is wrong
  } else {
    throw new IllegalStateException("Unexpected result form");
  }
}
 
Example 5
Source File: State.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
/**
 * Records a message was received in full from a peer.
 *
 * @param peer the peer that sent the message
 * @param attributes of the message
 * @param message the hash of the message
 */
public void receiveGossipMessage(Peer peer, String attributes, Bytes message, Bytes messageHash) {
  Bytes checkHash = messageHashingFunction.hash(message);
  if (!checkHash.equals(messageHash)) {
    return;
  }
  peerRepository.considerNewPeer(peer);
  MessageHandler handler = messageHandlers.computeIfAbsent(messageHash, MessageHandler::new);
  handler.fullMessageReceived(peer, attributes, message);
}
 
Example 6
Source File: ForkIdManager.java    From besu with Apache License 2.0 5 votes vote down vote up
private boolean isRemoteAwareOfPresent(final Bytes forkHash, final Long nextFork) {
  for (final ForkId j : forkAndHashList) {
    if (forkHash.equals(j.getHash())) {
      if (nextFork.equals(j.getNext())) {
        return true;
      } else if (j.getNext() == 0L) {
        return highestKnownFork <= nextFork; // Remote aware of an additional future fork
      } else {
        return false;
      }
    }
  }
  return false;
}
 
Example 7
Source File: Bucket.java    From besu with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the peer with the provided ID if it exists in the bucket.
 *
 * <p>This operation presupposes that the system has been in recent contact with this peer, hence
 * it relocates it to to the head of the list.
 *
 * @param id The peer's ID (public key).
 * @return An empty optional if the peer was not a member of this bucket, or a filled optional if
 *     it was.
 */
synchronized Optional<DiscoveryPeer> getAndTouch(final Bytes id) {
  for (int i = 0; i <= tailIndex; i++) {
    final DiscoveryPeer p = kBucket[i];
    if (id.equals(p.getId())) {
      arraycopy(kBucket, 0, kBucket, 1, i);
      kBucket[0] = p;
      return Optional.of(p);
    }
  }
  return Optional.empty();
}
 
Example 8
Source File: PrivateTransaction.java    From besu with Apache License 2.0 5 votes vote down vote up
private static Restriction convertToEnum(final Bytes readBytes) {
  if (readBytes.equals(Restriction.RESTRICTED.getBytes())) {
    return Restriction.RESTRICTED;
  } else if (readBytes.equals(Restriction.UNRESTRICTED.getBytes())) {
    return Restriction.UNRESTRICTED;
  }
  return Restriction.UNSUPPORTED;
}
 
Example 9
Source File: RLPxConnection.java    From incubator-tuweni with Apache License 2.0 4 votes vote down vote up
public RLPxMessage readFrame(Bytes messageFrame) {
  if (messageFrame.size() < 32) {
    return null;
  }
  Integer frameSize = lastFrameSize;
  if (frameSize == null) {

    Bytes macBytes = messageFrame.slice(16, 16);
    Bytes headerBytes = messageFrame.slice(0, 16);

    Bytes decryptedHeader = Bytes.wrap(new byte[16]);
    decryptionCipher.processBytes(headerBytes.toArrayUnsafe(), 0, 16, decryptedHeader.toArrayUnsafe(), 0);
    frameSize = decryptedHeader.get(0) & 0xff;
    frameSize = (frameSize << 8) + (decryptedHeader.get(1) & 0xff);
    frameSize = (frameSize << 8) + (decryptedHeader.get(2) & 0xff);

    Bytes expectedMac = calculateMac(headerBytes, true);

    if (!macBytes.equals(expectedMac)) {
      throw new InvalidMACException(
          String
              .format(
                  "Header MAC did not match expected MAC; expected: %s, received: %s",
                  expectedMac.toHexString(),
                  macBytes.toHexString()));
    }
  }
  int pad = frameSize % 16 == 0 ? 0 : 16 - frameSize % 16;
  if (messageFrame.size() < 32 + frameSize + pad + 16) {
    lastFrameSize = frameSize;
    return null;
  } else {
    lastFrameSize = null;
  }



  Bytes frameData = messageFrame.slice(32, frameSize + pad);
  Bytes frameMac = messageFrame.slice(32 + frameSize + pad, 16);

  Bytes newFrameMac = Bytes.wrap(new byte[16]);
  Bytes frameMacSeed = updateIngress(messageFrame.slice(32, frameSize + pad));
  macEncryptionEngine.processBlock(frameMacSeed.toArrayUnsafe(), 0, newFrameMac.toArrayUnsafe(), 0);
  Bytes expectedFrameMac = updateIngress(newFrameMac.xor(frameMacSeed.slice(0, 16))).slice(0, 16);
  if (!expectedFrameMac.equals(frameMac)) {
    throw new InvalidMACException(
        String
            .format(
                "Frame MAC did not match expected MAC; expected: %s, received: %s",
                expectedFrameMac.toHexString(),
                frameMac.toHexString()));
  }

  Bytes decryptedFrameData = Bytes.wrap(new byte[frameData.size()]);
  decryptionCipher
      .processBytes(frameData.toArrayUnsafe(), 0, frameData.size(), decryptedFrameData.toArrayUnsafe(), 0);

  int messageType = RLP.decodeInt(decryptedFrameData.slice(0, 1));

  Bytes messageData = decryptedFrameData.slice(1, decryptedFrameData.size() - 1 - pad);
  if (applySnappyCompression) {
    try {
      messageData = Bytes.wrap(Snappy.uncompress(messageData.toArrayUnsafe()));
    } catch (IOException e) {
      throw new IllegalArgumentException(e);
    }
  }

  return new RLPxMessage(messageType, messageData, 32 + frameSize + pad + 16);
}
 
Example 10
Source File: FileBackedFingerprintRepository.java    From incubator-tuweni with Apache License 2.0 4 votes vote down vote up
@Override
public boolean contains(String identifier, Bytes fingerprint) {
  return fingerprint.equals(fingerprints.get(identifier));
}
 
Example 11
Source File: SimpleOffsetSerializer.java    From teku with Apache License 2.0 4 votes vote down vote up
public static Bytes serialize(SimpleOffsetSerializable value) {
  // TODO assert sum(fixed_lengths + variable_lengths) < 2**(BYTES_PER_LENGTH_OFFSET *
  // BITS_PER_BYTE)
  // List<UnsignedLong> variable_lengths = new ArrayList<>();
  List<UnsignedLong> variable_offsets = new ArrayList<>();
  List<Bytes> interleaved_values = new ArrayList<>();
  UnsignedLong fixedLengthSum = UnsignedLong.ZERO;
  UnsignedLong varLengthSum = UnsignedLong.ZERO;

  // System.out.println("Fixed Part Size: " + value.get_fixed_parts().size());
  // System.out.println("Var Part Size: " + value.get_variable_parts().size());
  for (Bytes fixedPart : value.get_fixed_parts()) {
    UnsignedLong fixedPartSize = UnsignedLong.valueOf(fixedPart.size());
    if (fixedPartSize.equals(UnsignedLong.ZERO)) {
      fixedPartSize = UnsignedLong.valueOf(4L);
    }
    fixedLengthSum = fixedLengthSum.plus(fixedPartSize);
  }

  variable_offsets.add(fixedLengthSum);
  for (Bytes varPart : value.get_variable_parts()) {
    UnsignedLong varPartSize = UnsignedLong.valueOf(varPart.size());
    varLengthSum = varLengthSum.plus(varPartSize);
    variable_offsets.add(fixedLengthSum.plus(varLengthSum));
  }

  int interleavingIndex = 0;
  for (Bytes element : value.get_fixed_parts()) {
    if (!element.equals(Bytes.EMPTY)) {
      interleaved_values.add(element);
    } else {
      interleaved_values.add(
          SSZ.encodeUInt32(variable_offsets.get(interleavingIndex).longValue()));
    }
    ++interleavingIndex;
  }

  return Bytes.wrap(
      Bytes.concatenate(interleaved_values.toArray(new Bytes[0])),
      Bytes.concatenate(value.get_variable_parts().toArray(new Bytes[0])));
}