Java Code Examples for com.google.protobuf.ByteString#startsWith()

The following examples show how to use com.google.protobuf.ByteString#startsWith() . 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: ContractFunctionResult.java    From hedera-sdk-java with Apache License 2.0 6 votes vote down vote up
@Internal
// exposed for use in `TransactionRecord`
public ContractFunctionResult(ContractFunctionResultOrBuilder inner) {
    contractId = new ContractId(inner.getContractIDOrBuilder());

    String errMsg = inner.getErrorMessage();
    errorMessage = !errMsg.isEmpty() ? errMsg : null;

    ByteString callResult = inner.getContractCallResult();

    // if an exception was thrown, the call result is encoded like the params
    // for a function `Error(string)`
    // https://solidity.readthedocs.io/en/v0.6.2/control-structures.html#revert
    if (errorMessage != null && callResult.startsWith(errorPrefix)) {
        // trim off the function selector bytes
        rawResult = callResult.substring(4);
    } else {
        rawResult = callResult;
    }

    bloom = inner.getBloom().toByteArray();

    gasUsed = inner.getGasUsed();

    logs = inner.getLogInfoList().stream().map(ContractLogInfo::new).collect(Collectors.toList());
}
 
Example 2
Source File: HashedTrie.java    From snowblossom with Apache License 2.0 5 votes vote down vote up
/**
 * get entry from the given root hash or null of it does not exist
 */
public ByteString getLeafData(ByteString root_hash, ByteString key)
{
  TrieNode node = basedb.load(root_hash);
  if (node == null)
  {
    throw new RuntimeException(String.format("Referenced node %s not in database", HashUtils.getHexString(root_hash)));
  }
  if (node.getPrefix().equals(key))
  {
    if (node.getIsLeaf())
    {
      return node.getLeafData();
    }
    return null;
  }
  Assert.assertTrue(key.startsWith(node.getPrefix()));

  for(ChildEntry ce : node.getChildrenList())
  {
    ByteString p = node.getPrefix().concat(ce.getKey());
    if (key.startsWith(p))
    {
      return getLeafData(ce.getHash(), key);
    }
  }

  return null;
}
 
Example 3
Source File: HashedTrie.java    From snowblossom with Apache License 2.0 5 votes vote down vote up
public void getNodeDetails(ByteString hash, ByteString target_key, LinkedList<TrieNode> proof, LinkedList<TrieNode> results, int max_results)
{
  TrieNode node = basedb.load(hash);
  if (node == null)
  {
    throw new RuntimeException(String.format("Referenced node %s not in database", HashUtils.getHexString(hash)));
  }

  if (target_key.size() > node.getPrefix().size())
  {
    proof.add(node);
  }
  else
  {
    results.add(node);
  }
  if (results.size() >= max_results) return;


  for(ChildEntry ce : node.getChildrenList())
  {
    ByteString p = node.getPrefix().concat(ce.getKey());
    if (p.size() <= target_key.size())
    {
      if (target_key.startsWith(p))
      {
        getNodeDetails(ce.getHash(), target_key, proof, results, max_results);
      }
    }
    else
    {
      if(p.startsWith(target_key))
      {
        getNodeDetails(ce.getHash(), target_key, proof, results, max_results);
      }
    }
  }

 
}
 
Example 4
Source File: RocksDBMapMutationSet.java    From snowblossom with Apache License 2.0 5 votes vote down vote up
@Override
public List<ByteString> getSet(ByteString key, int max_reply)
{
ByteString dbKey = getDBKey(key);

  LinkedList<ByteString> set = new LinkedList<>();
  int count = 0;
  try(RocksIterator it = db.newIterator())
  {
    it.seek(dbKey.toByteArray());

    while(it.isValid())
    {
		ByteString curr_key = ByteString.copyFrom(it.key());
      if (!curr_key.startsWith(dbKey)) break;

      ByteString v = curr_key.substring(dbKey.size());
      set.add(v);
      count++;

      if (count > max_reply) throw new DBTooManyResultsException();

      it.next();
    }
  }

  return set;

}
 
Example 5
Source File: TextSource.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes the current element updating the buffer to only contain the unconsumed bytes.
 *
 * <p>This invalidates the currently stored {@code startOfDelimiterInBuffer} and {@code
 * endOfDelimiterInBuffer}.
 */
