Skip to content

Commit

Permalink
Merge pull request #44 from resilientdb/utxo
Browse files Browse the repository at this point in the history
Utxo
  • Loading branch information
cjcchen authored Feb 28, 2023
2 parents 4ac20e3 + cb3c4a2 commit 7e01a57
Show file tree
Hide file tree
Showing 64 changed files with 2,883 additions and 62 deletions.
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
build --cxxopt='-std=c++17' --copt=-O3 --jobs=40
build --action_env=PYTHON_BIN_PATH="/usr/bin/python3.10"
build --action_env=PYTHON_LIB_PATH="/usr/include/python3.10"
#build --action_env=PYTHON_BIN_PATH="/usr/bin/python3.10"
#build --action_env=PYTHON_LIB_PATH="/usr/include/python3.10"

1 change: 1 addition & 0 deletions application/utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cc_library(
hdrs = ["server_factory.h"],
deps = [
"//config:resdb_config_utils",
"//execution:custom_query",
"//ordering/pbft:consensus_service_pbft",
"//server:resdb_server",
],
Expand Down
43 changes: 43 additions & 0 deletions application/utils/server_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#pragma once

#include "config/resdb_config_utils.h"
#include "execution/custom_query.h"
#include "execution/transaction_executor_impl.h"
#include "ordering/pbft/consensus_service_pbft.h"
#include "server/resdb_server.h"
Expand All @@ -44,6 +45,13 @@ class ServerFactory {
char* config_file, char* private_key_file, char* cert_file,
std::unique_ptr<TransactionExecutorImpl> executor, char* logging_dir,
std::function<void(ResDBConfig* config)> config_handler);

template <typename ConsensusProtocol = ConsensusServicePBFT>
std::unique_ptr<ResDBServer> CustomCreateResDBServer(
char* config_file, char* private_key_file, char* cert_file,
std::unique_ptr<TransactionExecutorImpl> executor,
std::unique_ptr<CustomQuery> query_executor,
std::function<void(ResDBConfig* config)> config_handler);
};

std::unique_ptr<ResDBServer> GenerateResDBServer(
Expand All @@ -59,6 +67,13 @@ std::unique_ptr<ResDBServer> CustomGenerateResDBServer(
char* logging_dir = nullptr,
std::function<void(ResDBConfig* config)> config_handler = nullptr);

template <typename ConsensusProtocol>
std::unique_ptr<ResDBServer> CustomGenerateResDBServer(
char* config_file, char* private_key_file, char* cert_file,
std::unique_ptr<TransactionExecutorImpl> executor,
std::unique_ptr<CustomQuery> query_executor,
std::function<void(ResDBConfig* config)> config_handler = nullptr);

// ===================================================================
template <typename ConsensusProtocol>
std::unique_ptr<ResDBServer> ServerFactory::CustomCreateResDBServer(
Expand All @@ -76,6 +91,23 @@ std::unique_ptr<ResDBServer> ServerFactory::CustomCreateResDBServer(
std::make_unique<ConsensusProtocol>(*config, std::move(executor)));
}

template <typename ConsensusProtocol>
std::unique_ptr<ResDBServer> ServerFactory::CustomCreateResDBServer(
char* config_file, char* private_key_file, char* cert_file,
std::unique_ptr<TransactionExecutorImpl> executor,
std::unique_ptr<CustomQuery> query_executor,
std::function<void(ResDBConfig* config)> config_handler) {
std::unique_ptr<ResDBConfig> config =
GenerateResDBConfig(config_file, private_key_file, cert_file);

if (config_handler) {
config_handler(config.get());
}
return std::make_unique<ResDBServer>(
*config, std::make_unique<ConsensusProtocol>(*config, std::move(executor),
std::move(query_executor)));
}

template <typename ConsensusProtocol>
std::unique_ptr<ResDBServer> CustomGenerateResDBServer(
char* config_file, char* private_key_file, char* cert_file,
Expand All @@ -86,4 +118,15 @@ std::unique_ptr<ResDBServer> CustomGenerateResDBServer(
logging_dir, config_handler);
}

template <typename ConsensusProtocol>
std::unique_ptr<ResDBServer> CustomGenerateResDBServer(
char* config_file, char* private_key_file, char* cert_file,
std::unique_ptr<TransactionExecutorImpl> executor,
std::unique_ptr<CustomQuery> query_executor,
std::function<void(ResDBConfig* config)> config_handler) {
return ServerFactory().CustomCreateResDBServer<ConsensusProtocol>(
config_file, private_key_file, cert_file, std::move(executor),
std::move(query_executor), config_handler);
}

} // namespace resdb
12 changes: 12 additions & 0 deletions application/utxo/client/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "utxo_client",
srcs = ["utxo_client.cpp"],
hdrs = ["utxo_client.h"],
deps = [
"//application/utxo/proto:rpc_cc_proto",
"//application/utxo/proto:utxo_cc_proto",
"//client:resdb_user_client",
],
)
103 changes: 103 additions & 0 deletions application/utxo/client/utxo_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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/utxo/client/utxo_client.h"

#include <glog/logging.h>

#include "application/utxo/proto/rpc.pb.h"

