From 0541fe43bd1bbd69ecac5a1bc0d0cdd5ac04bc56 Mon Sep 17 00:00:00 2001 From: apratimshukla6 Date: Thu, 14 Nov 2024 19:38:46 -0800 Subject: [PATCH] Added external address addition function --- .../contract/executor/contract_executor.cpp | 11 +++++++ .../contract/executor/contract_executor.h | 1 + executor/contract/manager/address_manager.cpp | 4 +++ executor/contract/manager/address_manager.h | 2 +- interface/contract/contract_client.cpp | 13 ++++++++ interface/contract/contract_client.h | 1 + proto/contract/rpc.proto | 2 ++ .../contract/api_tools/contract_tools.cpp | 32 +++++++++++++++---- 8 files changed, 59 insertions(+), 7 deletions(-) diff --git a/executor/contract/executor/contract_executor.cpp b/executor/contract/executor/contract_executor.cpp index 6db35ad71..ad1c60571 100644 --- a/executor/contract/executor/contract_executor.cpp +++ b/executor/contract/executor/contract_executor.cpp @@ -60,6 +60,11 @@ std::unique_ptr ContractTransactionManager::ExecuteData( } else { ret = -1; } + } else if (request.cmd() == Request::ADD_ADDRESS) { // New command handling + absl::Status status = AddAddress(request); + if (!status.ok()) { + ret = -1; + } } response.set_ret(ret); @@ -80,6 +85,12 @@ absl::StatusOr ContractTransactionManager::CreateAccount() { return account; } +absl::Status ContractTransactionManager::AddAddress(const Request& request) { + Address address = AddressManager::HexToAddress(request.external_address()); + address_manager_->AddExternalAddress(address); + return absl::OkStatus(); +} + absl::StatusOr ContractTransactionManager::Deploy( const Request& request) { Address caller_address = diff --git a/executor/contract/executor/contract_executor.h b/executor/contract/executor/contract_executor.h index c902e2977..a5002598f 100644 --- a/executor/contract/executor/contract_executor.h +++ b/executor/contract/executor/contract_executor.h @@ -40,6 +40,7 @@ class ContractTransactionManager : public TransactionManager { absl::StatusOr CreateAccount(); absl::StatusOr Deploy(const Request& request); absl::StatusOr Execute(const Request& request); + absl::Status AddAddress(const Request& request); private: std::unique_ptr contract_manager_; diff --git a/executor/contract/manager/address_manager.cpp b/executor/contract/manager/address_manager.cpp index 2a886dac5..ca589f2b5 100644 --- a/executor/contract/manager/address_manager.cpp +++ b/executor/contract/manager/address_manager.cpp @@ -34,6 +34,10 @@ Address AddressManager::CreateRandomAddress() { return address; } +void AddressManager::AddExternalAddress(const Address& address) { + users_.insert(address); // New method implementation +} + bool AddressManager::Exist(const Address& address) { return users_.find(address) != users_.end(); } diff --git a/executor/contract/manager/address_manager.h b/executor/contract/manager/address_manager.h index 4e6055aad..14ae798fa 100644 --- a/executor/contract/manager/address_manager.h +++ b/executor/contract/manager/address_manager.h @@ -33,7 +33,7 @@ class AddressManager { // Create an address holding a 20 byte value Address CreateRandomAddress(); bool Exist(const Address& address); - + void AddExternalAddress(const Address& address); static Address CreateContractAddress(const Address& owner); static std::string AddressToHex(const Address& address); diff --git a/interface/contract/contract_client.cpp b/interface/contract/contract_client.cpp index 2d8b2e98e..77dec83f7 100644 --- a/interface/contract/contract_client.cpp +++ b/interface/contract/contract_client.cpp @@ -43,6 +43,19 @@ absl::StatusOr ContractClient::CreateAccount() { return response.account(); } +absl::Status ContractClient::AddExternalAddress( + const std::string& external_address) { + Request request; + Response response; + request.set_cmd(Request::ADD_ADDRESS); + request.set_external_address(external_address); + int ret = SendRequest(request, &response); + if (ret != 0 || response.ret() != 0) { + return absl::InternalError("Add address failed."); + } + return absl::OkStatus(); +} + absl::StatusOr ContractClient::DeployContract( const std::string& caller_address, const std::string& contract_name, const std::string& contract_path, diff --git a/interface/contract/contract_client.h b/interface/contract/contract_client.h index 7b0c5aed3..944e71e6a 100644 --- a/interface/contract/contract_client.h +++ b/interface/contract/contract_client.h @@ -32,6 +32,7 @@ class ContractClient : public TransactionConstructor { ContractClient(const ResDBConfig& config); absl::StatusOr CreateAccount(); + absl::Status AddExternalAddress(const std::string& external_address); absl::StatusOr DeployContract( const std::string& caller_address, const std::string& contract_name, const std::string& contract_path, diff --git a/proto/contract/rpc.proto b/proto/contract/rpc.proto index 8939fdef6..8df1dd407 100644 --- a/proto/contract/rpc.proto +++ b/proto/contract/rpc.proto @@ -31,6 +31,7 @@ message Request { CREATE_ACCOUNT = 1; // deploy contract DEPLOY = 2; // deploy contract EXECUTE = 3; // execute contract + ADD_ADDRESS = 4; // add address }; CMD cmd = 1; @@ -38,6 +39,7 @@ message Request { optional DeployInfo deploy_info = 3; optional string contract_address = 4; optional Params func_params = 5; + optional string external_address = 6; } diff --git a/service/tools/contract/api_tools/contract_tools.cpp b/service/tools/contract/api_tools/contract_tools.cpp index 0883db4ed..0271b312b 100644 --- a/service/tools/contract/api_tools/contract_tools.cpp +++ b/service/tools/contract/api_tools/contract_tools.cpp @@ -21,6 +21,7 @@ #include #include +#include // For getopt #include "interface/contract/contract_client.h" #include "platform/config/resdb_config_utils.h" @@ -31,11 +32,20 @@ using resdb::contract::ContractClient; void ShowUsage() { printf( - " -c -m -n -p -a \n"); + " -c -m -n -p -a -e \n"); exit(0); } +void AddAddress(ContractClient* client, const std::string& external_address) { + absl::Status status = client->AddExternalAddress(external_address); + if (!status.ok()) { + printf("Add address failed\n"); + } else { + printf("Address added successfully\n"); + } +} + void CreateAccount(ContractClient* client) { auto account = client->CreateAccount(); if (!account.ok()) { @@ -73,16 +83,16 @@ void ExecuteContract(ContractClient* client, const std::string& caller_address, int main(int argc, char** argv) { if (argc < 3) { - printf(" -c [config]\n"); + ShowUsage(); return 0; } std::string cmd = argv[1]; std::string caller_address, contract_name, contract_path, params, - contract_address, func_name; + contract_address, func_name, external_address; // Added external_address int c; std::string client_config_file; - while ((c = getopt(argc, argv, "m:c:a:n:p:h:f:s:")) != -1) { + while ((c = getopt(argc, argv, "m:c:a:n:p:h:f:s:e:")) != -1) { // Added 'e:' switch (c) { case 'm': caller_address = optarg; @@ -105,9 +115,15 @@ int main(int argc, char** argv) { case 's': contract_address = optarg; break; + case 'e': + external_address = optarg; // Handle the 'e' option + break; case 'h': ShowUsage(); break; + default: + ShowUsage(); + break; } } @@ -120,6 +136,8 @@ int main(int argc, char** argv) { if (cmd == "create") { CreateAccount(&client); + } else if (cmd == "add_address") { + AddAddress(&client, external_address); } else if (cmd == "deploy") { std::vector init_params; boost::split(init_params, params, boost::is_any_of(",")); @@ -137,5 +155,7 @@ int main(int argc, char** argv) { ExecuteContract(&client, caller_address, contract_address, func_name, func_params); + } else { + ShowUsage(); } -} +} \ No newline at end of file