1 /*
2  * Copyright 2019 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 #ifndef FCP_SECAGG_SERVER_SECAGG_SERVER_R2_MASKED_INPUT_COLL_STATE_H_
18 #define FCP_SECAGG_SERVER_SECAGG_SERVER_R2_MASKED_INPUT_COLL_STATE_H_
19 
20 #include <functional>
21 #include <memory>
22 #include <string>
23 
24 #include "fcp/secagg/server/secagg_scheduler.h"
25 #include "fcp/secagg/server/secagg_server_state.h"
26 
27 namespace fcp {
28 namespace secagg {
29 
30 // This class is the State for the SecAggServer when it is in the
31 // Round 2: Masked Input Collection state. This state receives masked inputs
32 // from clients and adds them together in preparation for the unmasking step. At
33 // the conclusion of masked input collection, if the server has collected enough
34 // masked inputs, it sends the clients a message with the set of clients that
35 // have not sent masked inputs and moved into Round 3: Unmasking. If too many
36 // clients abort, it can abort instead.
37 class SecAggServerR2MaskedInputCollState : public SecAggServerState {
38  public:
39   SecAggServerR2MaskedInputCollState(
40       std::unique_ptr<SecAggServerProtocolImpl> impl,
41       int number_of_clients_failed_after_sending_masked_input,
42       int number_of_clients_failed_before_sending_masked_input,
43       int number_of_clients_terminated_without_unmasking);
44 
45   ~SecAggServerR2MaskedInputCollState() override;
46 
47   bool IsNumberOfIncludedInputsCommitted() const override;
48 
49   int MinimumMessagesNeededForNextRound() const override;
50 
51   int NumberOfIncludedInputs() const override;
52 
53   int NumberOfPendingClients() const override;
54 
55   // This will return true only after minimum_number_of_clients_to_proceed
56   // clients have sent messages (and not subsequently aborted).
57   bool ReadyForNextRound() const override;
58 
59   // Handles a masked input response or abort message from a client.
60   Status HandleMessage(uint32_t client_id,
61                        const ClientToServerWrapperMessage& message) override;
62   Status HandleMessage(
63       uint32_t client_id,
64       std::unique_ptr<ClientToServerWrapperMessage> message) override;
65 
66   StatusOr<std::unique_ptr<SecAggServerState> > ProceedToNextRound() override;
67 
68   bool SetAsyncCallback(std::function<void()> async_callback) override;
69 
70  protected:
71   // Track the clients who abort this round and send this list to the clients.
72   std::vector<uint32_t> clients_aborted_at_round_2_;
73 
74  private:
75   std::shared_ptr<Accumulator<SecAggUnpackedVectorMap>> accumulator_;
76   void HandleAbort() override;
77 
78   void HandleAbortClient(uint32_t client_id,
79                          ClientDropReason reason_code) override;
80 };
81 
82 }  // namespace secagg
83 }  // namespace fcp
84 
85 #endif  // FCP_SECAGG_SERVER_SECAGG_SERVER_R2_MASKED_INPUT_COLL_STATE_H_
86