class LimitedTransactionsMessages {

  public static LimitedTransactionsMessages createLimited(
      final Iterable<Transaction> transactions) {
    final Set<Transaction> includedTransactions = new HashSet<>();
    final BytesValueRLPOutput message = new BytesValueRLPOutput();
    int messageSize = 0;
    message.startList();
    for (final Transaction transaction : transactions) {
      final BytesValueRLPOutput encodedTransaction = new BytesValueRLPOutput();
      transaction.writeTo(encodedTransaction);
      Bytes encodedBytes = encodedTransaction.encoded();
      // Break if individual transaction size exceeds limit
      if (encodedBytes.size() > LIMIT && (messageSize != 0)) {
        break;
      }
      message.writeRLPUnsafe(encodedBytes);
      includedTransactions.add(transaction);
      // Check if last transaction to add to the message
      messageSize += encodedBytes.size();
      if (messageSize > LIMIT) {
        break;
      }
    }
    message.endList();
    return new LimitedTransactionsMessages(
        new TransactionsMessage(message.encoded()), includedTransactions);
  }

  public  LimitedTransactionsMessages(
      final TransactionsMessage transactionsMessage, final Set<Transaction> includedTransactions);

}