Loading...
Searching...
No Matches
contract_client.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
24#include <fstream>
25#include <nlohmann/json.hpp>
26
27#include "proto/contract/rpc.pb.h"
28
29namespace resdb {
30namespace contract {
31
34
35absl::StatusOr<Account> ContractClient::CreateAccount() {
36 Request request;
37 Response response;
38 request.set_cmd(Request::CREATE_ACCOUNT);
39 int ret = SendRequest(request, &response);
40 if (ret != 0 || response.ret() != 0) {
41 return absl::InternalError("Account not exist.");
42 }
43 return response.account();
44}
45
46absl::StatusOr<Contract> ContractClient::DeployContract(
47 const std::string& caller_address, const std::string& contract_name,
48 const std::string& contract_path,
49 const std::vector<std::string>& init_params) {
50 std::ifstream contract_fstream(contract_path);
51 if (!contract_fstream) {
52 LOG(ERROR) << "could not find contract definition. file:" << contract_path
53 << " name:" << contract_name;
54 return absl::InvalidArgumentError("Contract not exist.");
55 }
56
57 nlohmann::json contracts_definition = nlohmann::json::parse(contract_fstream);
58
59 const auto all_contracts = contracts_definition["contracts"];
60
61 const std::string contract_code = all_contracts[contract_name]["bin"];
62 if (contract_code.empty()) {
63 LOG(ERROR) << "could not find contract definition. file:" << contract_path
64 << " name:" << contract_name;
65 return absl::InvalidArgumentError("Contract not exist.");
66 }
67
68 const auto func_hashes = all_contracts[contract_name]["hashes"];
69
70 DeployInfo deploy_info;
71 deploy_info.set_contract_bin(contract_code);
72 deploy_info.set_contract_name(contract_name);
73
74 for (auto& func : func_hashes.items()) {
75 FuncInfo* new_func = deploy_info.add_func_info();
76 new_func->set_func_name(func.key());
77 new_func->set_hash(func.value());
78 }
79
80 for (const std::string& param : init_params) {
81 deploy_info.add_init_param(param);
82 }
83
84 Request request;
85 Response response;
86 request.set_caller_address(caller_address);
87 *request.mutable_deploy_info() = deploy_info;
88
89 request.set_cmd(Request::DEPLOY);
90 LOG(ERROR) << "send request:" << request.DebugString();
91 int ret = SendRequest(request, &response);
92 if (ret != 0 || response.ret() != 0) {
93 return absl::InternalError("Deploy contract fail.");
94 }
95 return response.contract();
96}
97
98absl::StatusOr<std::string> ContractClient::ExecuteContract(
99 const std::string& caller_address, const std::string& contract_address,
100 const std::string& func_name, const std::vector<std::string>& func_params) {
101 Request request;
102 Response response;
103 request.set_caller_address(caller_address);
104 request.set_contract_address(contract_address);
105
106 request.mutable_func_params()->set_func_name(func_name);
107 for (const std::string& param : func_params) {
108 request.mutable_func_params()->add_param(param);
109 }
110
111 request.set_cmd(Request::EXECUTE);
112 LOG(ERROR) << "send request:" << request.DebugString();
113 int ret = SendRequest(request, &response);
114 if (ret != 0 || response.ret() != 0) {
115 return absl::InternalError("Deploy contract fail.");
116 }
117 return response.res();
118}
119
120} // namespace contract
121} // namespace resdb
int SendRequest(const google::protobuf::Message &message, Request::Type type=Request::TYPE_CLIENT_REQUEST)
absl::StatusOr< Account > CreateAccount()
absl::StatusOr< std::string > ExecuteContract(const std::string &caller_address, const std::string &contract_address, const std::string &func_name, const std::vector< std::string > &func_params)
absl::StatusOr< Contract > DeployContract(const std::string &caller_address, const std::string &contract_name, const std::string &contract_path, const std::vector< std::string > &init_params)
ContractClient(const ResDBConfig &config)