/* This file is part of TON Blockchain source code. TON Blockchain is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. TON Blockchain is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with TON Blockchain. If not, see . In addition, as a special exception, the copyright holders give permission to link the code of portions of this program with the OpenSSL library. You must obey the GNU General Public License in all respects for all of the code used other than OpenSSL. If you modify file(s) with this exception, you may extend this exception to your version of the file(s), but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. If you delete this exception statement from all source files in the program, then also delete it here. Copyright 2017-2019 Telegram Systems LLP */ #include "blockchain-explorer-http.hpp" #include "block/block-db.h" #include "block/block.h" #include "block/block-parse.h" #include "block/block-auto.h" #include "vm/boc.h" #include "vm/cellops.h" #include "vm/cells/MerkleProof.h" #include "block/mc-config.h" #include "ton/ton-shard.h" HttpAnswer& HttpAnswer::operator<<(AddressCell addr_c) { ton::WorkchainId wc; ton::StdSmcAddress addr; if (!block::tlb::t_MsgAddressInt.extract_std_address(addr_c.root, wc, addr)) { abort(""); return *this; } block::StdAddress caddr{wc, addr}; *this << "" << caddr.rserialize(true) << ""; return *this; } HttpAnswer& HttpAnswer::operator<<(MessageCell msg) { if (msg.root.is_null()) { abort(" src, dest; *this << "
get_hash() << "\">"; *this << "
\n" << "\n" << "\n"; switch (block::gen::t_CommonMsgInfo.get_tag(cs)) { case block::gen::CommonMsgInfo::ext_in_msg_info: { block::gen::CommonMsgInfo::Record_ext_in_msg_info info; if (!tlb::unpack(cs, info)) { abort(""); return *this; } *this << "\n" << "\n" << "\n"; break; } case block::gen::CommonMsgInfo::ext_out_msg_info: { block::gen::CommonMsgInfo::Record_ext_out_msg_info info; if (!tlb::unpack(cs, info)) { abort(""); return *this; } *this << "\n" << "\n" << "\n" << "\n" << "\n"; break; } case block::gen::CommonMsgInfo::int_msg_info: { block::gen::CommonMsgInfo::Record_int_msg_info info; if (!tlb::unpack(cs, info)) { abort("cannot unpack internal message"); return *this; } td::RefInt256 value; td::Ref extra; if (!block::unpack_CurrencyCollection(info.value, value, extra)) { abort("cannot unpack message value"); return *this; } *this << "\n" << "\n" << "\n" << "\n" << "\n" << "\n"; break; } default: abort("cannot unpack message"); return *this; } *this << "
hash" << msg.root->get_hash().to_hex() << "
typeexternal
sourceNONE
destination" << AddressCell{info.dest} << "
typeexternal OUT
source" << AddressCell{info.src} << "
destinationNONE
lt" << info.created_lt << "
time" << info.created_at << "
typeinternal
source" << AddressCell{info.src} << "
destination" << AddressCell{info.dest} << "
lt" << info.created_lt << "
time" << info.created_at << "
value" << value << "
\n"; *this << RawData{msg.root, block::gen::t_Anything} << "
"; return *this; } HttpAnswer& HttpAnswer::operator<<(ton::BlockIdExt block_id) { return *this << "" << block_id.id.to_str() << ""; } HttpAnswer& HttpAnswer::operator<<(ton::BlockId block_id) { return *this << "" << block_id.to_str() << ""; } HttpAnswer& HttpAnswer::operator<<(BlockSearch bs) { *this << "
" << "
" << "
" << "" << "" << "
\n" << "
" << "" << "
" << "
" << "" << "
" << "
" << "" << "
" << "
" << "
" << "" << "" << "
\n" << "
" << "" << "
" << "
" << "
" << "" << "
" << "
" << "" << "
" << "
\n"; return *this; } HttpAnswer& HttpAnswer::operator<<(AccountSearch bs) { *this << "
" << "
" << "
" << "" << "
" << "
" << "" << "
" << "
" << "" << "
" << "
" << "" << "
" << "
" << "
" << "
" << "" << "
" << "
" << "" << "
" << "
" << "
" << "" << "
" << "
\n" << "
\n"; return *this; } HttpAnswer& HttpAnswer::operator<<(TransactionSearch bs) { *this << "
" << "
" << "
" << "" << "
" << "
" << "" << "
" << "
" << "" << "
" << "
" << "" << "
" << "
" << "
" << "
" << "" << "
" << "
" << "" << "
" << "
" << "
" << "" << "
" << "
" << "
" << "" << "
" << "
" << "" << "
" << "
\n" << "
\n"; return *this; } HttpAnswer& HttpAnswer::operator<<(TransactionCell trans_c) { if (trans_c.root.is_null()) { abort("transaction not found"); return *this; } block::gen::Transaction::Record trans; if (!tlb::unpack_cell(trans_c.root, trans)) { abort("cannot unpack"); return *this; } *this << "
\n" << "\n" << "" << "" << "" << "" << "\n" << "\n" << "\n" << "\n" << "\n" << "
block" << trans_c.block_id.id.to_str() << "
workchain" << trans_c.addr.workchain << "
account hex" << trans_c.addr.addr.to_hex() << "
account" << trans_c.addr.rserialize(true) << "
hash" << trans_c.root->get_hash().to_hex() << "
lt" << trans.lt << "
time" << trans.now << "
out messages"; vm::Dictionary dict{trans.r1.out_msgs, 15}; for (td::int32 i = 0; i < trans.outmsg_cnt; i++) { auto out_msg = dict.lookup_ref(td::BitArray<15>{i}); *this << " " << i << ""; } *this << "
in message"; auto in_msg = trans.r1.in_msg->prefetch_ref(); if (in_msg.is_null()) { *this << "NONE"; } else { *this << "" << in_msg->get_hash() << ""; } *this << "
prev transaction"; auto prev_lt = trans.prev_trans_lt; auto prev_hash = trans.prev_trans_hash; if (prev_lt > 0) { *this << "lt=" << prev_lt << " hash=" << prev_hash.to_hex() << ""; } else { *this << "NONE"; } *this << "
\n"; if (in_msg.not_null()) { *this << "
" << MessageCell{in_msg}; } for (int x = 0; x < trans.outmsg_cnt && x < 100; x++) { auto out_msg = dict.lookup_ref(td::BitArray<15>{x}); *this << "
" << MessageCell{out_msg}; } *this << "
"; return *this << RawData{trans_c.root} << ""; } HttpAnswer& HttpAnswer::operator<<(AccountCell acc_c) { *this << "
"; auto block_id = acc_c.block_id; if (!block_id.is_valid_full()) { abort(PSTRING() << "shard block id " << block_id.to_str() << " in answer is invalid"); return *this; } if (!ton::shard_contains(block_id.shard_full(), ton::extract_addr_prefix(acc_c.addr.workchain, acc_c.addr.addr))) { abort(PSTRING() << "received data from shard block " << block_id.to_str() << " that cannot contain requested account " << acc_c.addr.workchain << ":" << acc_c.addr.addr.to_hex()); return *this; } if (acc_c.q_roots.size() != 2) { abort(PSTRING() << "account state proof must have exactly two roots"); return *this; } ton::LogicalTime last_trans_lt = 0; ton::Bits256 last_trans_hash; last_trans_hash.set_zero(); try { auto state_root = vm::MerkleProof::virtualize(acc_c.q_roots[1], 1); if (state_root.is_null()) { abort("account state proof is invalid"); return *this; } block::gen::ShardStateUnsplit::Record sstate; if (!(tlb::unpack_cell(std::move(state_root), sstate))) { abort("cannot unpack state header"); return *this; } vm::AugmentedDictionary accounts_dict{vm::load_cell_slice_ref(sstate.accounts), 256, block::tlb::aug_ShardAccounts}; auto acc_csr = accounts_dict.lookup(acc_c.addr.addr); if (acc_csr.not_null()) { if (acc_c.root.is_null()) { abort(PSTRING() << "account state proof shows that account state for " << acc_c.addr.workchain << ":" << acc_c.addr.addr.to_hex() << " must be non-empty, but it actually is empty"); return *this; } block::gen::ShardAccount::Record acc_info; if (!tlb::csr_unpack(std::move(acc_csr), acc_info)) { abort("cannot unpack ShardAccount from proof"); return *this; } if (acc_info.account->get_hash().bits().compare(acc_c.root->get_hash().bits(), 256)) { abort(PSTRING() << "account state hash mismatch: Merkle proof expects " << acc_info.account->get_hash().bits().to_hex(256) << " but received data has " << acc_c.root->get_hash().bits().to_hex(256)); return *this; } last_trans_hash = acc_info.last_trans_hash; last_trans_lt = acc_info.last_trans_lt; } else if (acc_c.root.not_null()) { abort(PSTRING() << "account state proof shows that account state for " << acc_c.addr.workchain << ":" << acc_c.addr.addr.to_hex() << " must be empty, but it is not"); return *this; } } catch (vm::VmError err) { abort(PSTRING() << "error while traversing account proof : " << err.get_msg()); return *this; } catch (vm::VmVirtError err) { abort(PSTRING() << "virtualization error while traversing account proof : " << err.get_msg()); return *this; } *this << "
\n" << "\n"; *this << ""; *this << ""; *this << ""; *this << ""; if (last_trans_lt > 0) { *this << "\n"; } else { *this << ""; } *this << "
block" << block_id.id.to_str() << "
workchain" << acc_c.addr.workchain << "
account hex" << acc_c.addr.addr.to_hex() << "
account" << acc_c.addr.rserialize(true) << "
last transaction" << "lt=" << last_trans_lt << " hash=" << last_trans_hash.to_hex() << "
last transactionno transactions
\n"; *this << "

