decode static method

Implementation

static TransactionUpdateMessage decode(BsatnDecoder decoder) {
  // 1. Read UpdateStatus (algebraic enum with table updates inside Committed variant)
  final statusTag = decoder.readU8();

  UpdateStatus status;
  final List<TableUpdate> tableUpdates;

  if (statusTag == 0) {
    // Committed
    status = Committed();
    tableUpdates = decoder.readList(() => TableUpdate.decode(decoder));
  } else if (statusTag == 1) {
    // Failed
    final errorMessage = decoder.readString();
    status = Failed(errorMessage);
    tableUpdates = [];
  } else if (statusTag == 2) {
    // OutOfEnergy — unit variant, no payload
    status = OutOfEnergy();
    tableUpdates = [];
  } else {
    throw ArgumentError('Unknown UpdateStatus tag: $statusTag');
  }

  // 2. Read timestamp (i64, serializes as u64)
  final timestamp = decoder.readU64();

  // 3. Read caller_identity (Identity: 32 bytes, NOT Option)
  final callerIdentity = decoder.readBytes(32);

  // 4. Read caller_connection_id (ConnectionId: u128 = 16 bytes, NOT Option)
  final callerConnectionId = decoder.readBytes(16);

  // 5. Read reducer_call (ReducerCallInfo struct)
  final reducerCall = ReducerInfo.decode(decoder);

  // 6. Read energy_quanta_used (EnergyQuanta: u128 = 16 bytes)
  final energyBytes = decoder.readBytes(16);
  // Convert to int (will lose precision for very large values, but acceptable)
  final energyQuantaUsed =
      energyBytes[0] |
      (energyBytes[1] << 8) |
      (energyBytes[2] << 16) |
      (energyBytes[3] << 24);

  // 7. Read total_host_execution_duration (TimeDuration: i64 microseconds)
  final totalHostExecutionDuration =
      decoder.readU64(); // i64 serializes as u64

  return TransactionUpdateMessage(
    transactionOffset: 0, // Not in wire protocol
    timestamp: timestamp,
    tableUpdates: tableUpdates,
    status: status,
    callerIdentity: callerIdentity,
    callerConnectionId: callerConnectionId,
    reducerCall: reducerCall,
    energyQuantaUsed: energyQuantaUsed,
    totalHostExecutionDuration: totalHostExecutionDuration,
  );
}