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

The following examples show how to use org.apache.tuweni.bytes.Bytes32#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: StoredMerklePatriciaTrie.java    From besu with Apache License 2.0 5 votes vote down vote up
/**
 * Create a trie.
 *
 * @param nodeLoader The {@link NodeLoader} to retrieve node data from.
 * @param rootHash The initial root has for the trie, which should be already present in {@code
 *     storage}.
 * @param valueSerializer A function for serializing values to bytes.
 * @param valueDeserializer A function for deserializing values from bytes.
 */
public StoredMerklePatriciaTrie(
    final NodeLoader nodeLoader,
    final Bytes32 rootHash,
    final Function<V, Bytes> valueSerializer,
    final Function<Bytes, V> valueDeserializer) {
  this.nodeFactory = new StoredNodeFactory<>(nodeLoader, valueSerializer, valueDeserializer);
  this.root =
      rootHash.equals(EMPTY_TRIE_NODE_HASH)
          ? NullNode.instance()
          : new StoredNode<>(nodeFactory, rootHash);
}
 
Example 2
Source File: StoredMerklePatriciaTrie.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public void commit(final NodeUpdater nodeUpdater) {
  final CommitVisitor<V> commitVisitor = new CommitVisitor<>(nodeUpdater);
  root.accept(commitVisitor);
  // Make sure root node was stored
  if (root.isDirty() && root.getRlpRef().size() < 32) {
    nodeUpdater.store(root.getHash(), root.getRlpRef());
  }
  // Reset root so dirty nodes can be garbage collected
  final Bytes32 rootHash = root.getHash();
  this.root =
      rootHash.equals(EMPTY_TRIE_NODE_HASH)
          ? NullNode.instance()
          : new StoredNode<>(nodeFactory, rootHash);
}
 
Example 3
Source File: WorldStateKeyValueStorage.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Bytes> getCode(final Bytes32 codeHash) {
  if (codeHash.equals(Hash.EMPTY)) {
    return Optional.of(Bytes.EMPTY);
  } else {
    return keyValueStorage.get(codeHash.toArrayUnsafe()).map(Bytes::wrap);
  }
}
 
Example 4
Source File: WorldStateKeyValueStorage.java    From besu with Apache License 2.0 5 votes vote down vote up
private Optional<Bytes> getTrieNode(final Bytes32 nodeHash) {
  if (nodeHash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
    return Optional.of(MerklePatriciaTrie.EMPTY_TRIE_NODE);
  } else {
    return keyValueStorage.get(nodeHash.toArrayUnsafe()).map(Bytes::wrap);
  }
}
 
Example 5
Source File: WorldStateKeyValueStorage.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Bytes> getNodeData(final Bytes32 hash) {
  if (hash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
    return Optional.of(MerklePatriciaTrie.EMPTY_TRIE_NODE);
  } else if (hash.equals(Hash.EMPTY)) {
    return Optional.of(Bytes.EMPTY);
  } else {
    return keyValueStorage.get(hash.toArrayUnsafe()).map(Bytes::wrap);
  }
}
 
Example 6
Source File: WorldStateKeyValueStorage.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public Updater putAccountStateTrieNode(final Bytes32 nodeHash, final Bytes node) {
  if (nodeHash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
    // Don't save empty nodes
    return this;
  }
  addedNodes.add(nodeHash);
  transaction.put(nodeHash.toArrayUnsafe(), node.toArrayUnsafe());
  return this;
}
 
Example 7
Source File: WorldStateKeyValueStorage.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public Updater putAccountStorageTrieNode(final Bytes32 nodeHash, final Bytes node) {
  if (nodeHash.equals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) {
    // Don't save empty nodes
    return this;
  }
  addedNodes.add(nodeHash);
  transaction.put(nodeHash.toArrayUnsafe(), node.toArrayUnsafe());
  return this;
}
 
Example 8
Source File: MainnetBlockBodyValidator.java    From besu with Apache License 2.0 5 votes vote down vote up
private static boolean validateTransactionsRoot(final Bytes32 expected, final Bytes32 actual) {
  if (!expected.equals(actual)) {
    LOG.warn(
        "Invalid block: transaction root mismatch (expected={}, actual={})", expected, actual);
    return false;
  }

  return true;
}
 
