xref: /aosp_15_r20/external/federated-compute/fcp/secagg/server/secagg_server_completed_state.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2018 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "fcp/secagg/server/secagg_server_completed_state.h"
18 
19 #include <memory>
20 #include <utility>
21 
22 #include "fcp/base/monitoring.h"
23 #include "fcp/tracing/tracing_span.h"
24 
25 namespace fcp {
26 namespace secagg {
27 
SecAggServerCompletedState(std::unique_ptr<SecAggServerProtocolImpl> impl,int number_of_clients_failed_after_sending_masked_input,int number_of_clients_failed_before_sending_masked_input,int number_of_clients_terminated_without_unmasking)28 SecAggServerCompletedState::SecAggServerCompletedState(
29     std::unique_ptr<SecAggServerProtocolImpl> impl,
30     int number_of_clients_failed_after_sending_masked_input,
31     int number_of_clients_failed_before_sending_masked_input,
32     int number_of_clients_terminated_without_unmasking)
33     : SecAggServerState(number_of_clients_failed_after_sending_masked_input,
34                         number_of_clients_failed_before_sending_masked_input,
35                         number_of_clients_terminated_without_unmasking,
36                         SecAggServerStateKind::COMPLETED, std::move(impl)) {
37   // Moving to this state means the protocol succeeded!
38   if (metrics()) {
39     metrics()->ProtocolOutcomes(SecAggServerOutcome::SUCCESS);
40   }
41   Trace<SecAggProtocolOutcome>(TracingSecAggServerOutcome_Success);
42 }
43 
~SecAggServerCompletedState()44 SecAggServerCompletedState::~SecAggServerCompletedState() {}
45 
IsCompletedSuccessfully() const46 bool SecAggServerCompletedState::IsCompletedSuccessfully() const {
47   return true;
48 }
49 
NumberOfIncludedInputs() const50 int SecAggServerCompletedState::NumberOfIncludedInputs() const {
51   return total_number_of_clients() -
52          number_of_clients_failed_before_sending_masked_input_;
53 }
54 
IsNumberOfIncludedInputsCommitted() const55 bool SecAggServerCompletedState::IsNumberOfIncludedInputsCommitted() const {
56   return true;
57 }
58 
59 StatusOr<std::unique_ptr<SecAggVectorMap> >
Result()60 SecAggServerCompletedState::Result() {
61   auto result = impl()->TakeResult();
62   if (!result) {
63     return FCP_STATUS(UNAVAILABLE)
64            << "Result is uninitialized or requested more than once";
65   }
66   return std::move(result);
67 }
68 
69 }  // namespace secagg
70 }  // namespace fcp
71