go to current state

\n"; if (acc_c.root.not_null()) { *this << RawData{acc_c.root}; } else { *this << "
account state is empty
"; } return *this << "
"; } HttpAnswer& HttpAnswer::operator<<(BlockHeaderCell head_c) { *this << "
"; vm::CellSlice cs{vm::NoVm{}, head_c.root}; auto block_id = head_c.block_id; try { auto virt_root = vm::MerkleProof::virtualize(head_c.root, 1); if (virt_root.is_null()) { abort("invalid merkle proof"); return *this; } ton::RootHash vhash{virt_root->get_hash().bits()}; std::vector prev; ton::BlockIdExt mc_blkid; bool after_split; auto res = block::unpack_block_prev_blk_ext(virt_root, block_id, prev, mc_blkid, after_split); if (res.is_error()) { abort(PSTRING() << "cannot unpack header for block " << block_id.to_str() << ": " << res); return *this; } block::gen::Block::Record blk; block::gen::BlockInfo::Record info; if (!(tlb::unpack_cell(virt_root, blk) && tlb::unpack_cell(blk.info, info))) { abort(PSTRING() << "cannot unpack header for block " << block_id.to_str()); return *this; } bool before_split = info.before_split; *this << "
\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n"; for (auto id : prev) { *this << "\n"; } if (!before_split) { *this << "\n"; } else { *this << "\n"; *this << "\n"; } *this << "\n" << "
block" << block_id.id.to_str() << "
roothash" << block_id.root_hash.to_hex() << "
filehash" << block_id.file_hash.to_hex() << "
time" << info.gen_utime << "
lt" << info.start_lt << " .. " << info.end_lt << "
global_id" << blk.global_id << "
version" << info.version << "
not_master" << info.not_master << "
after_merge" << info.after_merge << "
after_split" << info.after_split << "
before_split" << info.before_split << "
want_merge" << info.want_merge << "
want_split" << info.want_split << "
validator_list_hash_short" << info.gen_validator_list_hash_short << "
catchain_seqno" << info.gen_catchain_seqno << "
min_ref_mc_seqno" << info.min_ref_mc_seqno << "
prev block" << id << "
next block" << ton::BlockId{block_id.id.workchain, block_id.id.shard, block_id.id.seqno + 1} << "
next block" << ton::BlockId{block_id.id.workchain, ton::shard_child(block_id.id.shard, true), block_id.id.seqno + 1} << "
next block" << ton::BlockId{block_id.id.workchain, ton::shard_child(block_id.id.shard, false), block_id.id.seqno + 1} << "
masterchain block" << mc_blkid << "
"; } catch (vm::VmError err) { abort(PSTRING() << "error processing header : " << err.get_msg()); return *this; } catch (vm::VmVirtError err) { abort(PSTRING() << "error processing header : " << err.get_msg()); return *this; } return *this << "

