Skip to content

Commit

Permalink
common response methods added
Browse files Browse the repository at this point in the history
  • Loading branch information
adastreamer committed Jun 17, 2019
1 parent f222330 commit 3093c51
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,6 @@ add_subdirectory(lite-client/adnl)

add_subdirectory(lite-client/crypto)

add_executable (test-lite-client-with-webserver test-webserver/test-lite-client.cpp)
add_executable (test-lite-client-with-webserver src/lite-client.cpp)
target_link_libraries(test-lite-client-with-webserver tdutils tdactor adnllite tl_api
ton_crypto ton_block terminal)
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ void TestNode::get_account_state_web(std::string address, std::shared_ptr<HttpSe
ton::WorkchainId workchain = ton::masterchainId; // change to basechain later
ton::StdSmcAddress addr;
if (!TestNode::parse_account_addr(address, workchain, addr)){
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"{\"error\":\"parse_account_addr fail\"}");
web_error_response(response, "parse_account_addr fail");
return;
}

if (!mc_last_id_.is_valid()) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"{\"error\":\"must obtain last block information before making other queries\"}");
web_error_response(response, "must obtain last block information before making other queries");
return;
}
if (!(ready_ && !client_.empty())) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"{\"error\":\"server connection not ready\"}");
web_error_response(response, "server connection not ready");
return;
}

Expand All @@ -27,14 +24,12 @@ void TestNode::get_account_state_web(std::string address, std::shared_ptr<HttpSe
envelope_send_web(
std::move(b), [Self = actor_id(this), workchain, addr, response](td::Result<td::BufferSlice> R) -> void {
if (R.is_error()) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"{\"error\":\"Unknown Error\"}");
web_error_response(response, "Unknown Error");
return;
}
auto F = ton::fetch_tl_object<ton::ton_api::liteServer_accountState>(R.move_as_ok(), true);
if (F.is_error()) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"{\"error\":\"cannot parse answer to liteServer.getAccountState\"}");
web_error_response(response, "cannot parse answer to liteServer.getAccountState");
} else {
auto f = F.move_as_ok();
td::actor::send_closure_later(Self, &TestNode::got_account_state_web, ton::create_block_id(f->id_),
Expand All @@ -48,19 +43,18 @@ void TestNode::got_account_state_web(ton::BlockIdExt blk, ton::BlockIdExt shard_
td::BufferSlice proof, td::BufferSlice state, ton::WorkchainId workchain,
ton::StdSmcAddress addr, std::shared_ptr<HttpServer::Response> response) {
if (state.empty()) {
response -> write("{\"error\":\"account state is empty\"}");
web_error_response(response, "account state is empty");
} else {
auto R = vm::std_boc_deserialize(state.clone());
if (R.is_error()) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"{\"error\":\"cannot deserialize account state\"}");
web_error_response(response, "cannot deserialize account state");
return;
}
auto root = R.move_as_ok();
std::ostringstream outp;
block::gen::t_Account.print_ref(outp, root);
vm::load_cell_slice(root).print_rec(outp);

response -> write("{\"result\":\"" + outp.str() + "\"}");
web_success_response(response, outp.str());
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
void TestNode::get_block_web(std::string blkid_str, std::shared_ptr<HttpServer::Response> response, bool dump) {
ton::BlockIdExt blkid;
if(!TestNode::parse_block_id_ext(blkid_str, blkid, true))
{ response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"{\"error\":\"cannot parse block_id\"}");
{
web_error_response(response, "cannot parse block_id");
}

auto b = ton::serialize_tl_object(
ton::create_tl_object<ton::ton_api::liteServer_getBlock>(ton::create_tl_block_id(blkid)), true);
envelope_send_query(
std::move(b), [ Self = actor_id(this), blkid, dump, response](td::Result<td::BufferSlice> res)->void {
if (res.is_error()) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"{\"error\":\"cannot obtain block " + blkid.to_str() + \
" from server : " + res.move_as_error().to_string()+"\"}");
web_error_response(response, "cannot obtain block " + blkid.to_str() + \
" from server : " + res.move_as_error().to_string());
return;
} else {
auto F = ton::fetch_tl_object<ton::ton_api::liteServer_blockData>(res.move_as_ok(), true);
if (F.is_error()) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error, \
"{\"error\":\"cannot parse answer to liteServer.getBlock : " +\
res.move_as_error().to_string()+"\"}");
web_error_response(response, "cannot parse answer to liteServer.getBlock : " +\
res.move_as_error().to_string());
} else {
auto f = F.move_as_ok();
auto blk_id = ton::create_block_id(f->id_);
if (blk_id != blkid) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error, \
"{\"error\":\"block id mismatch: expected data for block " + blkid.to_str() +\
", obtained for " + blk_id.to_str()+"\"}");
web_error_response(response, "block id mismatch: expected data for block " + blkid.to_str() +\
", obtained for " + blk_id.to_str());
}
td::actor::send_closure_later(Self, &TestNode::got_block_web, blk_id, std::move(f->data_), dump, response);
}
Expand All @@ -40,32 +37,27 @@ void TestNode::got_block_web(ton::BlockIdExt blkid, td::BufferSlice data, bool d
ton::FileHash fhash;
td::sha256(data.as_slice(), fhash.as_slice());
if (fhash != blkid.file_hash) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error, \
"{\"error\":\"file hash mismatch for block " + blkid.to_str() +\
": expected " + blkid.file_hash.to_hex() + ", computed " + fhash.to_hex()+"\"}");
web_error_response(response, "file hash mismatch for block " + blkid.to_str() +\
": expected " + blkid.file_hash.to_hex() + ", computed " + fhash.to_hex());
return;
}
if (!db_root_.empty()) {
auto res = save_db_file(fhash, data.clone());
if (res.is_error()) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error, \
"{\"error\":\"error saving block file: " + res.to_string() + "\"}");
web_error_response(response, "error saving block file: " + res.to_string());
}
}
if (dump) {
auto res = vm::std_boc_deserialize(data.clone());
if (res.is_error()) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error, \
"{\"error\":\"cannot deserialize block data " + res.move_as_error().to_string() + "\"}");
web_error_response(response, "cannot deserialize block data " + res.move_as_error().to_string());
return;
}
auto root = res.move_as_ok();
ton::RootHash rhash{root->get_hash().bits()};
if (rhash != blkid.root_hash) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error, \
"{\"error\":\"block root hash mismatch: data has " + rhash.to_hex() +\
" , expected " + blkid.root_hash.to_hex() + "\"}");

