This is an automated email from the ASF dual-hosted git repository.

apratim pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-resilientdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 0541fe43 Added external address addition function
0541fe43 is described below

commit 0541fe43bd1bbd69ecac5a1bc0d0cdd5ac04bc56
Author: apratimshukla6 <[email protected]>
AuthorDate: Thu Nov 14 19:38:46 2024 -0800

    Added external address addition function
---
 executor/contract/executor/contract_executor.cpp   | 11 ++++++++
 executor/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 ++
 .../tools/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 6db35ad7..ad1c6057 100644
--- a/executor/contract/executor/contract_executor.cpp
+++ b/executor/contract/executor/contract_executor.cpp
@@ -60,6 +60,11 @@ std::unique_ptr<std::string> 
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<Account> 
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<Contract> 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 c902e297..a5002598 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<Account> CreateAccount();
   absl::StatusOr<Contract> Deploy(const Request& request);
   absl::StatusOr<std::string> Execute(const Request& request);
+  absl::Status AddAddress(const Request& request);
 
  private:
   std::unique_ptr<ContractManager> contract_manager_;
diff --git a/executor/contract/manager/address_manager.cpp 
b/executor/contract/manager/address_manager.cpp
index 2a886dac..ca589f2b 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 4e6055aa..14ae798f 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 2d8b2e98..77dec83f 100644
--- a/interface/contract/contract_client.cpp
+++ b/interface/contract/contract_client.cpp
@@ -43,6 +43,19 @@ absl::StatusOr<Account> 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<Contract> 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 7b0c5aed..944e71e6 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<Account> CreateAccount();
+  absl::Status AddExternalAddress(const std::string& external_address);
   absl::StatusOr<Contract> 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 8939fdef..8df1dd40 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 0883db4e..0271b312 100644
--- a/service/tools/contract/api_tools/contract_tools.cpp
+++ b/service/tools/contract/api_tools/contract_tools.cpp
@@ -21,6 +21,7 @@
 
 #include <boost/algorithm/string.hpp>
 #include <vector>
+#include <unistd.h>  // 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(
-      "<cmd> -c <config> -m <caller address> -n <contract name> -p <contact "
-      "path> -a <params> \n");
+      "<cmd> -c <config> -m <caller address> -n <contract name> -p <contract "
+      "path> -a <params> -e <external address>\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("<cmd> -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<std::string> 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

Reply via email to