/**
   * Write commands to log file
   *
   * @param commands array of commands. Each command is an array of bytes
   * @param msgCtx
   */
  private void saveCommands(byte[][] commands, MessageContext[] msgCtx) {
    if (!config.isToLog()) return;

    if (commands.length != msgCtx.length) {
      System.out.println("----SIZE OF COMMANDS AND MESSAGE CONTEXTS IS DIFFERENT----");
      System.out.println(
          "----COMMANDS: " + commands.length + ", CONTEXTS: " + msgCtx.length + " ----");
    }

    logLock.lock();

    int cid = msgCtx[0].getConsensusId();
    int batchStart = 0;
    for (int i = 0; i <= msgCtx.length; i++) {
      if (i
          == msgCtx
              .length) { // the batch command contains only one command or it is the last position
                         // of the array
        byte[][] batch = Arrays.copyOfRange(commands, batchStart, i);
        MessageContext[] batchMsgCtx = Arrays.copyOfRange(msgCtx, batchStart, i);
        log.addMessageBatch(batch, batchMsgCtx, cid);
        log.setLastCID(cid, globalCheckpointPeriod, checkpointPortion);
        //				if(batchStart > 0)
        //					System.out.println("Last batch: " + commands.length + "," + batchStart + "-" + i +
        // "," + batch.length);
      } else {
        if (msgCtx[i].getConsensusId()
            > cid) { // saves commands when the CID changes or when it is the last batch
          byte[][] batch = Arrays.copyOfRange(commands, batchStart, i);
          MessageContext[] batchMsgCtx = Arrays.copyOfRange(msgCtx, batchStart, i);
          //					System.out.println("THERE IS MORE THAN ONE CID in this batch." + commands.length +
          // "," + batchStart + "-" + i + "," + batch.length);
          log.addMessageBatch(batch, batchMsgCtx, cid);
          log.setLastCID(cid, globalCheckpointPeriod, checkpointPortion);
          cid = msgCtx[i].getConsensusId();
          batchStart = i;
        }
      }
    }
    logLock.unlock();
  }
  private void saveState(byte[] snapshot, int lastCID) {
    logLock.lock();

    Logger.println("(TOMLayer.saveState) Saving state of CID " + lastCID);

    log.newCheckpoint(snapshot, computeHash(snapshot), lastCID);
    log.setLastCID(-1);
    log.setLastCheckpointCID(lastCID);

    logLock.unlock();
    Logger.println("(TOMLayer.saveState) Finished saving state of CID " + lastCID);
  }