Skip to content

Commit

Permalink
Added external address addition function
Browse files Browse the repository at this point in the history
  • Loading branch information
apratimshukla6 committed Nov 15, 2024
1 parent 282fe2d commit 0541fe4
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 7 deletions.
11 changes: 11 additions & 0 deletions executor/contract/executor/contract_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 =
Expand Down
1 change: 1 addition & 0 deletions executor/contract/executor/contract_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
4 changes: 4 additions & 0 deletions executor/contract/manager/address_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion executor/contract/manager/address_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions interface/contract/contract_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions interface/contract/contract_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions proto/contract/rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ message Request {
CREATE_ACCOUNT = 1; // deploy contract
DEPLOY = 2; // deploy contract
EXECUTE = 3; // execute contract
ADD_ADDRESS = 4; // add address
};

CMD cmd = 1;
string caller_address = 2;
optional DeployInfo deploy_info = 3;
optional string contract_address = 4;
optional Params func_params = 5;
optional string external_address = 6;
}


Expand Down
32 changes: 26 additions & 6 deletions service/tools/contract/api_tools/contract_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand All @@ -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(","));
Expand All @@ -137,5 +155,7 @@ int main(int argc, char** argv) {

ExecuteContract(&client, caller_address, contract_address, func_name,
func_params);
} else {
ShowUsage();
}
}
}

0 comments on commit 0541fe4

Please sign in to comment.