|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "platform/consensus/ordering/poe/framework/consensus.h" |
| 21 | + |
| 22 | +#include <glog/logging.h> |
| 23 | +#include <unistd.h> |
| 24 | + |
| 25 | +#include "common/utils/utils.h" |
| 26 | + |
| 27 | +namespace resdb { |
| 28 | +namespace poe { |
| 29 | + |
| 30 | +Consensus::Consensus(const ResDBConfig& config, |
| 31 | + std::unique_ptr<TransactionManager> executor) |
| 32 | + : common::Consensus(config, std::move(executor)) { |
| 33 | + int total_replicas = config_.GetReplicaNum(); |
| 34 | + int f = (total_replicas - 1) / 3; |
| 35 | + |
| 36 | + Init(); |
| 37 | + |
| 38 | + start_ = 0; |
| 39 | + |
| 40 | + if (config_.GetPublicKeyCertificateInfo() |
| 41 | + .public_key() |
| 42 | + .public_key_info() |
| 43 | + .type() != CertificateKeyInfo::CLIENT) { |
| 44 | + poe_ = std::make_unique<PoE>(config_.GetSelfInfo().id(), f, total_replicas, |
| 45 | + GetSignatureVerifier()); |
| 46 | + InitProtocol(poe_.get()); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +int Consensus::ProcessCustomConsensus(std::unique_ptr<Request> request) { |
| 51 | + if (request->user_type() == MessageType::Propose) { |
| 52 | + std::unique_ptr<Transaction> txn = std::make_unique<Transaction>(); |
| 53 | + if (!txn->ParseFromString(request->data())) { |
| 54 | + assert(1 == 0); |
| 55 | + LOG(ERROR) << "parse proposal fail"; |
| 56 | + return -1; |
| 57 | + } |
| 58 | + poe_->ReceivePropose(std::move(txn)); |
| 59 | + return 0; |
| 60 | + } else if (request->user_type() == MessageType::Prepare) { |
| 61 | + std::unique_ptr<Proposal> proposal = std::make_unique<Proposal>(); |
| 62 | + if (!proposal->ParseFromString(request->data())) { |
| 63 | + LOG(ERROR) << "parse proposal fail"; |
| 64 | + assert(1 == 0); |
| 65 | + return -1; |
| 66 | + } |
| 67 | + poe_->ReceivePrepare(std::move(proposal)); |
| 68 | + return 0; |
| 69 | + } |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +int Consensus::ProcessNewTransaction(std::unique_ptr<Request> request) { |
| 74 | + std::unique_ptr<Transaction> txn = std::make_unique<Transaction>(); |
| 75 | + txn->set_data(request->data()); |
| 76 | + txn->set_hash(request->hash()); |
| 77 | + txn->set_proxy_id(request->proxy_id()); |
| 78 | + txn->set_uid(request->uid()); |
| 79 | + return poe_->ReceiveTransaction(std::move(txn)); |
| 80 | +} |
| 81 | + |
| 82 | +int Consensus::CommitMsg(const google::protobuf::Message& msg) { |
| 83 | + return CommitMsgInternal(dynamic_cast<const Transaction&>(msg)); |
| 84 | +} |
| 85 | + |
| 86 | +int Consensus::CommitMsgInternal(const Transaction& txn) { |
| 87 | + std::unique_ptr<Request> request = std::make_unique<Request>(); |
| 88 | + request->set_data(txn.data()); |
| 89 | + request->set_seq(txn.seq()); |
| 90 | + request->set_uid(txn.uid()); |
| 91 | + request->set_proxy_id(txn.proxy_id()); |
| 92 | + |
| 93 | + transaction_executor_->Commit(std::move(request)); |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace poe |
| 98 | +} // namespace resdb |
0 commit comments