Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion executor/contract/manager/address_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bool AddressManager::Exist(const Address& address) {
}

Address AddressManager::CreateContractAddress(const Address& owner) {
return eevm::generate_address(owner, 0u);
return eevm::generate_address(owner, rand());
}

std::string AddressManager::AddressToHex(const Address& address) {
Expand Down
26 changes: 13 additions & 13 deletions executor/contract/manager/global_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/


#include "executor/contract/manager/global_state.h"

#include <glog/logging.h>
Expand All @@ -30,16 +29,15 @@ using eevm::Address;
using eevm::Code;
using eevm::SimpleAccount;


uint256_t AccountToAddress(const eevm::Address& account) {
std::vector<uint8_t> code;
code.resize(64);
std::fill(code.begin(), code.end(), 0);
eevm::to_big_endian(account, code.data());

uint8_t h[32];
eevm::keccak_256(code.data(), static_cast<unsigned int>(64), h);
return eevm::from_big_endian(h, sizeof(h));
std::vector<uint8_t> code;
code.resize(64);
std::fill(code.begin(), code.end(), 0);
eevm::to_big_endian(account, code.data());

uint8_t h[32];
eevm::keccak_256(code.data(), static_cast<unsigned int>(64), h);
return eevm::from_big_endian(h, sizeof(h));
}

GlobalState::GlobalState(resdb::Storage* storage) : storage_(storage) {}
Expand All @@ -59,7 +57,7 @@ AccountState GlobalState::get(const Address& addr) {

AccountState GlobalState::create(const Address& addr, const uint256_t& balance,
const Code& code) {
Insert({SimpleAccount(addr, balance, code), GlobalView(storage_)});
Insert({SimpleAccount(addr, balance, code), GlobalView(addr, storage_)});

return get(addr);
}
Expand All @@ -79,8 +77,10 @@ std::string GlobalState::GetBalance(const eevm::Address& account) {
return storage_->GetValue(eevm::to_hex_string(AccountToAddress(account)));
}

int GlobalState::SetBalance(const eevm::Address& account, const uint256_t& balance) {
return storage_->SetValue(eevm::to_hex_string(AccountToAddress(account)), eevm::to_hex_string(balance));
int GlobalState::SetBalance(const eevm::Address& account,
const uint256_t& balance) {
return storage_->SetValue(eevm::to_hex_string(AccountToAddress(account)),
eevm::to_hex_string(balance));
}

} // namespace contract
Expand Down
11 changes: 7 additions & 4 deletions executor/contract/manager/global_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/


#include "executor/contract/manager/global_view.h"

#include <glog/logging.h>
Expand All @@ -27,14 +26,18 @@
namespace resdb {
namespace contract {

GlobalView::GlobalView(resdb::Storage* storage) : storage_(storage) {}
GlobalView::GlobalView(const Address& addr, resdb::Storage* storage)
: address_(addr), storage_(storage) {}

void GlobalView::store(const uint256_t& key, const uint256_t& value) {
storage_->SetValue(eevm::to_hex_string(key), eevm::to_hex_string(value));
storage_->SetValue(
eevm::to_hex_string(address_) + ":" + eevm::to_hex_string(key),
eevm::to_hex_string(value));
}

uint256_t GlobalView::load(const uint256_t& key) {
return eevm::to_uint256(storage_->GetValue(eevm::to_hex_string(key)));
return eevm::to_uint256(storage_->GetValue(eevm::to_hex_string(address_) +
":" + eevm::to_hex_string(key)));
}

bool GlobalView::remove(const uint256_t& key) { return true; }
Expand Down
6 changes: 4 additions & 2 deletions executor/contract/manager/global_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@

#include <map>

#include "eEVM/storage.h"
#include "chain/storage/storage.h"
#include "eEVM/storage.h"
#include "executor/contract/manager/utils.h"

namespace resdb {
namespace contract {

class GlobalView : public eevm::Storage {
public:
GlobalView(resdb::Storage* storage);
GlobalView(const Address& address, resdb::Storage* storage);
virtual ~GlobalView() = default;

void store(const uint256_t& key, const uint256_t& value) override;
uint256_t load(const uint256_t& key) override;
bool remove(const uint256_t& key) override;

private:
const Address address_;
resdb::Storage* storage_;
};

Expand Down
6 changes: 3 additions & 3 deletions repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def nexres_repositories():
maybe(
http_archive,
name = "eEVM",
strip_prefix = "eEVM-118a9355d023748318a318bc07fc79063f015a94",
sha256 = "e86568aec425405fd8a48bbe487edeae4c0641be23b19411288e3b736018e1b6",
url = "https://github.com/microsoft/eEVM/archive/118a9355d023748318a318bc07fc79063f015a94.tar.gz",
strip_prefix = "eEVM-05efed8658b4e10a21253df8408b1f9bdb6f1445",
sha256 = "95652edde062b9be7a66cfc1ba32f3f8f08855539aac007492727dc2d9f36f7d",
url = "https://github.com/microsoft/eEVM/archive/05efed8658b4e10a21253df8408b1f9bdb6f1445.tar.gz",
build_file = "@com_resdb_nexres//third_party:eEVM.BUILD",
)
maybe(
Expand Down
54 changes: 26 additions & 28 deletions service/tools/contract/api_tools/contract_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
* under the License.
*/

#include <getopt.h>
#include <glog/logging.h>
#include <unistd.h> // For getopt

#include <getopt.h>
#include <nlohmann/json.hpp>
#include <boost/algorithm/string.hpp>
#include <vector>
#include <fstream>
#include <nlohmann/json.hpp>
#include <string>
#include <vector>

#include "interface/contract/contract_client.h"
#include "platform/config/resdb_config_utils.h"
Expand All @@ -32,8 +34,6 @@ using resdb::GenerateResDBConfig;
using resdb::ResDBConfig;
using resdb::contract::ContractClient;



void ShowUsage() {
printf(
"<cmd> -c <config> -m <caller address> -n <contract name> -p <contact "
Expand All @@ -42,16 +42,13 @@ void ShowUsage() {
}

static struct option long_options[] = {
{ "cmd", required_argument, NULL, 'm'},
{ "config_file", required_argument, NULL, 'f'},
{ 0, 0, 0, 0 }
};

{"cmd", required_argument, NULL, 'm'},
{"config_file", required_argument, NULL, 'f'},
{0, 0, 0, 0}};

void CreateAccount(ContractClient* client) {
auto account = client->CreateAccount();
if (!account.ok()) {
printf("create account fail\n");
return;
}
LOG(ERROR) << "create account:\n" << account->DebugString();
Expand All @@ -63,6 +60,7 @@ void DeployContract(ContractClient* client, const std::string& caller_address,
const std::vector<std::string>& init_params) {
auto contract = client->DeployContract(caller_address, contract_name,
contract_path, init_params);

if (!contract.ok()) {
printf("deploy contract fail\n");
return;
Expand All @@ -84,40 +82,38 @@ void ExecuteContract(ContractClient* client, const std::string& caller_address,
}

nlohmann::json ReadJSConfig(const std::string& config_path) {

std::ifstream contract_fstream(config_path);
if (!contract_fstream) {
throw std::runtime_error( "Unable to open config file "+config_path);
throw std::runtime_error("Unable to open config file " + config_path);
}

return nlohmann::json::parse(contract_fstream);
}

std::string GetValue(const nlohmann::json& js, std::string key){
if(!js.contains(key)){
printf("need %s\n", key.c_str());
exit(0);
}
return js[key];
std::string GetValue(const nlohmann::json& js, std::string key) {
if (!js.contains(key)) {
printf("need %s\n", key.c_str());
exit(0);
}
return js[key];
}


int main(int argc, char** argv) {
if (argc < 2) {
printf("<cmd> -c [config]\n");
return 0;
}


std::string config_file;

std::string cmd;
std::string caller_address, contract_name, contract_path, params,
contract_address, func_name;
int c;
int option_index;
std::string client_config_file;
while ((c = getopt_long(argc, argv, "c:h", long_options, &option_index)) != -1) {
while ((c = getopt_long(argc, argv, "f:c:h", long_options, &option_index)) !=
-1) {
switch (c) {
case -1:
break;
Expand All @@ -136,7 +132,8 @@ int main(int argc, char** argv) {
nlohmann::json js = ReadJSConfig(config_file);
cmd = GetValue(js, "command");

printf("client config path = %s config path = %s cmd = %s\n", client_config_file.c_str(), config_file.c_str(), cmd.c_str());
printf("client config path = %s config path = %s cmd = %s\n",
client_config_file.c_str(), config_file.c_str(), cmd.c_str());
ResDBConfig config = GenerateResDBConfig(client_config_file);
config.SetClientTimeoutMs(100000);

Expand All @@ -145,21 +142,23 @@ int main(int argc, char** argv) {
if (cmd == "create_account") {
CreateAccount(&client);
} else if (cmd == "deploy") {

contract_path = GetValue(js, "contract_path");
contract_name = GetValue(js, "contract_name");
contract_address = GetValue(js, "contract_address");
params = GetValue(js, "init_params");

printf("contract path %s cmd %s contract name %s caller_address %s init params %s\n", contract_path.c_str(), cmd.c_str(), contract_name.c_str(), contract_address.c_str(), params.c_str());
printf(
"contract path %s cmd %s contract name %s caller_address %s init "
"params %s\n",
contract_path.c_str(), cmd.c_str(), contract_name.c_str(),
contract_address.c_str(), params.c_str());

std::vector<std::string> init_params;
boost::split(init_params, params, boost::is_any_of(","));

DeployContract(&client, contract_address, contract_name, contract_path,
init_params);
} else if (cmd == "execute") {

caller_address = GetValue(js, "caller_address");
contract_address = GetValue(js, "contract_address");
func_name = GetValue(js, "func_name");
Expand All @@ -177,4 +176,3 @@ int main(int argc, char** argv) {
func_params);
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<<<<<<< HEAD
=======
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
Expand All @@ -18,7 +16,6 @@
# specific language governing permissions and limitations
# under the License.
#
>>>>>>> master
killall -9 contract_service

SERVER_PATH=./bazel-bin/service/contract/contract_service
Expand Down