web_error_response(response, "block root hash mismatch: data has " + rhash.to_hex() +\
" , expected " + blkid.root_hash.to_hex());
return;
}
//auto out = td::TerminalIO::out();
Expand All @@ -74,21 +66,26 @@ void TestNode::got_block_web(ton::BlockIdExt blkid, td::BufferSlice data, bool d
block::gen::t_Block.print_ref(block_data, root);
vm::load_cell_slice(root).print_rec(vm_data);
give_block_header_description(header_data, blkid, std::move(root), 0xffff);
response -> write("{\"result\": {\"block\":\""+block_data.str()+
"\", \"vm\":\""+vm_data.str()+
"\", \"header\":\""+header_data.str()+"\"}}");


pt::ptree result;
result.put("block", block_data.str());
result.put("vm", vm_data.str());
result.put("header", header_data.str());

web_success_response(response, result);
} else {
auto res = lazy_boc_deserialize(data.clone());
if (res.is_error()) {
LOG(ERROR) << "cannot lazily deserialize block data : " << res.move_as_error().to_string();
web_error_response(response, "cannot lazily deserialize block data : " + res.move_as_error().to_string());
return;
}
auto pair = res.move_as_ok();
auto root = std::move(pair.first);
ton::RootHash rhash{root->get_hash().bits()};
if (rhash != blkid.root_hash) {
LOG(ERROR) << "block root hash mismatch: data has " << rhash.to_hex() << " , expected "
<< blkid.root_hash.to_hex();
web_error_response(response, "block root hash mismatch: data has " + rhash.to_hex() +\
" , expected " + blkid.root_hash.to_hex());
return;
}
show_block_header(blkid, std::move(root), 0xffff);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ bool TestNode::get_server_mc_block_id_web(std::shared_ptr<HttpServer::Response>
auto b = ton::serialize_tl_object(ton::create_tl_object<ton::ton_api::liteServer_getMasterchainInfo>(), true);
return envelope_send_query(std::move(b), [Self = actor_id(this), response](td::Result<td::BufferSlice> res)->void {
if (res.is_error()) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"{\"error\":\"cannot get masterchain info from server\"}");
web_error_response(response, "cannot get masterchain info from server");
return;
} else {
auto F = ton::fetch_tl_object<ton::ton_api::liteServer_masterchainInfo>(res.move_as_ok(), true);
if (F.is_error()) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"{\"error\":\"cannot parse answer to liteServer.getMasterchainInfo\"}");
web_error_response(response, "cannot parse answer to liteServer.getMasterchainInfo");
} else {
auto f = F.move_as_ok();
auto blk_id = create_block_id(f->last_);
// auto zstate_id = create_zero_state_id(f->init_);
response -> write("{\"result\":\"" + blk_id.to_str() + "\"}");
web_success_response(response, blk_id.to_str());
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ void TestNode::get_server_time_web(std::shared_ptr<HttpServer::Response> respons

envelope_send_web(std::move(b), [&, Self = actor_id(this), response](td::Result<td::BufferSlice> res) -> void {
if (res.is_error()) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"{\"error\":\"cannot get server time\"}");
web_error_response(response, "cannot get server time");
return;
} else {
auto F = ton::fetch_tl_object<ton::ton_api::liteServer_currentTime>(res.move_as_ok(), true);
if (F.is_error()) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"{\"error\":\"cannot parse answer to liteServer.getTime\"}");
web_error_response(response, "cannot parse answer to liteServer.getTime");
} else {
server_time_ = F.move_as_ok()->now_;
server_time_got_at_ = static_cast<td::uint32>(td::Clocks::system());
response -> write("{\"result\":\"" + std::to_string(server_time_) + "\"}");
web_success_response(response, std::to_string(server_time_));
}
}
}, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,13 @@ bool TestNode::envelope_send_web(td::BufferSlice query,
td::Promise<td::BufferSlice> promise,
std::shared_ptr<HttpServer::Response> response) {
if (!ready_ || client_.empty()) {
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"failed to send query to server: not ready");
web_error_response(response, "failed to send query to server: not ready");
return false;
}
auto P = td::PromiseCreator::lambda([promise = std::move(promise), response](td::Result<td::BufferSlice> R) mutable {
if (R.is_error()) {
auto err = R.move_as_error();
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"failed query");
web_error_response(response, "failed query");
promise.set_error(std::move(err));
return;
}
Expand All @@ -75,8 +73,7 @@ bool TestNode::envelope_send_web(td::BufferSlice query,
if (F.is_ok()) {
auto f = F.move_as_ok();
auto err = td::Status::Error(f->code_, f->message_);
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
"received error");
web_error_response(response, "received error");
promise.set_error(std::move(err));
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

#include "server_http.hpp"

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

// Short alias for this namespace
namespace pt = boost::property_tree;

using td::Ref;

using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>;
Expand Down Expand Up @@ -168,6 +174,9 @@ class TestNode : public td::actor::Actor {

// Web Server Methods
static void run_web_server(td::actor::Scheduler* scheduler, td::actor::ActorOwn<TestNode>* x);
static void web_error_response(std::shared_ptr<HttpServer::Response> response, std::string msg);
static void web_success_response(std::shared_ptr<HttpServer::Response> response, std::string msg);
static void web_success_response(std::shared_ptr<HttpServer::Response> response, pt::ptree root);

void web_last(){
get_server_mc_block_id_silent();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
void TestNode::web_error_response(std::shared_ptr<HttpServer::Response> response, std::string msg) {
pt::ptree root;
std::ostringstream oss;
root.put("error", msg);
pt::write_json(oss, root);
response -> write(SimpleWeb::StatusCode::server_error_internal_server_error,
oss.str());
}

void TestNode::web_success_response(std::shared_ptr<HttpServer::Response> response, std::string msg) {
pt::ptree root;
std::ostringstream oss;
root.put("result", msg);
pt::write_json(oss, root);
response -> write(oss.str());
}

void TestNode::web_success_response(std::shared_ptr<HttpServer::Response> response, pt::ptree result) {
pt::ptree root;
std::ostringstream oss;
root.put_child("result", result);
pt::write_json(oss, root);
response -> write(oss.str());
}

void TestNode::run_web_server(td::actor::Scheduler* scheduler, td::actor::ActorOwn<TestNode>* x){
HttpServer server;
server.config.port = 8000;
Expand Down

0 comments on commit 3093c51

Please sign in to comment.