Example 9
Source File: MainnetBlockBodyValidator.java    From besu with Apache License 2.0 5 votes vote down vote up
private static boolean validateReceiptsRoot(final Bytes32 expected, final Bytes32 actual) {
  if (!expected.equals(actual)) {
    LOG.warn("Invalid block: receipts root mismatch (expected={}, actual={})", expected, actual);
    return false;
  }

  return true;
}
 
Example 10
Source File: MainnetBlockBodyValidator.java    From besu with Apache License 2.0 5 votes vote down vote up
private static boolean validateStateRoot(final Bytes32 expected, final Bytes32 actual) {
  if (!expected.equals(actual)) {
    LOG.warn("Invalid block: state root mismatch (expected={}, actual={})", expected, actual);
    return false;
  }

  return true;
}
 
Example 11
Source File: MainnetBlockBodyValidator.java    From besu with Apache License 2.0 5 votes vote down vote up
private static boolean validateOmmersHash(final Bytes32 expected, final Bytes32 actual) {
  if (!expected.equals(actual)) {
    LOG.warn("Invalid block: ommers hash mismatch (expected={}, actual={})", expected, actual);
    return false;
  }

  return true;
}
 
Example 12
Source File: OnChainPrivacyPrecompiledContract.java    From besu with Apache License 2.0 5 votes vote down vote up
protected boolean onChainPrivacyGroupVersionMatches(
    final MessageFrame messageFrame,
    final ProcessableBlockHeader currentBlockHeader,
    final Bytes32 version,
    final WorldUpdater publicWorldState,
    final Bytes32 privacyGroupId,
    final Blockchain currentBlockchain,
    final MutableWorldState disposablePrivateState,
    final WorldUpdater privateWorldStateUpdater) {
  // We need the "version" of the group for every single transaction but we don't want this
  // call to affect the state
  // privateTransactionProcessor.processTransaction(...) commits the state if the process was
  // successful before it returns
  final PrivateTransactionProcessor.Result getVersionResult =
      simulateTransaction(
          messageFrame,
          currentBlockHeader,
          publicWorldState,
          privacyGroupId,
          currentBlockchain,
          disposablePrivateState,
          privateWorldStateUpdater,
          OnChainGroupManagement.GET_VERSION_METHOD_SIGNATURE);

  if (version.equals(getVersionResult.getOutput())) {
    return true;
  }
  LOG.debug(
      "Privacy Group {} version mismatch for commitment {}: expecting {} but got {}",
      privacyGroupId.toBase64String(),
      messageFrame.getTransactionHash(),
      getVersionResult.getOutput(),
      version);
  return false;
}
 
Example 13
Source File: BeaconStateUtil.java    From teku with Apache License 2.0 3 votes vote down vote up
/**
 * Verify that the given ``leaf`` is on the merkle branch ``branch`` starting with the given
 * ``root``.
 *
 * @param leaf
 * @param branch
 * @param depth
 * @param index
 * @param root
 * @return A boolean depending on the merkle branch being valid
 * @see
 *     <a>https://github.com/ethereum/eth2.0-specs/blob/v0.8.0/specs/core/0_beacon-chain.md#is_valid_merkle_branch</a>
 */
public static boolean is_valid_merkle_branch(
    Bytes32 leaf, SSZVector<Bytes32> branch, int depth, int index, Bytes32 root) {
  Bytes32 value = leaf;
  for (int i = 0; i < depth; i++) {
    if (Math.floor(index / Math.pow(2, i)) % 2 == 1) {
      value = Hash.sha2_256(Bytes.concatenate(branch.get(i), value));
    } else {
      value = Hash.sha2_256(Bytes.concatenate(value, branch.get(i)));
    }
  }
  return value.equals(root);
}
 
Example 14
Source File: ForkIdManager.java    From besu with Apache License 2.0 2 votes vote down vote up
/**
 * Non EIP-2124 behaviour
 *
 * @param peerGenesisHash Hash to be validated.
 * @return boolean
 */
public boolean peerCheck(final Bytes32 peerGenesisHash) {
  return !peerGenesisHash.equals(genesisHash);
}