private static long computeGasUsed(
      final Deque<FlatTrace.Context> tracesContexts,
      final FlatTrace.Context currentContext,
      final TransactionTrace transactionTrace,
      final TraceFrame traceFrame) {

    final long gasRemainingBeforeProcessed;
    final long gasRemainingAfterProcessed;
    long gasRefund = 0;
    if (tracesContexts.size() == 1) {
      gasRemainingBeforeProcessed = transactionTrace.getTraceFrames().get(0).getGasRemaining();
      gasRemainingAfterProcessed = transactionTrace.getResult().getGasRemaining();
      if (gasRemainingAfterProcessed > traceFrame.getGasRemaining()) {
        gasRefund = gasRemainingAfterProcessed - traceFrame.getGasRemaining();
      } else {
        gasRefund = traceFrame.getGasRefund();
      }
    } else {
      final Action.Builder actionBuilder = currentContext.getBuilder().getActionBuilder();
      gasRemainingBeforeProcessed = Long.decode(actionBuilder.getGas());
      gasRemainingAfterProcessed = traceFrame.getGasRemaining();
    }
    return gasRemainingBeforeProcessed - gasRemainingAfterProcessed + gasRefund;
  }
Generates a stream of {@link Trace} from the passed {@link TransactionTrace} and {@link Block}
data with additional Transaction Information added to FlatTrace

@param protocolSchedule the current {@link ProtocolSchedule} to use
@param transactionTrace the {@link TransactionTrace} to use
@param block the {@link Block} to use
@return a stream of generated traces {@link Trace}
ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/tracing/flat/FlatTraceGenerator.java