download block" << "view block\n" << "

"; } HttpAnswer& HttpAnswer::operator<<(BlockShardsCell shards_c) { block::ShardConfig sh_conf; if (!sh_conf.unpack(vm::load_cell_slice_ref(shards_c.root))) { abort("cannot extract shard block list from shard configuration"); return *this; } else { auto ids = sh_conf.get_shard_hash_ids(true); auto workchain = ton::masterchainId; *this << "
\n" << "\n\n" << "\n" << "\n" << "" << "" << "" << "" << "" << "" << "" << "\n" << "\n"; for (auto id : ids) { auto ref = sh_conf.get_shard_hash(ton::ShardIdFull(id)); if (id.workchain != workchain) { if (workchain != ton::masterchainId) { *this << "\n"; } workchain = id.workchain; } *this << ""; ton::ShardIdFull shard{id.workchain, id.shard}; if (ref.not_null()) { *this << "" << "" << "" << "" << ""; } else { *this << ""; } *this << ""; } return *this << "
shardseqnocreatedwantsplitwantmergebeforesplitbeforemerge
" << shard.to_str() << "top_block_id()} << "\">" << ref->top_block_id().id.seqno << "" << ref->created_at() << "" << ref->want_split_ << "" << ref->want_merge_ << "" << ref->before_split_ << "" << ref->before_merge_ << "" << shard.to_str() << "
"; } } HttpAnswer& HttpAnswer::operator<<(AccountLink account) { *this << prefix_ << "account?"; if (account.block_id.is_valid()) { block_id_link(account.block_id); *this << "&"; } return *this << "account=" << account.account_id.rserialize(true); } HttpAnswer& HttpAnswer::operator<<(MessageLink msg) { return *this << "#msg" << msg.root->get_hash(); } HttpAnswer& HttpAnswer::operator<<(TransactionLink trans) { return *this << prefix_ << "transaction?" << "account=" << trans.account_id.rserialize(true) << "<=" << trans.lt << "&hash=" << trans.hash; } HttpAnswer& HttpAnswer::operator<<(TransactionLinkShort trans) { *this << prefix_ << "transaction2?"; block_id_link(trans.block_id); return *this << "&account=" << trans.account_id.rserialize(true) << "<=" << trans.lt; } HttpAnswer& HttpAnswer::operator<<(BlockLink block) { *this << prefix_ << "block?"; block_id_link(block.block_id); return *this; } HttpAnswer& HttpAnswer::operator<<(BlockViewLink block) { *this << prefix_ << "viewblock?"; block_id_link(block.block_id); return *this; } HttpAnswer& HttpAnswer::operator<<(BlockDownloadLink block) { *this << prefix_ << "download?"; block_id_link(block.block_id); return *this; } HttpAnswer& HttpAnswer::operator<<(TransactionList trans) { *this << "
\n" << "\n\n" << "\n" << "\n" << "" << "" << "" << "" << "" << "\n" << "\n"; td::uint32 idx = 0; for (auto& x : trans.vec) { *this << "" << "" << "" << "" << ""; } if (trans.vec.size() == trans.req_count_) { *this << "" << "" << "" << ""; } return *this << "
seqaccountlthashlink
" << ++idx << "" << x.addr.rserialize(true) << "" << x.lt << "" << x.hash.to_hex() << "view
" << ++idx << "moremoremore
"; } HttpAnswer& HttpAnswer::operator<<(Error error) { return *this << "
" << error.error.to_string() << "
"; } void HttpAnswer::block_id_link(ton::BlockIdExt block_id) { *this << "workchain=" << block_id.id.workchain << "&shard=" << ton::shard_to_str(block_id.id.shard) << "&seqno=" << block_id.id.seqno << "&roothash=" << block_id.root_hash << "&filehash=" << block_id.file_hash; } std::string HttpAnswer::abort(td::Status error) { if (error_.is_ok()) { error_ = std::move(error); } return header() + "
" + error_.to_string() + "
" + footer(); } std::string HttpAnswer::abort(std::string error) { return abort(td::Status::Error(404, error)); } std::string HttpAnswer::header() { sb_->clear(); *this << "\n" << "" << title_ << "\n" << "\n" << "\n" << "\n" << "\n" << "" << "\n" << "\n" << "\n" << "\n" << "\n" << "\n" << "
\n" << "\n"; *this << "

\n" << "\n" << "Search block\n" << "\n" << "\n" << "Search account\n" << "\n" << "\n" << "Search transaction\n" << "\n" << "

\n"; *this << "
\n" << "
\n" << "
\n" << BlockSearch{block_id_} << "
\n"; *this << "
\n" << "
\n" << AccountSearch{block_id_, account_id_} << "
\n"; *this << "
\n" << "
\n" << TransactionSearch{block_id_, account_id_, 0, ton::Bits256::zero()} << "
\n"; return sb_->as_cslice().c_str(); } std::string HttpAnswer::footer() { return PSTRING() << "
"; } std::string HttpAnswer::finish() { if (error_.is_ok()) { std::string data = sb_->as_cslice().c_str(); return header() + data + footer(); } else { return header() + "
" + error_.to_string() + "
" + footer(); } }