namespace resdb {
namespace utxo {

UTXOClient::UTXOClient(const ResDBConfig& config) : ResDBUserClient(config) {}

int UTXOClient::Transfer(const UTXO& utxo) {
UTXORequest request;
UTXOResponse response;
*request.mutable_utxo() = utxo;

int ret = SendRequest(request, &response);
if (ret != 0 || response.ret() < 0) {
return -1;
}
return response.ret();
}

std::vector<UTXO> UTXOClient::GetList(int64_t end_id, int num) {
UTXOQuery query;
query.set_query_transaction(true);
query.set_end_id(end_id);
query.set_num(num);

CustomQueryResponse response;

int ret = SendRequest(query, Request::TYPE_CUSTOM_QUERY);
if (ret) {
LOG(ERROR) << "send request fail";
return std::vector<UTXO>();
}

ret = RecvRawMessage(&response);
if (ret) {
LOG(ERROR) << "recv response fail";
return std::vector<UTXO>();
}

UTXOQueryResponse utxo_response;
utxo_response.ParseFromString(response.resp_str());
std::vector<UTXO> utxo_list;
for (const auto utxo : utxo_response.utxos()) {
utxo_list.push_back(utxo);
}
return utxo_list;
}

int64_t UTXOClient::GetWallet(const std::string& address) {
UTXOQuery query;
query.set_query_transaction(false);
query.set_address(address);

CustomQueryResponse response;

int ret = SendRequest(query, Request::TYPE_CUSTOM_QUERY);
if (ret) {
LOG(ERROR) << "send request fail";
return -1;
}

ret = RecvRawMessage(&response);
if (ret) {
LOG(ERROR) << "recv response fail";
return -1;
}

UTXOQueryResponse utxo_response;
utxo_response.ParseFromString(response.resp_str());
return utxo_response.value();
}

} // namespace utxo
} // namespace resdb
46 changes: 46 additions & 0 deletions application/utxo/client/utxo_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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/utxo/proto/utxo.pb.h"
#include "client/resdb_user_client.h"

namespace resdb {
namespace utxo {

class UTXOClient : public ResDBUserClient {
public:
UTXOClient(const ResDBConfig& config);

int Transfer(const UTXO& utxo);

std::vector<UTXO> GetList(int64_t end_id, int num);

int64_t GetWallet(const std::string& address);
};

} // namespace utxo
} // namespace resdb
36 changes: 36 additions & 0 deletions application/utxo/proto/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package(default_visibility = ["//visibility:public"])

load("@rules_cc//cc:defs.bzl", "cc_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

proto_library(
name = "utxo_proto",
srcs = ["utxo.proto"],
)

cc_proto_library(
name = "utxo_cc_proto",
deps = [":utxo_proto"],
)

proto_library(
name = "config_proto",
srcs = ["config.proto"],
deps = ["utxo_proto"],
)

cc_proto_library(
name = "config_cc_proto",
deps = [":config_proto"],
)

proto_library(
name = "rpc_proto",
srcs = ["rpc.proto"],
deps = ["utxo_proto"],
)

cc_proto_library(
name = "rpc_cc_proto",
deps = [":rpc_proto"],
)
15 changes: 15 additions & 0 deletions application/utxo/proto/config.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

package resdb.utxo;

import "application/utxo/proto/utxo.proto";

message GenesisUTXO {
repeated UTXO transactions = 1;
}

message Config {
GenesisUTXO genesis_transactions = 1;
}


25 changes: 25 additions & 0 deletions application/utxo/proto/rpc.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
syntax = "proto3";

package resdb.utxo;

import "application/utxo/proto/utxo.proto";

message UTXORequest {
UTXO utxo = 1;
}

message UTXOResponse {
int64 ret = 1;
}

message UTXOQuery {
bool query_transaction = 1;
int64 end_id = 2;
int32 num = 3;
string address = 4;
}

message UTXOQueryResponse {
repeated UTXO utxos = 1;
int64 value = 2;
}
23 changes: 23 additions & 0 deletions application/utxo/proto/utxo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
syntax = "proto3";

package resdb.utxo;

message UTXOIn {
int64 prev_id = 1;
int32 out_idx = 2;
}

message UTXOOut{
string address = 1;
int64 value = 2;
bool spent = 3;
string pub_key = 4;
}

message UTXO {
repeated UTXOIn in = 1;
repeated UTXOOut out = 2;
string sig = 3; // signed by the owner.
string address = 4;
int64 transaction_id = 5;
}
12 changes: 12 additions & 0 deletions application/utxo/server/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package(default_visibility = ["//visibility:public"])

cc_binary(
name = "utxo_server",
srcs = ["utxo_server.cpp"],
deps = [
"//application/utils:server_factory",
"//application/utxo/service:utxo_executor",
"//config:resdb_config_utils",
"//ordering/pbft:consensus_service_pbft",
],
)
28 changes: 28 additions & 0 deletions application/utxo/server/config/server_config.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
region : {
replica_info : {
id:1,
ip:"127.0.0.1",
port: 10001,
},
replica_info : {
id:2,
ip:"127.0.0.1",
port: 10002,
},
replica_info : {
id:3,
ip:"127.0.0.1",
port: 10003,
},
replica_info : {
id:4,
ip:"127.0.0.1",
port: 10004,
},
region_id: 1,
},
self_region_id:1,
}


Loading

0 comments on commit 7e01a57

Please sign in to comment.