-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from resilientdb/smart-contract
Smart contract
- Loading branch information
Showing
44 changed files
with
2,183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
cc_library( | ||
name = "contract_client", | ||
srcs = ["contract_client.cpp"], | ||
hdrs = ["contract_client.h"], | ||
deps = [ | ||
"//application/contract/proto:account_cc_proto", | ||
"//application/contract/proto:contract_cc_proto", | ||
"//application/contract/proto:rpc_cc_proto", | ||
"//client:resdb_user_client", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright (c) 2019-2022 ExpoLab, UC Davis | ||
* | ||
* Permission is hereby granted, free of charge, to any person | ||
* obtaining a copy of this software and associated documentation | ||
* files (the "Software"), to deal in the Software without | ||
* restriction, including without limitation the rights to use, | ||
* copy, modify, merge, publish, distribute, sublicense, and/or | ||
* sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
|
||
#include "application/contract/client/contract_client.h" | ||
|
||
#include <glog/logging.h> | ||
|
||
#include <fstream> | ||
#include <nlohmann/json.hpp> | ||
|
||
#include "application/contract/proto/rpc.pb.h" | ||
|
||
namespace resdb { | ||
namespace contract { | ||
|
||
ContractClient::ContractClient(const ResDBConfig& config) | ||
: ResDBUserClient(config) {} | ||
|
||
absl::StatusOr<Account> ContractClient::CreateAccount() { | ||
Request request; | ||
Response response; | ||
request.set_cmd(Request::CREATE_ACCOUNT); | ||
int ret = SendRequest(request, &response); | ||
if (ret != 0 || response.ret() != 0) { | ||
return absl::InternalError("Account not exist."); | ||
} | ||
return response.account(); | ||
} | ||
|
||
absl::StatusOr<Contract> ContractClient::DeployContract( | ||
const std::string& caller_address, const std::string& contract_name, | ||
const std::string& contract_path, | ||
const std::vector<std::string>& init_params) { | ||
std::ifstream contract_fstream(contract_path); | ||
if (!contract_fstream) { | ||
LOG(ERROR) << "could not find contract definition. file:" << contract_path | ||
<< " name:" << contract_name; | ||
return absl::InvalidArgumentError("Contract not exist."); | ||
} | ||
|
||
nlohmann::json contracts_definition = nlohmann::json::parse(contract_fstream); | ||
|
||
const auto all_contracts = contracts_definition["contracts"]; | ||
|
||
const std::string contract_code = all_contracts[contract_name]["bin"]; | ||
if (contract_code.empty()) { | ||
LOG(ERROR) << "could not find contract definition. file:" << contract_path | ||
<< " name:" << contract_name; | ||
return absl::InvalidArgumentError("Contract not exist."); | ||
} | ||
|
||
const auto func_hashes = all_contracts[contract_name]["hashes"]; | ||
|
||
DeployInfo deploy_info; | ||
deploy_info.set_contract_bin(contract_code); | ||
deploy_info.set_contract_name(contract_name); | ||
|
||
for (auto& func : func_hashes.items()) { | ||
FuncInfo* new_func = deploy_info.add_func_info(); | ||
new_func->set_func_name(func.key()); | ||
new_func->set_hash(func.value()); | ||
} | ||
|
||
for (const std::string& param : init_params) { | ||
deploy_info.add_init_param(param); | ||
} | ||
|
||
Request request; | ||
Response response; | ||
request.set_caller_address(caller_address); | ||
*request.mutable_deploy_info() = deploy_info; | ||
|
||
request.set_cmd(Request::DEPLOY); | ||
LOG(ERROR) << "send request:" << request.DebugString(); | ||
int ret = SendRequest(request, &response); | ||
if (ret != 0 || response.ret() != 0) { | ||
return absl::InternalError("Deploy contract fail."); | ||
} | ||
return response.contract(); | ||
} | ||
|
||
absl::StatusOr<std::string> ContractClient::ExecuteContract( | ||
const std::string& caller_address, const std::string& contract_address, | ||
const std::string& func_name, const std::vector<std::string>& func_params) { | ||
Request request; | ||
Response response; | ||
request.set_caller_address(caller_address); | ||
request.set_contract_address(contract_address); | ||
|
||
request.mutable_func_params()->set_func_name(func_name); | ||
for (const std::string& param : func_params) { | ||
request.mutable_func_params()->add_param(param); | ||
} | ||
|
||
request.set_cmd(Request::EXECUTE); | ||
LOG(ERROR) << "send request:" << request.DebugString(); | ||
int ret = SendRequest(request, &response); | ||
if (ret != 0 || response.ret() != 0) { | ||
return absl::InternalError("Deploy contract fail."); | ||
} | ||
return response.res(); | ||
} | ||
|
||
} // namespace contract | ||
} // namespace resdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) 2019-2022 ExpoLab, UC Davis | ||
* | ||
* Permission is hereby granted, free of charge, to any person | ||
* obtaining a copy of this software and associated documentation | ||
* files (the "Software"), to deal in the Software without | ||
* restriction, including without limitation the rights to use, | ||
* copy, modify, merge, publish, distribute, sublicense, and/or | ||
* sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "application/contract/proto/account.pb.h" | ||
#include "application/contract/proto/contract.pb.h" | ||
#include "client/resdb_user_client.h" | ||
|
||
namespace resdb { | ||
namespace contract { | ||
|
||
// ContractClient to send data to the contract server. | ||
class ContractClient : public ResDBUserClient { | ||
public: | ||
ContractClient(const ResDBConfig& config); | ||
|
||
absl::StatusOr<Account> CreateAccount(); | ||
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); | ||
|
||
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); | ||
}; | ||
|
||
} // namespace contract | ||
} // namespace resdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
cc_library( | ||
name = "utils", | ||
hdrs = ["utils.h"], | ||
deps = [ | ||
"//third_party:evm_lib", | ||
], | ||
) | ||
|
||
cc_library( | ||
name = "contract_manager", | ||
srcs = ["contract_manager.cpp"], | ||
hdrs = ["contract_manager.h"], | ||
deps = [ | ||
":address_manager", | ||
":utils", | ||
"//application/contract/proto:func_params_cc_proto", | ||
"//common:comm", | ||
], | ||
) | ||
|
||
cc_test( | ||
name = "contract_manager_test", | ||
srcs = ["contract_manager_test.cpp"], | ||
data = [ | ||
"//application/contract/manager/test_data:contract.json", | ||
], | ||
deps = [ | ||
":contract_manager", | ||
"//common/test:test_main", | ||
], | ||
) | ||
|
||
cc_library( | ||
name = "address_manager", | ||
srcs = ["address_manager.cpp"], | ||
hdrs = ["address_manager.h"], | ||
deps = [ | ||
":utils", | ||
"//common:comm", | ||
], | ||
) | ||
|
||
cc_test( | ||
name = "address_manager_test", | ||
srcs = ["address_manager_test.cpp"], | ||
deps = [ | ||
":address_manager", | ||
"//common/test:test_main", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2019-2022 ExpoLab, UC Davis | ||
* | ||
* Permission is hereby granted, free of charge, to any person | ||
* obtaining a copy of this software and associated documentation | ||
* files (the "Software"), to deal in the Software without | ||
* restriction, including without limitation the rights to use, | ||
* copy, modify, merge, publish, distribute, sublicense, and/or | ||
* sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
|
||
#include "application/contract/manager/address_manager.h" | ||
|
||
#include <glog/logging.h> | ||
|
||
#include "eEVM/util.h" | ||
|
||
namespace resdb { | ||
namespace contract { | ||
|
||
Address AddressManager::CreateRandomAddress() { | ||
std::vector<uint8_t> raw(20); | ||
std::generate(raw.begin(), raw.end(), []() { return rand(); }); | ||
Address address = eevm::from_big_endian(raw.data(), raw.size()); | ||
users_.insert(address); | ||
return address; | ||
} | ||
|
||
bool AddressManager::Exist(const Address& address) { | ||
return users_.find(address) != users_.end(); | ||
} | ||
|
||
Address AddressManager::CreateContractAddress(const Address& owner) { | ||
return eevm::generate_address(owner, 0u); | ||
} | ||
|
||
std::string AddressManager::AddressToHex(const Address& address) { | ||
return eevm::to_hex_string(address); | ||
} | ||
|
||
Address AddressManager::HexToAddress(const std::string& address) { | ||
return eevm::to_uint256(address); | ||
} | ||
|
||
} // namespace contract | ||
} // namespace resdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) 2019-2022 ExpoLab, UC Davis | ||
* | ||
* Permission is hereby granted, free of charge, to any person | ||
* obtaining a copy of this software and associated documentation | ||
* files (the "Software"), to deal in the Software without | ||
* restriction, including without limitation the rights to use, | ||
* copy, modify, merge, publish, distribute, sublicense, and/or | ||
* sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <set> | ||
|
||
#include "application/contract/manager/utils.h" | ||
|
||
namespace resdb { | ||
namespace contract { | ||
|
||
class AddressManager { | ||
public: | ||
AddressManager() {} | ||
|
||
// Create an address holding a 20 byte value | ||
Address CreateRandomAddress(); | ||
bool Exist(const Address& address); | ||
|
||
static Address CreateContractAddress(const Address& owner); | ||
|
||
static std::string AddressToHex(const Address& address); | ||
static Address HexToAddress(const std::string& address); | ||
|
||
private: | ||
std::set<Address> users_; | ||
}; | ||
|
||
} // namespace contract | ||
} // namespace resdb |
Oops, something went wrong.