Loading...
Searching...
No Matches
contract_executor.cpp
Go to the documentation of this file.
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
21
22#include <glog/logging.h>
23
24namespace resdb {
25namespace contract {
26
28 : contract_manager_(std::make_unique<ContractManager>()),
29 address_manager_(std::make_unique<AddressManager>()) {}
30
31std::unique_ptr<std::string> ContractTransactionManager::ExecuteData(
32 const std::string& client_request) {
33 Request request;
34 Response response;
35
36 if (!request.ParseFromString(client_request)) {
37 LOG(ERROR) << "parse data fail";
38 return nullptr;
39 }
40
41 int ret = 0;
42 if (request.cmd() == Request::CREATE_ACCOUNT) {
43 absl::StatusOr<Account> account_or = CreateAccount();
44 if (account_or.ok()) {
45 response.mutable_account()->Swap(&(*account_or));
46 } else {
47 ret = -1;
48 }
49 } else if (request.cmd() == Request::DEPLOY) {
50 absl::StatusOr<Contract> contract_or = Deploy(request);
51 if (contract_or.ok()) {
52 response.mutable_contract()->Swap(&(*contract_or));
53 } else {
54 ret = -1;
55 }
56 } else if (request.cmd() == Request::EXECUTE) {
57 auto res_or = Execute(request);
58 if (res_or.ok()) {
59 response.set_res(*res_or);
60 } else {
61 ret = -1;
62 }
63 }
64
65 response.set_ret(ret);
66
67 std::unique_ptr<std::string> resp_str = std::make_unique<std::string>();
68 if (!response.SerializeToString(resp_str.get())) {
69 return nullptr;
70 }
71
72 return resp_str;
73}
74
75absl::StatusOr<Account> ContractTransactionManager::CreateAccount() {
76 std::string address =
77 AddressManager::AddressToHex(address_manager_->CreateRandomAddress());
78 Account account;
79 account.set_address(address);
80 return account;
81}
82
83absl::StatusOr<Contract> ContractTransactionManager::Deploy(
84 const Request& request) {
85 Address caller_address =
86 AddressManager::HexToAddress(request.caller_address());
87 if (!address_manager_->Exist(caller_address)) {
88 LOG(ERROR) << "caller doesn't have an account";
89 return absl::InvalidArgumentError("Account not exist.");
90 }
91
92 Address contract_address =
93 contract_manager_->DeployContract(caller_address, request.deploy_info());
94
95 if (contract_address > 0) {
96 Contract contract;
97 contract.set_owner_address(request.caller_address());
98 contract.set_contract_address(
99 AddressManager::AddressToHex(contract_address));
100 contract.set_contract_name(request.deploy_info().contract_name());
101 return contract;
102 }
103 return absl::InternalError("Deploy Contract fail.");
104}
105
106absl::StatusOr<std::string> ContractTransactionManager::Execute(
107 const Request& request) {
108 Address caller_address =
109 AddressManager::HexToAddress(request.caller_address());
110 if (!address_manager_->Exist(caller_address)) {
111 LOG(ERROR) << "caller doesn't have an account";
112 return absl::InvalidArgumentError("Account not exist.");
113 }
114
115 return contract_manager_->ExecContract(
116 caller_address, AddressManager::HexToAddress(request.contract_address()),
117 request.func_params());
118}
119
120} // namespace contract
121} // namespace resdb
static std::string AddressToHex(const Address &address)
static Address HexToAddress(const std::string &address)
std::unique_ptr< std::string > ExecuteData(const std::string &request) override
eevm::Address Address
Definition utils.h:27