private void decodeCurrentElement() throws IOException {
  ByteString dataToDecode = buffer.substring(0, startOfDelimiterInBuffer);
  // If present, the UTF8 Byte Order Mark (BOM) will be removed.
  if (startOfRecord == 0 && dataToDecode.startsWith(UTF8_BOM)) {
    dataToDecode = dataToDecode.substring(UTF8_BOM.size());
  }
  currentValue = dataToDecode.toStringUtf8();
  elementIsPresent = true;
  buffer = buffer.substring(endOfDelimiterInBuffer);
}
 
Example 6
Source File: RocksDBMapMutationSet.java    From jelectrum with MIT License 5 votes vote down vote up
public Set<ByteString> getSet(ByteString key, int max_reply)
{
ByteString dbKey = getDBKey(key);

  HashSet<ByteString> set = new HashSet<>();
  int count = 0;
  RocksIterator it = db.newIterator();

  try
  {
    it.seek(dbKey.toByteArray());

    while(it.isValid())
    {
		ByteString curr_key = ByteString.copyFrom(it.key());
      if (!curr_key.startsWith(dbKey)) break;

      ByteString v = curr_key.substring(dbKey.size());
      set.add(v);
      count++;

      if (count > max_reply) throw new DBTooManyResultsException();

      it.next();

    }

  }
  finally
  {
    it.dispose();
  }


  return set;

}
 
Example 7
Source File: GetUTXOUtil.java    From snowblossom with Apache License 2.0 4 votes vote down vote up
private static void descend(
  ByteString prefix, 
  ByteString search, 
  UserServiceBlockingStub stub,
  List<TransactionBridge> bridges,
  Map<ByteString, TrieNode> node_map,
  ByteString expected_hash,
  ByteString utxo_root)
  throws ValidationException
{
  if (prefix.size() > search.size())
  {
    if (!node_map.containsKey(prefix))
    {
      logger.log(Level.FINE,"Doing additional scan into " + HexUtil.getHexString(prefix) + ".");
      for(TrieNode n : getNodesByPrefix(prefix, stub, false, utxo_root))
      {
        node_map.put(n.getPrefix(), n);
      }
    }
  }

  if (!node_map.containsKey(prefix))
  {
    throw new ValidationException("No node at prefix: " + HexUtil.getHexString(prefix) + ".");
  }

  TrieNode node = node_map.get(prefix);
  if (!node.getHash().equals(expected_hash))
  {
    throw new ValidationException("Hash mismatch at prefix: " + HexUtil.getHexString(prefix) + ".");
  }
  if (node.getIsLeaf())
  {
    bridges.add(new TransactionBridge(node));
  }
  
  for(ChildEntry ce : node.getChildrenList())
  {
    ByteString next = prefix.concat(ce.getKey());
    if (next.size() <= search.size())
    {
      if (search.startsWith(next))
      {
        descend(next, search, stub, bridges, node_map, ce.getHash(), utxo_root);
      }
    }
    if (next.size() > search.size())
    {
      if (next.startsWith(search))
      {
        descend(next, search, stub, bridges, node_map, ce.getHash(), utxo_root);
      }
    }
  }

}
 
Example 8
Source File: RocksDBMap.java    From snowblossom with Apache License 2.0 4 votes vote down vote up
@Override
public Map<ByteString, ByteString> getByPrefix(ByteString key, int max_reply, boolean allow_partial)
{
  ByteString key_str = prefix.concat(key);

  LinkedList<ByteString> set = new LinkedList<>();
Map<ByteString, ByteString> map = new HashMap<>(16,0.5f);

  int count = 0;
  RocksIterator it = db.newIterator();

  try
  {
    it.seek(key_str.toByteArray());

    while(it.isValid())
    {
      ByteString curr_key = ByteString.copyFrom(it.key());
      if (!curr_key.startsWith(key_str)) break;

      ByteString k = curr_key.substring(prefix.size());
      
     	map.put(k, ByteString.copyFrom(it.value()));
		count++;

      if (count > max_reply)
      {
        if (allow_partial) return map;
        else throw new DBTooManyResultsException();
      }

      it.next();
    }
  }
  finally
  {
    it.dispose();
  }

  return map;

}
 
Example 9
Source File: ByteSequence.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * Tests if this <code>ByteSequence</code> starts with the specified prefix.
 *
 * @param  prefix the prefix.
 * @return        <code>true</code> if the byte sequence represented by the argument is a prefix of the
 *                byte sequence represented by this string; <code>false</code> otherwise.
 */
public boolean startsWith(ByteSequence prefix) {
    ByteString baseByteString = this.getByteString();
    ByteString prefixByteString = prefix.getByteString();
    return baseByteString.startsWith(prefixByteString);
}