├── CMakeLists.txt ├── DISCLAIMER.MD ├── LICENSE.LGPL ├── LICENSE.MD ├── README.md ├── blockchain-api-http.cpp ├── blockchain-api-http.hpp ├── blockchain-api-query.cpp ├── blockchain-api-query.hpp ├── blockchain-api.cpp ├── blockchain-api.hpp ├── config.json ├── global-config.json ├── indexes.sql ├── query-impl ├── block-info.cpp ├── block-search-extended.cpp ├── block-search-hash.cpp ├── block-search.cpp ├── config.cpp ├── run-method.cpp ├── search-by-height.cpp ├── validators.cpp ├── view-account.cpp ├── view-last-block.cpp ├── view-transaction-extended.cpp └── view-transaction.cpp └── swagger.yaml /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) 2 | 3 | find_path( 4 | UNI_INCLUDE_DIR 5 | NAMES univalue.h 6 | DOC "univalue include dir" 7 | ) 8 | 9 | set(CMAKE_CXX_STANDARD 14) 10 | 11 | set(UNI_INCLUDE_DIRS ${UNI_INCLUDE_DIR}) 12 | 13 | find_package(MHD) 14 | add_cxx_compiler_flag("-lpq") 15 | add_cxx_compiler_flag("-lpqxx") 16 | 17 | if (MHD_FOUND) 18 | 19 | set(BLOCHAIN_API_SOURCE 20 | blockchain-api.cpp 21 | blockchain-api.hpp 22 | blockchain-api-http.cpp 23 | blockchain-api-http.hpp 24 | blockchain-api-query.cpp 25 | blockchain-api-query.hpp 26 | query-impl/block-info.cpp 27 | query-impl/block-search.cpp 28 | query-impl/run-method.cpp 29 | query-impl/view-account.cpp 30 | query-impl/view-transaction-extended.cpp 31 | query-impl/block-search-extended.cpp 32 | query-impl/block-search-hash.cpp 33 | query-impl/config.cpp 34 | query-impl/search-by-height.cpp 35 | query-impl/validators.cpp 36 | query-impl/view-last-block.cpp 37 | query-impl/view-transaction.cpp 38 | ) 39 | 40 | add_executable(blockchain-api ${BLOCHAIN_API_SOURCE}) 41 | target_include_directories(blockchain-api PUBLIC ${MHD_INCLUDE_DIRS} ${UNI_INCLUDE_DIR}, "/usr/include/postgresql/") 42 | target_link_libraries(blockchain-api tdutils tdactor adnllite tl_lite_api tl-lite-utils univalue 43 | ton_crypto ton_block ${MHD_LIBRARY}) 44 | 45 | install(TARGETS blockchain-api RUNTIME DESTINATION bin) 46 | 47 | endif() 48 | -------------------------------------------------------------------------------- /DISCLAIMER.MD: -------------------------------------------------------------------------------- 1 | We kindly inform you that the object code provided in this repository is original, and was independently developed by Blockchair Core Developers. 2 | However, when the object code is compiled to executable, it implements the TON Blockchain Library which may be found at https://github.com/ton-blockchain/ton. You may find the pull up of TON Blockchain Library for the compilation purposes at [link to a relevant docker file]. To compile our Software, please read [link to documentation]. 3 | Please, beware that TON Blockchain Library and its use is subject to GNU LIBRARY GENERAL PUBLIC LICENSE Version 2 of 1997, the exact version of which should be read from https://github.com/ton-blockchain/ton/blob/master/LICENSE.LGPL. 4 | 5 | We hereby accompany you with the copy of this license available at [LICENSE.LGPL](LICENSE.LGPL) -------------------------------------------------------------------------------- /LICENSE.LGPL: -------------------------------------------------------------------------------- 1 | GNU LIBRARY GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1991 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the library GPL. It is 10 | numbered 2 because it goes with version 2 of the ordinary GPL.] 11 | 12 | Preamble 13 | 14 | The licenses for most software are designed to take away your 15 | freedom to share and change it. By contrast, the GNU General Public 16 | Licenses are intended to guarantee your freedom to share and change 17 | free software--to make sure the software is free for all its users. 18 | 19 | This license, the Library General Public License, applies to some 20 | specially designated Free Software Foundation software, and to any 21 | other libraries whose authors decide to use it. You can use it for 22 | your libraries, too. 23 | 24 | When we speak of free software, we are referring to freedom, not 25 | price. Our General Public Licenses are designed to make sure that you 26 | have the freedom to distribute copies of free software (and charge for 27 | this service if you wish), that you receive source code or can get it 28 | if you want it, that you can change the software or use pieces of it 29 | in new free programs; and that you know you can do these things. 30 | 31 | To protect your rights, we need to make restrictions that forbid 32 | anyone to deny you these rights or to ask you to surrender the rights. 33 | These restrictions translate to certain responsibilities for you if 34 | you distribute copies of the library, or if you modify it. 35 | 36 | For example, if you distribute copies of the library, whether gratis 37 | or for a fee, you must give the recipients all the rights that we gave 38 | you. You must make sure that they, too, receive or can get the source 39 | code. If you link a program with the library, you must provide 40 | complete object files to the recipients so that they can relink them 41 | with the library, after making changes to the library and recompiling 42 | it. And you must show them these terms so they know their rights. 43 | 44 | Our method of protecting your rights has two steps: (1) copyright 45 | the library, and (2) offer you this license which gives you legal 46 | permission to copy, distribute and/or modify the library. 47 | 48 | Also, for each distributor's protection, we want to make certain 49 | that everyone understands that there is no warranty for this free 50 | library. If the library is modified by someone else and passed on, we 51 | want its recipients to know that what they have is not the original 52 | version, so that any problems introduced by others will not reflect on 53 | the original authors' reputations. 54 | 55 | Finally, any free program is threatened constantly by software 56 | patents. We wish to avoid the danger that companies distributing free 57 | software will individually obtain patent licenses, thus in effect 58 | transforming the program into proprietary software. To prevent this, 59 | we have made it clear that any patent must be licensed for everyone's 60 | free use or not licensed at all. 61 | 62 | Most GNU software, including some libraries, is covered by the ordinary 63 | GNU General Public License, which was designed for utility programs. This 64 | license, the GNU Library General Public License, applies to certain 65 | designated libraries. This license is quite different from the ordinary 66 | one; be sure to read it in full, and don't assume that anything in it is 67 | the same as in the ordinary license. 68 | 69 | The reason we have a separate public license for some libraries is that 70 | they blur the distinction we usually make between modifying or adding to a 71 | program and simply using it. Linking a program with a library, without 72 | changing the library, is in some sense simply using the library, and is 73 | analogous to running a utility program or application program. However, in 74 | a textual and legal sense, the linked executable is a combined work, a 75 | derivative of the original library, and the ordinary General Public License 76 | treats it as such. 77 | 78 | Because of this blurred distinction, using the ordinary General 79 | Public License for libraries did not effectively promote software 80 | sharing, because most developers did not use the libraries. We 81 | concluded that weaker conditions might promote sharing better. 82 | 83 | However, unrestricted linking of non-free programs would deprive the 84 | users of those programs of all benefit from the free status of the 85 | libraries themselves. This Library General Public License is intended to 86 | permit developers of non-free programs to use free libraries, while 87 | preserving your freedom as a user of such programs to change the free 88 | libraries that are incorporated in them. (We have not seen how to achieve 89 | this as regards changes in header files, but we have achieved it as regards 90 | changes in the actual functions of the Library.) The hope is that this 91 | will lead to faster development of free libraries. 92 | 93 | The precise terms and conditions for copying, distribution and 94 | modification follow. Pay close attention to the difference between a 95 | "work based on the library" and a "work that uses the library". The 96 | former contains code derived from the library, while the latter only 97 | works together with the library. 98 | 99 | Note that it is possible for a library to be covered by the ordinary 100 | General Public License rather than by this special one. 101 | 102 | GNU LIBRARY GENERAL PUBLIC LICENSE 103 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 104 | 105 | 0. This License Agreement applies to any software library which 106 | contains a notice placed by the copyright holder or other authorized 107 | party saying it may be distributed under the terms of this Library 108 | General Public License (also called "this License"). Each licensee is 109 | addressed as "you". 110 | 111 | A "library" means a collection of software functions and/or data 112 | prepared so as to be conveniently linked with application programs 113 | (which use some of those functions and data) to form executables. 114 | 115 | The "Library", below, refers to any such software library or work 116 | which has been distributed under these terms. A "work based on the 117 | Library" means either the Library or any derivative work under 118 | copyright law: that is to say, a work containing the Library or a 119 | portion of it, either verbatim or with modifications and/or translated 120 | straightforwardly into another language. (Hereinafter, translation is 121 | included without limitation in the term "modification".) 122 | 123 | "Source code" for a work means the preferred form of the work for 124 | making modifications to it. For a library, complete source code means 125 | all the source code for all modules it contains, plus any associated 126 | interface definition files, plus the scripts used to control compilation 127 | and installation of the library. 128 | 129 | Activities other than copying, distribution and modification are not 130 | covered by this License; they are outside its scope. The act of 131 | running a program using the Library is not restricted, and output from 132 | such a program is covered only if its contents constitute a work based 133 | on the Library (independent of the use of the Library in a tool for 134 | writing it). Whether that is true depends on what the Library does 135 | and what the program that uses the Library does. 136 | 137 | 1. You may copy and distribute verbatim copies of the Library's 138 | complete source code as you receive it, in any medium, provided that 139 | you conspicuously and appropriately publish on each copy an 140 | appropriate copyright notice and disclaimer of warranty; keep intact 141 | all the notices that refer to this License and to the absence of any 142 | warranty; and distribute a copy of this License along with the 143 | Library. 144 | 145 | You may charge a fee for the physical act of transferring a copy, 146 | and you may at your option offer warranty protection in exchange for a 147 | fee. 148 | 149 | 2. You may modify your copy or copies of the Library or any portion 150 | of it, thus forming a work based on the Library, and copy and 151 | distribute such modifications or work under the terms of Section 1 152 | above, provided that you also meet all of these conditions: 153 | 154 | a) The modified work must itself be a software library. 155 | 156 | b) You must cause the files modified to carry prominent notices 157 | stating that you changed the files and the date of any change. 158 | 159 | c) You must cause the whole of the work to be licensed at no 160 | charge to all third parties under the terms of this License. 161 | 162 | d) If a facility in the modified Library refers to a function or a 163 | table of data to be supplied by an application program that uses 164 | the facility, other than as an argument passed when the facility 165 | is invoked, then you must make a good faith effort to ensure that, 166 | in the event an application does not supply such function or 167 | table, the facility still operates, and performs whatever part of 168 | its purpose remains meaningful. 169 | 170 | (For example, a function in a library to compute square roots has 171 | a purpose that is entirely well-defined independent of the 172 | application. Therefore, Subsection 2d requires that any 173 | application-supplied function or table used by this function must 174 | be optional: if the application does not supply it, the square 175 | root function must still compute square roots.) 176 | 177 | These requirements apply to the modified work as a whole. If 178 | identifiable sections of that work are not derived from the Library, 179 | and can be reasonably considered independent and separate works in 180 | themselves, then this License, and its terms, do not apply to those 181 | sections when you distribute them as separate works. But when you 182 | distribute the same sections as part of a whole which is a work based 183 | on the Library, the distribution of the whole must be on the terms of 184 | this License, whose permissions for other licensees extend to the 185 | entire whole, and thus to each and every part regardless of who wrote 186 | it. 187 | 188 | Thus, it is not the intent of this section to claim rights or contest 189 | your rights to work written entirely by you; rather, the intent is to 190 | exercise the right to control the distribution of derivative or 191 | collective works based on the Library. 192 | 193 | In addition, mere aggregation of another work not based on the Library 194 | with the Library (or with a work based on the Library) on a volume of 195 | a storage or distribution medium does not bring the other work under 196 | the scope of this License. 197 | 198 | 3. You may opt to apply the terms of the ordinary GNU General Public 199 | License instead of this License to a given copy of the Library. To do 200 | this, you must alter all the notices that refer to this License, so 201 | that they refer to the ordinary GNU General Public License, version 2, 202 | instead of to this License. (If a newer version than version 2 of the 203 | ordinary GNU General Public License has appeared, then you can specify 204 | that version instead if you wish.) Do not make any other change in 205 | these notices. 206 | 207 | Once this change is made in a given copy, it is irreversible for 208 | that copy, so the ordinary GNU General Public License applies to all 209 | subsequent copies and derivative works made from that copy. 210 | 211 | This option is useful when you wish to copy part of the code of 212 | the Library into a program that is not a library. 213 | 214 | 4. You may copy and distribute the Library (or a portion or 215 | derivative of it, under Section 2) in object code or executable form 216 | under the terms of Sections 1 and 2 above provided that you accompany 217 | it with the complete corresponding machine-readable source code, which 218 | must be distributed under the terms of Sections 1 and 2 above on a 219 | medium customarily used for software interchange. 220 | 221 | If distribution of object code is made by offering access to copy 222 | from a designated place, then offering equivalent access to copy the 223 | source code from the same place satisfies the requirement to 224 | distribute the source code, even though third parties are not 225 | compelled to copy the source along with the object code. 226 | 227 | 5. A program that contains no derivative of any portion of the 228 | Library, but is designed to work with the Library by being compiled or 229 | linked with it, is called a "work that uses the Library". Such a 230 | work, in isolation, is not a derivative work of the Library, and 231 | therefore falls outside the scope of this License. 232 | 233 | However, linking a "work that uses the Library" with the Library 234 | creates an executable that is a derivative of the Library (because it 235 | contains portions of the Library), rather than a "work that uses the 236 | library". The executable is therefore covered by this License. 237 | Section 6 states terms for distribution of such executables. 238 | 239 | When a "work that uses the Library" uses material from a header file 240 | that is part of the Library, the object code for the work may be a 241 | derivative work of the Library even though the source code is not. 242 | Whether this is true is especially significant if the work can be 243 | linked without the Library, or if the work is itself a library. The 244 | threshold for this to be true is not precisely defined by law. 245 | 246 | If such an object file uses only numerical parameters, data 247 | structure layouts and accessors, and small macros and small inline 248 | functions (ten lines or less in length), then the use of the object 249 | file is unrestricted, regardless of whether it is legally a derivative 250 | work. (Executables containing this object code plus portions of the 251 | Library will still fall under Section 6.) 252 | 253 | Otherwise, if the work is a derivative of the Library, you may 254 | distribute the object code for the work under the terms of Section 6. 255 | Any executables containing that work also fall under Section 6, 256 | whether or not they are linked directly with the Library itself. 257 | 258 | 6. As an exception to the Sections above, you may also compile or 259 | link a "work that uses the Library" with the Library to produce a 260 | work containing portions of the Library, and distribute that work 261 | under terms of your choice, provided that the terms permit 262 | modification of the work for the customer's own use and reverse 263 | engineering for debugging such modifications. 264 | 265 | You must give prominent notice with each copy of the work that the 266 | Library is used in it and that the Library and its use are covered by 267 | this License. You must supply a copy of this License. If the work 268 | during execution displays copyright notices, you must include the 269 | copyright notice for the Library among them, as well as a reference 270 | directing the user to the copy of this License. Also, you must do one 271 | of these things: 272 | 273 | a) Accompany the work with the complete corresponding 274 | machine-readable source code for the Library including whatever 275 | changes were used in the work (which must be distributed under 276 | Sections 1 and 2 above); and, if the work is an executable linked 277 | with the Library, with the complete machine-readable "work that 278 | uses the Library", as object code and/or source code, so that the 279 | user can modify the Library and then relink to produce a modified 280 | executable containing the modified Library. (It is understood 281 | that the user who changes the contents of definitions files in the 282 | Library will not necessarily be able to recompile the application 283 | to use the modified definitions.) 284 | 285 | b) Accompany the work with a written offer, valid for at 286 | least three years, to give the same user the materials 287 | specified in Subsection 6a, above, for a charge no more 288 | than the cost of performing this distribution. 289 | 290 | c) If distribution of the work is made by offering access to copy 291 | from a designated place, offer equivalent access to copy the above 292 | specified materials from the same place. 293 | 294 | d) Verify that the user has already received a copy of these 295 | materials or that you have already sent this user a copy. 296 | 297 | For an executable, the required form of the "work that uses the 298 | Library" must include any data and utility programs needed for 299 | reproducing the executable from it. However, as a special exception, 300 | the source code distributed need not include anything that is normally 301 | distributed (in either source or binary form) with the major 302 | components (compiler, kernel, and so on) of the operating system on 303 | which the executable runs, unless that component itself accompanies 304 | the executable. 305 | 306 | It may happen that this requirement contradicts the license 307 | restrictions of other proprietary libraries that do not normally 308 | accompany the operating system. Such a contradiction means you cannot 309 | use both them and the Library together in an executable that you 310 | distribute. 311 | 312 | 7. You may place library facilities that are a work based on the 313 | Library side-by-side in a single library together with other library 314 | facilities not covered by this License, and distribute such a combined 315 | library, provided that the separate distribution of the work based on 316 | the Library and of the other library facilities is otherwise 317 | permitted, and provided that you do these two things: 318 | 319 | a) Accompany the combined library with a copy of the same work 320 | based on the Library, uncombined with any other library 321 | facilities. This must be distributed under the terms of the 322 | Sections above. 323 | 324 | b) Give prominent notice with the combined library of the fact 325 | that part of it is a work based on the Library, and explaining 326 | where to find the accompanying uncombined form of the same work. 327 | 328 | 8. You may not copy, modify, sublicense, link with, or distribute 329 | the Library except as expressly provided under this License. Any 330 | attempt otherwise to copy, modify, sublicense, link with, or 331 | distribute the Library is void, and will automatically terminate your 332 | rights under this License. However, parties who have received copies, 333 | or rights, from you under this License will not have their licenses 334 | terminated so long as such parties remain in full compliance. 335 | 336 | 9. You are not required to accept this License, since you have not 337 | signed it. However, nothing else grants you permission to modify or 338 | distribute the Library or its derivative works. These actions are 339 | prohibited by law if you do not accept this License. Therefore, by 340 | modifying or distributing the Library (or any work based on the 341 | Library), you indicate your acceptance of this License to do so, and 342 | all its terms and conditions for copying, distributing or modifying 343 | the Library or works based on it. 344 | 345 | 10. Each time you redistribute the Library (or any work based on the 346 | Library), the recipient automatically receives a license from the 347 | original licensor to copy, distribute, link with or modify the Library 348 | subject to these terms and conditions. You may not impose any further 349 | restrictions on the recipients' exercise of the rights granted herein. 350 | You are not responsible for enforcing compliance by third parties to 351 | this License. 352 | 353 | 11. If, as a consequence of a court judgment or allegation of patent 354 | infringement or for any other reason (not limited to patent issues), 355 | conditions are imposed on you (whether by court order, agreement or 356 | otherwise) that contradict the conditions of this License, they do not 357 | excuse you from the conditions of this License. If you cannot 358 | distribute so as to satisfy simultaneously your obligations under this 359 | License and any other pertinent obligations, then as a consequence you 360 | may not distribute the Library at all. For example, if a patent 361 | license would not permit royalty-free redistribution of the Library by 362 | all those who receive copies directly or indirectly through you, then 363 | the only way you could satisfy both it and this License would be to 364 | refrain entirely from distribution of the Library. 365 | 366 | If any portion of this section is held invalid or unenforceable under any 367 | particular circumstance, the balance of the section is intended to apply, 368 | and the section as a whole is intended to apply in other circumstances. 369 | 370 | It is not the purpose of this section to induce you to infringe any 371 | patents or other property right claims or to contest validity of any 372 | such claims; this section has the sole purpose of protecting the 373 | integrity of the free software distribution system which is 374 | implemented by public license practices. Many people have made 375 | generous contributions to the wide range of software distributed 376 | through that system in reliance on consistent application of that 377 | system; it is up to the author/donor to decide if he or she is willing 378 | to distribute software through any other system and a licensee cannot 379 | impose that choice. 380 | 381 | This section is intended to make thoroughly clear what is believed to 382 | be a consequence of the rest of this License. 383 | 384 | 12. If the distribution and/or use of the Library is restricted in 385 | certain countries either by patents or by copyrighted interfaces, the 386 | original copyright holder who places the Library under this License may add 387 | an explicit geographical distribution limitation excluding those countries, 388 | so that distribution is permitted only in or among countries not thus 389 | excluded. In such case, this License incorporates the limitation as if 390 | written in the body of this License. 391 | 392 | 13. The Free Software Foundation may publish revised and/or new 393 | versions of the Library General Public License from time to time. 394 | Such new versions will be similar in spirit to the present version, 395 | but may differ in detail to address new problems or concerns. 396 | 397 | Each version is given a distinguishing version number. If the Library 398 | specifies a version number of this License which applies to it and 399 | "any later version", you have the option of following the terms and 400 | conditions either of that version or of any later version published by 401 | the Free Software Foundation. If the Library does not specify a 402 | license version number, you may choose any version ever published by 403 | the Free Software Foundation. 404 | 405 | 14. If you wish to incorporate parts of the Library into other free 406 | programs whose distribution conditions are incompatible with these, 407 | write to the author to ask for permission. For software which is 408 | copyrighted by the Free Software Foundation, write to the Free 409 | Software Foundation; we sometimes make exceptions for this. Our 410 | decision will be guided by the two goals of preserving the free status 411 | of all derivatives of our free software and of promoting the sharing 412 | and reuse of software generally. 413 | 414 | NO WARRANTY 415 | 416 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 417 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 418 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 419 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 420 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 421 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 422 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 423 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 424 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 425 | 426 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 427 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 428 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 429 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 430 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 431 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 432 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 433 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 434 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 435 | DAMAGES. 436 | 437 | END OF TERMS AND CONDITIONS 438 | 439 | How to Apply These Terms to Your New Libraries 440 | 441 | If you develop a new library, and you want it to be of the greatest 442 | possible use to the public, we recommend making it free software that 443 | everyone can redistribute and change. You can do so by permitting 444 | redistribution under these terms (or, alternatively, under the terms of the 445 | ordinary General Public License). 446 | 447 | To apply these terms, attach the following notices to the library. It is 448 | safest to attach them to the start of each source file to most effectively 449 | convey the exclusion of warranty; and each file should have at least the 450 | "copyright" line and a pointer to where the full notice is found. 451 | 452 | 453 | Copyright (C) 454 | 455 | This library is free software; you can redistribute it and/or 456 | modify it under the terms of the GNU Library General Public 457 | License as published by the Free Software Foundation; either 458 | version 2 of the License, or (at your option) any later version. 459 | 460 | This library is distributed in the hope that it will be useful, 461 | but WITHOUT ANY WARRANTY; without even the implied warranty of 462 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 463 | Library General Public License for more details. 464 | 465 | You should have received a copy of the GNU Library General Public 466 | License along with this library; if not, write to the Free Software 467 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 468 | 469 | Also add information on how to contact you by electronic and paper mail. 470 | 471 | You should also get your employer (if you work as a programmer) or your 472 | school, if any, to sign a "copyright disclaimer" for the library, if 473 | necessary. Here is a sample; alter the names: 474 | 475 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 476 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 477 | 478 | , 1 April 1990 479 | Ty Coon, President of Vice 480 | 481 | That's all there is to it! 482 | -------------------------------------------------------------------------------- /LICENSE.MD: -------------------------------------------------------------------------------- 1 | Copyright (c) 2023 Blockchair Core Team 2 | 3 | === Modified MIT License=== 4 | 5 | SUBJECT TO THE PROVISIONS BELOW, 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: 6 | (1) The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | (2) In case any work (and any of its copy) starts using the Software or any substantial portion of it, the prominent and good faith notice should be given within the work. The notice should state that the Software (or the applicable substantial portion of Software) used in the work was developed by Blockchair Core Team. This provision applies to any form of work, being it an object code or an executable. 8 | 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # THE API SHOULD NOT BE EXPOSED TO PUBLIC IF YOU USE [INDEXER](https://github.com/Blockchair/ton-indexer) BECAUSE SQL INJECTION CAN BE EXPLOITED 2 | 3 | # What is it 4 | It is a submodule for a TON blockchain project that provides JSON-RPC-like API. Using this API you can send requests to open or your TON nodes and get answer in JSON format. 5 | 6 | 7 | # Preparation 8 | This project is a submodule of TON. To start building the project you need to add in global CMakeLists file: 9 | 10 | ```add_subdirectory(blockchain-api)``` 11 | 12 | Also you need MHD and libpq to be installed. 13 | 14 | # Building 15 | All additional libraries are needed to be installed before we start building. This project uses same stack as TON community. So you don't need to install some special libraries except mentioned before. It also uses cmake and clang with C++-14. 16 | Use cmake to build it: 17 | 18 | ```cmake --build --config Debug --target blockchain-api --``` 19 | 20 | # Starting 21 | The application has standard args: 22 | - -h - prints_help 23 | - -I - hides ips from status 24 | - -u - change user 25 | - -C - file to read global config 26 | - -a - connect to node ip:port 27 | - -p - remote public key for node 28 | - -v - set verbosity level 29 | - -D - set database config file (as it example) 30 | - -H - listen on http port 31 | 32 | Simply you can start it and connect to any node from global config, in example: 33 | 34 | ``` ./blockchain-api -C ton-global.config -H 14001 ``` 35 | 36 | # Usage 37 | 38 | After the application was built and started, you can try a simple request to API: 39 | ```bash 40 | curl --location --request GET 'http://ip:port/lastNum' 41 | ``` 42 | 43 | And it will give you an answer: 44 | 45 | ``` json 46 | { 47 | "-1": { 48 | "8000000000000000": { 49 | "seqno": 27757512, 50 | "roothash": "A413222C718F9B0D28BABFFFA9E6DF0C66891A26B11FB268AA3E30C7BC63E0FE", 51 | "filehash": "23E7A553CC7EA9567EB984CDDF5DAB2E08404890E0D5C141DC296B9724EC9716" 52 | } 53 | }, 54 | "0": { 55 | "8000000000000000": { 56 | "seqno": 33335875, 57 | "roothash": "6D288D95A58B4414179F1815A78B5D96CB5FE10D3C378EB950461CAAD92F51A4", 58 | "filehash": "CD04B0430EEF3846CE05510E4F3A42667F90C75BFAEDA39F6D1EBBF9796E50A4" 59 | } 60 | } 61 | } 62 | ``` 63 | 64 | # All endpoints 65 | You can find all endpoints with examples of answers in swagger by building a docker container with it: 66 | 67 | ```docker run -e SWAGGER_JSON=ton.yaml -e SWAGGER_PORT=8080 -p 8080:8080 —name swagger —network= swagger/swagger-ui``` 68 | 69 | License 70 | 71 | TON API is released under the terms of the MIT license. See [LICENSE.MD](LICENSE.MD) for more information. 72 | 73 | By contributing to this repository, you agree to license your work under the MIT license unless specified otherwise at the top of the file itself. Any work contributed where you are not the original author must contain its license header with the original author and source. -------------------------------------------------------------------------------- /blockchain-api-http.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "vm/boc.h" 5 | #include "vm/cellops.h" 6 | #include "td/utils/Random.h" 7 | #include "td/utils/base64.h" 8 | #include "td/utils/crypto.h" 9 | #include "block/block.h" 10 | #include "univalue.h" 11 | #include "block/block-auto.h" 12 | 13 | #include "crypto/block/check-proof.h" 14 | #include "crypto/vm/utils.h" 15 | 16 | #include "vm/boc.h" 17 | #include "vm/cellops.h" 18 | #include "vm/cells/MerkleProof.h" 19 | #include "vm/vm.h" 20 | #include "vm/cp0.h" 21 | 22 | extern bool local_scripts; 23 | 24 | enum unpackRC { 25 | uOK = 0, 26 | uNULL_ptr, 27 | 28 | uERR = 255, 29 | }; 30 | 31 | class HttpAnswer { 32 | public: 33 | struct MessageCell { 34 | td::Ref root; 35 | }; 36 | struct AddressCell { 37 | td::Ref root; 38 | }; 39 | struct TransactionCell { 40 | block::StdAddress addr; 41 | ton::BlockIdExt block_id; 42 | td::Ref root; 43 | }; 44 | struct TransactionSmall { 45 | ton::BlockIdExt block_id; 46 | td::Ref root; 47 | }; 48 | struct Stack { 49 | td::Ref stack; 50 | }; 51 | 52 | struct AccountCell { 53 | block::StdAddress addr; 54 | ton::BlockIdExt block_id; 55 | td::Ref root; 56 | std::vector> q_roots; 57 | }; 58 | struct BlockHeaderCell { 59 | ton::BlockIdExt block_id; 60 | td::Ref root; 61 | }; 62 | struct BlockShardsCell { 63 | ton::BlockIdExt block_id; 64 | td::Ref root; 65 | }; 66 | struct BlockShardsCellSmall { 67 | ton::BlockIdExt block_id; 68 | td::Ref root; 69 | }; 70 | 71 | struct AccountLink { 72 | block::StdAddress account_id; 73 | ton::BlockIdExt block_id; 74 | }; 75 | struct MessageLink { 76 | td::Ref root; 77 | }; 78 | struct TransactionLink { 79 | block::StdAddress account_id; 80 | ton::LogicalTime lt; 81 | ton::Bits256 hash; 82 | }; 83 | struct TransactionLinkShort { 84 | ton::BlockIdExt block_id; 85 | block::StdAddress account_id; 86 | ton::LogicalTime lt; 87 | }; 88 | struct BlockLink { 89 | ton::BlockIdExt block_id; 90 | }; 91 | struct BlockViewLink { 92 | ton::BlockIdExt block_id; 93 | }; 94 | struct ConfigViewLink { 95 | ton::BlockIdExt block_id; 96 | }; 97 | struct BlockDownloadLink { 98 | ton::BlockIdExt block_id; 99 | }; 100 | struct BlockSearch { 101 | ton::BlockIdExt block_id; 102 | }; 103 | struct AccountSearch { 104 | ton::BlockIdExt block_id; 105 | block::StdAddress addr; 106 | }; 107 | struct TransactionSearch { 108 | ton::BlockIdExt block_id; 109 | block::StdAddress addr; 110 | ton::LogicalTime lt; 111 | ton::Bits256 hash; 112 | }; 113 | 114 | typedef struct TransactionDescr { 115 | TransactionDescr(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash) : addr(addr), lt(lt), hash(hash) { 116 | } 117 | TransactionDescr() { 118 | } 119 | TransactionDescr(const TransactionDescr& transDescr) : addr(transDescr.addr), lt(transDescr.lt), hash(transDescr.hash) { 120 | 121 | } 122 | ~TransactionDescr() { 123 | } 124 | block::StdAddress addr; 125 | ton::LogicalTime lt; 126 | ton::Bits256 hash; 127 | } TransactionDescr; 128 | 129 | struct TransactionList { 130 | ton::BlockIdExt block_id; 131 | std::vector vec; 132 | td::uint32 req_count_; 133 | }; 134 | struct CodeBlock { 135 | std::string data; 136 | }; 137 | struct ConfigParam { 138 | td::int32 idx; 139 | td::Ref root; 140 | }; 141 | struct ValidatorSet { 142 | td::int32 idx; 143 | td::Ref root; 144 | }; 145 | struct Error { 146 | td::Status error; 147 | }; 148 | 149 | template 150 | 151 | struct RawData { 152 | td::Ref root; 153 | T x; 154 | template 155 | RawData(td::Ref root, Args &&... args) : root(std::move(root)), x(std::forward(args)...) { 156 | } 157 | }; 158 | 159 | public: 160 | HttpAnswer(std::string title, std::string prefix) : title_(title), prefix_(prefix) { 161 | buf_ = td::BufferSlice{1 << 28}; 162 | sb_ = std::make_unique(buf_.as_slice()); 163 | jsonObject.setObject(); 164 | } 165 | 166 | HttpAnswer(std::string title, std::string prefix, bool code) : title_(title), code_(code), prefix_(prefix) { 167 | buf_ = td::BufferSlice{1 << 28}; 168 | sb_ = std::make_unique(buf_.as_slice()); 169 | jsonObject.setObject(); 170 | } 171 | 172 | void set_title(std::string title) { 173 | title_ = title; 174 | } 175 | void set_block_id(ton::BlockIdExt block_id) { 176 | block_id_ = block_id; 177 | workchain_id_ = block_id_.id.workchain; 178 | } 179 | void set_account_id(block::StdAddress addr) { 180 | account_id_ = addr; 181 | } 182 | void set_workchain(ton::WorkchainId workchain_id) { 183 | workchain_id_ = workchain_id; 184 | } 185 | 186 | void abort(td::Status error); 187 | void abort(std::string error); 188 | 189 | 190 | std::string finish(); // return json in txt(json) format 191 | std::string footer(); 192 | 193 | template 194 | void serializeObject(T x) { 195 | jsonObject.pushKV("answer", x); 196 | } 197 | 198 | td::StringBuilder &sb() { 199 | return *sb_; 200 | } 201 | 202 | void serializeObject(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash, UniValue &uvObj); 203 | 204 | UniValue serializeObject(Stack stack); 205 | UniValue serializeStackEntry(vm::StackEntry se); 206 | UniValue serializeCellSlice(vm::CellSlice cs); 207 | 208 | bool checkNeededType(vm::StackEntry se, std::string expectedType); 209 | 210 | UniValue serializeObject(AddressCell addr_c); 211 | unpackRC serializeObject(AddressCell addr_c, std::string &address); 212 | 213 | // UniValue serializeObject(MessageCell msg, int messageType); 214 | 215 | inline UniValue serializeBlockIdExt(ton::BlockIdExt block_id); 216 | void unpackBlockVectorForOneChain(td::vector block_id); 217 | 218 | UniValue serializeBlockId(ton::BlockId block_id); 219 | 220 | void serializeObject(TransactionCell trans_c); 221 | void serializeObject(TransactionSmall trans_c, UniValue &uvObj, 222 | std::shared_ptr transDescr = nullptr); 223 | TransactionDescr serializeTransactionSmall(TransactionSmall trans_c, UniValue &uvObj); 224 | 225 | void serializeObject(AccountCell acc_c); 226 | 227 | void serializeObject(BlockHeaderCell head_c); 228 | UniValue serializeBlockHeaderCellLite(BlockHeaderCell head_c); 229 | 230 | void serializeObject(BlockShardsCell shards_c); 231 | UniValue serializeBlockShardsCellLite(BlockShardsCell shards_c); 232 | void serializeObject(BlockShardsCellSmall shards_c); 233 | 234 | UniValue serializeObject(AccountLink account); 235 | 236 | UniValue serializeObject(MessageLink msg); 237 | 238 | UniValue serializeTransaction(TransactionLink trans); 239 | // void serializeObject(TransactionLink trans); 240 | 241 | UniValue serializeObject(BlockLink block); 242 | 243 | void serializeObject(Error error); 244 | 245 | void serializeObject(TransactionList trans); 246 | 247 | UniValue serializeObject(ValidatorSet conf); 248 | void unpackValidatorsTime(ValidatorSet conf, std::int64_t &from, std::int64_t &to); 249 | 250 | unpackRC unpackValidators(vm::CellSlice cs, UniValue &uvObj); 251 | unpackRC unpackValidatorsExt(vm::CellSlice cs, UniValue &uvObj); 252 | unpackRC unpackValidatorDescr(vm::CellSlice cs, UniValue &uvObj); 253 | unpackRC unpackValidatorRecord(vm::CellSlice cs, UniValue &uvObj); 254 | unpackRC unpackValidatorRecordAddr(vm::CellSlice cs, UniValue &uvObj); 255 | unpackRC unpackSigPubKey(vm::CellSlice cs, UniValue &uvObj); 256 | 257 | UniValue serializeObject(CodeBlock block) { 258 | UniValue uvObj; 259 | uvObj.setObject(); 260 | if(code_) { 261 | uvObj.pushKV("code", block.data); 262 | } 263 | return uvObj; 264 | } 265 | 266 | void serializeObject(ConfigParam conf); 267 | 268 | template 269 | UniValue serializeObject(RawData data) { 270 | std::ostringstream outp; 271 | data.x.print_ref(outp, data.root); 272 | auto dt = vm::load_cell_slice(data.root); 273 | return serializeObject(CodeBlock{outp.str()}); 274 | } 275 | 276 | bool serializeBlockData(td::Ref block_root_, ton::BlockIdExt block_id); 277 | 278 | void putInJson(std::string key, UniValue uvObj) { 279 | jsonObject.pushKV(key, uvObj); 280 | } 281 | 282 | void uvObj2Json(UniValue uvObj) { 283 | jsonObject = uvObj; 284 | } 285 | 286 | void putInJsonArray(UniValue uvObj) { 287 | if (jsonObject.isArray()) { 288 | jsonObject.push_back(uvObj); 289 | } 290 | } 291 | 292 | void changeJsonObject2Array() { 293 | jsonObject.setArray(); 294 | } 295 | 296 | private: 297 | enum TokenType { 298 | Jetton = 0, 299 | NFT = 1, 300 | Comment = 2, 301 | 302 | undef = 255, 303 | }; 304 | 305 | public: 306 | static std::map tokenTypeString; 307 | 308 | private: 309 | 310 | enum TransferType { 311 | InterWallet = 0, 312 | TransferNotify = 1, 313 | OwnershipAssigned = 2, 314 | 315 | NoType = 255, 316 | }; 317 | 318 | const std::map transferTypeString{ 319 | {TransferType::InterWallet, "internal_transfer"}, 320 | {TransferType::TransferNotify, "transfer_notification"}, 321 | {TransferType::OwnershipAssigned, "ownership_assigned"}}; 322 | 323 | ton::LogicalTime lt; 324 | ton::UnixTime now; 325 | 326 | std::string v1r1 = 327 | "B5EE9C72410101010044000084FF0020DDA4F260810200D71820D70B1FED44D0D31FD3FFD15112BAF2A122F901541044F910F2A2F80001D3" 328 | "1F3120D74A96D307D402FB00DED1A4C8CB1FCBFFC9ED5441FDF089"; 329 | std::string v1r2 = 330 | "B5EE9C724101010100530000A2FF0020DD2082014C97BA9730ED44D0D70B1FE0A4F260810200D71820D70B1FED44D0D31FD3FFD15112BAF2" 331 | "A122F901541044F910F2A2F80001D31F3120D74A96D307D402FB00DED1A4C8CB1FCBFFC9ED54D0E2786F"; 332 | std::string v1r3 = 333 | "B5EE9C7241010101005F0000BAFF0020DD2082014C97BA218201339CBAB19C71B0ED44D0D31FD70BFFE304E0A4F260810200D71820D70B1F" 334 | "ED44D0D31FD3FFD15112BAF2A122F901541044F910F2A2F80001D31F3120D74A96D307D402FB00DED1A4C8CB1FCBFFC9ED54B5B86E42"; 335 | std::string v2r1 = 336 | "B5EE9C724101010100570000AAFF0020DD2082014C97BA9730ED44D0D70B1FE0A4F2608308D71820D31FD31F01F823BBF263ED44D0D31FD3" 337 | "FFD15131BAF2A103F901541042F910F2A2F800029320D74A96D307D402FB00E8D1A4C8CB1FCBFFC9ED54A1370BB6"; 338 | std::string v2r2 = 339 | "B5EE9C724101010100630000C2FF0020DD2082014C97BA218201339CBAB19C71B0ED44D0D31FD70BFFE304E0A4F2608308D71820D31FD31F" 340 | "01F823BBF263ED44D0D31FD3FFD15131BAF2A103F901541042F910F2A2F800029320D74A96D307D402FB00E8D1A4C8CB1FCBFFC9ED54044C" 341 | "D7A1"; 342 | std::string v3r1 = 343 | "B5EE9C724101010100620000C0FF0020DD2082014C97BA9730ED44D0D70B1FE0A4F2608308D71820D31FD31FD31FF82313BBF263ED44D0D3" 344 | "1FD31FD3FFD15132BAF2A15144BAF2A204F901541055F910F2A3F8009320D74A96D307D402FB00E8D101A4C8CB1FCB1FCBFFC9ED543FBE6E" 345 | "E0"; 346 | std::string v3r2 = 347 | "B5EE9C724101010100710000DEFF0020DD2082014C97BA218201339CBAB19F71B0ED44D0D31FD31F31D70BFFE304E0A4F2608308D71820D3" 348 | "1FD31FD31FF82313BBF263ED44D0D31FD31FD3FFD15132BAF2A15144BAF2A204F901541055F910F2A3F8009320D74A96D307D402FB00E8D1" 349 | "01A4C8CB1FCB1FCBFFC9ED5410BD6DAD"; 350 | 351 | std::string v4r2 = 352 | "B5EE9C72410214010002D4000114FF00F4A413F4BCF2C80B010201200203020148040504F8F28308D71820D31FD31FD31F02F823BBF264ED" 353 | "44D0D31FD31FD3FFF404D15143BAF2A15151BAF2A205F901541064F910F2A3F80024A4C8CB1F5240CB1F5230CBFF5210F400C9ED54F80F01" 354 | "D30721C0009F6C519320D74A96D307D402FB00E830E021C001E30021C002E30001C0039130E30D03A4C8CB1F12CB1FCBFF1011121302E6D0" 355 | "01D0D3032171B0925F04E022D749C120925F04E002D31F218210706C7567BD22821064737472BDB0925F05E003FA403020FA4401C8CA07CB" 356 | "FFC9D0ED44D0810140D721F404305C810108F40A6FA131B3925F07E005D33FC8258210706C7567BA923830E30D03821064737472BA925F06" 357 | "E30D06070201200809007801FA00F40430F8276F2230500AA121BEF2E0508210706C7567831EB17080185004CB0526CF1658FA0219F400CB" 358 | "6917CB1F5260CB3F20C98040FB0006008A5004810108F45930ED44D0810140D720C801CF16F400C9ED540172B08E23821064737472831EB1" 359 | "7080185005CB055003CF1623FA0213CB6ACB1FCB3FC98040FB00925F03E20201200A0B0059BD242B6F6A2684080A06B90FA0218470D40808" 360 | "47A4937D29910CE6903E9FF9837812801B7810148987159F31840201580C0D0011B8C97ED44D0D70B1F8003DB29DFB513420405035C87D01" 361 | "0C00B23281F2FFF274006040423D029BE84C600201200E0F0019ADCE76A26840206B90EB85FFC00019AF1DF6A26840106B90EB858FC0006E" 362 | "D207FA00D4D422F90005C8CA0715CBFFC9D077748018C8CB05CB0222CF165005FA0214CB6B12CCCCC973FB00C84014810108F451F2A70200" 363 | "70810108D718FA00D33FC8542047810108F451F2A782106E6F746570748018C8CB05CB025006CF165004FA0214CB6A12CB1FCB3FC973FB00" 364 | "02006C810108D718FA00D33F305224810108F459F2A782106473747270748018C8CB05CB025005CF165003FA0213CB6ACB1F12CB3FC973FB" 365 | "00000AF400C9ED54696225E5"; 366 | 367 | std::string lock_up = 368 | "B5EE9C7241021E01000261000114FF00F4A413F4BCF2C80B010201200203020148040501F2F28308D71820D31FD31FD31F802403F823BB13" 369 | "F2F2F003802251A9BA1AF2F4802351B7BA1BF2F4801F0BF9015410C5F9101AF2F4F8005057F823F0065098F823F0062071289320D74A8E8B" 370 | "D30731D4511BDB3C12B001E8309229A0DF72FB02069320D74A96D307D402FB00E8D103A4476814154330F004ED541D0202CD060702012013" 371 | "1402012008090201200F100201200A0B002D5ED44D0D31FD31FD3FFD3FFF404FA00F404FA00F404D1803F7007434C0C05C6C2497C0F83E90" 372 | "0C0871C02497C0F80074C7C87040A497C1383C00D46D3C00608420BABE7114AC2F6C2497C338200A208420BABE7106EE86BCBD20084AE084" 373 | "0EE6B2802FBCBD01E0C235C62008087E4055040DBE4404BCBD34C7E00A60840DCEAA7D04EE84BCBD34C034C7CC0078C3C412040DD78CA00C" 374 | "0D0E00130875D27D2A1BE95B0C60000C1039480AF00500161037410AF0050810575056001010244300F004ED540201201112004548E1E228" 375 | "020F4966FA520933023BB9131E2209835FA00D113A14013926C21E2B3E6308003502323287C5F287C572FFC4F2FFFD00007E80BD00007E80" 376 | "BD00326000431448A814C4E0083D039BE865BE803444E800A44C38B21400FE809004E0083D10C06002012015160015BDE9F780188242F847" 377 | "800C02012017180201481B1C002DB5187E006D88868A82609E00C6207E00C63F04EDE20B30020158191A0017ADCE76A268699F98EB85FFC0" 378 | "0017AC78F6A268698F98EB858FC00011B325FB513435C2C7E00017B1D1BE08E0804230FB50F620002801D0D3030178B0925B7FE0FA4031FA" 379 | "403001F001A80EDAA4"; 380 | 381 | UniValue jsonObject; 382 | td::BigInt256 fees = td::BigInt256(0); 383 | 384 | UniValue block_id_link(ton::BlockIdExt block_id); 385 | 386 | enum transactionTypes { 387 | trans_ord, 388 | trans_storage, 389 | trans_tick_tock, 390 | trans_split_prepare, 391 | trans_split_install, 392 | trans_merge_prepare, 393 | trans_merge_install 394 | }; 395 | 396 | typedef struct StateInit { 397 | td::Ref code, data, library; 398 | std::string address; 399 | signed char split_depth; 400 | std::string walletType = ""; 401 | } StateInit; 402 | 403 | typedef struct TransferData { 404 | td::RefInt256 query_id = td::RefInt256{}; 405 | TokenType tokenType = TokenType::undef; 406 | std::string from = ""; 407 | std::string to = ""; 408 | td::RefInt256 amount = td::RefInt256{}; 409 | std::string token = ""; 410 | std::string transferType = ""; 411 | std::string comment = ""; 412 | bool isInit = false; 413 | 414 | UniValue serialize() { 415 | if(!isInit) { 416 | return UniValue::VNULL; 417 | } 418 | UniValue uvToken; 419 | uvToken.setObject(); 420 | uvToken.pushKV("type", HttpAnswer::tokenTypeString.find(tokenType)->second); 421 | if (query_id.not_null()) { 422 | uvToken.pushKV("query_id", query_id.write().to_dec_string()); 423 | } else { 424 | uvToken.pushKV("query_id", UniValue::VNULL); 425 | } 426 | uvToken.pushKV("transfer_type", transferType); 427 | uvToken.pushKV("from", from); 428 | uvToken.pushKV("to", to); 429 | if (amount.not_null()) { 430 | uvToken.pushKV("amount", amount.write().to_dec_string()); 431 | } else { 432 | uvToken.pushKV("amount", UniValue::VNULL); 433 | } 434 | uvToken.pushKV("comment", comment); 435 | 436 | uvToken.pushKV("token", token); 437 | return uvToken; 438 | } 439 | 440 | void init() { 441 | isInit = true; 442 | } 443 | 444 | } TransferData; 445 | 446 | std::string transactionStatus(char status); 447 | UniValue MonteCarloDescription(vm::Ref description); 448 | 449 | unpackRC unpackVarUInteger16(vm::CellSlice varUInteger16, td::RefInt256 &out); 450 | unpackRC unpackVarUInteger7(vm::CellSlice varUInteger7, td::RefInt256 &out); 451 | unpackRC unpackVarUInteger3(vm::CellSlice varUInteger3, td::RefInt256 &out); 452 | unpackRC unpackMaybeVarUInteger3(vm::CellSlice varUInteger3, td::RefInt256 &out); 453 | 454 | unpackRC unpackStorageUsedShort(vm::CellSlice storageUsedSHort, UniValue &uvObj); 455 | 456 | unpackRC unpackInt32(vm::CellSlice int32, td::RefInt256 &out); 457 | unpackRC unpackMaybeInt32(vm::CellSlice int32, td::RefInt256 &out); 458 | 459 | unpackRC unpackMaybeTrStoragePhase(vm::CellSlice storage_ph, UniValue &uvObj); 460 | unpackRC unpackTrStoragePhase(vm::CellSlice storage_ph, UniValue &uvObj); 461 | 462 | unpackRC unpackMaybeTrCreditPhase(vm::CellSlice credit_ph, UniValue &uvObj); 463 | unpackRC unpackTrCreditPhase(vm::CellSlice credit_ph, UniValue &uvObj); 464 | 465 | unpackRC unpackTrComputePhase(vm::CellSlice compute_ph, UniValue &uvObj); 466 | unpackRC MonteCarloTrComputePhase(vm::CellSlice compute_ph, UniValue &uvObj); 467 | 468 | unpackRC unpackTrActionPhase(vm::CellSlice action_ph, UniValue &uvObj); 469 | unpackRC unpackMaybeRefTrActionPhase(vm::CellSlice action_ph, UniValue &uvObj); 470 | 471 | unpackRC unpackSplitMergeInfo(vm::CellSlice split_info, UniValue &uvObj); 472 | 473 | bool unpackComputeSkipped(block::gen::TrComputePhase::Record_tr_phase_compute_skipped record_tr_phase_compute_skipped, 474 | UniValue &uvObj); 475 | unpackRC unpackComputeVM(block::gen::TrComputePhase::Record_tr_phase_compute_vm record_tr_phase_compute_vm, 476 | UniValue &uvObj); 477 | bool parseTrComputePhase_aux(block::gen::TrComputePhase_aux::Record record_TrComputePhase_aux, UniValue &uvObj); 478 | 479 | unpackRC unpackGrams(vm::CellSlice grams, block::CurrencyCollection &gramsCurColl); 480 | unpackRC unpackMaybeGrams(vm::CellSlice &grams, block::CurrencyCollection &gramsCurCol); 481 | 482 | bool unpackOrdinaryTrans(block::gen::TransactionDescr::Record_trans_ord description, UniValue &uvObj); 483 | bool unpackTickTockTrans(block::gen::TransactionDescr::Record_trans_tick_tock description, UniValue &uvObj); 484 | bool unpackMergePrepareTrans(block::gen::TransactionDescr::Record_trans_merge_prepare description, UniValue &uvObj); 485 | bool unpackMergeInstallTrans(block::gen::TransactionDescr::Record_trans_merge_install description, UniValue &uvObj); 486 | bool unpackSplitInstallTrans(block::gen::TransactionDescr::Record_trans_split_install description, UniValue &uvObj); 487 | bool unpackSplitPrepareTrans(block::gen::TransactionDescr::Record_trans_split_prepare description, UniValue &uvObj); 488 | bool unpackStorageTrans(block::gen::TransactionDescr::Record_trans_storage description, UniValue &uvObj); 489 | bool unpackTransaction(vm::CellSlice transaction, UniValue &uvObj); 490 | unpackRC unpackTransactionSmall(vm::CellSlice transaction, ton::BlockIdExt block_id, UniValue &uvObj, 491 | std::shared_ptr transDescr = nullptr); 492 | 493 | bool checkTransactionType(vm::Ref description, transactionTypes type, std::string ¤tType); 494 | std::string transactionType2String(transactionTypes type); 495 | 496 | unpackRC unpack_CurrencyCollection(block::CurrencyCollection collection, std::string &grams); 497 | unpackRC unpack_CurrencyCollection(block::CurrencyCollection collection, std::string &grams, td::BigInt256 &fees); 498 | unpackRC unpackBalance(td::Ref balance_ref, int64_t &balance_out); 499 | unpackRC unpackBalance(td::Ref balance_ref, int64_t &balance_out, td::BigInt256 &fees); 500 | unpackRC unpackMaybeBalance(vm::CellSlice &grams, int64_t &gramsCurCol); 501 | unpackRC unpackMessage(MessageCell msg, UniValue &uvObj, int messageType, bool small = true); 502 | 503 | unpackRC unpackMessageBodyJetton(vm::CellSlice body, TransferData &transferData); 504 | unpackRC unpackMessageBodyNFT(vm::CellSlice body, TransferData &transferData); 505 | unpackRC unpackMessageBodyComment(vm::CellSlice body, TransferData &transferData); 506 | std::string unpackMsgAddress(vm::CellSlice &body); 507 | 508 | 509 | unpackRC unpackMsgAddressExt(vm::CellSlice &body, UniValue& uvObj); 510 | unpackRC unpackMsgAddressExt(vm::CellSlice &body, std::string& addr); 511 | 512 | unpackRC unpackMsgAddressInt(vm::CellSlice &body, UniValue& uvObj); 513 | unpackRC unpackMsgAddressInt(vm::CellSlice &body, std::string& addr); 514 | 515 | 516 | unpackRC buildContractDataStruct(vm::CellSlice in_msg_state, StateInit &dataStateInit); 517 | unpackRC unpackStateInit(vm::CellSlice cs, std::string destination, td::Ref destination_cs, 518 | UniValue &uvObj, TransferData &transferData); 519 | unpackRC unpackStateInit(vm::CellSlice cs, td::Ref destination_cs, UniValue &uvObj); 520 | std::string walletType(std::string code); 521 | 522 | void serializeStateInit(StateInit dataStateInit, UniValue &uvObj); 523 | std::string printInString(char *data, size_t size); 524 | unpackRC processContractMethod(StateInit dataStateInit, td::Ref address, 525 | std::string method, td::Ref &stack); 526 | 527 | std::vector>>> methods = 528 | { 529 | {"get_wallet_data", {"jetton_wallet", {"balance", "owner", "jetton", "jetton_wallet_code"}}}, 530 | {"get_nft_data", {"nft", {"init", "index", "collection_address", "owner_address", "individual_content"}}}, 531 | {"get_collection_data", {"nft_collection", {"next_item_index", "collection_content", "owner_address"}}}, 532 | {"get_jetton_data", {"jetton_root", {"total_supply", "mintable", "admin_address", "jetton_content", "jetton_wallet_code"}}} 533 | }; 534 | 535 | const unsigned char Off_Chain_Content = 0x01; 536 | const unsigned char On_Chain_Content = 0x00; 537 | const unsigned char Snake_Format = 0x00; 538 | const unsigned char Chunked_Format = 0x01; 539 | 540 | typedef struct Metadata { 541 | std::vector data; 542 | std::vector meta{ 543 | "uri", "name", "description", "image", "image_data", "symbol", "decimals", "amount_style", "render_type", 544 | }; 545 | 546 | UniValue serialize() { 547 | UniValue uvObj; 548 | uvObj.setObject(); 549 | for (size_t i = 0; i < data.size() && i < meta.size(); i++) { 550 | uvObj.pushKV(meta[i], data[i]); 551 | } 552 | return uvObj; 553 | } 554 | 555 | } Metadata; 556 | 557 | unpackRC getContractData(StateInit &dataStateInit, td::Ref address, UniValue &uvObj, 558 | TransferData &transferData); 559 | unpackRC unpackContentData(vm::CellSlice content, UniValue &uvObj); 560 | unpackRC unpackContractData(vm::Stack stack, UniValue &uvObj, TransferData &transferData, std::pair> args); 561 | unpackRC unpackLongHashMap(vm::CellSlice address, std::string &outInfo, bool firstTime = true); 562 | 563 | ///////////////////////// OLEG's CODE //////////////// 564 | unpackRC unpackMessageBody(vm::CellSlice in_msg_raw, const int out_msg_count, vm::CellSlice out_msg_map, UniValue& uvObj); 565 | unpackRC unpackMsgBody_Elector(vm::CellSlice &in_msg, vm::CellSlice &out_msg, UniValue &uvObj); 566 | unpackRC tryExtractMsgBody(td::Ref msg_ref, vm::CellSlice &ret); 567 | unpackRC elector_processNewStake(vm::CellSlice &in_msg, td::BigInt256 ans_tag, UniValue &uvObj); 568 | 569 | std::string sender; 570 | std::string recipient; //maybe use the their internal format? 571 | td::BigInt256 gram_amount_in, gram_amount_out; 572 | std::string ELECTOR_CONST = "Ef8zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzM0vF"; 573 | unpackRC elector_recoverStake(td::BigInt256 ans_tag, UniValue& uvObj); 574 | ///////////////////////////////////////////////////////////////////////////////////////////////// 575 | 576 | std::string title_; 577 | ton::BlockIdExt block_id_; 578 | ton::WorkchainId workchain_id_ = ton::workchainInvalid; 579 | block::StdAddress account_id_; 580 | bool code_ = false; 581 | 582 | std::string prefix_; 583 | td::Status error_; 584 | 585 | std::unique_ptr sb_; 586 | td::BufferSlice buf_; 587 | }; 588 | 589 | template <> 590 | struct HttpAnswer::RawData { 591 | td::Ref root; 592 | RawData(td::Ref root) : root(std::move(root)) { 593 | } 594 | }; 595 | 596 | template <> 597 | inline UniValue HttpAnswer::serializeObject(RawData data) { 598 | std::ostringstream outp; 599 | vm::load_cell_slice(data.root).print_rec(outp); 600 | return serializeObject(CodeBlock{outp.str()}); 601 | } -------------------------------------------------------------------------------- /blockchain-api-query.cpp: -------------------------------------------------------------------------------- 1 | #include "blockchain-api-query.hpp" 2 | #include 3 | 4 | // namespace { 5 | 6 | td::Ref prepare_vm_c7(ton::UnixTime now, ton::LogicalTime lt, td::Ref my_addr, 7 | const block::CurrencyCollection &balance) { 8 | td::BitArray<256> rand_seed; 9 | td::RefInt256 rand_seed_int{true}; 10 | td::Random::secure_bytes(rand_seed.as_slice()); 11 | if (!rand_seed_int.unique_write().import_bits(rand_seed.cbits(), 256, false)) { 12 | return {}; 13 | } 14 | auto tuple = vm::make_tuple_ref(td::make_refint(0x076ef1ea), // [ magic:0x076ef1ea 15 | td::make_refint(0), // actions:Integer 16 | td::make_refint(0), // msgs_sent:Integer 17 | td::make_refint(now), // unixtime:Integer 18 | td::make_refint(lt), // block_lt:Integer 19 | td::make_refint(lt), // trans_lt:Integer 20 | std::move(rand_seed_int), // rand_seed:Integer 21 | balance.as_vm_tuple(), // balance_remaining:[Integer (Maybe Cell)] 22 | my_addr, // myself:MsgAddressInt 23 | vm::StackEntry()); // global_config:(Maybe Cell) ] = SmartContractInfo; 24 | LOG(DEBUG) << "SmartContractInfo initialized with " << vm::StackEntry(tuple).to_string(); 25 | return vm::make_tuple_ref(std::move(tuple)); 26 | } 27 | 28 | // } // namespace 29 | 30 | td::Result parse_block_id(std::map &opts, bool allow_empty) { 31 | if (allow_empty) { 32 | if (opts.count("workchain") == 0 && opts.count("shard") == 0 && opts.count("seqno") == 0) { 33 | return ton::BlockIdExt{}; 34 | } 35 | } 36 | try { 37 | ton::BlockIdExt block_id; 38 | auto it = opts.find("workchain"); 39 | if (it == opts.end()) { 40 | return td::Status::Error(ton::ErrorCode::protoviolation, "workchain not set"); 41 | } 42 | block_id.id.workchain = std::stoi(it->second); 43 | it = opts.find("shard"); 44 | if (it == opts.end()) { 45 | return td::Status::Error(ton::ErrorCode::protoviolation, "shard not set"); 46 | } 47 | block_id.id.shard = std::stoull(it->second, nullptr, 16); 48 | it = opts.find("seqno"); 49 | if (it == opts.end()) { 50 | return td::Status::Error(ton::ErrorCode::protoviolation, "seqno not set"); 51 | } 52 | auto s = std::stoull(it->second); 53 | auto seqno = static_cast(s); 54 | if (s != seqno) { 55 | return td::Status::Error(ton::ErrorCode::protoviolation, "seqno too big"); 56 | } 57 | block_id.id.seqno = seqno; 58 | it = opts.find("roothash"); 59 | if (it == opts.end()) { 60 | return td::Status::Error(ton::ErrorCode::protoviolation, "roothash not set"); 61 | } 62 | if (it->second.length() != 64) { 63 | return td::Status::Error(ton::ErrorCode::protoviolation, "roothash bad length"); 64 | } 65 | auto R = td::hex_decode(td::Slice(it->second)); 66 | if (R.is_error()) { 67 | return td::Status::Error(ton::ErrorCode::protoviolation, "roothash bad hex"); 68 | } 69 | block_id.root_hash.as_slice().copy_from(td::as_slice(R.move_as_ok())); 70 | it = opts.find("filehash"); 71 | if (it == opts.end()) { 72 | return td::Status::Error(ton::ErrorCode::protoviolation, "filehash not set"); 73 | } 74 | if (it->second.length() != 64) { 75 | return td::Status::Error(ton::ErrorCode::protoviolation, "filehash bad length"); 76 | } 77 | R = td::hex_decode(td::Slice(it->second)); 78 | if (R.is_error()) { 79 | return td::Status::Error(ton::ErrorCode::protoviolation, "filehash bad hex"); 80 | } 81 | block_id.file_hash.as_slice().copy_from(td::as_slice(R.move_as_ok())); 82 | return block_id; 83 | } catch (...) { 84 | return td::Status::Error(ton::ErrorCode::protoviolation, "cannot parse int"); 85 | } 86 | } 87 | 88 | td::Result parse_account_prefix(std::map &opts, bool allow_empty) { 89 | if (allow_empty) { 90 | if (opts.count("workchain") == 0 && opts.count("shard") == 0 && opts.count("account") == 0) { 91 | return ton::AccountIdPrefixFull{ton::masterchainId, 0}; 92 | } 93 | } 94 | try { 95 | ton::AccountIdPrefixFull account_id; 96 | auto it = opts.find("workchain"); 97 | if (it == opts.end()) { 98 | return td::Status::Error(ton::ErrorCode::protoviolation, "workchain not set"); 99 | } 100 | account_id.workchain = std::stoi(it->second); 101 | it = opts.find("shard"); 102 | if (it == opts.end()) { 103 | it = opts.find("account"); 104 | if (it == opts.end()) { 105 | return td::Status::Error(ton::ErrorCode::protoviolation, "shard/account not set"); 106 | } 107 | } 108 | account_id.account_id_prefix = std::stoull(it->second, nullptr, 16); 109 | return account_id; 110 | } catch (...) { 111 | return td::Status::Error(ton::ErrorCode::protoviolation, "cannot parse int"); 112 | } 113 | } 114 | 115 | td::Result parse_account_addr(std::map &opts) { 116 | auto it = opts.find("account"); 117 | if (it == opts.end()) { 118 | return td::Status::Error(ton::ErrorCode::error, "no account id"); 119 | } 120 | std::string acc_string = it->second; 121 | block::StdAddress a; 122 | if (a.parse_addr(td::Slice(acc_string))) { 123 | return a; 124 | } 125 | ton::WorkchainId workchain_id; 126 | it = opts.find("accountworkchain"); 127 | if (it == opts.end()) { 128 | it = opts.find("workchain"); 129 | if (it == opts.end()) { 130 | return td::Status::Error(ton::ErrorCode::error, "no account workchain id"); 131 | } 132 | } 133 | try { 134 | workchain_id = std::stoi(it->second); 135 | } catch (...) { 136 | return td::Status::Error(ton::ErrorCode::error, "bad account workchain id"); 137 | } 138 | if (acc_string.size() == 64) { 139 | TRY_RESULT(R, td::hex_decode(acc_string)); 140 | a.addr.as_slice().copy_from(td::Slice(R)); 141 | a.workchain = workchain_id; 142 | return a; 143 | } 144 | return td::Status::Error(ton::ErrorCode::error, "bad account id"); 145 | } 146 | 147 | void HttpQueryCommon::abort_query(td::Status error) { 148 | if (promise_) { 149 | HttpAnswer A{"error", prefix_}; 150 | A.abort(std::move(error)); 151 | auto page = A.finish(); 152 | auto R = MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY); 153 | MHD_add_response_header(R, "Content-Type", "application/json"); 154 | promise_.set_value(std::move(R)); 155 | } 156 | stop(); 157 | } 158 | 159 | void HttpQueryCommon::got_shard_info(td::BufferSlice result) { 160 | auto F = ton::fetch_tl_object(std::move(result), true); 161 | if (F.is_error()) { 162 | abort_query(F.move_as_error()); 163 | return; 164 | } 165 | shard_data_ = std::move(F.move_as_ok()->data_); 166 | 167 | if (!--pending_queries_) { 168 | finish_query(); 169 | } 170 | } 171 | 172 | #define CHECKIFERROR(F, ELSE) \ 173 | if (F.is_error()) { \ 174 | abort_query(F.move_as_error()); \ 175 | } else \ 176 | ELSE 177 | 178 | void HttpQueryCommon::got_block_header(td::BufferSlice data) { 179 | if (std::string(typeid(*this).name()).find("HttpQueryBlockSearchHash") != std::string::npos) { 180 | auto F = ton::fetch_tl_object(std::move(data), true); 181 | CHECKIFERROR(F, { 182 | auto f = F.move_as_ok(); 183 | data_ = std::move(f->header_proof_); 184 | block_id_ = ton::create_block_id(f->id_); 185 | }) 186 | } 187 | if (std::string(typeid(*this).name()).find("HttpQueryBlockInfo") != std::string::npos ) { 188 | auto F = ton::fetch_tl_object(std::move(data), true); 189 | CHECKIFERROR(F, { 190 | auto f = F.move_as_ok(); 191 | data_ = std::move(f->data_); 192 | }) 193 | } 194 | 195 | if (!--pending_queries_) { 196 | finish_query(); 197 | } 198 | } 199 | 200 | void HttpQueryCommon::next(td::Status error) { 201 | pending_queries_ -= 1; 202 | if (pending_queries_ == 0) { 203 | finish_query(); 204 | } 205 | } 206 | 207 | void HttpQueryCommon::failed_to_get_shard_info(td::Status error) { 208 | error_ = std::move(error); 209 | if (!--pending_queries_) { 210 | finish_query(); 211 | } 212 | } 213 | 214 | -------------------------------------------------------------------------------- /blockchain-api-query.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "td/actor/actor.h" 4 | #include "td/utils/crypto.h" 5 | 6 | #include "ton/ton-types.h" 7 | #include "ton/ton-tl.hpp" 8 | #include "ton/lite-tl.hpp" 9 | 10 | #include "block/block.h" 11 | #include "block/block-auto.h" 12 | #include "block/mc-config.h" 13 | 14 | #include "blockchain-api.hpp" 15 | #include "blockchain-api-http.hpp" 16 | 17 | #include 18 | 19 | #include 20 | 21 | #include // fixed precison integers 22 | #include // std::accumulate 23 | #include // std::count 24 | 25 | #include 26 | #include "univalue.h" 27 | 28 | #include "crypto/block/check-proof.h" 29 | #include "crypto/vm/utils.h" 30 | 31 | #include "auto/tl/lite_api.h" 32 | 33 | #include "tl-utils/tl-utils.hpp" 34 | #include "tl-utils/lite-utils.hpp" 35 | 36 | #include "tl/tl_json.h" 37 | 38 | #include "tdutils/td/utils/JsonBuilder.h" 39 | 40 | #include "common/errorcode.h" 41 | 42 | #include "vm/boc.h" 43 | #include "vm/cellops.h" 44 | #include "vm/cells/MerkleProof.h" 45 | #include "vm/vm.h" 46 | #include "vm/cp0.h" 47 | 48 | td::Ref prepare_vm_c7(ton::UnixTime now, ton::LogicalTime lt, td::Ref my_addr, 49 | const block::CurrencyCollection &balance); 50 | td::Result parse_block_id(std::map &opts, bool allow_empty = false); 51 | td::Result parse_account_addr(std::map &opts); 52 | td::Result parse_account_prefix(std::map &opts, bool allow_empty); 53 | 54 | class HttpAnswer; 55 | 56 | typedef struct todoS { 57 | uint64_t pos; 58 | uint32_t depth; 59 | std::string sofar; 60 | } todoS; 61 | 62 | struct BlockData { 63 | BlockData(ton::BlockIdExt block_id, td::BufferSlice data, td::BufferSlice shard_data, 64 | std::vector transactionsInfo) 65 | : block_id_(std::move(block_id)) 66 | , data_(std::move(data)) 67 | , shard_data_(std::move(shard_data)) 68 | , transactionsInfo(std::move(transactionsInfo)) { 69 | } 70 | ton::BlockIdExt block_id_; 71 | td::BufferSlice data_; 72 | td::BufferSlice shard_data_; 73 | std::vector transactionsInfo; 74 | }; 75 | 76 | struct TransactionDescr { 77 | TransactionDescr(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash) 78 | : addr(addr), lt(lt), hash(hash) { 79 | } 80 | block::StdAddress addr; 81 | ton::LogicalTime lt; 82 | ton::Bits256 hash; 83 | }; 84 | 85 | class SearchBlockRunner { 86 | public: 87 | SearchBlockRunner(std::function>)> func, 88 | std::shared_ptr scheduler_ptr3) { 89 | auto P = td::PromiseCreator::lambda([Self = this](td::Result> R) { 90 | if (R.is_ok()) { 91 | Self->finish(R.move_as_ok()); 92 | } else { 93 | Self->finish(nullptr); 94 | } 95 | }); 96 | mutex_.lock(); 97 | scheduler_ptr3->run_in_context_external([&]() { func(std::move(P)); }); 98 | } 99 | 100 | void finish(std::unique_ptr response) { 101 | response_ = std::move(response); 102 | mutex_.unlock(); 103 | } 104 | std::unique_ptr wait() { 105 | mutex_.lock(); 106 | mutex_.unlock(); 107 | return std::move(response_); 108 | } 109 | 110 | private: 111 | std::unique_ptr response_; 112 | std::mutex mutex_; 113 | }; 114 | 115 | class HttpQueryCommon : public td::actor::Actor { 116 | public: 117 | HttpQueryCommon(std::string prefix, td::Promise promise) 118 | : prefix_(std::move(prefix)), promise_(std::move(promise)) { 119 | } 120 | HttpQueryCommon(std::string prefix, td::Promise promise, ton::BlockIdExt block_id) 121 | : block_id_(std::move(block_id)), prefix_(std::move(prefix)), promise_(std::move(promise)) { 122 | } 123 | HttpQueryCommon(std::string prefix, td::Promise> promise) 124 | : prefix_(std::move(prefix)), promise_data_(std::move(promise)) { 125 | } 126 | void start_up() override { 127 | if (error_.is_error()) { 128 | abort_query(std::move(error_)); 129 | return; 130 | } 131 | start_up_query(); 132 | } 133 | virtual void start_up_query() { 134 | UNREACHABLE(); 135 | } 136 | virtual void finish_query() { 137 | UNREACHABLE(); 138 | } 139 | 140 | virtual void abort_query(td::Status error); 141 | void create_header(HttpAnswer &ans) { 142 | } 143 | 144 | #define SELETLASTBLOCKDB \ 145 | std::string( \ 146 | "select seqno, shard_id, encode(filehash, 'hex') as filehash, encode(roothash, 'hex') as roothash from " \ 147 | "ton_block " \ 148 | "tb where seqno = (select max(seqno) from ton_block tb where tb.workchain = -1) and workchain = -1;") \ 149 | .c_str() 150 | 151 | protected: 152 | td::int32 pending_queries_ = 0; 153 | td::Status error_; 154 | td::BufferSlice data_; 155 | ton::BlockIdExt block_id_; 156 | ton::BlockIdExt res_block_id_; 157 | 158 | std::shared_ptr conn; 159 | std::mutex *mtx; 160 | 161 | td::BufferSlice shard_data_; 162 | void got_shard_info(td::BufferSlice result); 163 | void failed_to_get_shard_info(td::Status error); 164 | virtual void got_block_header(td::BufferSlice result); 165 | void next(td::Status error); 166 | 167 | std::string prefix_; 168 | td::Promise promise_; 169 | td::Promise> promise_data_; 170 | 171 | void getFromDB() { 172 | if (conn != nullptr) { 173 | mtx->lock(); 174 | std::unique_ptr resSQL(PQexec(conn.get(), SELETLASTBLOCKDB), &PQclear); 175 | mtx->unlock(); 176 | if (PQresultStatus(resSQL.get()) != PGRES_TUPLES_OK) { 177 | error_ = td::Status::Error("Select failed: " + std::string(PQresultErrorMessage(resSQL.get()))); 178 | resSQL.reset(); 179 | finish_query(); 180 | } else { 181 | auto x = ton::FileHash(); 182 | auto y = ton::RootHash(); 183 | res_block_id_.id.seqno = static_cast(std::stoul(PQgetvalue(resSQL.get(), 0, 0))); 184 | res_block_id_.id.shard = std::stoll(PQgetvalue(resSQL.get(), 0, 1)); 185 | x.from_hex(td::Slice(PQgetvalue(resSQL.get(), 0, 2))); 186 | res_block_id_.file_hash = x; 187 | y.from_hex(td::Slice(PQgetvalue(resSQL.get(), 0, 3))); 188 | res_block_id_.root_hash = y; 189 | res_block_id_.id.workchain = -1; 190 | } 191 | } else { 192 | error_ = td::Status::Error(404, PSTRING() << "unnable to connect to db"); 193 | finish_query(); 194 | } 195 | } 196 | }; 197 | 198 | class HttpQueryBlockInfo : public HttpQueryCommon { 199 | public: 200 | HttpQueryBlockInfo(ton::BlockIdExt block_id, std::string prefix, td::Promise promise); 201 | HttpQueryBlockInfo(std::map opts, std::string prefix, td::Promise promise); 202 | HttpQueryBlockInfo(std::map opts, std::string prefix, td::Promise promise, 203 | DatabaseConfigParams *conn = nullptr); 204 | 205 | void finish_query() override; 206 | 207 | void start_up_query() override; 208 | void got_transactions(td::BufferSlice result); 209 | void got_full_transaction(td::BufferSlice result); 210 | void got_config(td::BufferSlice result); 211 | 212 | void start_transaction_query(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash); 213 | 214 | private: 215 | 216 | std::vector transactionsInfo; 217 | td::BufferSlice shard_data_; 218 | 219 | 220 | std::vector transactions_; 221 | std::vector transactionsNotPassed; 222 | td::uint32 trans_req_count_; 223 | 224 | td::BufferSlice state_proof_; 225 | td::BufferSlice config_proof_; 226 | 227 | bool unpackValidators(HttpAnswer &A, std::shared_ptr conn); 228 | }; 229 | 230 | class HttpQueryBlockSearch2 : public HttpQueryCommon { 231 | public: 232 | HttpQueryBlockSearch2(std::map opts, std::string prefix, 233 | td::Promise promise, std::shared_ptr scheduler_ptr); 234 | 235 | void finish_query() override; 236 | void start_up_query() override; 237 | 238 | private: 239 | ton::AccountIdPrefixFull account_prefix_; 240 | 241 | ton::BlockSeqno seqnoFrom = 0; 242 | ton::BlockSeqno seqnoTo = 0; 243 | 244 | std::vector> dataTo; 245 | std::shared_ptr scheduler_ptr2; 246 | 247 | ton::BlockSeqno start; 248 | td::uint32 trans_req_count_; 249 | }; 250 | 251 | class HttpQueryBlockSearchHash : public HttpQueryCommon { 252 | public: 253 | HttpQueryBlockSearchHash(std::map opts, std::string prefix, 254 | td::Promise promise); 255 | 256 | void finish_query() override; 257 | 258 | void start_up_query() override; 259 | 260 | private: 261 | ton::AccountIdPrefixFull account_prefix_; 262 | td::uint32 mode_ = 0; 263 | ton::BlockSeqno seqno_ = 0; 264 | }; 265 | 266 | class HttpQuerySearchByHeight : public HttpQueryCommon { 267 | public: 268 | HttpQuerySearchByHeight(std::map opts, std::string prefix, 269 | td::Promise promise, DatabaseConfigParams *conn); 270 | 271 | void finish_query() override; 272 | 273 | void find_shards(); 274 | void start_up_query() override; 275 | 276 | private: 277 | ton::AccountIdPrefixFull account_prefix_; 278 | td::uint32 mode_ = 0; 279 | ton::BlockSeqno seqno_ = 0; 280 | std::vector data_; 281 | std::vector block_id_; 282 | 283 | ton::LogicalTime lt_ = 0; 284 | ton::UnixTime utime_ = 0; 285 | 286 | std::vector shards; 287 | 288 | }; 289 | 290 | std::vector buildMemo(const std::string &S); 291 | uint64_t findChildren(std::string &S, uint64_t pos, uint32_t depth, std::vector &memo); 292 | void unpackNode(std::string &S, uint64_t pos, uint32_t depth, std::vector &memo, std::string sofar, 293 | std::vector &todo, std::vector &ret); 294 | std::string bits2hex(const char *bit_str); 295 | std::vector unpackShards(std::string shardstate); 296 | std::string hex2bits(const char *hex_str); 297 | std::string rtrim(const std::string &s); 298 | std::string pad2flush(const std::string &bit_str, size_t target); 299 | 300 | class HttpQueryViewAccount : public HttpQueryCommon { 301 | public: 302 | HttpQueryViewAccount(ton::BlockIdExt block_id, block::StdAddress addr, std::string prefix, 303 | td::Promise promise); 304 | HttpQueryViewAccount(std::map opts, std::string prefix, td::Promise promise, 305 | bool fromDB = false, DatabaseConfigParams *dbConfParams = nullptr); 306 | 307 | void finish_query() override; 308 | 309 | void start_up_query() override; 310 | void got_account(td::BufferSlice result); 311 | 312 | private: 313 | bool fromDB = false; 314 | block::StdAddress addr_; 315 | td::BufferSlice proof_; 316 | }; 317 | 318 | class HttpQueryViewTransaction : public HttpQueryCommon { 319 | public: 320 | HttpQueryViewTransaction(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash, std::string prefix, 321 | td::Promise promise); 322 | HttpQueryViewTransaction(std::map opts, std::string prefix, 323 | td::Promise promise, bool fromDB = false, 324 | DatabaseConfigParams *dbConfParams = nullptr); 325 | 326 | void finish_query() override; 327 | 328 | void start_up_query() override; 329 | void got_transaction(td::BufferSlice result); 330 | 331 | private: 332 | void readTransactionsFromDB(); 333 | void unpackFromDB(std::string data, ton::BlockIdExt res_block_id_); 334 | void unpackRestart(); 335 | 336 | bool fromDB = false; 337 | block::StdAddress addr_; 338 | ton::LogicalTime lt_; 339 | ton::Bits256 hash_; 340 | bool lastCountTry = false; 341 | 342 | bool code_ = false; 343 | td::int32 count = 10; 344 | UniValue uvArr; 345 | bool firstTryToGetTransactions = true; 346 | }; 347 | 348 | class HttpQueryViewTransaction2 : public HttpQueryCommon { 349 | public: 350 | HttpQueryViewTransaction2(ton::BlockIdExt block_id, block::StdAddress addr, ton::LogicalTime lt, std::string prefix, 351 | td::Promise promise); 352 | HttpQueryViewTransaction2(std::map opts, std::string prefix, 353 | td::Promise promise, bool fromDB = false, DatabaseConfigParams *dbConfParams = nullptr); 354 | 355 | void finish_query() override; 356 | 357 | void start_up_query() override; 358 | void got_transaction(td::BufferSlice result); 359 | 360 | private: 361 | void readFromFile(td::BufferSlice &data); 362 | bool fromDB = false; 363 | block::StdAddress addr_; 364 | ton::LogicalTime lt_; 365 | ton::Bits256 hash_; 366 | 367 | }; 368 | 369 | class HttpQueryViewLastBlock : public HttpQueryCommon { 370 | public: 371 | HttpQueryViewLastBlock(std::string prefix, td::Promise promise); 372 | HttpQueryViewLastBlock(std::map opts, std::string prefix, 373 | td::Promise promise); 374 | 375 | void finish_query() override; 376 | 377 | void start_up() override; 378 | void got_result(td::BufferSlice result); 379 | 380 | }; 381 | 382 | class HttpQueryViewLastBlockNumber : public HttpQueryCommon { 383 | public: 384 | HttpQueryViewLastBlockNumber(std::string prefix, td::Promise promise); 385 | HttpQueryViewLastBlockNumber(std::map opts, std::string prefix, 386 | td::Promise promise, bool fromDB = false, 387 | DatabaseConfigParams *dbConfParams = nullptr); 388 | 389 | void finish_query() override; 390 | 391 | void start_up() override; 392 | void get_shards(); 393 | void got_result(td::BufferSlice result); 394 | 395 | private: 396 | td::int32 workchain_; 397 | bool fromDb = false; 398 | }; 399 | 400 | class HttpQueryConfig : public HttpQueryCommon { 401 | public: 402 | HttpQueryConfig(std::string prefix, ton::BlockIdExt block_id, std::vector params, 403 | td::Promise promise); 404 | HttpQueryConfig(std::map opts, std::string prefix, td::Promise promise); 405 | 406 | void finish_query() override; 407 | 408 | void start_up() override; 409 | void got_block(td::BufferSlice result); 410 | void send_main_query(); 411 | void got_result(td::BufferSlice result); 412 | 413 | private: 414 | std::vector params_ = {32, 34, 36}; 415 | 416 | td::BufferSlice state_proof_; 417 | td::BufferSlice config_proof_; 418 | 419 | bool code_ = true; 420 | }; 421 | 422 | class HttpQueryValidators : public HttpQueryCommon { 423 | public: 424 | HttpQueryValidators(std::string prefix, ton::BlockIdExt block_id, td::Promise promise); 425 | HttpQueryValidators(std::map opts, std::string prefix, td::Promise promise); 426 | 427 | void finish_query() override; 428 | 429 | void start_up() override; 430 | void got_block(td::BufferSlice result); 431 | void send_main_query(); 432 | void got_result(td::BufferSlice result); 433 | 434 | private: 435 | std::vector params_ = {32, 34}; 436 | 437 | td::BufferSlice state_proof_; 438 | td::BufferSlice config_proof_; 439 | 440 | bool code_ = true; 441 | }; 442 | 443 | class HttpQueryRunMethod : public HttpQueryCommon { 444 | public: 445 | HttpQueryRunMethod(ton::BlockIdExt block_id, block::StdAddress addr, std::string method_name, 446 | std::vector params, std::string prefix, td::Promise promise); 447 | HttpQueryRunMethod(std::map opts, std::string prefix, td::Promise promise); 448 | 449 | void finish_query() override; 450 | 451 | void start_up_query() override; 452 | void got_account(td::BufferSlice result); 453 | 454 | private: 455 | block::StdAddress addr_; 456 | 457 | std::string method_name_; 458 | std::vector params_; 459 | 460 | td::BufferSlice proof_; 461 | td::BufferSlice shard_proof_; 462 | 463 | bool code_ = true; 464 | }; 465 | 466 | class BlockSearch : public HttpQueryCommon { 467 | public: 468 | BlockSearch(ton::WorkchainId workchain, ton::AccountIdPrefix account, ton::BlockSeqno seqno, std::string prefix, 469 | td::Promise promise); 470 | BlockSearch(ton::WorkchainId workchain, ton::AccountIdPrefix account, ton::LogicalTime lt, std::string prefix, 471 | td::Promise promise); 472 | BlockSearch(ton::WorkchainId workchain, ton::AccountIdPrefix account, bool dummy, ton::UnixTime utime, 473 | std::string prefix, td::Promise promise); 474 | 475 | BlockSearch(std::map opts, std::string prefix, td::Promise promise); 476 | 477 | BlockSearch(std::map opts, std::string prefix, td::Promise promise, 478 | bool returnData); 479 | 480 | BlockSearch(ton::WorkchainId wc, ton::ShardId shard, ton::BlockSeqno seqno_, std::string prefix, 481 | td::Promise> promise) 482 | : HttpQueryCommon(std::move(prefix), std::move(promise)), account_prefix_{wc, shard}, seqno_(seqno_) { 483 | } 484 | 485 | void start_up_query() override; 486 | void got_block_header(td::BufferSlice result) override; 487 | 488 | void got_transactions(td::BufferSlice result); 489 | void got_full_transaction(td::BufferSlice result); 490 | 491 | void start_transaction_query(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash); 492 | 493 | protected: 494 | ton::AccountIdPrefixFull account_prefix_; 495 | td::uint32 mode_ = 0; 496 | ton::BlockSeqno seqno_ = 0; 497 | ton::LogicalTime lt_ = 0; 498 | ton::UnixTime utime_ = 0; 499 | 500 | std::vector transactions_; 501 | td::uint32 trans_req_count_; 502 | std::vector transactionsInfo; 503 | 504 | bool returnData = false; 505 | }; 506 | 507 | class HttpQueryBlockSearch : public BlockSearch { 508 | 509 | public: 510 | HttpQueryBlockSearch(std::map opts, std::string prefix, td::Promise promise): 511 | BlockSearch(std::move(opts), std::move(prefix), std::move(promise)) { } 512 | 513 | void finish_query() override; 514 | 515 | }; 516 | 517 | class HttpQueryBlockSearchNoMHD : public BlockSearch { 518 | public: 519 | HttpQueryBlockSearchNoMHD(ton::WorkchainId wc, ton::ShardId shard, ton::BlockSeqno seqno_, std::string prefix, 520 | td::Promise> promise) 521 | : BlockSearch(std::move(wc), std::move(shard), std::move(seqno_), std::move(prefix), std::move(promise)) { 522 | mode_ = 1; 523 | } 524 | 525 | void finish_query() override { 526 | if (promise_data_) { 527 | std::unique_ptr bd2(new BlockData(std::move(block_id_), std::move(data_), std::move(shard_data_), std::move(transactionsInfo))); 528 | promise_data_.set_value(std::move(bd2)); 529 | } 530 | stop(); 531 | } 532 | }; 533 | -------------------------------------------------------------------------------- /blockchain-api.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "adnl/adnl-ext-client.h" 3 | #include "adnl/utils.hpp" 4 | #include "auto/tl/ton_api_json.h" 5 | #include "td/utils/OptionParser.h" 6 | #include "td/utils/Time.h" 7 | #include "td/utils/filesystem.h" 8 | #include "td/utils/format.h" 9 | #include "td/utils/Random.h" 10 | #include "td/utils/crypto.h" 11 | #include "td/utils/port/signals.h" 12 | #include "td/utils/port/user.h" 13 | #include "td/utils/port/FileFd.h" 14 | #include "ton/ton-tl.hpp" 15 | #include "block/block-db.h" 16 | #include "block/block.h" 17 | #include "block/block-auto.h" 18 | #include "vm/boc.h" 19 | #include "vm/cellops.h" 20 | #include "vm/cells/MerkleProof.h" 21 | #include "block/mc-config.h" 22 | #include "blockchain-api.hpp" 23 | #include "blockchain-api-http.hpp" 24 | #include "blockchain-api-query.hpp" 25 | 26 | #include "vm/boc.h" 27 | #include "vm/cellops.h" 28 | #include "vm/cells/MerkleProof.h" 29 | #include "vm/cp0.h" 30 | 31 | #include "auto/tl/lite_api.h" 32 | #include "ton/lite-tl.hpp" 33 | #include "tl-utils/lite-utils.hpp" 34 | 35 | #include 36 | 37 | #if TD_DARWIN || TD_LINUX 38 | #include 39 | #include 40 | #endif 41 | #include 42 | #include 43 | 44 | int verbosity; 45 | 46 | std::shared_ptr scheduler_ptr; 47 | 48 | class HttpQueryRunner { 49 | public: 50 | HttpQueryRunner(std::function)> func) { 51 | auto P = td::PromiseCreator::lambda([Self = this](td::Result R) { 52 | if (R.is_ok()) { 53 | Self->finish(R.move_as_ok()); 54 | } else { 55 | Self->finish(nullptr); 56 | } 57 | }); 58 | mutex_.lock(); 59 | scheduler_ptr->run_in_context_external([&]() { func(std::move(P)); }); 60 | } 61 | void finish(MHD_Response* response) { 62 | response_ = response; 63 | mutex_.unlock(); 64 | } 65 | MHD_Response* wait() { 66 | mutex_.lock(); 67 | mutex_.unlock(); 68 | return response_; 69 | } 70 | 71 | private: 72 | std::function)> func_; 73 | MHD_Response* response_; 74 | std::mutex mutex_; 75 | }; 76 | 77 | static std::string urldecode(td::Slice from, bool decode_plus_sign_as_space) { 78 | size_t to_i = 0; 79 | 80 | td::BufferSlice x{from.size()}; 81 | auto to = x.as_slice(); 82 | 83 | for (size_t from_i = 0, n = from.size(); from_i < n; from_i++) { 84 | if (from[from_i] == '%' && from_i + 2 < n) { 85 | int high = td::hex_to_int(from[from_i + 1]); 86 | int low = td::hex_to_int(from[from_i + 2]); 87 | if (high < 16 && low < 16) { 88 | to[to_i++] = static_cast(high * 16 + low); 89 | from_i += 2; 90 | continue; 91 | } 92 | } 93 | to[to_i++] = decode_plus_sign_as_space && from[from_i] == '+' ? ' ' : from[from_i]; 94 | } 95 | 96 | return to.truncate(to_i).str(); 97 | } 98 | 99 | class CoreActor : public CoreActorInterface { 100 | private: 101 | std::string global_config_ = "ton-global.config"; 102 | std::mutex databaseMT; 103 | const std::string getIndexes = 104 | "create index if not exists block_index on ton_block (seqno, workchain);" 105 | "create index if not exists lt_index on ton_transaction(logical_time desc);"; 106 | 107 | std::vector> clients_; 108 | 109 | td::uint32 http_port_ = 80; 110 | MHD_Daemon* daemon_ = nullptr; 111 | 112 | td::IPAddress remote_addr_; 113 | ton::PublicKey remote_public_key_; 114 | 115 | bool hide_ips_ = false; 116 | 117 | std::unique_ptr make_callback(td::uint32 idx) { 118 | class Callback : public ton::adnl::AdnlExtClient::Callback { 119 | public: 120 | void on_ready() override { 121 | td::actor::send_closure(id_, &CoreActor::conn_ready, idx_); 122 | } 123 | void on_stop_ready() override { 124 | td::actor::send_closure(id_, &CoreActor::conn_closed, idx_); 125 | } 126 | Callback(td::actor::ActorId id, td::uint32 idx) : id_(std::move(id)), idx_(idx) { 127 | } 128 | 129 | private: 130 | td::actor::ActorId id_; 131 | td::uint32 idx_; 132 | }; 133 | 134 | return std::make_unique(actor_id(this), idx); 135 | } 136 | 137 | std::shared_ptr new_result_; 138 | td::int32 attempt_ = 0; 139 | td::int32 waiting_ = 0; 140 | 141 | std::vector ready_; 142 | 143 | void run_queries(); 144 | void got_result(td::uint32 idx, td::int32 attempt, td::Result data); 145 | void send_query(td::uint32 idx); 146 | 147 | void add_result() { 148 | if (new_result_) { 149 | auto ts = static_cast(new_result_->ts_.at_unix()); 150 | results_.emplace(ts, std::move(new_result_)); 151 | } 152 | } 153 | 154 | void alarm() override { 155 | auto t = static_cast(td::Clocks::system() / 60); 156 | if (t <= attempt_) { 157 | alarm_timestamp() = td::Timestamp::at_unix((attempt_ + 1) * 60); 158 | return; 159 | } 160 | if (waiting_ > 0 && new_result_) { 161 | add_result(); 162 | } 163 | attempt_ = t; 164 | run_queries(); 165 | alarm_timestamp() = td::Timestamp::at_unix((attempt_ + 1) * 60); 166 | } 167 | 168 | public: 169 | std::mutex queue_mutex_; 170 | std::mutex res_mutex_; 171 | std::map> results_; 172 | std::vector addrs_; 173 | static CoreActor* instance_; 174 | td::actor::ActorId self_id_; 175 | DatabaseConfigParams* dbParams; 176 | 177 | void conn_ready(td::uint32 idx) { 178 | ready_.at(idx) = true; 179 | } 180 | void conn_closed(td::uint32 idx) { 181 | ready_.at(idx) = false; 182 | } 183 | void set_global_config(std::string str) { 184 | global_config_ = str; 185 | } 186 | void set_http_port(td::uint32 port) { 187 | http_port_ = port; 188 | } 189 | void set_remote_addr(td::IPAddress addr) { 190 | remote_addr_ = addr; 191 | } 192 | void set_connection_database(std::shared_ptr conn) { 193 | dbParams = new DatabaseConfigParams{std::move(conn), &databaseMT}; 194 | if (!check_database_indexes()) { 195 | LOG(FATAL) << "No indexes for database, check indexes.sql"; 196 | } 197 | } 198 | 199 | bool check_database_indexes() { 200 | if (dbParams->conn != NULL) { 201 | dbParams->mtxDB->lock(); 202 | std::unique_ptr resSQL(PQexec(dbParams->conn.get(), getIndexes.c_str()), &PQclear); 203 | dbParams->mtxDB->unlock(); 204 | if (PQresultStatus(resSQL.get()) == PGRES_COMMAND_OK) { 205 | return true; 206 | } else { 207 | resSQL.reset(); 208 | LOG(FATAL) << "No connection to database"; 209 | } 210 | } 211 | return false; 212 | } 213 | 214 | void set_remote_public_key(td::BufferSlice file_name) { 215 | auto R = [&]() -> td::Result { 216 | TRY_RESULT_PREFIX(conf_data, td::read_file(file_name.as_slice().str()), "failed to read: "); 217 | return ton::PublicKey::import(conf_data.as_slice()); 218 | }(); 219 | 220 | if (R.is_error()) { 221 | LOG(FATAL) << "bad server public key: " << R.move_as_error(); 222 | } 223 | remote_public_key_ = R.move_as_ok(); 224 | } 225 | void set_hide_ips(bool value) { 226 | hide_ips_ = value; 227 | } 228 | 229 | void send_lite_query(td::uint32 idx, td::BufferSlice query, td::Promise promise); 230 | void send_lite_query(td::BufferSlice data, td::Promise promise) override { 231 | return send_lite_query(0, std::move(data), std::move(promise)); 232 | } 233 | void get_last_result(td::Promise> promise) override { 234 | } 235 | void get_results(td::uint32 max, td::Promise promise) override { 236 | RemoteNodeStatusList r; 237 | r.ips = hide_ips_ ? std::vector{addrs_.size()} : addrs_; 238 | auto it = results_.rbegin(); 239 | while (it != results_.rend() && r.results.size() < max) { 240 | r.results.push_back(it->second); 241 | it++; 242 | } 243 | promise.set_value(std::move(r)); 244 | } 245 | 246 | void start_up() override { 247 | instance_ = this; 248 | auto t = td::Clocks::system(); 249 | attempt_ = static_cast(t / 60); 250 | auto next_t = (attempt_ + 1) * 60; 251 | alarm_timestamp() = td::Timestamp::at_unix(next_t); 252 | self_id_ = actor_id(this); 253 | } 254 | void tear_down() override { 255 | if (daemon_ != nullptr) { 256 | MHD_stop_daemon(daemon_); 257 | daemon_ = nullptr; 258 | } 259 | } 260 | 261 | CoreActor() { 262 | } 263 | 264 | ~CoreActor() { 265 | tear_down(); 266 | delete dbParams; 267 | } 268 | 269 | static MHD_RESULT get_arg_iterate(void* cls, enum MHD_ValueKind kind, const char* key, const char* value) { 270 | auto X = static_cast*>(cls); 271 | if (key != nullptr && value != nullptr && std::strlen(key) > 0 && std::strlen(value) > 0) { 272 | X->emplace(key, urldecode(td::Slice{value}, false)); 273 | } 274 | return MHD_YES; 275 | } 276 | 277 | struct dataBlock { 278 | int workchain; 279 | std::string shard; 280 | std::string seqno; 281 | }; 282 | 283 | struct HttpRequestExtra { 284 | HttpRequestExtra(MHD_Connection* connection, bool is_post) { 285 | if (is_post) { 286 | postprocessor = MHD_create_post_processor(connection, 1000000, iterate_post, &block); 287 | } 288 | } 289 | ~HttpRequestExtra() { 290 | LOG(INFO) << "HttpRequestExtra destructor\n"; 291 | MHD_destroy_post_processor(postprocessor); 292 | } 293 | static MHD_RESULT iterate_post(void* coninfo_cls, enum MHD_ValueKind kind, const char* key, const char* filename, 294 | const char* content_type, const char* transfer_encoding, const char* data, 295 | uint64_t off, size_t size) { 296 | auto* ptr = static_cast(coninfo_cls); 297 | ptr->total_size += strlen(key) + size; 298 | if (ptr->total_size > MAX_POST_SIZE) { 299 | return MHD_NO; 300 | } 301 | std::string k = key; 302 | if (ptr->opts[k].size() < off + size) { 303 | ptr->opts[k].resize(off + size); 304 | } 305 | td::MutableSlice(ptr->opts[k]).remove_prefix(off).copy_from(td::Slice(data, size)); 306 | return MHD_YES; 307 | } 308 | dataBlock block; 309 | MHD_PostProcessor* postprocessor; 310 | std::map opts; 311 | td::uint64 total_size = 0; 312 | }; 313 | 314 | static void request_completed(void* cls, struct MHD_Connection* connection, void** ptr, 315 | enum MHD_RequestTerminationCode toe) { 316 | auto* e = static_cast(*ptr); 317 | if (e != nullptr) { 318 | delete e; 319 | } 320 | } 321 | 322 | static MHD_RESULT process_http_request(void* cls, struct MHD_Connection* connection, const char* url, 323 | const char* method, const char* version, const char* upload_data, 324 | size_t* upload_data_size, void** ptr) { 325 | struct MHD_Response* response = nullptr; 326 | MHD_RESULT ret = MHD_NO; 327 | dataBlock block; 328 | 329 | bool is_post = false; 330 | if (std::strcmp(method, "GET") == 0) { 331 | is_post = false; 332 | } else if (std::strcmp(method, "POST") == 0) { 333 | is_post = true; 334 | } else { 335 | return MHD_NO; /* unexpected method */ 336 | } 337 | std::map opts; 338 | if (!is_post) { 339 | if (*ptr == nullptr) { 340 | *ptr = static_cast(new HttpRequestExtra{connection, false}); 341 | return MHD_YES; 342 | } 343 | if (0 != *upload_data_size) 344 | return MHD_NO; /* upload data in a GET!? */ 345 | } else { 346 | if (*ptr == nullptr) { 347 | *ptr = static_cast(new HttpRequestExtra{connection, true}); 348 | return MHD_YES; 349 | } 350 | auto* e = static_cast(*ptr); 351 | if (*upload_data_size != 0) { 352 | *upload_data_size = 0; 353 | return MHD_YES; 354 | } 355 | for (auto& o : e->opts) { 356 | opts[o.first] = std::move(o.second); 357 | } 358 | } 359 | 360 | std::string url_s = url; 361 | 362 | *ptr = nullptr; /* clear context pointer */ 363 | 364 | auto pos = url_s.rfind('/'); 365 | std::string prefix; 366 | std::string command; 367 | if (pos == std::string::npos) { 368 | prefix = ""; 369 | command = url_s; 370 | } else { 371 | prefix = url_s.substr(0, pos + 1); 372 | command = url_s.substr(pos + 1); 373 | } 374 | 375 | MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, get_arg_iterate, static_cast(&opts)); 376 | if (command == "blockLite") { 377 | HttpQueryRunner g{[&](td::Promise promise) { 378 | td::actor::create_actor("blockinfo", opts, prefix, std::move(promise), 379 | static_cast(cls)) 380 | .release(); 381 | }}; 382 | response = g.wait(); 383 | // DONE 384 | } else if (command == "search") { 385 | HttpQueryRunner g{[&](td::Promise promise) { 386 | td::actor::create_actor("blocksearch", opts, prefix, std::move(promise)).release(); 387 | }}; 388 | response = g.wait(); 389 | // DONE 390 | } else if (command == "search2") { 391 | HttpQueryRunner g{[&](td::Promise promise) { 392 | td::actor::create_actor("blocksearch2", opts, prefix, std::move(promise), scheduler_ptr) 393 | .release(); 394 | }}; 395 | response = g.wait(); 396 | // DONE 397 | } else if (command == "last") { 398 | HttpQueryRunner g{[&](td::Promise promise) { 399 | td::actor::create_actor("", opts, prefix, std::move(promise)).release(); 400 | }}; 401 | response = g.wait(); 402 | } else if (command == "lastNum") { 403 | HttpQueryRunner g{[&](td::Promise promise) { 404 | td::actor::create_actor("", opts, prefix, std::move(promise)).release(); 405 | }}; 406 | response = g.wait(); 407 | } else if (command == "lastNumDB") { 408 | HttpQueryRunner g{[&](td::Promise promise) { 409 | td::actor::create_actor("lastNum", opts, prefix, std::move(promise), true, 410 | static_cast(cls)) 411 | .release(); 412 | }}; 413 | response = g.wait(); 414 | } else if (command == "getHash") { 415 | HttpQueryRunner g{[&](td::Promise promise) { 416 | td::actor::create_actor("getHash", opts, prefix, std::move(promise)).release(); 417 | }}; 418 | response = g.wait(); 419 | } else if (command == "getHashByHeight") { 420 | HttpQueryRunner g{[&](td::Promise promise) { 421 | td::actor::create_actor("getHash", opts, prefix, std::move(promise), 422 | static_cast(cls)) 423 | .release(); 424 | }}; 425 | response = g.wait(); 426 | } else if (command == "account") { 427 | HttpQueryRunner g{[&](td::Promise promise) { 428 | td::actor::create_actor("viewaccount", opts, prefix, std::move(promise)).release(); 429 | }}; 430 | response = g.wait(); 431 | } else if (command == "accountDB") { 432 | HttpQueryRunner g{[&](td::Promise promise) { 433 | td::actor::create_actor("viewaccountDB", opts, prefix, std::move(promise), true, 434 | static_cast(cls)) 435 | .release(); 436 | }}; 437 | response = g.wait(); 438 | } else if (command == "transaction") { 439 | HttpQueryRunner g{[&](td::Promise promise) { 440 | td::actor::create_actor("viewtransaction", opts, prefix, std::move(promise)) 441 | .release(); 442 | }}; 443 | response = g.wait(); 444 | } else if (command == "transactionDB") { 445 | HttpQueryRunner g{[&](td::Promise promise) { 446 | td::actor::create_actor("viewtransaction", opts, prefix, std::move(promise), true, 447 | static_cast(cls)) 448 | .release(); 449 | }}; 450 | response = g.wait(); 451 | } else if (command == "transaction2") { 452 | HttpQueryRunner g{[&](td::Promise promise) { 453 | td::actor::create_actor("viewtransaction2", opts, prefix, std::move(promise)) 454 | .release(); 455 | }}; 456 | response = g.wait(); 457 | } else if (command == "transaction2DB") { 458 | HttpQueryRunner g{[&](td::Promise promise) { 459 | td::actor::create_actor("viewtransaction2", opts, prefix, std::move(promise), true, 460 | static_cast(cls)) 461 | .release(); 462 | }}; 463 | response = g.wait(); 464 | } else if (command == "config") { 465 | HttpQueryRunner g{[&](td::Promise promise) { 466 | td::actor::create_actor("getconfig", opts, prefix, std::move(promise)).release(); 467 | }}; 468 | response = g.wait(); 469 | } else if (command == "validators") { 470 | HttpQueryRunner g{[&](td::Promise promise) { 471 | td::actor::create_actor("validators", opts, prefix, std::move(promise)).release(); 472 | }}; 473 | response = g.wait(); 474 | } else if (command == "runmethod") { 475 | HttpQueryRunner g{[&](td::Promise promise) { 476 | td::actor::create_actor("runmethod", opts, prefix, std::move(promise)).release(); 477 | }}; 478 | response = g.wait(); 479 | } else { 480 | ret = MHD_NO; 481 | } 482 | if (response != nullptr) { 483 | ret = MHD_queue_response(connection, MHD_HTTP_OK, response); 484 | MHD_destroy_response(response); 485 | } else { 486 | ret = MHD_NO; 487 | } 488 | return ret; 489 | } 490 | 491 | void exit_nicely() { 492 | dbParams->conn.reset(); 493 | } 494 | 495 | void run() { 496 | if (remote_public_key_.empty()) { 497 | auto G = td::read_file(global_config_).move_as_ok(); 498 | auto gc_j = td::json_decode(G.as_slice()).move_as_ok(); 499 | ton::ton_api::liteclient_config_global gc; 500 | ton::ton_api::from_json(gc, gc_j.get_object()).ensure(); 501 | 502 | CHECK(gc.liteservers_.size() > 0); 503 | td::uint32 size = static_cast(gc.liteservers_.size()); 504 | ready_.resize(size, false); 505 | 506 | for (td::uint32 i = 0; i < size; i++) { 507 | auto& cli = gc.liteservers_[i]; 508 | td::IPAddress addr; 509 | addr.init_host_port(td::IPAddress::ipv4_to_str(cli->ip_), cli->port_).ensure(); 510 | addrs_.push_back(addr); 511 | clients_.emplace_back(ton::adnl::AdnlExtClient::create(ton::adnl::AdnlNodeIdFull::create(cli->id_).move_as_ok(), 512 | addr, make_callback(i))); 513 | } 514 | } else { 515 | if (!remote_addr_.is_valid()) { 516 | LOG(FATAL) << "remote addr not set"; 517 | } 518 | ready_.resize(1, false); 519 | addrs_.push_back(remote_addr_); 520 | clients_.emplace_back(ton::adnl::AdnlExtClient::create(ton::adnl::AdnlNodeIdFull{remote_public_key_}, 521 | remote_addr_, make_callback(0))); 522 | } 523 | 524 | daemon_ = MHD_start_daemon(MHD_USE_INTERNAL_POLLING_THREAD, static_cast(http_port_), nullptr, nullptr, 525 | &process_http_request, dbParams, MHD_OPTION_NOTIFY_COMPLETED, request_completed, nullptr, 526 | MHD_OPTION_THREAD_POOL_SIZE, 16, MHD_OPTION_END); 527 | CHECK(daemon_ != nullptr); 528 | } 529 | }; 530 | 531 | void CoreActor::got_result(td::uint32 idx, td::int32 attempt, td::Result R) { 532 | if (attempt != attempt_) { 533 | return; 534 | } 535 | if (R.is_error()) { 536 | waiting_--; 537 | if (waiting_ == 0) { 538 | add_result(); 539 | } 540 | return; 541 | } 542 | auto data = R.move_as_ok(); 543 | { 544 | auto F = ton::fetch_tl_object(data.clone(), true); 545 | if (F.is_ok()) { 546 | auto f = F.move_as_ok(); 547 | auto err = td::Status::Error(f->code_, f->message_); 548 | waiting_--; 549 | if (waiting_ == 0) { 550 | add_result(); 551 | } 552 | return; 553 | } 554 | } 555 | auto F = ton::fetch_tl_object(std::move(data), true); 556 | if (F.is_error()) { 557 | waiting_--; 558 | if (waiting_ == 0) { 559 | add_result(); 560 | } 561 | return; 562 | } 563 | auto f = F.move_as_ok(); 564 | new_result_->values_[idx] = ton::create_block_id(f->last_); 565 | waiting_--; 566 | CHECK(waiting_ >= 0); 567 | if (waiting_ == 0) { 568 | add_result(); 569 | } 570 | } 571 | 572 | void CoreActor::send_query(td::uint32 idx) { 573 | if (!ready_[idx]) { 574 | return; 575 | } 576 | waiting_++; 577 | auto query = ton::create_tl_object(); 578 | auto q = ton::create_tl_object(serialize_tl_object(query, true)); 579 | 580 | auto P = 581 | td::PromiseCreator::lambda([SelfId = actor_id(this), idx, attempt = attempt_](td::Result R) { 582 | td::actor::send_closure(SelfId, &CoreActor::got_result, idx, attempt, std::move(R)); 583 | }); 584 | td::actor::send_closure(clients_[idx], &ton::adnl::AdnlExtClient::send_query, "query", serialize_tl_object(q, true), 585 | td::Timestamp::in(10.0), std::move(P)); 586 | } 587 | 588 | void CoreActor::run_queries() { 589 | waiting_ = 0; 590 | new_result_ = std::make_shared(ready_.size(), td::Timestamp::at_unix(attempt_ * 60)); 591 | for (td::uint32 i = 0; i < ready_.size(); i++) { 592 | send_query(i); 593 | } 594 | CHECK(waiting_ >= 0); 595 | if (waiting_ == 0) { 596 | add_result(); 597 | } 598 | } 599 | 600 | void CoreActor::send_lite_query(td::uint32 idx, td::BufferSlice query, td::Promise promise) { 601 | if (!ready_[idx]) { 602 | promise.set_error(td::Status::Error(ton::ErrorCode::notready, "ext conn not ready")); 603 | return; 604 | } 605 | auto P = td::PromiseCreator::lambda([promise = std::move(promise)](td::Result R) mutable { 606 | if (R.is_error()) { 607 | promise.set_error(R.move_as_error()); 608 | return; 609 | } 610 | auto B = R.move_as_ok(); 611 | { 612 | auto F = ton::fetch_tl_object(B.clone(), true); 613 | if (F.is_ok()) { 614 | auto f = F.move_as_ok(); 615 | promise.set_error(td::Status::Error(f->code_, f->message_)); 616 | return; 617 | } 618 | } 619 | promise.set_value(std::move(B)); 620 | }); 621 | auto q = ton::create_tl_object(std::move(query)); 622 | td::actor::send_closure(clients_[idx], &ton::adnl::AdnlExtClient::send_query, "query", serialize_tl_object(q, true), 623 | td::Timestamp::in(10.0), std::move(P)); 624 | } 625 | 626 | td::actor::ActorId CoreActorInterface::instance_actor_id() { 627 | auto instance = CoreActor::instance_; 628 | CHECK(instance); 629 | return instance->self_id_; 630 | } 631 | 632 | CoreActor* CoreActor::instance_ = nullptr; 633 | 634 | td::actor::ActorOwn x; 635 | 636 | void signalHandler(int signum) { 637 | exit(0); 638 | } 639 | 640 | int main(int argc, char* argv[]) { 641 | SET_VERBOSITY_LEVEL(verbosity_INFO); 642 | td::set_default_failure_signal_handler().ensure(); 643 | 644 | td::OptionParser p; 645 | p.set_description("TON Blockchain API"); 646 | p.add_checked_option('h', "help", "prints_help", [&]() { 647 | char b[10240]; 648 | td::StringBuilder sb(td::MutableSlice{b, 10000}); 649 | sb << p; 650 | std::exit(2); 651 | return td::Status::OK(); 652 | }); 653 | p.add_checked_option('I', "hide-ips", "hides ips from status", [&]() { 654 | td::actor::send_closure(x, &CoreActor::set_hide_ips, true); 655 | return td::Status::OK(); 656 | }); 657 | p.add_checked_option('u', "user", "change user", [&](td::Slice user) { return td::change_user(user.str()); }); 658 | p.add_checked_option('C', "global-config", "file to read global config", [&](td::Slice fname) { 659 | td::actor::send_closure(x, &CoreActor::set_global_config, fname.str()); 660 | return td::Status::OK(); 661 | }); 662 | p.add_checked_option('a', "addr", "connect to ip:port", [&](td::Slice arg) { 663 | td::IPAddress addr; 664 | TRY_STATUS(addr.init_host_port(arg.str())); 665 | td::actor::send_closure(x, &CoreActor::set_remote_addr, addr); 666 | return td::Status::OK(); 667 | }); 668 | p.add_checked_option('p', "pub", "remote public key", [&](td::Slice arg) { 669 | td::actor::send_closure(x, &CoreActor::set_remote_public_key, td::BufferSlice{arg}); 670 | return td::Status::OK(); 671 | }); 672 | p.add_checked_option('v', "verbosity", "set verbosity level", [&](td::Slice arg) { 673 | verbosity = td::to_integer(arg); 674 | SET_VERBOSITY_LEVEL(VERBOSITY_NAME(FATAL) + verbosity); 675 | return (verbosity >= 0 && verbosity <= 9) ? td::Status::OK() : td::Status::Error("verbosity must be 0..9"); 676 | }); 677 | p.add_checked_option('D', "database", "set database config file", [&](td::Slice arg) { 678 | auto db = td::read_file(arg.str()).move_as_ok(); 679 | auto IC_j = td::json_decode(db.as_slice()).move_as_ok(); 680 | auto db_str = td::get_json_object_string_field(IC_j.get_object(), "db_conn_string", false).move_as_ok(); 681 | std::shared_ptr conn(PQconnectdb(db_str.c_str()), &PQfinish); 682 | if (PQstatus(conn.get()) != CONNECTION_OK) { 683 | LOG(INFO) << PQerrorMessage(conn.get()); 684 | conn.reset(); 685 | LOG(FATAL) << "finitasse"; 686 | } 687 | td::actor::send_closure(x, &CoreActor::set_connection_database, std::move(conn)); 688 | return td::Status::OK(); 689 | }); 690 | p.add_checked_option('d', "daemonize", "set SIGHUP", [&]() { 691 | td::set_signal_handler(td::SignalType::HangUp, [](int sig) { 692 | #if TD_DARWIN || TD_LINUX 693 | close(0); 694 | setsid(); 695 | #endif 696 | }).ensure(); 697 | return td::Status::OK(); 698 | }); 699 | p.add_checked_option('H', "http-port", "listen on http port", [&](td::Slice arg) { 700 | td::actor::send_closure(x, &CoreActor::set_http_port, td::to_integer(arg)); 701 | return td::Status::OK(); 702 | }); 703 | p.add_checked_option('L', "local-scripts", "use local copy of ajax/bootstrap/... JS", [&]() { 704 | local_scripts = true; 705 | return td::Status::OK(); 706 | }); 707 | #if TD_DARWIN || TD_LINUX 708 | p.add_checked_option('l', "logname", "log to file", [&](td::Slice fname) { 709 | auto FileLog = td::FileFd::open(td::CSlice(fname.str().c_str()), 710 | td::FileFd::Flags::Create | td::FileFd::Flags::Append | td::FileFd::Flags::Write) 711 | .move_as_ok(); 712 | 713 | dup2(FileLog.get_native_fd().fd(), 1); 714 | dup2(FileLog.get_native_fd().fd(), 2); 715 | return td::Status::OK(); 716 | }); 717 | #endif 718 | 719 | vm::init_op_cp0(); 720 | 721 | std::shared_ptr scheduler_ptrg(new td::actor::Scheduler({2})); 722 | scheduler_ptr = std::move(scheduler_ptrg); 723 | scheduler_ptr->run_in_context([&] { x = td::actor::create_actor("testnode"); }); 724 | 725 | scheduler_ptr->run_in_context([&] { p.run(argc, argv).ensure(); }); 726 | scheduler_ptr->run_in_context([&] { 727 | td::actor::send_closure(x, &CoreActor::run); 728 | x.release(); 729 | }); 730 | scheduler_ptr->run(); 731 | return 0; 732 | } 733 | -------------------------------------------------------------------------------- /blockchain-api.hpp: -------------------------------------------------------------------------------- 1 | #ifndef blockchain_explorer 2 | #define blockchain_explorer 3 | 4 | #include "td/actor/actor.h" 5 | #include "td/utils/buffer.h" 6 | #include "ton/ton-types.h" 7 | #include "td/utils/port/IPAddress.h" 8 | #include 9 | #include 10 | #include 11 | 12 | #define MAX_POST_SIZE (64 << 10) 13 | 14 | // Beginning with v0.9.71, libmicrohttpd changed the return type of most 15 | // functions from int to enum MHD_Result 16 | // https://git.gnunet.org/gnunet.git/tree/src/include/gnunet_mhd_compat.h 17 | // proposes to define a constant for the return type so it works well 18 | // with all versions of libmicrohttpd 19 | #if MHD_VERSION >= 0x00097002 20 | #define MHD_RESULT enum MHD_Result 21 | #else 22 | #define MHD_RESULT int 23 | #endif 24 | 25 | extern bool local_scripts_; 26 | 27 | // #define pg_ptr PGconn, decltype(&PQfinish) 28 | 29 | typedef struct DatabaseConfigParams { 30 | DatabaseConfigParams(std::shared_ptr conn, std::mutex *mtxDB) : conn(std::move(conn)) { 31 | this->mtxDB = mtxDB; 32 | } 33 | std::shared_ptr conn; 34 | std::mutex *mtxDB; 35 | } DatabaseConfigParams; 36 | 37 | class CoreActorInterface : public td::actor::Actor { 38 | public: 39 | struct RemoteNodeStatus { 40 | std::vector values_; 41 | td::Timestamp ts_; 42 | RemoteNodeStatus(size_t size, td::Timestamp ts) : ts_(ts) { 43 | values_.resize(size); 44 | } 45 | }; 46 | 47 | struct RemoteNodeStatusList { 48 | std::vector ips; 49 | std::vector> results; 50 | }; 51 | virtual ~CoreActorInterface() = default; 52 | 53 | virtual void send_lite_query(td::BufferSlice data, td::Promise promise) = 0; 54 | virtual void get_last_result(td::Promise> promise) = 0; 55 | virtual void get_results(td::uint32 max, td::Promise promise) = 0; 56 | 57 | static td::actor::ActorId instance_actor_id(); 58 | }; 59 | 60 | #endif -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "db_conn_string" : "postgres://postgres:postgres@localhost:5432/postgres" 3 | } 4 | -------------------------------------------------------------------------------- /global-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "@type": "config.global", 3 | "dht": { 4 | "@type": "dht.config.global", 5 | "k": 6, 6 | "a": 3, 7 | "static_nodes": { 8 | "@type": "dht.nodes", 9 | "nodes": [ 10 | { 11 | "@type": "dht.node", 12 | "id": { 13 | "@type": "pub.ed25519", 14 | "key": "C1uy64rfGxp10SPSqbsxWhbumy5SM0YbvljCudwpZeI=" 15 | }, 16 | "addr_list": { 17 | "@type": "adnl.addressList", 18 | "addrs": [ 19 | { 20 | "@type": "adnl.address.udp", 21 | "ip": -1307380867, 22 | "port": 15888 23 | } 24 | ], 25 | "version": 0, 26 | "reinit_date": 0, 27 | "priority": 0, 28 | "expire_at": 0 29 | }, 30 | "version": -1, 31 | "signature": "s+tnHMTzPYG8abau+1dUs8tBJ+CDt+jIPmGfaVd7nmfb1gt6lL10G2IwkNeWhkxjZcAHRc0azWFVxp+IjIOOBQ==" 32 | }, 33 | { 34 | "@type": "dht.node", 35 | "id": { 36 | "@type": "pub.ed25519", 37 | "key": "bn8klhFZgE2sfIDfvVI6m6+oVNi1nBRlnHoxKtR9WBU=" 38 | }, 39 | "addr_list": { 40 | "@type": "adnl.addressList", 41 | "addrs": [ 42 | { 43 | "@type": "adnl.address.udp", 44 | "ip": -1307380860, 45 | "port": 15888 46 | } 47 | ], 48 | "version": 0, 49 | "reinit_date": 0, 50 | "priority": 0, 51 | "expire_at": 0 52 | }, 53 | "version": -1, 54 | "signature": "fQ5zAa6ot4pfFWzvuJOR8ijM5ELWndSDsRhFKstW1tqVSNfwAdOC7tDC8mc4vgTJ6fSYSWmhnXGK/+T5f6sDCw==" 55 | }, 56 | { 57 | "@type": "dht.node", 58 | "id": { 59 | "@type": "pub.ed25519", 60 | "key": "KlNrfVSyO6oISNi4Bx8J2klAN4RnKmEPQpfr1bghGSk=" 61 | }, 62 | "addr_list": { 63 | "@type": "adnl.addressList", 64 | "addrs": [ 65 | { 66 | "@type": "adnl.address.udp", 67 | "ip": -1307380856, 68 | "port": 15888 69 | } 70 | ], 71 | "version": 0, 72 | "reinit_date": 0, 73 | "priority": 0, 74 | "expire_at": 0 75 | }, 76 | "version": -1, 77 | "signature": "0/1trU+HDc+Co/q8gw5lPrSJH9YCOXxVh0caR2CGqXE5820DguuSmVnnLQ9S2+RmxHv0biYZuH8FiJv2wPwyDA==" 78 | }, 79 | { 80 | "@type": "dht.node", 81 | "id": { 82 | "@type": "pub.ed25519", 83 | "key": "HU0bQDDmXnUENi2qQgGUQSopWz+s0dFA1l6NgB6HdQ0=" 84 | }, 85 | "addr_list": { 86 | "@type": "adnl.addressList", 87 | "addrs": [ 88 | { 89 | "@type": "adnl.address.udp", 90 | "ip": -1185526389, 91 | "port": 26907 92 | } 93 | ], 94 | "version": 0, 95 | "reinit_date": 0, 96 | "priority": 0, 97 | "expire_at": 0 98 | }, 99 | "version": -1, 100 | "signature": "RrZILQv72PtJ/oADGh+txXgo6qfUL9RFLU+YjMXsMZTAo2lCWYwNoeFOEZrS5MKfLmkL6O0MmOR/EEAFrr3mAw==" 101 | }, 102 | { 103 | "@type": "dht.node", 104 | "id": { 105 | "@type": "pub.ed25519", 106 | "key": "xHfihhu4rFeKUNjxH2aHCJIG1s9PTaypqjocrm82U24=" 107 | }, 108 | "addr_list": { 109 | "@type": "adnl.addressList", 110 | "addrs": [ 111 | { 112 | "@type": "adnl.address.udp", 113 | "ip": 1560268637, 114 | "port": 29503 115 | } 116 | ], 117 | "version": 0, 118 | "reinit_date": 0, 119 | "priority": 0, 120 | "expire_at": 0 121 | }, 122 | "version": -1, 123 | "signature": "m+ah3Bn9AMaV13QaXrfT/c/z1fY6DzDMQkCEIgByXygnskwYwTfWxa8Z7DXy80UX2OYpSL8GwTJ4HpQsdcxMCA==" 124 | }, 125 | { 126 | "@type": "dht.node", 127 | "id": { 128 | "@type": "pub.ed25519", 129 | "key": "4R0C/zU56k+x2HGMsLWjX2rP/SpoTPIHSSAmidGlsb8=" 130 | }, 131 | "addr_list": { 132 | "@type": "adnl.addressList", 133 | "addrs": [ 134 | { 135 | "@type": "adnl.address.udp", 136 | "ip": -1952265919, 137 | "port": 14395 138 | } 139 | ], 140 | "version": 0, 141 | "reinit_date": 0, 142 | "priority": 0, 143 | "expire_at": 0 144 | }, 145 | "version": -1, 146 | "signature": "0uwWyCFn2KjPnnlbSFYXLZdwIakaSgI9WyRo87J3iCGwb5TvJSztgA224A9kNAXeutOrXMIPYv1b8Zt8ImsrCg==" 147 | }, 148 | { 149 | "@type": "dht.node", 150 | "id": { 151 | "@type": "pub.ed25519", 152 | "key": "/YDNd+IwRUgL0mq21oC0L3RxrS8gTu0nciSPUrhqR78=" 153 | }, 154 | "addr_list": { 155 | "@type": "adnl.addressList", 156 | "addrs": [ 157 | { 158 | "@type": "adnl.address.udp", 159 | "ip": -1402455171, 160 | "port": 14432 161 | } 162 | ], 163 | "version": 0, 164 | "reinit_date": 0, 165 | "priority": 0, 166 | "expire_at": 0 167 | }, 168 | "version": -1, 169 | "signature": "6+oVk6HDtIFbwYi9khCc8B+fTFceBUo1PWZDVTkb4l84tscvr5QpzAkdK7sS5xGzxM7V7YYQ6gUQPrsP9xcLAw==" 170 | }, 171 | { 172 | "@type": "dht.node", 173 | "id": { 174 | "@type": "pub.ed25519", 175 | "key": "DA0H568bb+LoO2LGY80PgPee59jTPCqqSJJzt1SH+KE=" 176 | }, 177 | "addr_list": { 178 | "@type": "adnl.addressList", 179 | "addrs": [ 180 | { 181 | "@type": "adnl.address.udp", 182 | "ip": -1402397332, 183 | "port": 14583 184 | } 185 | ], 186 | "version": 0, 187 | "reinit_date": 0, 188 | "priority": 0, 189 | "expire_at": 0 190 | }, 191 | "version": -1, 192 | "signature": "cL79gDTrixhaM9AlkCdZWccCts7ieQYQBmPxb/R7d7zHw3bEHL8Le96CFJoB1KHu8C85iDpFK8qlrGl1Yt/ZDg==" 193 | }, 194 | { 195 | "@type": "dht.node", 196 | "id": { 197 | "@type": "pub.ed25519", 198 | "key": "fZnkoIAxrTd4xeBgVpZFRm5SvVvSx7eN3Vbe8c83YMk=" 199 | }, 200 | "addr_list": { 201 | "@type": "adnl.addressList", 202 | "addrs": [ 203 | { 204 | "@type": "adnl.address.udp", 205 | "ip": 1091897261, 206 | "port": 15813 207 | } 208 | ], 209 | "version": 0, 210 | "reinit_date": 0, 211 | "priority": 0, 212 | "expire_at": 0 213 | }, 214 | "version": -1, 215 | "signature": "cmaMrV/9wuaHOOyXYjoxBnckJktJqrQZ2i+YaY3ehIyiL3LkW81OQ91vm8zzsx1kwwadGZNzgq4hI4PCB/U5Dw==" 216 | }, 217 | { 218 | "@type": "dht.node", 219 | "id": { 220 | "@type": "pub.ed25519", 221 | "key": "zDBLsKjns4bBqQokzY0wOzC2vwbOeiE1J7aOjfCp5mg=" 222 | }, 223 | "addr_list": { 224 | "@type": "adnl.addressList", 225 | "addrs": [ 226 | { 227 | "@type": "adnl.address.udp", 228 | "ip": -1573440928, 229 | "port": 12821 230 | } 231 | ], 232 | "version": 0, 233 | "reinit_date": 0, 234 | "priority": 0, 235 | "expire_at": 0 236 | }, 237 | "version": -1, 238 | "signature": "qORMhem9RyG7wnNYF822YL3EXwEoTO82h2TarFbjd0jikMIGizAdir1JyxSfyKkhHdFKGcLMeoPb2dfMIvQwAA==" 239 | }, 240 | { 241 | "@type": "dht.node", 242 | "id": { 243 | "@type": "pub.ed25519", 244 | "key": "CU9ytJok8WBnpl29T740gfC/h69kgvQJp7FJMq/N60g=" 245 | }, 246 | "addr_list": { 247 | "@type": "adnl.addressList", 248 | "addrs": [ 249 | { 250 | "@type": "adnl.address.udp", 251 | "ip": 391653587, 252 | "port": 15895 253 | } 254 | ], 255 | "version": 0, 256 | "reinit_date": 0, 257 | "priority": 0, 258 | "expire_at": 0 259 | }, 260 | "version": -1, 261 | "signature": "DKyGF2nPRxmerpIHxE5FN1Lod3zvJu728NP0iYc1hpNyPvl5epu+7amjimLy1VdzNqFzTJAoJ/gqPPMkXS/kDw==" 262 | }, 263 | { 264 | "@type": "dht.node", 265 | "id": { 266 | "@type": "pub.ed25519", 267 | "key": "d1lL6xZO8UWMzkWD++8Yr3hf6585X6qoOZZTeLtGl4o=" 268 | }, 269 | "addr_list": { 270 | "@type": "adnl.addressList", 271 | "addrs": [ 272 | { 273 | "@type": "adnl.address.udp", 274 | "ip": -1537553981, 275 | "port": 18513 276 | } 277 | ], 278 | "version": 0, 279 | "reinit_date": 0, 280 | "priority": 0, 281 | "expire_at": 0 282 | }, 283 | "version": -1, 284 | "signature": "sDxyCuvZmi9fJvQl4DEBKZOlt532xflnVD1dvC2ia2Na5MN8dT6x4HizEpS4pUqky8LzR/A/4BCqIQXhD45vDg==" 285 | }, 286 | { 287 | "@type": "dht.node", 288 | "id": { 289 | "@type": "pub.ed25519", 290 | "key": "SP2Bjle9u+GoZhlEhm3mIPiND3Yh+Nr7QsgV6bGPw/I=" 291 | }, 292 | "addr_list": { 293 | "@type": "adnl.addressList", 294 | "addrs": [ 295 | { 296 | "@type": "adnl.address.udp", 297 | "ip": -1591529174, 298 | "port": 11369 299 | } 300 | ], 301 | "version": 0, 302 | "reinit_date": 0, 303 | "priority": 0, 304 | "expire_at": 0 305 | }, 306 | "version": -1, 307 | "signature": "qbbeE53SOyUvOWD2hIZmTyZGY4k2XHxgldqBIakBma33oC5sWDD/+cApuFLMbi2Gnd6fQtQ3LaZtfQzJrCiYAw==" 308 | }, 309 | { 310 | "@type": "dht.node", 311 | "id": { 312 | "@type": "pub.ed25519", 313 | "key": "76SsMv8NrqzkCFEVumZEx2phYYSUZvSH3UlUTuPdZYk=" 314 | }, 315 | "addr_list": { 316 | "@type": "adnl.addressList", 317 | "addrs": [ 318 | { 319 | "@type": "adnl.address.udp", 320 | "ip": -1906451518, 321 | "port": 16937 322 | } 323 | ], 324 | "version": 0, 325 | "reinit_date": 0, 326 | "priority": 0, 327 | "expire_at": 0 328 | }, 329 | "version": -1, 330 | "signature": "YgrIN/KH3XVPyk09wgZbFaXLZ/BeGjvfO6ohxN4M8Tp/CsYN0G3tNLSjJGeuH22bIKMFRoEpp3v7Sz54Q/FTCg==" 331 | }, 332 | { 333 | "@type": "dht.node", 334 | "id": { 335 | "@type": "pub.ed25519", 336 | "key": "IHrCooudVGonmudcmTZYk+Pfdsxz2NGws33bHtXnOv8=" 337 | }, 338 | "addr_list": { 339 | "@type": "adnl.addressList", 340 | "addrs": [ 341 | { 342 | "@type": "adnl.address.udp", 343 | "ip": -1882838653, 344 | "port": 41623 345 | } 346 | ], 347 | "version": 0, 348 | "reinit_date": 0, 349 | "priority": 0, 350 | "expire_at": 0 351 | }, 352 | "version": -1, 353 | "signature": "pm3PQn0ZYQH7SkmK/8q4p1igFKxJSQLCmVcFtyaRxEV9ecjvB5PiW3o3wfrTRPk0v0mnFmH8UuYtLEztj+LcBw==" 354 | }, 355 | { 356 | "@type": "dht.node", 357 | "id": { 358 | "@type": "pub.ed25519", 359 | "key": "eDPKd9v6aHhutnHU3z2ykV4eUA3LdFI+oSBg6z8JyS0=" 360 | }, 361 | "addr_list": { 362 | "@type": "adnl.addressList", 363 | "addrs": [ 364 | { 365 | "@type": "adnl.address.udp", 366 | "ip": -1481864647, 367 | "port": 22261 368 | } 369 | ], 370 | "version": 0, 371 | "reinit_date": 0, 372 | "priority": 0, 373 | "expire_at": 0 374 | }, 375 | "version": -1, 376 | "signature": "InY5jkkQ6fuJtutkmlPLYEhqQ0F4DyEhqoPB4KMBIJc8hhZFzm4jNAxSfy3VVQbACdJ/pj76qbjaktG/m1ipDQ==" 377 | }, 378 | { 379 | "@type": "dht.node", 380 | "id": { 381 | "@type": "pub.ed25519", 382 | "key": "znOAvy1ECxyzeKishi4PdSO2edaVx78wynVyNKLBmQ8=" 383 | }, 384 | "addr_list": { 385 | "@type": "adnl.addressList", 386 | "addrs": [ 387 | { 388 | "@type": "adnl.address.udp", 389 | "ip": -1068377703, 390 | "port": 6302 391 | } 392 | ], 393 | "version": 0, 394 | "reinit_date": 0, 395 | "priority": 0, 396 | "expire_at": 0 397 | }, 398 | "version": -1, 399 | "signature": "KLH17nNKmOk3carKwbsUcVBc4JZpdAUdUOMxe8FSyqnkOw/lolnltbylJcC+lvPpIV5ySI/Qx8UZdNRV/4HzCA==" 400 | }, 401 | { 402 | "@type": "dht.node", 403 | "id": { 404 | "@type": "pub.ed25519", 405 | "key": "Qjhv9rmeqXm0a+nYYhCJG1AH7C2TM6DAmyIM3FgO0Eo=" 406 | }, 407 | "addr_list": { 408 | "@type": "adnl.addressList", 409 | "addrs": [ 410 | { 411 | "@type": "adnl.address.udp", 412 | "ip": -1057912003, 413 | "port": 6302 414 | } 415 | ], 416 | "version": 0, 417 | "reinit_date": 0, 418 | "priority": 0, 419 | "expire_at": 0 420 | }, 421 | "version": -1, 422 | "signature": "2Gw5eIsZR+SdbWCU139DCuBI8Rv8T9iUioxDkgV6/IjcCHf6hNz8WCyUsKd5l5P1NBs/kdaxUBIybINDpYXoCw==" 423 | }, 424 | { 425 | "@type": "dht.node", 426 | "id": { 427 | "@type": "pub.ed25519", 428 | "key": "2YsTRIu3aRYzZe8eoR8PK2N2ydHJyKllwKcLPk676d4=" 429 | }, 430 | "addr_list": { 431 | "@type": "adnl.addressList", 432 | "addrs": [ 433 | { 434 | "@type": "adnl.address.udp", 435 | "ip": -1057911744, 436 | "port": 6302 437 | } 438 | ], 439 | "version": 0, 440 | "reinit_date": 0, 441 | "priority": 0, 442 | "expire_at": 0 443 | }, 444 | "version": -1, 445 | "signature": "9/TJsaj0wELvRKXVIrBdyZWjgLKhfSvl7v0Oqq/9p9MsU/t9iRuGcpAzHqQF4bQAWrN8j9ARwMumRata7dH8Bg==" 446 | }, 447 | { 448 | "@type": "dht.node", 449 | "id": { 450 | "@type": "pub.ed25519", 451 | "key": "SHrXmMEEUBGa51TWZwHSA+2RF4Vyavw51jgtnAz1ypU=" 452 | }, 453 | "addr_list": { 454 | "@type": "adnl.addressList", 455 | "addrs": [ 456 | { 457 | "@type": "adnl.address.udp", 458 | "ip": -1057911148, 459 | "port": 6302 460 | } 461 | ], 462 | "version": 0, 463 | "reinit_date": 0, 464 | "priority": 0, 465 | "expire_at": 0 466 | }, 467 | "version": -1, 468 | "signature": "R4ku8+tvjKSLIGe18zWHBHDv1maQHD5tGbAUOgbldGpBvfqH+/b76XkJjJzDsjnCO/bpxwUZfcI1sM1h6vFJCQ==" 469 | }, 470 | { 471 | "@type": "dht.node", 472 | "id": { 473 | "@type": "pub.ed25519", 474 | "key": "G+Lr6UtSWUcyYHTUutwbxrIG9GGZan3h96j8qQPLIXQ=" 475 | }, 476 | "addr_list": { 477 | "@type": "adnl.addressList", 478 | "addrs": [ 479 | { 480 | "@type": "adnl.address.udp", 481 | "ip": -960017601, 482 | "port": 6302 483 | } 484 | ], 485 | "version": 0, 486 | "reinit_date": 0, 487 | "priority": 0, 488 | "expire_at": 0 489 | }, 490 | "version": -1, 491 | "signature": "fWU9hSNLvmaSCQOBW9M4Lja5pIWcqOzU1g9vtSywdgtASj9oQpwAslvr2sjNh9E2Np1c26NW8Sc5gUKf8YY7BA==" 492 | }, 493 | { 494 | "@type": "dht.node", 495 | "id": { 496 | "@type": "pub.ed25519", 497 | "key": "/tp8WsXfk/XpzOyaaxuOlvbOhDoH7/L81eWi0QMn0gg=" 498 | }, 499 | "addr_list": { 500 | "@type": "adnl.addressList", 501 | "addrs": [ 502 | { 503 | "@type": "adnl.address.udp", 504 | "ip": 84478511, 505 | "port": 6302 506 | } 507 | ], 508 | "version": 0, 509 | "reinit_date": 0, 510 | "priority": 0, 511 | "expire_at": 0 512 | }, 513 | "version": -1, 514 | "signature": "2kA9P0LBI7H8gmmGsnZ2bQPZP3rZDFugrc5zQWlFrPIMLvwH7/J69HIGCVYgcaEsf0HMnIJeUMl5n4qFp0quBQ==" 515 | }, 516 | { 517 | "@type": "dht.node", 518 | "id": { 519 | "@type": "pub.ed25519", 520 | "key": "fnYl5kAHcbhK65FbYxfwk83X1Sn6ZiuXWMD0F0Rh+v4=" 521 | }, 522 | "addr_list": { 523 | "@type": "adnl.addressList", 524 | "addrs": [ 525 | { 526 | "@type": "adnl.address.udp", 527 | "ip": 84478479, 528 | "port": 6302 529 | } 530 | ], 531 | "version": 0, 532 | "reinit_date": 0, 533 | "priority": 0, 534 | "expire_at": 0 535 | }, 536 | "version": -1, 537 | "signature": "h+K+YttdhqE4LzihZTnKLFBiXyY79Rqqcx8dCbkDVXu3FD7ZrTBNV5b/bf7BQbuF0PXTc7YqH0jEmqz8aX6aBg==" 538 | }, 539 | { 540 | "@type": "dht.node", 541 | "id": { 542 | "@type": "pub.ed25519", 543 | "key": "HwOhm4Vh1YGqBNmUrDwJpeo8kXAPI7J3sSH38JaAyzQ=" 544 | }, 545 | "addr_list": { 546 | "@type": "adnl.addressList", 547 | "addrs": [ 548 | { 549 | "@type": "adnl.address.udp", 550 | "ip": -2018145068, 551 | "port": 6302 552 | } 553 | ], 554 | "version": 0, 555 | "reinit_date": 0, 556 | "priority": 0, 557 | "expire_at": 0 558 | }, 559 | "version": -1, 560 | "signature": "Ianf1Wm9Y6HT9r32LFNUieKi86cSBbCckczHy+ZyBo15MpIsZxouUgfAyeW20sZm1hN5+Yx4lPwzL+Ovm6KaCw==" 561 | }, 562 | { 563 | "@type": "dht.node", 564 | "id": { 565 | "@type": "pub.ed25519", 566 | "key": "CXo+qxdYclubZqoqvVhoeYDdPV+VhlWcurf2OX0iPZs=" 567 | }, 568 | "addr_list": { 569 | "@type": "adnl.addressList", 570 | "addrs": [ 571 | { 572 | "@type": "adnl.address.udp", 573 | "ip": -2018145059, 574 | "port": 6302 575 | } 576 | ], 577 | "version": 0, 578 | "reinit_date": 0, 579 | "priority": 0, 580 | "expire_at": 0 581 | }, 582 | "version": -1, 583 | "signature": "P72kraIX5pIxQBnh7It4kyK6MPuZ56ZFZKZxegtrxwx/Vpi1wQ4PsfxWf6N0HojbNMYsVZsvwHYTLxj5nhd6Dw==" 584 | }, 585 | { 586 | "@type": "dht.node", 587 | "id": { 588 | "@type": "pub.ed25519", 589 | "key": "KiKtUV+kJWBd+M29zNvtRqdvUrtX4lfi5CyY+DRm+lk=" 590 | }, 591 | "addr_list": { 592 | "@type": "adnl.addressList", 593 | "addrs": [ 594 | { 595 | "@type": "adnl.address.udp", 596 | "ip": 1091931625, 597 | "port": 6302 598 | } 599 | ], 600 | "version": 0, 601 | "reinit_date": 0, 602 | "priority": 0, 603 | "expire_at": 0 604 | }, 605 | "version": -1, 606 | "signature": "GjarYvxTVPik8m5yI9Eq/1lW/8CuReBdhUdFUb4wJJVVc/EvHf7j47mY5ECskHjeo9MYttgF/9KQaf8KNea1Dg==" 607 | }, 608 | { 609 | "@type": "dht.node", 610 | "id": { 611 | "@type": "pub.ed25519", 612 | "key": "o15mg8SB9CY2m971NvU+aCzAEnZFg3iAnIsqBMmqnj0=" 613 | }, 614 | "addr_list": { 615 | "@type": "adnl.addressList", 616 | "addrs": [ 617 | { 618 | "@type": "adnl.address.udp", 619 | "ip": 1091931590, 620 | "port": 6302 621 | } 622 | ], 623 | "version": 0, 624 | "reinit_date": 0, 625 | "priority": 0, 626 | "expire_at": 0 627 | }, 628 | "version": -1, 629 | "signature": "6mJPM7RZMOL5uCMRCGINjxAG7L7LHt7o89caD7Kk75ahpwAhqJ3ri9zL1rzJZjmyOMLkPoGcckJsG8phCRbVDQ==" 630 | }, 631 | { 632 | "@type": "dht.node", 633 | "id": { 634 | "@type": "pub.ed25519", 635 | "key": "VCu471G41Hj8onyyeJdq8t6AZu3SR7BoGuCLs8SppBk=" 636 | }, 637 | "addr_list": { 638 | "@type": "adnl.addressList", 639 | "addrs": [ 640 | { 641 | "@type": "adnl.address.udp", 642 | "ip": 1091931623, 643 | "port": 6302 644 | } 645 | ], 646 | "version": 0, 647 | "reinit_date": 0, 648 | "priority": 0, 649 | "expire_at": 0 650 | }, 651 | "version": -1, 652 | "signature": "7cOhypsjGb4xczR20M6eg7ly8sdvzdodkKVXzvr00FsXHcguz6bP0zm/dwhiQgsJgSosYypCk/LvKQrMy+C3AQ==" 653 | }, 654 | { 655 | "@type": "dht.node", 656 | "id": { 657 | "@type": "pub.ed25519", 658 | "key": "0uEnHB6Rg4sVjiepDgHoZ3CuKjCRjU3ul4IGmmZZoig=" 659 | }, 660 | "addr_list": { 661 | "@type": "adnl.addressList", 662 | "addrs": [ 663 | { 664 | "@type": "adnl.address.udp", 665 | "ip": 1091931589, 666 | "port": 6302 667 | } 668 | ], 669 | "version": 0, 670 | "reinit_date": 0, 671 | "priority": 0, 672 | "expire_at": 0 673 | }, 674 | "version": -1, 675 | "signature": "ju40qeS5mgbJDMLUxL7qSquSdqgo3Uib4Z/Va/bpIWJJA0W3VQStJMBbV/pQySi6MoM794Du3o8Gl1bjdpwDAg==" 676 | }, 677 | { 678 | "@type": "dht.node", 679 | "id": { 680 | "@type": "pub.ed25519", 681 | "key": "MJr8xja0xpu9DoisFXBrkNHNx1XozR7HHw9fJdSyEdo=" 682 | }, 683 | "addr_list": { 684 | "@type": "adnl.addressList", 685 | "addrs": [ 686 | { 687 | "@type": "adnl.address.udp", 688 | "ip": -2018147130, 689 | "port": 6302 690 | } 691 | ], 692 | "version": 0, 693 | "reinit_date": 0, 694 | "priority": 0, 695 | "expire_at": 0 696 | }, 697 | "version": -1, 698 | "signature": "XcR5JaWcf4QMdI8urLSc1zwv5+9nCuItSE1EDa0dSwYF15R/BtJoKU5YHA4/T8SiO18aVPQk2SL1pbhevuMrAQ==" 699 | }, 700 | { 701 | "@type": "dht.node", 702 | "id": { 703 | "@type": "pub.ed25519", 704 | "key": "Fhldu4zlnb20/TUj9TXElZkiEmbndIiE/DXrbGKu+0c=" 705 | }, 706 | "addr_list": { 707 | "@type": "adnl.addressList", 708 | "addrs": [ 709 | { 710 | "@type": "adnl.address.udp", 711 | "ip": -2018147075, 712 | "port": 6302 713 | } 714 | ], 715 | "version": 0, 716 | "reinit_date": 0, 717 | "priority": 0, 718 | "expire_at": 0 719 | }, 720 | "version": -1, 721 | "signature": "nUGB77UAkd2+ZAL5PgInb3TvtuLLXJEJ2icjAUKLv4qIGB3c/O9k/v0NKwSzhsMP0ljeTGbcIoMDw24qf3goCg==" 722 | } 723 | ] 724 | } 725 | }, 726 | "liteservers": [ 727 | { 728 | "ip": -2018135749, 729 | "port": 53312, 730 | "id": { 731 | "@type": "pub.ed25519", 732 | "key": "aF91CuUHuuOv9rm2W5+O/4h38M3sRm40DtSdRxQhmtQ=" 733 | } 734 | } 735 | ], 736 | "validator": { 737 | "@type": "validator.config.global", 738 | "zero_state": { 739 | "workchain": -1, 740 | "shard": -9223372036854775808, 741 | "seqno": 0, 742 | "root_hash": "F6OpKZKqvqeFp6CQmFomXNMfMj2EnaUSOXN+Mh+wVWk=", 743 | "file_hash": "XplPz01CXAps5qeSWUtxcyBfdAo5zVb1N979KLSKD24=" 744 | }, 745 | "init_block": { 746 | "root_hash": "iyOany4cPE2u6h/Um7OAmHDQ+Nba8Am+g/qZJ5M4P9M=", 747 | "seqno": 18155329, 748 | "file_hash": "Yqmli3gIUgt3KjeU4n2d1ZmcJ3R4zJBMYWhM+tZF4V8=", 749 | "workchain": -1, 750 | "shard": -9223372036854775808 751 | }, 752 | "hardforks": [ 753 | { 754 | "file_hash": "t/9VBPODF7Zdh4nsnA49dprO69nQNMqYL+zk5bCjV/8=", 755 | "seqno": 8536841, 756 | "root_hash": "08Kpc9XxrMKC6BF/FeNHPS3MEL1/Vi/fQU/C9ELUrkc=", 757 | "workchain": -1, 758 | "shard": -9223372036854775808 759 | } 760 | ] 761 | } 762 | } -------------------------------------------------------------------------------- /indexes.sql: -------------------------------------------------------------------------------- 1 | create index block_index on ton_block (seqno, workchain); 2 | create index lt_index on ton_transaction (logical_time desc); -------------------------------------------------------------------------------- /query-impl/block-info.cpp: -------------------------------------------------------------------------------- 1 | #include "../blockchain-api-query.hpp" 2 | 3 | HttpQueryBlockInfo::HttpQueryBlockInfo(ton::BlockIdExt block_id, std::string prefix, 4 | td::Promise promise) 5 | : HttpQueryCommon(std::move(prefix), std::move(promise), std::move(block_id)) { 6 | } 7 | 8 | HttpQueryBlockInfo::HttpQueryBlockInfo(std::map opts, std::string prefix, 9 | td::Promise promise) 10 | : HttpQueryCommon(std::move(prefix), std::move(promise)) { 11 | auto R = parse_block_id(opts); 12 | if (R.is_ok()) { 13 | block_id_ = R.move_as_ok(); 14 | } else { 15 | error_ = R.move_as_error(); 16 | } 17 | } 18 | 19 | HttpQueryBlockInfo::HttpQueryBlockInfo(std::map opts, std::string prefix, 20 | td::Promise promise, DatabaseConfigParams *dbConfParams) 21 | : HttpQueryCommon(std::move(prefix), std::move(promise)) { 22 | auto R = parse_block_id(opts); 23 | if (R.is_ok()) { 24 | block_id_ = R.move_as_ok(); 25 | } else { 26 | error_ = R.move_as_error(); 27 | } 28 | if (dbConfParams != NULL) { 29 | if (dbConfParams->conn != NULL) { 30 | this->conn = dbConfParams->conn; 31 | } else { 32 | error_ = td::Status::Error("Connection is empty"); 33 | } 34 | if (dbConfParams->mtxDB != NULL) { 35 | this->mtx = dbConfParams->mtxDB; 36 | } else { 37 | error_ = td::Status::Error("Mutex is null"); 38 | } 39 | } else { 40 | error_ = td::Status::Error("DB not connected"); 41 | } 42 | } 43 | 44 | void HttpQueryBlockInfo::start_up_query() { 45 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 46 | if (R.is_error()) { 47 | td::actor::send_closure(SelfId, &HttpQueryBlockInfo::abort_query, R.move_as_error_prefix("litequery failed: ")); 48 | } else { 49 | td::actor::send_closure(SelfId, &HttpQueryBlockInfo::got_block_header, R.move_as_ok()); 50 | } 51 | }); 52 | auto query = ton::serialize_tl_object( 53 | ton::create_tl_object(ton::create_tl_lite_block_id(block_id_)), true); 54 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 55 | std::move(query), std::move(P)); 56 | pending_queries_ = 1; 57 | 58 | if (block_id_.is_masterchain()) { 59 | auto P_2 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 60 | if (R.is_error()) { 61 | td::actor::send_closure(SelfId, &HttpQueryBlockInfo::failed_to_get_shard_info, 62 | R.move_as_error_prefix("litequery failed: ")); 63 | } else { 64 | td::actor::send_closure(SelfId, &HttpQueryBlockInfo::got_shard_info, R.move_as_ok()); 65 | } 66 | }); 67 | auto query_2 = ton::serialize_tl_object( 68 | ton::create_tl_object(ton::create_tl_lite_block_id(block_id_)), 69 | true); 70 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 71 | std::move(query_2), std::move(P_2)); 72 | pending_queries_++; 73 | auto P_4 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 74 | if (R.is_error()) { 75 | td::actor::send_closure(SelfId, &HttpQueryBlockInfo::abort_query, R.move_as_error()); 76 | } else { 77 | td::actor::send_closure(SelfId, &HttpQueryBlockInfo::got_config, R.move_as_ok()); 78 | } 79 | }); 80 | auto query_4 = ton::serialize_tl_object(ton::create_tl_object( 81 | 0, ton::create_tl_lite_block_id(block_id_), std::vector({34})), 82 | true); 83 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 84 | std::move(query_4), std::move(P_4)); 85 | 86 | pending_queries_++; 87 | } 88 | auto query_3 = ton::serialize_tl_object(ton::create_tl_object( 89 | ton::create_tl_lite_block_id(block_id_), 7, 1024, nullptr, false, false), 90 | true); 91 | auto P_3 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 92 | if (R.is_error()) { 93 | td::actor::send_closure(SelfId, &HttpQueryBlockInfo::abort_query, R.move_as_error_prefix("litequery failed: ")); 94 | } else { 95 | td::actor::send_closure(SelfId, &HttpQueryBlockInfo::got_transactions, R.move_as_ok()); 96 | } 97 | }); 98 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 99 | std::move(query_3), std::move(P_3)); 100 | pending_queries_++; 101 | } 102 | 103 | void HttpQueryBlockInfo::got_transactions(td::BufferSlice data) { 104 | auto F = ton::fetch_tl_object(std::move(data), true); 105 | if (F.is_error()) { 106 | abort_query(F.move_as_error()); 107 | return; 108 | } 109 | auto f = F.move_as_ok(); 110 | trans_req_count_ = f->req_count_; 111 | 112 | for (std::size_t i = 0; i < f->ids_.size(); i++) { 113 | transactions_.emplace_back(block::StdAddress{block_id_.id.workchain, f->ids_[i]->account_}, 114 | static_cast(f->ids_[i]->lt_), f->ids_[i]->hash_); 115 | start_transaction_query(block::StdAddress{block_id_.id.workchain, f->ids_[i]->account_}, 116 | static_cast(f->ids_[i]->lt_), f->ids_[i]->hash_); 117 | } 118 | 119 | if (f->incomplete_ && transactions_.size() > 0) { 120 | const auto &T = *transactions_.rbegin(); 121 | auto query_3 = ton::serialize_tl_object( 122 | ton::create_tl_object( 123 | ton::create_tl_lite_block_id(block_id_), 7 + 128, 1024, 124 | ton::create_tl_object(T.addr.addr, T.lt), false, false), 125 | true); 126 | auto P_3 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 127 | if (R.is_error()) { 128 | td::actor::send_closure(SelfId, &HttpQueryBlockInfo::abort_query, R.move_as_error_prefix("litequery failed: ")); 129 | } else { 130 | td::actor::send_closure(SelfId, &HttpQueryBlockInfo::got_transactions, R.move_as_ok()); 131 | } 132 | }); 133 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 134 | std::move(query_3), std::move(P_3)); 135 | } else { 136 | if (!--pending_queries_) { 137 | finish_query(); 138 | } 139 | } 140 | } 141 | 142 | void HttpQueryBlockInfo::got_full_transaction(td::BufferSlice result) { 143 | auto F = ton::fetch_tl_object(std::move(result), true); 144 | if (F.is_error()) { 145 | abort_query(F.move_as_error()); 146 | return; 147 | } 148 | 149 | auto f = F.move_as_ok(); 150 | transactionsInfo.push_back(std::move(f->transactions_)); 151 | if (f->ids_.size() == 0) { 152 | abort_query(td::Status::Error("no transactions found")); 153 | return; 154 | } 155 | if (!--pending_queries_) { 156 | finish_query(); 157 | } 158 | } 159 | 160 | void HttpQueryBlockInfo::got_config(td::BufferSlice data) { 161 | auto F = ton::fetch_tl_object(std::move(data), true); 162 | if (F.is_error()) { 163 | abort_query(F.move_as_error()); 164 | return; 165 | } 166 | auto f = F.move_as_ok(); 167 | 168 | state_proof_ = std::move(f->state_proof_); 169 | config_proof_ = std::move(f->config_proof_); 170 | 171 | if (!--pending_queries_) { 172 | finish_query(); 173 | } 174 | } 175 | 176 | void HttpQueryBlockInfo::start_transaction_query(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash) { 177 | auto P = 178 | td::PromiseCreator::lambda([SelfId = actor_id(this), addr = addr, lt = lt, hash = hash, this](td::Result R) { 179 | if (R.is_error()) { 180 | mtx->lock(); 181 | // LOG(INFO) << "transaction failed for block: " << block_id_.to_str() << " addr: " << addr.rserialize(true) << '\n'; 182 | transactionsNotPassed.push_back(TransactionDescr{addr, lt, hash}); 183 | mtx->unlock(); 184 | td::actor::send_closure(SelfId, &HttpQueryBlockInfo::next, R.move_as_error_prefix("litequery failed: ")); 185 | } else { 186 | td::actor::send_closure(SelfId, &HttpQueryBlockInfo::got_full_transaction, R.move_as_ok()); 187 | } 188 | }); 189 | auto a = ton::create_tl_object(addr.workchain, addr.addr); 190 | auto query = ton::serialize_tl_object( 191 | ton::create_tl_object(1, std::move(a), lt, hash), true); 192 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 193 | std::move(query), std::move(P)); 194 | pending_queries_++; 195 | } 196 | 197 | // it will return only one column 198 | 199 | #define VALIDATORSql(x, y) \ 200 | std::string( \ 201 | std::string("select distinct encode(wallet, 'escape') from validators v where \"time\" >= to_timestamp(") + \ 202 | std::to_string(x) + std::string(") and \"time\" <= to_timestamp(") + std::to_string(y) + std::string(");")) \ 203 | .c_str() 204 | 205 | bool HttpQueryBlockInfo::unpackValidators(HttpAnswer &A, std::shared_ptr conn) { 206 | UniValue uvArr; 207 | uvArr.setArray(); 208 | auto R = block::check_extract_state_proof(block_id_, state_proof_.as_slice(), config_proof_.as_slice()); 209 | if (R.is_error()) { 210 | A.abort(PSTRING() << "masterchain state proof for " << block_id_.to_str() << " is invalid : " << R.move_as_error());\ 211 | return false; 212 | } 213 | try { 214 | auto res = block::Config::extract_from_state(R.move_as_ok(), 0); 215 | if (res.is_error()) { 216 | A.abort(PSTRING() << "cannot unpack configuration: " << res.move_as_error()); 217 | return false; 218 | } 219 | std::unique_ptr config = res.move_as_ok(); 220 | td::Ref value = config->get_config_param(34); 221 | if (value.not_null()) { 222 | std::int64_t from = 0; 223 | std::int64_t to = 0; 224 | A.unpackValidatorsTime(HttpAnswer::ValidatorSet{34, value}, from, to); 225 | if (conn != nullptr) { 226 | std::unique_ptr resSQL(PQexec(conn.get(), VALIDATORSql(from, to)), &PQclear); 227 | if (PQresultStatus(resSQL.get()) != PGRES_TUPLES_OK) { 228 | A.abort(PSTRING() << "Select failed: " << PQresultErrorMessage(resSQL.get())); 229 | } else { 230 | for (int i = 0; i < PQntuples(resSQL.get()); i++) { 231 | for (int j = 0; j < PQnfields(resSQL.get()); j++) { 232 | uvArr.push_back(std::string(PQgetvalue(resSQL.get(), i, j))); 233 | } 234 | } 235 | } 236 | resSQL.reset(); 237 | } else { 238 | abort_query(td::Status::Error(404, PSTRING() << "unnable to connect to db")); 239 | A.abort(td::Status::Error(404, PSTRING() << "unnable to connect to db")); 240 | return false; 241 | } 242 | } else { 243 | abort_query(td::Status::Error(404, PSTRING() << "empty param " << 34)); 244 | A.abort(td::Status::Error(404, PSTRING() << "empty param " << 34)); 245 | return false; 246 | } 247 | } catch (vm::VmError &err) { 248 | A.abort(PSTRING() << "error while traversing configuration: " << err.get_msg()); 249 | return false; 250 | } catch (vm::VmVirtError &err) { 251 | A.abort(PSTRING() << "virtualization error while traversing configuration: " << err.get_msg()); 252 | return false; 253 | } 254 | A.putInJson("validators", uvArr); 255 | return true; 256 | } 257 | 258 | void HttpQueryBlockInfo::finish_query() { 259 | if (promise_) { 260 | auto page = [&](std::shared_ptr conn) -> std::string { 261 | HttpAnswer A{"blockinfo", prefix_}; 262 | A.set_block_id(block_id_); 263 | create_header(A); 264 | auto res = vm::std_boc_deserialize(data_.clone()); 265 | if (res.is_error()) { 266 | A.abort(PSTRING() << "cannot deserialize block header data: " << res.move_as_error()); 267 | return A.finish(); 268 | } 269 | auto data = HttpAnswer::RawData{res.move_as_ok()}; 270 | A.serializeBlockData(data.root, block_id_); 271 | 272 | UniValue uvArr; 273 | uvArr.setArray(); 274 | for(auto &tr : transactionsInfo) { 275 | auto R = vm::std_boc_deserialize_multi(std::move(tr)); 276 | if (R.is_error()) { 277 | A.abort(PSTRING() << "FATAL: cannot deserialize transactions BoC"); 278 | return A.finish(); 279 | } 280 | auto list = R.move_as_ok(); 281 | auto n = list.size(); 282 | if (n != 1) { 283 | A.abort(PSTRING() << "obtained " << n << " transaction, but only 1 have been requested"); 284 | return A.finish(); 285 | } else { 286 | UniValue uvObj; 287 | uvObj.setObject(); 288 | A.serializeObject(HttpAnswer::TransactionSmall{block_id_, list[0]}, uvObj); 289 | uvArr.push_back(uvObj); 290 | } 291 | } 292 | for (auto &tr : transactionsNotPassed) { 293 | UniValue uvObj; 294 | uvObj.setObject(); 295 | A.serializeObject(tr.addr, tr.lt, tr.hash, uvObj); 296 | uvArr.push_back(uvObj); 297 | } 298 | HttpAnswer::TransactionList I; 299 | I.block_id = block_id_; 300 | I.req_count_ = trans_req_count_; 301 | for (auto &T : transactions_) { 302 | I.vec.emplace_back(T.addr, T.lt, T.hash); 303 | } 304 | A.serializeObject(I); 305 | I.vec.clear(); 306 | transactions_.clear(); 307 | transactionsNotPassed.clear(); 308 | transactionsInfo.clear(); 309 | A.putInJson("transactions", uvArr); 310 | if (block_id_.id.workchain == -1) { 311 | unpackValidators(A, conn); 312 | } 313 | 314 | return A.finish(); 315 | }(this->conn); 316 | auto R = MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY); 317 | MHD_add_response_header(R, "Content-Type", "application/json"); 318 | promise_.set_value(std::move(R)); 319 | } 320 | stop(); 321 | } 322 | -------------------------------------------------------------------------------- /query-impl/block-search-extended.cpp: -------------------------------------------------------------------------------- 1 | #include "../blockchain-api-query.hpp" 2 | 3 | HttpQueryBlockSearch2::HttpQueryBlockSearch2(std::map opts, std::string prefix, 4 | td::Promise promise, std::shared_ptr scheduler_ptr_in) 5 | : HttpQueryCommon(std::move(prefix), std::move(promise)) { 6 | auto R2 = parse_account_prefix(opts, false); 7 | if (R2.is_ok()) { 8 | account_prefix_ = R2.move_as_ok(); 9 | } else { 10 | error_ = R2.move_as_error(); 11 | return; 12 | } 13 | if (opts.count("seqnoFrom") == 1) { 14 | try { 15 | seqnoFrom = static_cast(std::stoull(opts["seqnoFrom"])); 16 | start = seqnoFrom; 17 | } catch (...) { 18 | error_ = td::Status::Error("cannot parse seqno"); 19 | return; 20 | } 21 | } 22 | if (opts.count("seqnoTo") == 1) { 23 | try { 24 | seqnoTo = static_cast(std::stoull(opts["seqnoTo"])); 25 | } catch (...) { 26 | error_ = td::Status::Error("cannot parse seqno"); 27 | return; 28 | } 29 | } 30 | scheduler_ptr2 = scheduler_ptr_in; 31 | } 32 | 33 | void HttpQueryBlockSearch2::start_up_query() { 34 | for (ton::BlockSeqno i = seqnoFrom; i <= seqnoTo; i++) { 35 | SearchBlockRunner g{[&](td::Promise> promise) { 36 | td::actor::create_actor("blocksearchN", account_prefix_.workchain, 37 | account_prefix_.account_id_prefix, i, "test", std::move(promise)).release(); 38 | }, scheduler_ptr2}; 39 | dataTo.push_back(g.wait()); 40 | } 41 | finish_query(); 42 | } 43 | 44 | void HttpQueryBlockSearch2::finish_query() { 45 | if (promise_) { 46 | auto page = [&]() -> std::string { 47 | HttpAnswer A{"blockinfo", prefix_}; 48 | create_header(A); 49 | UniValue uvArr; 50 | uvArr.setArray(); 51 | for(auto &bd : dataTo) { 52 | UniValue uvObjBD; 53 | uvObjBD.setObject(); 54 | auto res = vm::std_boc_deserialize(bd->data_.clone()); 55 | // check for null 56 | if (res.is_error()) { 57 | A.abort(PSTRING() << "cannot deserialize block header data: " << res.move_as_error()); 58 | return A.finish(); 59 | } 60 | uvObjBD.pushKV("header", A.serializeBlockHeaderCellLite(HttpAnswer::BlockHeaderCell{bd->block_id_, res.move_as_ok()})); 61 | 62 | if (bd->shard_data_.size() > 0) { 63 | auto R = vm::std_boc_deserialize(bd->shard_data_.clone()); 64 | if (R.is_error()) { 65 | A.abort(PSTRING() << "cannot deserialize shard configuration: " << R.move_as_error()); 66 | return A.finish(); 67 | } else { 68 | uvObjBD.pushKV("shards", A.serializeBlockShardsCellLite(HttpAnswer::BlockShardsCell{bd->block_id_, R.move_as_ok()})); 69 | } 70 | } 71 | UniValue uvArrT; 72 | uvArrT.setArray(); 73 | for (auto &tr : bd->transactionsInfo) { 74 | auto R = vm::std_boc_deserialize_multi(std::move(tr)); 75 | if (R.is_error()) { 76 | A.abort(PSTRING() << "FATAL: cannot deserialize transactions BoC"); 77 | return A.finish(); 78 | } 79 | auto list = R.move_as_ok(); 80 | auto n = list.size(); 81 | if (n != 1) { 82 | A.abort(PSTRING() << "obtained " << n << " transaction, but only 1 have been requested"); 83 | return A.finish(); 84 | } else { 85 | UniValue uvObj; 86 | uvObj.setObject(); 87 | A.serializeObject(HttpAnswer::TransactionSmall{std::move(bd->block_id_), std::move(list[0])}, uvObj); // here some problems by valgrind 88 | uvArrT.push_back(uvObj); 89 | } 90 | } 91 | uvObjBD.pushKV("transactions", uvArrT); 92 | uvArr.push_back(uvObjBD); 93 | bd->transactionsInfo.clear(); 94 | bd->data_.clear(); 95 | bd->shard_data_.clear(); 96 | bd.reset(); 97 | } 98 | A.putInJson("answer", uvArr); 99 | return A.finish(); 100 | }(); 101 | dataTo.clear(); 102 | auto R = MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY); 103 | MHD_add_response_header(R, "Content-Type", "application/json"); 104 | promise_.set_value(std::move(R)); 105 | } 106 | stop(); 107 | } -------------------------------------------------------------------------------- /query-impl/block-search-hash.cpp: -------------------------------------------------------------------------------- 1 | #include "../blockchain-api-query.hpp" 2 | 3 | HttpQueryBlockSearchHash::HttpQueryBlockSearchHash(std::map opts, std::string prefix, 4 | td::Promise promise) 5 | : HttpQueryCommon(std::move(prefix), std::move(promise)) { 6 | auto R2 = parse_account_prefix(opts, false); 7 | if (R2.is_ok()) { 8 | account_prefix_ = R2.move_as_ok(); 9 | } else { 10 | error_ = R2.move_as_error(); 11 | return; 12 | } 13 | if (opts.count("seqno") + opts.count("lt") + opts.count("utime") != 1) { 14 | error_ = td::Status::Error(ton::ErrorCode::protoviolation, "exactly one of seqno/lt/utime must be set"); 15 | return; 16 | } 17 | if (opts.count("seqno") == 1) { 18 | try { 19 | seqno_ = static_cast(std::stoull(opts["seqno"])); 20 | mode_ = 1; 21 | } catch (...) { 22 | error_ = td::Status::Error("cannot parse seqno"); 23 | return; 24 | } 25 | } 26 | } 27 | 28 | void HttpQueryBlockSearchHash::start_up_query() { 29 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 30 | if (R.is_error()) { 31 | td::actor::send_closure(SelfId, &HttpQueryBlockSearchHash::abort_query, R.move_as_error_prefix("litequery failed: ")); 32 | } else { 33 | td::actor::send_closure(SelfId, &HttpQueryBlockSearchHash::got_block_header, R.move_as_ok()); 34 | } 35 | }); 36 | auto query = ton::serialize_tl_object(ton::create_tl_object( 37 | 1, 38 | ton::create_tl_lite_block_id_simple(ton::BlockId{ 39 | account_prefix_.workchain, account_prefix_.account_id_prefix, seqno_}), 40 | 0, 0), 41 | true); 42 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 43 | std::move(query), std::move(P)); 44 | pending_queries_++; 45 | } 46 | 47 | void HttpQueryBlockSearchHash::finish_query() { 48 | if (promise_) { 49 | auto page = [&]() -> std::string { 50 | HttpAnswer A{"lastBlock", prefix_}; 51 | if(error_.is_error()) { 52 | A.abort(error_.move_as_error()); 53 | return A.finish(); 54 | } 55 | A.set_block_id(block_id_); 56 | A.putInJson("block", A.serializeObject(HttpAnswer::BlockLink{block_id_})); 57 | return A.finish(); 58 | }(); 59 | auto R = MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY); 60 | MHD_add_response_header(R, "Content-Type", "application/json"); 61 | promise_.set_value(std::move(R)); 62 | } 63 | stop(); 64 | } 65 | -------------------------------------------------------------------------------- /query-impl/block-search.cpp: -------------------------------------------------------------------------------- 1 | #include "../blockchain-api-query.hpp" 2 | 3 | 4 | BlockSearch::BlockSearch(ton::WorkchainId workchain, ton::AccountIdPrefix account, 5 | ton::BlockSeqno seqno, std::string prefix, 6 | td::Promise promise) 7 | : HttpQueryCommon(std::move(prefix), std::move(promise)) 8 | , account_prefix_{workchain, account} 9 | , mode_(1) 10 | , seqno_(seqno) { 11 | } 12 | BlockSearch::BlockSearch(ton::WorkchainId workchain, ton::AccountIdPrefix account, 13 | ton::LogicalTime lt, std::string prefix, td::Promise promise) 14 | : HttpQueryCommon(std::move(prefix), std::move(promise)), account_prefix_{workchain, account}, mode_(2), lt_(lt) { 15 | } 16 | BlockSearch::BlockSearch(ton::WorkchainId workchain, ton::AccountIdPrefix account, bool dummy, 17 | ton::UnixTime utime, std::string prefix, td::Promise promise) 18 | : HttpQueryCommon(std::move(prefix), std::move(promise)) 19 | , account_prefix_{workchain, account} 20 | , mode_(4) 21 | , utime_(utime) { 22 | } 23 | 24 | 25 | BlockSearch::BlockSearch(std::map opts, std::string prefix, 26 | td::Promise promise) 27 | : HttpQueryCommon(std::move(prefix), std::move(promise)) { 28 | auto R2 = parse_account_prefix(opts, false); 29 | if (R2.is_ok()) { 30 | account_prefix_ = R2.move_as_ok(); 31 | } else { 32 | error_ = R2.move_as_error(); 33 | return; 34 | } 35 | if (opts.count("seqno") + opts.count("lt") + opts.count("utime") != 1) { 36 | error_ = td::Status::Error(ton::ErrorCode::protoviolation, "exactly one of seqno/lt/utime must be set"); 37 | return; 38 | } 39 | if (opts.count("seqno") == 1) { 40 | try { 41 | seqno_ = static_cast(std::stoull(opts["seqno"])); 42 | mode_ = 1; 43 | } catch (...) { 44 | error_ = td::Status::Error("cannot parse seqno"); 45 | return; 46 | } 47 | } 48 | if (opts.count("lt") == 1) { 49 | try { 50 | lt_ = std::stoull(opts["lt"]); 51 | mode_ = 2; 52 | } catch (...) { 53 | error_ = td::Status::Error("cannot parse lt"); 54 | return; 55 | } 56 | } 57 | if (opts.count("utime") == 1) { 58 | try { 59 | seqno_ = static_cast(std::stoull(opts["utime"])); 60 | mode_ = 1; 61 | } catch (...) { 62 | error_ = td::Status::Error("cannot parse utime"); 63 | return; 64 | } 65 | } 66 | } 67 | 68 | void BlockSearch::start_up_query() { 69 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 70 | if (R.is_error()) { 71 | td::actor::send_closure(SelfId, &BlockSearch::abort_query, R.move_as_error_prefix("litequery failed: ")); 72 | } else { 73 | td::actor::send_closure(SelfId, &BlockSearch::got_block_header, R.move_as_ok()); 74 | } 75 | }); 76 | auto query = ton::serialize_tl_object(ton::create_tl_object( 77 | mode_, 78 | ton::create_tl_lite_block_id_simple(ton::BlockId{ 79 | account_prefix_.workchain, account_prefix_.account_id_prefix, seqno_}), 80 | lt_, utime_), 81 | true); 82 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 83 | std::move(query), std::move(P)); 84 | } 85 | 86 | void BlockSearch::got_block_header(td::BufferSlice data) { 87 | auto F = ton::fetch_tl_object(std::move(data), true); 88 | if (F.is_error()) { 89 | abort_query(F.move_as_error()); 90 | return; 91 | } 92 | auto f = F.move_as_ok(); 93 | data_ = std::move(f->header_proof_); 94 | block_id_ = ton::create_block_id(f->id_); 95 | 96 | if (block_id_.is_masterchain()) { 97 | auto P_2 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 98 | if (R.is_error()) { 99 | td::actor::send_closure(SelfId, &BlockSearch::failed_to_get_shard_info, 100 | R.move_as_error_prefix("litequery failed: ")); 101 | } else { 102 | td::actor::send_closure(SelfId, &BlockSearch::got_shard_info, R.move_as_ok()); 103 | } 104 | }); 105 | auto query_2 = ton::serialize_tl_object( 106 | ton::create_tl_object(ton::create_tl_lite_block_id(block_id_)), 107 | true); 108 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 109 | std::move(query_2), std::move(P_2)); 110 | pending_queries_++; 111 | } 112 | 113 | auto query_3 = ton::serialize_tl_object(ton::create_tl_object( 114 | ton::create_tl_lite_block_id(block_id_), 7, 1024, nullptr, false, false), 115 | true); 116 | auto P_3 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 117 | if (R.is_error()) { 118 | td::actor::send_closure(SelfId, &BlockSearch::abort_query, R.move_as_error_prefix("litequery failed: ")); 119 | } else { 120 | td::actor::send_closure(SelfId, &BlockSearch::got_transactions, R.move_as_ok()); 121 | } 122 | }); 123 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 124 | std::move(query_3), std::move(P_3)); 125 | pending_queries_++; 126 | } 127 | 128 | void BlockSearch::got_full_transaction(td::BufferSlice result) { 129 | auto F = ton::fetch_tl_object(std::move(result), true); 130 | if (F.is_error()) { 131 | abort_query(F.move_as_error()); 132 | return; 133 | } 134 | 135 | auto f = F.move_as_ok(); 136 | transactionsInfo.push_back(std::move(f->transactions_)); 137 | if (f->ids_.size() == 0) { 138 | abort_query(td::Status::Error("no transactions found")); 139 | return; 140 | } 141 | if (!--pending_queries_) { 142 | finish_query(); 143 | } 144 | } 145 | 146 | void BlockSearch::start_transaction_query(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash) { 147 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 148 | if (R.is_error()) { 149 | td::actor::send_closure(SelfId, &BlockSearch::abort_query, 150 | R.move_as_error_prefix("litequery failed: ")); 151 | } else { 152 | td::actor::send_closure(SelfId, &BlockSearch::got_full_transaction, R.move_as_ok()); 153 | } 154 | }); 155 | auto a = ton::create_tl_object(addr.workchain, addr.addr); 156 | auto query = ton::serialize_tl_object( 157 | ton::create_tl_object(1, std::move(a), lt, hash), true); 158 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 159 | std::move(query), std::move(P)); 160 | pending_queries_++; 161 | } 162 | 163 | void BlockSearch::got_transactions(td::BufferSlice data) { 164 | auto F = ton::fetch_tl_object(std::move(data), true); 165 | if (F.is_error()) { 166 | abort_query(F.move_as_error()); 167 | return; 168 | } 169 | auto f = F.move_as_ok(); 170 | trans_req_count_ = f->req_count_; 171 | 172 | for(std::size_t i = 0; i < f->ids_.size(); i++) { 173 | transactions_.emplace_back(block::StdAddress{block_id_.id.workchain, f->ids_[i]->account_}, 174 | static_cast(f->ids_[i]->lt_), f->ids_[i]->hash_); 175 | start_transaction_query(block::StdAddress{block_id_.id.workchain, f->ids_[i]->account_}, 176 | static_cast(f->ids_[i]->lt_), f->ids_[i]->hash_); 177 | } 178 | 179 | if (f->incomplete_ && transactions_.size() > 0) { 180 | const auto &T = *transactions_.rbegin(); 181 | auto query_3 = ton::serialize_tl_object( 182 | ton::create_tl_object( 183 | ton::create_tl_lite_block_id(block_id_), 7 + 128, 1024, 184 | ton::create_tl_object(T.addr.addr, T.lt), false, false), 185 | true); 186 | auto P_3 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 187 | if (R.is_error()) { 188 | td::actor::send_closure(SelfId, &BlockSearch::abort_query, 189 | R.move_as_error_prefix("litequery failed: ")); 190 | } else { 191 | td::actor::send_closure(SelfId, &BlockSearch::got_transactions, R.move_as_ok()); 192 | } 193 | }); 194 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 195 | std::move(query_3), std::move(P_3)); 196 | } else { 197 | if (!--pending_queries_) { 198 | finish_query(); 199 | } 200 | } 201 | } 202 | 203 | void HttpQueryBlockSearch::finish_query() { 204 | if (promise_) { 205 | auto page = [&]() -> std::string { 206 | HttpAnswer A{"blockinfo", prefix_}; 207 | A.set_block_id(block_id_); 208 | create_header(A); 209 | if (error_.is_error()) { 210 | A.abort(error_.move_as_error()); 211 | return A.finish(); 212 | } 213 | auto res = vm::std_boc_deserialize(data_.clone()); 214 | if (res.is_error()) { 215 | A.abort(res.move_as_error()); 216 | return A.finish(); 217 | } 218 | 219 | A.serializeObject(HttpAnswer::BlockHeaderCell{block_id_, res.move_as_ok()}); 220 | 221 | if (shard_data_.size() > 0) { 222 | auto R = vm::std_boc_deserialize(shard_data_.clone()); 223 | if (R.is_error()) { 224 | A.abort(R.move_as_error()); 225 | return A.finish(); 226 | } else { 227 | A.serializeObject(HttpAnswer::BlockShardsCell{block_id_, R.move_as_ok()}); 228 | } 229 | } 230 | 231 | UniValue uvArr; 232 | uvArr.setArray(); 233 | for (auto &tr : transactionsInfo) { 234 | auto R = vm::std_boc_deserialize_multi(std::move(tr)); 235 | if (R.is_error()) { 236 | A.abort(R.move_as_error()); 237 | return A.finish(); 238 | } 239 | auto list = R.move_as_ok(); 240 | auto n = list.size(); 241 | if (n != 1) { 242 | A.abort(PSTRING() << "obtained " << n << " transaction, but only 1 have been requested"); 243 | return A.finish(); 244 | } else { 245 | UniValue uvObj; 246 | uvObj.setObject(); 247 | A.serializeObject(HttpAnswer::TransactionSmall{block_id_, list[0]}, uvObj); 248 | uvArr.push_back(uvObj); 249 | } 250 | } 251 | A.putInJson("transactions", uvArr); 252 | transactions_.clear(); 253 | return A.finish(); 254 | }(); 255 | auto R = MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY); 256 | MHD_add_response_header(R, "Content-Type", "application/json"); 257 | promise_.set_value(std::move(R)); 258 | } 259 | stop(); 260 | } 261 | 262 | -------------------------------------------------------------------------------- /query-impl/config.cpp: -------------------------------------------------------------------------------- 1 | #include "../blockchain-api-query.hpp" 2 | 3 | 4 | HttpQueryConfig::HttpQueryConfig(std::string prefix, ton::BlockIdExt block_id, std::vector params, 5 | td::Promise promise) 6 | : HttpQueryCommon(prefix, std::move(promise), std::move(block_id)), params_(std::move(params)) { 7 | } 8 | 9 | HttpQueryConfig::HttpQueryConfig(std::map opts, std::string prefix, 10 | td::Promise promise) 11 | : HttpQueryCommon(prefix, std::move(promise)) { 12 | auto R = parse_block_id(opts, true); 13 | if (R.is_error()) { 14 | error_ = R.move_as_error(); 15 | return; 16 | } 17 | block_id_ = R.move_as_ok(); 18 | 19 | auto it = opts.find("param"); 20 | if (it != opts.end()) { 21 | auto R2 = td::to_integer_safe(it->second); 22 | if (R2.is_error()) { 23 | error_ = R2.move_as_error(); 24 | return; 25 | } 26 | params_.push_back(R2.move_as_ok()); 27 | } 28 | } 29 | 30 | void HttpQueryConfig::start_up() { 31 | if (error_.is_error()) { 32 | abort_query(std::move(error_)); 33 | return; 34 | } 35 | if (block_id_.is_valid()) { 36 | send_main_query(); 37 | } else { 38 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 39 | if (R.is_error()) { 40 | td::actor::send_closure(SelfId, &HttpQueryConfig::abort_query, R.move_as_error()); 41 | } else { 42 | td::actor::send_closure(SelfId, &HttpQueryConfig::got_block, R.move_as_ok()); 43 | } 44 | }); 45 | 46 | auto query = ton::serialize_tl_object(ton::create_tl_object(), true); 47 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 48 | std::move(query), std::move(P)); 49 | } 50 | } 51 | 52 | void HttpQueryConfig::got_block(td::BufferSlice data) { 53 | auto F = ton::fetch_tl_object(std::move(data), true); 54 | if (F.is_error()) { 55 | abort_query(F.move_as_error()); 56 | return; 57 | } 58 | auto f = F.move_as_ok(); 59 | block_id_ = ton::create_block_id(f->last_); 60 | 61 | send_main_query(); 62 | } 63 | 64 | void HttpQueryConfig::send_main_query() { 65 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 66 | if (R.is_error()) { 67 | td::actor::send_closure(SelfId, &HttpQueryConfig::abort_query, R.move_as_error()); 68 | } else { 69 | td::actor::send_closure(SelfId, &HttpQueryConfig::got_result, R.move_as_ok()); 70 | } 71 | }); 72 | auto query = 73 | params_.size() > 0 74 | ? ton::serialize_tl_object(ton::create_tl_object( 75 | 0, ton::create_tl_lite_block_id(block_id_), std::vector(params_)), 76 | true) 77 | : ton::serialize_tl_object(ton::create_tl_object( 78 | 0, ton::create_tl_lite_block_id(block_id_)), 79 | true); 80 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 81 | std::move(query), std::move(P)); 82 | } 83 | 84 | void HttpQueryConfig::got_result(td::BufferSlice data) { 85 | auto F = ton::fetch_tl_object(std::move(data), true); 86 | if (F.is_error()) { 87 | abort_query(F.move_as_error()); 88 | return; 89 | } 90 | auto f = F.move_as_ok(); 91 | 92 | state_proof_ = std::move(f->state_proof_); 93 | config_proof_ = std::move(f->config_proof_); 94 | 95 | finish_query(); 96 | } 97 | 98 | void HttpQueryConfig::finish_query() { 99 | if (promise_) { 100 | auto page = [&]() -> std::string { 101 | HttpAnswer A{"config", prefix_, code_}; 102 | A.set_block_id(block_id_); 103 | auto R = block::check_extract_state_proof(block_id_, state_proof_.as_slice(), config_proof_.as_slice()); 104 | if (R.is_error()) { 105 | A.abort(PSTRING() << "masterchain state proof for " << block_id_.to_str() 106 | << " is invalid : " << R.move_as_error()); 107 | return A.finish(); 108 | } 109 | try { 110 | auto res = block::Config::extract_from_state(R.move_as_ok(), 0); 111 | if (res.is_error()) { 112 | A.abort(PSTRING() << "cannot unpack configuration: " << res.move_as_error()); 113 | return A.finish(); 114 | } 115 | auto config = res.move_as_ok(); 116 | if (params_.size() > 0) { 117 | for (int i : params_) { 118 | auto value = config->get_config_param(i); 119 | if (value.not_null()) { 120 | A.serializeObject(HttpAnswer::ConfigParam{i, value}); 121 | } else { 122 | A.abort(td::Status::Error(404, PSTRING() << "empty param " << i)); 123 | return A.finish(); 124 | } 125 | } 126 | } else { 127 | config->foreach_config_param([&](int i, td::Ref value) { 128 | if (value.not_null()) { 129 | A.serializeObject(HttpAnswer::ConfigParam{i, value}); 130 | } 131 | return true; 132 | }); 133 | config.reset(); 134 | } 135 | } catch (vm::VmError &err) { 136 | A.abort(PSTRING() << "error while traversing configuration: " << err.get_msg()); 137 | } catch (vm::VmVirtError &err) { 138 | A.abort(PSTRING() << "virtualization error while traversing configuration: " << err.get_msg()); 139 | } 140 | return A.finish(); 141 | }(); 142 | auto R = MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY); 143 | MHD_add_response_header(R, "Content-Type", "application/json"); 144 | promise_.set_value(std::move(R)); 145 | } 146 | stop(); 147 | } 148 | 149 | -------------------------------------------------------------------------------- /query-impl/run-method.cpp: -------------------------------------------------------------------------------- 1 | #include "../blockchain-api-query.hpp" 2 | 3 | HttpQueryRunMethod::HttpQueryRunMethod(ton::BlockIdExt block_id, block::StdAddress addr, std::string method_name, 4 | std::vector params, std::string prefix, 5 | td::Promise promise) 6 | : HttpQueryCommon(std::move(prefix), std::move(promise), std::move(block_id)) 7 | , addr_(addr) 8 | , method_name_(std::move(method_name)) 9 | , params_(std::move(params)) { 10 | } 11 | 12 | HttpQueryRunMethod::HttpQueryRunMethod(std::map opts, std::string prefix, 13 | td::Promise promise) 14 | : HttpQueryCommon(std::move(prefix), std::move(promise)) { 15 | auto R = parse_block_id(opts, true); 16 | if (R.is_ok()) { 17 | block_id_ = R.move_as_ok(); 18 | if (!block_id_.is_valid()) { 19 | block_id_.id.workchain = ton::masterchainId; 20 | block_id_.id.shard = ton::shardIdAll; 21 | block_id_.id.seqno = static_cast(0xffffffff); 22 | block_id_.root_hash.set_zero(); 23 | block_id_.file_hash.set_zero(); 24 | } 25 | } else { 26 | error_ = R.move_as_error(); 27 | return; 28 | } 29 | auto R2 = parse_account_addr(opts); 30 | if (R2.is_ok()) { 31 | addr_ = R2.move_as_ok(); 32 | } else { 33 | error_ = R2.move_as_error(); 34 | return; 35 | } 36 | auto it = opts.find("method"); 37 | if (it == opts.end()) { 38 | error_ = td::Status::Error("no method"); 39 | return; 40 | } else { 41 | method_name_ = it->second; 42 | } 43 | it = opts.find("params"); 44 | if (it != opts.end()) { 45 | auto R3 = vm::parse_stack_entries(it->second); 46 | if (R3.is_error()) { 47 | error_ = R3.move_as_error(); 48 | return; 49 | } 50 | params_ = R3.move_as_ok(); 51 | } 52 | } 53 | 54 | void HttpQueryRunMethod::start_up_query() { 55 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 56 | if (R.is_error()) { 57 | td::actor::send_closure(SelfId, &HttpQueryRunMethod::abort_query, R.move_as_error_prefix("litequery failed: ")); 58 | } else { 59 | td::actor::send_closure(SelfId, &HttpQueryRunMethod::got_account, R.move_as_ok()); 60 | } 61 | }); 62 | auto a = ton::create_tl_object(addr_.workchain, addr_.addr); 63 | auto query = ton::serialize_tl_object(ton::create_tl_object( 64 | ton::create_tl_lite_block_id(block_id_), std::move(a)), 65 | true); 66 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 67 | std::move(query), std::move(P)); 68 | } 69 | 70 | void HttpQueryRunMethod::got_account(td::BufferSlice data) { 71 | auto F = ton::fetch_tl_object(std::move(data), true); 72 | if (F.is_error()) { 73 | abort_query(F.move_as_error()); 74 | return; 75 | } 76 | 77 | auto f = F.move_as_ok(); 78 | data_ = std::move(f->state_); 79 | proof_ = std::move(f->proof_); 80 | shard_proof_ = std::move(f->shard_proof_); 81 | block_id_ = ton::create_block_id(f->id_); 82 | res_block_id_ = ton::create_block_id(f->shardblk_); 83 | 84 | finish_query(); 85 | } 86 | 87 | void HttpQueryRunMethod::finish_query() { 88 | if (promise_) { 89 | auto page = [&]() -> std::string { 90 | HttpAnswer A{"account", prefix_, code_}; 91 | A.set_account_id(addr_); 92 | A.set_block_id(res_block_id_); 93 | 94 | block::AccountState account_state; 95 | account_state.blk = block_id_; 96 | account_state.shard_blk = res_block_id_; 97 | account_state.shard_proof = std::move(shard_proof_); 98 | account_state.proof = std::move(proof_); 99 | account_state.state = std::move(data_); 100 | auto r_info = account_state.validate(block_id_, addr_); 101 | if (r_info.is_error()) { 102 | A.abort(r_info.move_as_error()); 103 | return A.finish(); 104 | } 105 | auto info = r_info.move_as_ok(); 106 | if (info.root.is_null()) { 107 | A.abort(PSTRING() << "account state of " << addr_ << " is empty (cannot run method `" << method_name_ << "`)"); 108 | return A.finish(); 109 | } 110 | block::gen::Account::Record_account acc; 111 | block::gen::AccountStorage::Record store; 112 | block::CurrencyCollection balance; 113 | if (!(tlb::unpack_cell(info.root, acc) && tlb::csr_unpack(acc.storage, store) && 114 | balance.validate_unpack(store.balance))) { 115 | A.abort("error unpacking account state"); 116 | return A.finish(); 117 | } 118 | int tag = block::gen::t_AccountState.get_tag(*store.state); 119 | switch (tag) { 120 | case block::gen::AccountState::account_uninit: 121 | A.abort(PSTRING() << "account " << addr_ << " not initialized yet (cannot run any methods)"); 122 | return A.finish(); 123 | case block::gen::AccountState::account_frozen: 124 | A.abort(PSTRING() << "account " << addr_ << " frozen (cannot run any methods)"); 125 | return A.finish(); 126 | } 127 | 128 | CHECK(store.state.write().fetch_ulong(1) == 1); // account_init$1 _:StateInit = AccountState; 129 | block::gen::StateInit::Record state_init; 130 | CHECK(tlb::csr_unpack(store.state, state_init)); 131 | auto code = state_init.code->prefetch_ref(); 132 | auto data = state_init.data->prefetch_ref(); 133 | auto stack = td::make_ref(std::move(params_)); 134 | td::int64 method_id = (td::crc16(td::Slice{method_name_}) & 0xffff) | 0x10000; 135 | stack.write().push_smallint(method_id); 136 | long long gas_limit = vm::GasLimits::infty; 137 | vm::GasLimits gas{gas_limit}; 138 | LOG(DEBUG) << "creating VM"; 139 | vm::VmState vm{code, std::move(stack), gas, 1, data, vm::VmLog()}; 140 | vm.set_c7(prepare_vm_c7(info.gen_utime, info.gen_lt, acc.addr, balance)); // tuple with SmartContractInfo 141 | // vm.incr_stack_trace(1); // enable stack dump after each step 142 | int exit_code = ~vm.run(); 143 | if (exit_code != 0) { 144 | A.abort(PSTRING() << "VM terminated with error code " << exit_code); 145 | return A.finish(); 146 | } 147 | stack = vm.get_stack_ref(); 148 | std::ostringstream os; 149 | os << "result: "; 150 | stack->dump(os, 3); 151 | A.serializeObject(HttpAnswer::Stack{stack}); 152 | 153 | return A.finish(); 154 | }(); 155 | auto R = MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY); 156 | MHD_add_response_header(R, "Content-Type", "application/json"); 157 | promise_.set_value(std::move(R)); 158 | } 159 | stop(); 160 | } 161 | 162 | 163 | -------------------------------------------------------------------------------- /query-impl/search-by-height.cpp: -------------------------------------------------------------------------------- 1 | #include "../blockchain-api-query.hpp" 2 | 3 | 4 | HttpQuerySearchByHeight::HttpQuerySearchByHeight(std::map opts, std::string prefix, 5 | td::Promise promise, DatabaseConfigParams *dbConfParams) 6 | : HttpQueryCommon(std::move(prefix), std::move(promise)) { 7 | if (opts.count("workchain") == 1) { 8 | try { 9 | account_prefix_.workchain = static_cast(std::stoull(opts["workchain"])); 10 | mode_ = 1; 11 | } catch (...) { 12 | error_ = td::Status::Error("cannot parse workchain"); 13 | return; 14 | } 15 | } 16 | if (opts.count("seqno") == 1) { 17 | try { 18 | seqno_ = static_cast(std::stoull(opts["seqno"])); 19 | mode_ = 1; 20 | } catch (...) { 21 | error_ = td::Status::Error("cannot parse seqno"); 22 | return; 23 | } 24 | } 25 | if (dbConfParams != NULL) { 26 | if (dbConfParams->conn != NULL) { 27 | this->conn = dbConfParams->conn; 28 | } else { 29 | error_ = td::Status::Error("Connection is empty"); 30 | } 31 | if (dbConfParams->mtxDB != NULL) { 32 | this->mtx = dbConfParams->mtxDB; 33 | } else { 34 | error_ = td::Status::Error("Mutex is null"); 35 | } 36 | } else { 37 | error_ = td::Status::Error("DB not connected"); 38 | } 39 | } 40 | 41 | #define SELECTShards(height, wc) \ 42 | std::string( \ 43 | "select shard_id, encode(filehash, 'hex') as filehash, encode(roothash, 'hex') as roothash from ton_block tb where " \ 44 | "tb.workchain = " + \ 45 | std::to_string(wc) + " and tb.seqno = " + std::to_string(height)) \ 46 | .c_str() 47 | 48 | void HttpQuerySearchByHeight::find_shards() { 49 | std::string incomeShard; 50 | if (conn != nullptr) { 51 | mtx->lock(); 52 | std::unique_ptr resSQL( 53 | PQexec(conn.get(), SELECTShards(seqno_, account_prefix_.workchain)), &PQclear); 54 | mtx->unlock(); 55 | if (PQresultStatus(resSQL.get()) != PGRES_TUPLES_OK) { 56 | error_ = td::Status::Error("Select failed: " + std::string(PQresultErrorMessage(resSQL.get()))); 57 | resSQL.reset(); 58 | finish_query(); 59 | } else { 60 | for (int i = 0; i < PQntuples(resSQL.get()); i++) { 61 | auto x = ton::FileHash(); 62 | auto y = ton::RootHash(); 63 | std::string dt; 64 | ton::BlockIdExt res_block_id_; 65 | for (int j = 0; j < PQnfields(resSQL.get()); j++) { 66 | switch (j) { 67 | case 0: 68 | res_block_id_.id.shard = std::stoll(PQgetvalue(resSQL.get(), i, j)); 69 | break; 70 | case 1: 71 | x.from_hex(td::Slice(PQgetvalue(resSQL.get(), i, j))); 72 | res_block_id_.file_hash = x; 73 | break; 74 | case 2: 75 | y.from_hex(td::Slice(PQgetvalue(resSQL.get(), i, j))); 76 | res_block_id_.root_hash = y; 77 | break; 78 | default: 79 | break; 80 | } 81 | } 82 | res_block_id_.id.seqno = seqno_; 83 | res_block_id_.id.workchain = account_prefix_.workchain; 84 | block_id_.push_back(res_block_id_); 85 | } 86 | } 87 | } else { 88 | error_ = td::Status::Error(404, PSTRING() << "unnable to connect to db"); 89 | } 90 | } 91 | 92 | void HttpQuerySearchByHeight::start_up_query() { 93 | if (error_.is_error()) { 94 | finish_query(); 95 | } 96 | find_shards(); 97 | finish_query(); 98 | } 99 | 100 | void HttpQuerySearchByHeight::finish_query() { 101 | if (promise_) { 102 | auto page = [&]() -> std::string { 103 | HttpAnswer A{"lastBlock", prefix_}; 104 | if (error_.is_error()) { 105 | A.abort(error_.move_as_error()); 106 | return A.finish(); 107 | } 108 | A.unpackBlockVectorForOneChain(block_id_); 109 | data_.clear(); 110 | block_id_.clear(); 111 | shards.clear(); 112 | return A.finish(); 113 | }(); 114 | auto R = MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY); 115 | MHD_add_response_header(R, "Content-Type", "application/json"); 116 | promise_.set_value(std::move(R)); 117 | } 118 | stop(); 119 | } 120 | -------------------------------------------------------------------------------- /query-impl/validators.cpp: -------------------------------------------------------------------------------- 1 | #include "../blockchain-api-query.hpp" 2 | 3 | 4 | HttpQueryValidators::HttpQueryValidators(std::string prefix, ton::BlockIdExt block_id, 5 | td::Promise promise) 6 | : HttpQueryCommon(prefix, std::move(promise), std::move(block_id)) { 7 | } 8 | 9 | HttpQueryValidators::HttpQueryValidators(std::map opts, std::string prefix, 10 | td::Promise promise) 11 | : HttpQueryCommon(prefix, std::move(promise)) { 12 | auto R = parse_block_id(opts, true); 13 | if (R.is_error()) { 14 | error_ = R.move_as_error(); 15 | return; 16 | } 17 | block_id_ = R.move_as_ok(); 18 | } 19 | 20 | void HttpQueryValidators::start_up() { 21 | if (error_.is_error()) { 22 | abort_query(std::move(error_)); 23 | return; 24 | } 25 | if (block_id_.is_valid()) { 26 | send_main_query(); 27 | } else { 28 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 29 | if (R.is_error()) { 30 | td::actor::send_closure(SelfId, &HttpQueryValidators::abort_query, R.move_as_error()); 31 | } else { 32 | td::actor::send_closure(SelfId, &HttpQueryValidators::got_block, R.move_as_ok()); 33 | } 34 | }); 35 | 36 | auto query = ton::serialize_tl_object(ton::create_tl_object(), true); 37 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 38 | std::move(query), std::move(P)); 39 | } 40 | } 41 | 42 | void HttpQueryValidators::got_block(td::BufferSlice data) { 43 | auto F = ton::fetch_tl_object(std::move(data), true); 44 | if (F.is_error()) { 45 | abort_query(F.move_as_error()); 46 | return; 47 | } 48 | auto f = F.move_as_ok(); 49 | block_id_ = ton::create_block_id(f->last_); 50 | 51 | send_main_query(); 52 | } 53 | 54 | void HttpQueryValidators::send_main_query() { 55 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 56 | if (R.is_error()) { 57 | td::actor::send_closure(SelfId, &HttpQueryValidators::abort_query, R.move_as_error()); 58 | } else { 59 | td::actor::send_closure(SelfId, &HttpQueryValidators::got_result, R.move_as_ok()); 60 | } 61 | }); 62 | auto query = ton::serialize_tl_object(ton::create_tl_object( 63 | 0, ton::create_tl_lite_block_id(block_id_), std::vector(params_)), 64 | true); 65 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 66 | std::move(query), std::move(P)); 67 | } 68 | 69 | void HttpQueryValidators::got_result(td::BufferSlice data) { 70 | auto F = ton::fetch_tl_object(std::move(data), true); 71 | if (F.is_error()) { 72 | abort_query(F.move_as_error()); 73 | return; 74 | } 75 | auto f = F.move_as_ok(); 76 | 77 | state_proof_ = std::move(f->state_proof_); 78 | config_proof_ = std::move(f->config_proof_); 79 | 80 | finish_query(); 81 | } 82 | 83 | void HttpQueryValidators::finish_query() { 84 | if (promise_) { 85 | auto page = [&]() -> std::string { 86 | HttpAnswer A{"config", prefix_, code_}; 87 | A.set_block_id(block_id_); 88 | auto R = block::check_extract_state_proof(block_id_, state_proof_.as_slice(), config_proof_.as_slice()); 89 | if (R.is_error()) { 90 | A.abort(PSTRING() << "masterchain state proof for " << block_id_.to_str() 91 | << " is invalid : " << R.move_as_error()); 92 | return A.finish(); 93 | } 94 | try { 95 | auto res = block::Config::extract_from_state(R.move_as_ok(), 0); 96 | if (res.is_error()) { 97 | A.abort(PSTRING() << "cannot unpack configuration: " << res.move_as_error()); 98 | return A.finish(); 99 | } 100 | auto config = res.move_as_ok(); 101 | auto value = config->get_config_param(params_[0]); 102 | if (value.not_null()) { 103 | A.uvObj2Json(A.serializeObject(HttpAnswer::ValidatorSet{params_[0], value})); 104 | } else { 105 | A.abort(td::Status::Error(404, PSTRING() << "empty param " << params_[0])); 106 | return A.finish(); 107 | } 108 | } catch (vm::VmError &err) { 109 | A.abort(PSTRING() << "error while traversing configuration: " << err.get_msg()); 110 | } catch (vm::VmVirtError &err) { 111 | A.abort(PSTRING() << "virtualization error while traversing configuration: " << err.get_msg()); 112 | } 113 | return A.finish(); 114 | }(); 115 | auto R = MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY); 116 | MHD_add_response_header(R, "Content-Type", "application/json"); 117 | promise_.set_value(std::move(R)); 118 | } 119 | stop(); 120 | } 121 | 122 | -------------------------------------------------------------------------------- /query-impl/view-account.cpp: -------------------------------------------------------------------------------- 1 | #include "../blockchain-api-query.hpp" 2 | 3 | 4 | HttpQueryViewAccount::HttpQueryViewAccount(ton::BlockIdExt block_id, block::StdAddress addr, std::string prefix, 5 | td::Promise promise) 6 | : HttpQueryCommon(std::move(prefix), std::move(promise), std::move(block_id)), addr_(addr) { 7 | } 8 | 9 | HttpQueryViewAccount::HttpQueryViewAccount(std::map opts, std::string prefix, 10 | td::Promise promise, bool fromDB, 11 | DatabaseConfigParams *dbConfParams) 12 | : HttpQueryCommon(std::move(prefix), std::move(promise)), fromDB(fromDB) { 13 | auto R = parse_block_id(opts, true); 14 | if (R.is_ok()) { 15 | block_id_ = R.move_as_ok(); 16 | if (!block_id_.is_valid()) { 17 | block_id_.id.workchain = ton::masterchainId; 18 | block_id_.id.shard = ton::shardIdAll; 19 | block_id_.id.seqno = static_cast(0xffffffff); 20 | block_id_.root_hash.set_zero(); 21 | block_id_.file_hash.set_zero(); 22 | } 23 | } else { 24 | error_ = R.move_as_error(); 25 | return; 26 | } 27 | auto R2 = parse_account_addr(opts); 28 | if (R2.is_ok()) { 29 | addr_ = R2.move_as_ok(); 30 | } else { 31 | error_ = R2.move_as_error(); 32 | return; 33 | } 34 | if (fromDB && !dbConfParams) { 35 | error_ = td::Status::Error("DB connection error"); 36 | return; 37 | } 38 | if (fromDB && dbConfParams) { 39 | if (dbConfParams->conn != NULL) { 40 | this->conn = dbConfParams->conn; 41 | } else { 42 | error_ = td::Status::Error("Connection is empty"); 43 | } 44 | if (dbConfParams->mtxDB != NULL) { 45 | this->mtx = dbConfParams->mtxDB; 46 | } else { 47 | error_ = td::Status::Error("Mutex is null"); 48 | } 49 | getFromDB(); 50 | block_id_ = std::move(res_block_id_); 51 | } 52 | } 53 | 54 | void HttpQueryViewAccount::start_up_query() { 55 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 56 | if (R.is_error()) { 57 | td::actor::send_closure(SelfId, &HttpQueryViewAccount::abort_query, R.move_as_error_prefix("litequery failed: ")); 58 | } else { 59 | td::actor::send_closure(SelfId, &HttpQueryViewAccount::got_account, R.move_as_ok()); 60 | } 61 | }); 62 | auto a = ton::create_tl_object(addr_.workchain, addr_.addr); 63 | auto query = ton::serialize_tl_object(ton::create_tl_object( 64 | ton::create_tl_lite_block_id(block_id_), std::move(a)), 65 | true); 66 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 67 | std::move(query), std::move(P)); 68 | } 69 | 70 | void HttpQueryViewAccount::got_account(td::BufferSlice data) { 71 | auto F = ton::fetch_tl_object(std::move(data), true); 72 | if (F.is_error()) { 73 | abort_query(F.move_as_error()); 74 | return; 75 | } 76 | 77 | auto f = F.move_as_ok(); 78 | data_ = std::move(f->state_); 79 | proof_ = std::move(f->proof_); 80 | res_block_id_ = ton::create_block_id(f->shardblk_); 81 | 82 | finish_query(); 83 | } 84 | 85 | void HttpQueryViewAccount::finish_query() { 86 | if (promise_) { 87 | auto page = [&]() -> std::string { 88 | HttpAnswer A{"account", prefix_}; 89 | if(error_.is_error()) { 90 | abort_query(error_.move_as_error()); 91 | } 92 | A.set_account_id(addr_); 93 | A.set_block_id(res_block_id_); 94 | auto R = vm::std_boc_deserialize(data_.clone()); 95 | // LOG(INFO) << (int)data_[0]; 96 | if (R.is_error()) { 97 | A.abort(PSTRING() << "FATAL: cannot deserialize account state" << R.move_as_error()); 98 | return A.finish(); 99 | } 100 | auto Q = vm::std_boc_deserialize_multi(proof_.clone()); 101 | if (Q.is_error()) { 102 | A.abort(PSTRING() << "FATAL: cannot deserialize account proof" << Q.move_as_error()); 103 | return A.finish(); 104 | } 105 | auto Q_roots = Q.move_as_ok(); 106 | auto root = R.move_as_ok(); 107 | A.serializeObject(HttpAnswer::AccountCell{addr_, res_block_id_, root, Q_roots}); 108 | return A.finish(); 109 | }(); 110 | auto R = MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY); 111 | MHD_add_response_header(R, "Content-Type", "application/json"); 112 | promise_.set_value(std::move(R)); 113 | } 114 | stop(); 115 | } 116 | -------------------------------------------------------------------------------- /query-impl/view-last-block.cpp: -------------------------------------------------------------------------------- 1 | #include "../blockchain-api-query.hpp" 2 | 3 | 4 | HttpQueryViewLastBlock::HttpQueryViewLastBlock(std::string prefix, td::Promise promise) 5 | : HttpQueryCommon(std::move(prefix), std::move(promise)) { 6 | } 7 | 8 | HttpQueryViewLastBlock::HttpQueryViewLastBlock(std::map opts, std::string prefix, 9 | td::Promise promise) 10 | : HttpQueryCommon(std::move(prefix), std::move(promise)) { 11 | } 12 | 13 | void HttpQueryViewLastBlock::start_up() { 14 | if (error_.is_error()) { 15 | abort_query(std::move(error_)); 16 | return; 17 | } 18 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 19 | if (R.is_error()) { 20 | td::actor::send_closure(SelfId, &HttpQueryViewLastBlock::abort_query, R.move_as_error()); 21 | } else { 22 | td::actor::send_closure(SelfId, &HttpQueryViewLastBlock::got_result, R.move_as_ok()); 23 | } 24 | }); 25 | 26 | auto query = ton::serialize_tl_object(ton::create_tl_object(), true); 27 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 28 | std::move(query), std::move(P)); 29 | } 30 | 31 | void HttpQueryViewLastBlock::got_result(td::BufferSlice data) { 32 | auto F = ton::fetch_tl_object(std::move(data), true); 33 | if (F.is_error()) { 34 | abort_query(F.move_as_error()); 35 | return; 36 | } 37 | auto f = F.move_as_ok(); 38 | res_block_id_ = ton::create_block_id(f->last_); 39 | 40 | finish_query(); 41 | } 42 | 43 | void HttpQueryViewLastBlock::finish_query() { 44 | if (promise_) { 45 | td::actor::create_actor("blockinfo", res_block_id_, prefix_, std::move(promise_)).release(); 46 | } 47 | stop(); 48 | } 49 | 50 | HttpQueryViewLastBlockNumber::HttpQueryViewLastBlockNumber(std::string prefix, td::Promise promise) 51 | : HttpQueryCommon(std::move(prefix), std::move(promise)) { 52 | } 53 | 54 | HttpQueryViewLastBlockNumber::HttpQueryViewLastBlockNumber(std::map opts, std::string prefix, 55 | td::Promise promise, bool fromDB, 56 | DatabaseConfigParams *dbConfParams) 57 | : HttpQueryCommon(std::move(prefix), std::move(promise)) { 58 | if (fromDB && dbConfParams) { 59 | if (dbConfParams->conn != NULL) { 60 | this->conn = dbConfParams->conn; 61 | } else { 62 | error_ = td::Status::Error("Connection is empty"); 63 | } 64 | if (dbConfParams->mtxDB != NULL) { 65 | this->mtx = dbConfParams->mtxDB; 66 | } else { 67 | error_ = td::Status::Error("Mutex is null"); 68 | } 69 | this->fromDb = true; 70 | } 71 | } 72 | 73 | void HttpQueryViewLastBlockNumber::start_up() { 74 | if (error_.is_error()) { 75 | abort_query(std::move(error_)); 76 | return; 77 | } 78 | if (fromDb) { 79 | getFromDB(); 80 | get_shards(); 81 | } else { 82 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 83 | if (R.is_error()) { 84 | td::actor::send_closure(SelfId, &HttpQueryViewLastBlockNumber::abort_query, R.move_as_error()); 85 | } else { 86 | td::actor::send_closure(SelfId, &HttpQueryViewLastBlockNumber::got_result, R.move_as_ok()); 87 | } 88 | }); 89 | 90 | auto query = ton::serialize_tl_object(ton::create_tl_object(), true); 91 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 92 | std::move(query), std::move(P)); 93 | 94 | pending_queries_++; 95 | } 96 | } 97 | 98 | void HttpQueryViewLastBlockNumber::get_shards() { 99 | auto P_2 = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 100 | if (R.is_error()) { 101 | td::actor::send_closure(SelfId, &HttpQueryViewLastBlockNumber::failed_to_get_shard_info, 102 | R.move_as_error_prefix("litequery failed: ")); 103 | } else { 104 | td::actor::send_closure(SelfId, &HttpQueryViewLastBlockNumber::got_shard_info, R.move_as_ok()); 105 | } 106 | }); 107 | auto query_2 = ton::serialize_tl_object( 108 | ton::create_tl_object(ton::create_tl_lite_block_id(res_block_id_)), 109 | true); 110 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 111 | std::move(query_2), std::move(P_2)); 112 | pending_queries_++; 113 | } 114 | 115 | 116 | void HttpQueryViewLastBlockNumber::got_result(td::BufferSlice data) { 117 | auto F = ton::fetch_tl_object(std::move(data), true); 118 | if (F.is_error()) { 119 | abort_query(F.move_as_error()); 120 | return; 121 | } 122 | auto f = F.move_as_ok(); 123 | res_block_id_ = ton::create_block_id(f->last_); 124 | pending_queries_--; 125 | get_shards(); 126 | } 127 | 128 | void HttpQueryViewLastBlockNumber::finish_query() { 129 | if (promise_) { 130 | auto page = [&]() -> std::string { 131 | HttpAnswer A{"lastBlock", prefix_}; 132 | A.set_block_id(res_block_id_); 133 | if (error_.is_error()) { 134 | A.abort(error_.move_as_error()); 135 | return A.finish(); 136 | } 137 | auto R = vm::std_boc_deserialize(shard_data_.clone()); 138 | if (R.is_error()) { 139 | A.abort(PSTRING() << "cannot deserialize shard configuration: " << R.move_as_error()); 140 | return A.finish(); 141 | } else { 142 | A.serializeObject(HttpAnswer::BlockShardsCellSmall{res_block_id_, R.move_as_ok()}); 143 | } 144 | return A.finish(); 145 | }(); 146 | auto R = MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY); 147 | MHD_add_response_header(R, "Content-Type", "application/json"); 148 | promise_.set_value(std::move(R)); 149 | } 150 | stop(); 151 | } 152 | -------------------------------------------------------------------------------- /query-impl/view-transaction-extended.cpp: -------------------------------------------------------------------------------- 1 | #include "../blockchain-api-query.hpp" 2 | 3 | 4 | HttpQueryViewTransaction2::HttpQueryViewTransaction2(ton::BlockIdExt block_id, block::StdAddress addr, 5 | ton::LogicalTime lt, std::string prefix, 6 | td::Promise promise) 7 | : HttpQueryCommon(std::move(prefix), std::move(promise), std::move(block_id)), addr_(addr), lt_(lt) { 8 | } 9 | 10 | HttpQueryViewTransaction2::HttpQueryViewTransaction2(std::map opts, std::string prefix, 11 | td::Promise promise, bool fromDB, DatabaseConfigParams *dbConfParams) 12 | : HttpQueryCommon(std::move(prefix), std::move(promise)), fromDB(fromDB) { 13 | auto R2 = parse_account_addr(opts); 14 | if (R2.is_ok()) { 15 | addr_ = R2.move_as_ok(); 16 | } else { 17 | error_ = R2.move_as_error(); 18 | return; 19 | } 20 | try { 21 | lt_ = std::stoull(opts["lt"]); 22 | } catch (...) { 23 | error_ = td::Status::Error("cannot trans parse lt"); 24 | return; 25 | } 26 | try { 27 | auto h = opts["hash"]; 28 | if (h.length() != 64) { 29 | error_ = td::Status::Error("cannot trans parse hash"); 30 | return; 31 | } 32 | auto R = td::hex_decode(td::Slice(h)); 33 | if (R.is_error()) { 34 | error_ = td::Status::Error("cannot trans parse hash"); 35 | return; 36 | } 37 | hash_.as_slice().copy_from(R.move_as_ok()); 38 | } catch (...) { 39 | error_ = td::Status::Error("cannot trans parse hash"); 40 | return; 41 | } 42 | if(fromDB && !dbConfParams) { 43 | error_ = td::Status::Error("DB connection error"); 44 | return; 45 | } 46 | if (fromDB && dbConfParams) { 47 | if (dbConfParams->conn != NULL) { 48 | this->conn = dbConfParams->conn; 49 | } else { 50 | error_ = td::Status::Error("Connection is empty"); 51 | } 52 | if (dbConfParams->mtxDB != NULL) { 53 | this->mtx = dbConfParams->mtxDB; 54 | } else { 55 | error_ = td::Status::Error("Mutex is null"); 56 | } 57 | } 58 | } 59 | 60 | #define SELECTTRBD(account, hs, lt) \ 61 | std::string(std::string("select encode(blob, 'hex') from ton_transaction where \ 62 | account = decode('") + account + \ 63 | std::string("', 'hex') and hash = decode('") + hs + \ 64 | std::string("', 'hex') and logical_time = ") + std::to_string(lt)).c_str() 65 | 66 | void HttpQueryViewTransaction2::readFromFile(td::BufferSlice &data) { 67 | 68 | if (conn != nullptr) { 69 | mtx->lock(); 70 | std::unique_ptr resSQL( 71 | PQexec(conn.get(), SELECTTRBD(addr_.addr.to_hex(), hash_.to_hex(), lt_)), &PQclear); 72 | mtx->unlock(); 73 | // LOG(INFO) << SELECTTRBD(addr_.addr.to_hex(), hash_.to_hex(), lt_); 74 | if (PQresultStatus(resSQL.get()) != PGRES_TUPLES_OK) { 75 | error_ = td::Status::Error("Select failed: " + std::string(PQresultErrorMessage(resSQL.get()))); 76 | // std::cout << "service error: " + std::string(PQresultErrorMessage(resSQL.get())) << '\n'; 77 | resSQL.reset(); 78 | finish_query(); 79 | } else { 80 | std::string dt = td::hex_decode(td::Slice(std::string(PQgetvalue(resSQL.get(), 0, 0)))).move_as_ok(); 81 | data = td::BufferSlice(dt.c_str(), dt.size()); 82 | } 83 | } else { 84 | error_ = td::Status::Error(404, PSTRING() << "unnable to connect to db"); 85 | finish_query(); 86 | } 87 | 88 | } 89 | 90 | void HttpQueryViewTransaction2::start_up_query() { 91 | if (fromDB) { 92 | readFromFile(data_); 93 | finish_query(); 94 | } else { 95 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 96 | if (R.is_error()) { 97 | td::actor::send_closure(SelfId, &HttpQueryViewTransaction2::abort_query, 98 | R.move_as_error_prefix("litequery failed: ")); 99 | } else { 100 | td::actor::send_closure(SelfId, &HttpQueryViewTransaction2::got_transaction, R.move_as_ok()); 101 | } 102 | }); 103 | auto a = ton::create_tl_object(addr_.workchain, addr_.addr); 104 | auto query = ton::serialize_tl_object( 105 | ton::create_tl_object(1, std::move(a), lt_, hash_), true); 106 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 107 | std::move(query), std::move(P)); 108 | } 109 | } 110 | 111 | void HttpQueryViewTransaction2::got_transaction(td::BufferSlice data) { 112 | auto F = ton::fetch_tl_object(std::move(data), true); 113 | if (F.is_error()) { 114 | abort_query(F.move_as_error()); 115 | return; 116 | } 117 | 118 | auto f = F.move_as_ok(); 119 | data_ = std::move(f->transactions_); 120 | if (f->ids_.size() == 0) { 121 | abort_query(td::Status::Error("no transactions found")); 122 | return; 123 | } 124 | block_id_ = ton::create_block_id(f->ids_[0]); 125 | 126 | finish_query(); 127 | } 128 | 129 | void HttpQueryViewTransaction2::finish_query() { 130 | if (promise_) { 131 | auto page = [&]() -> std::string { 132 | HttpAnswer A{"transaction", prefix_}; 133 | if (error_.is_error()) { 134 | A.abort(error_.move_as_error()); 135 | return A.finish(); 136 | } 137 | A.set_block_id(block_id_); 138 | A.set_account_id(addr_); 139 | auto R = vm::std_boc_deserialize_multi(std::move(data_)); 140 | if (R.is_error()) { 141 | A.abort(PSTRING() << "FATAL: cannot deserialize transactions BoC"); 142 | return A.finish(); 143 | } 144 | auto list = R.move_as_ok(); 145 | A.serializeObject(HttpAnswer::TransactionCell{addr_, block_id_, list[0]}); 146 | return A.finish(); 147 | }(); 148 | auto R = MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY); 149 | MHD_add_response_header(R, "Content-Type", "application/json"); 150 | promise_.set_value(std::move(R)); 151 | } 152 | stop(); 153 | } -------------------------------------------------------------------------------- /query-impl/view-transaction.cpp: -------------------------------------------------------------------------------- 1 | #include "../blockchain-api-query.hpp" 2 | 3 | 4 | HttpQueryViewTransaction::HttpQueryViewTransaction(block::StdAddress addr, ton::LogicalTime lt, ton::Bits256 hash, 5 | std::string prefix, td::Promise promise) 6 | : HttpQueryCommon(std::move(prefix), std::move(promise)), addr_(addr), lt_(lt), hash_(hash) { 7 | uvArr.setArray(); 8 | } 9 | 10 | HttpQueryViewTransaction::HttpQueryViewTransaction(std::map opts, std::string prefix, 11 | td::Promise promise, bool fromDB, DatabaseConfigParams *dbConfParams) 12 | : HttpQueryCommon(std::move(prefix), std::move(promise)), fromDB(fromDB) { 13 | auto R2 = parse_account_addr(opts); 14 | if (R2.is_ok()) { 15 | addr_ = R2.move_as_ok(); 16 | } else { 17 | error_ = R2.move_as_error(); 18 | return; 19 | } 20 | auto it = opts.find("count"); 21 | if (it != opts.end()) { 22 | try { 23 | count = std::stoi(it->second); 24 | } catch (...) { 25 | error_ = td::Status::Error(ton::ErrorCode::error, "bad count"); 26 | return; 27 | } 28 | } 29 | try { 30 | lt_ = std::stoull(opts["lt"]); 31 | } catch (...) { 32 | error_ = td::Status::Error("cannot trans parse lt"); 33 | return; 34 | } 35 | try { 36 | auto h = opts["hash"]; 37 | if (h.length() != 64) { 38 | error_ = td::Status::Error("cannot trans parse hash"); 39 | return; 40 | } 41 | auto R = td::hex_decode(td::Slice(h)); 42 | if (R.is_error()) { 43 | error_ = td::Status::Error("cannot trans parse hash"); 44 | return; 45 | } 46 | hash_.as_slice().copy_from(R.move_as_ok()); 47 | } catch (...) { 48 | error_ = td::Status::Error("cannot trans parse hash"); 49 | return; 50 | } 51 | try { 52 | auto codeIt = opts.find("code"); 53 | if(codeIt != opts.end()) { 54 | code_ = true; 55 | } 56 | } catch (...) { 57 | error_ = td::Status::Error("cannot trans parse hash"); 58 | return; 59 | } 60 | if(fromDB && !dbConfParams) { 61 | error_ = td::Status::Error("DB connection error"); 62 | return; 63 | } 64 | if (fromDB && dbConfParams) { 65 | if (dbConfParams->conn != NULL) { 66 | this->conn = dbConfParams->conn; 67 | } else { 68 | error_ = td::Status::Error("Connection is empty"); 69 | } 70 | if (dbConfParams->mtxDB != NULL) { 71 | this->mtx = dbConfParams->mtxDB; 72 | } else { 73 | error_ = td::Status::Error("Mutex is null"); 74 | } 75 | } 76 | uvArr.setArray(); 77 | } 78 | 79 | void HttpQueryViewTransaction::start_up_query() { 80 | if (fromDB) { 81 | readTransactionsFromDB(); 82 | finish_query(); 83 | } else { 84 | auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result R) { 85 | if (R.is_error()) { 86 | td::actor::send_closure(SelfId, &HttpQueryViewTransaction::abort_query, 87 | R.move_as_error_prefix("litequery failed: ")); 88 | } else { 89 | td::actor::send_closure(SelfId, &HttpQueryViewTransaction::got_transaction, R.move_as_ok()); 90 | } 91 | }); 92 | auto a = ton::create_tl_object(addr_.workchain, addr_.addr); 93 | auto query = ton::serialize_tl_object( 94 | ton::create_tl_object(count, std::move(a), lt_, hash_), true); 95 | td::actor::send_closure(CoreActorInterface::instance_actor_id(), &CoreActorInterface::send_lite_query, 96 | std::move(query), std::move(P)); 97 | } 98 | } 99 | 100 | void HttpQueryViewTransaction::got_transaction(td::BufferSlice data) { 101 | auto F = ton::fetch_tl_object(std::move(data), true); 102 | if (F.is_error()) { 103 | abort_query(F.move_as_error()); 104 | return; 105 | } 106 | 107 | auto f = F.move_as_ok(); 108 | data_ = std::move(f->transactions_); 109 | if (f->ids_.size() == 0) { 110 | abort_query(td::Status::Error("no transactions found")); 111 | return; 112 | } 113 | res_block_id_ = ton::create_block_id(f->ids_[0]); 114 | 115 | unpackRestart(); 116 | } 117 | 118 | #define SELECTTRBD(account, lt, count) \ 119 | std::string( \ 120 | "select blob, workchain, shard_id, seqno, encode(filehash, 'hex') as filehash, encode(roothash, 'hex') as \ 121 | roothash from (select encode(blob, 'hex') as blob, block, logical_time from ton_transaction tt where tt.account = decode('" + \ 122 | account + \ 123 | "', 'hex') \ 124 | and tt.logical_time <=" + \ 125 | std::to_string(lt) + \ 126 | " ) as tt \ 127 | join ton_block tb on \ 128 | tt.block = tb.id \ 129 | order by tt.logical_time desc limit " + \ 130 | std::to_string(count) + ";") \ 131 | .c_str() 132 | 133 | void HttpQueryViewTransaction::readTransactionsFromDB() { 134 | if (conn != nullptr) { 135 | mtx->lock(); 136 | std::unique_ptr resSQL( 137 | PQexec(conn.get(), SELECTTRBD(addr_.addr.to_hex(), lt_, count)), &PQclear); 138 | mtx->unlock(); 139 | // LOG(INFO) << SELECTTRBD(addr_.addr.to_hex(), lt_, count); 140 | if (PQresultStatus(resSQL.get()) != PGRES_TUPLES_OK) { 141 | error_ = td::Status::Error("Select failed: " + std::string(PQresultErrorMessage(resSQL.get()))); 142 | resSQL.reset(); 143 | finish_query(); 144 | } else { 145 | for (int i = 0; i < PQntuples(resSQL.get()); i++) { 146 | auto x = ton::FileHash(); 147 | auto y = ton::RootHash(); 148 | std::string dt; 149 | ton::BlockIdExt res_block_id_; 150 | for (int j = 0; j < PQnfields(resSQL.get()); j++) { 151 | switch (j) { 152 | case 0: 153 | dt = td::hex_decode(td::Slice(std::string(PQgetvalue(resSQL.get(), i, j)))).move_as_ok(); 154 | break; 155 | case 1: 156 | res_block_id_.id.workchain = std::atoi(PQgetvalue(resSQL.get(), i, j)); 157 | break; 158 | case 2: 159 | res_block_id_.id.shard = std::stoll(PQgetvalue(resSQL.get(), i, j)); 160 | break; 161 | case 3: 162 | res_block_id_.id.seqno = static_cast(std::stoul(PQgetvalue(resSQL.get(), i, j))); 163 | break; 164 | case 4: 165 | x.from_hex(td::Slice(PQgetvalue(resSQL.get(), i, j))); 166 | res_block_id_.file_hash = x; 167 | break; 168 | case 5: 169 | y.from_hex(td::Slice(PQgetvalue(resSQL.get(), i, j))); 170 | res_block_id_.root_hash = y; 171 | break; 172 | default: 173 | break; 174 | } 175 | } 176 | unpackFromDB(dt, res_block_id_); 177 | } 178 | 179 | } 180 | } else { 181 | error_ = td::Status::Error(404, PSTRING() << "unnable to connect to db"); 182 | finish_query(); 183 | } 184 | } 185 | 186 | void HttpQueryViewTransaction::unpackFromDB(std::string data, ton::BlockIdExt res_block_id_) { 187 | HttpAnswer A{"transaction", prefix_, code_}; 188 | auto R = vm::std_boc_deserialize_multi(std::move(td::BufferSlice(data.c_str(), data.size()))); 189 | if (R.is_error()) { 190 | error_ = td::Status::Error(404, PSTRING() << "error of unpacking query"); 191 | finish_query(); 192 | } 193 | auto list = R.move_as_ok(); 194 | UniValue uvObj; 195 | uvObj.setObject(); 196 | A.serializeObject(HttpAnswer::TransactionSmall{res_block_id_, list[0]}, uvObj); 197 | uvArr.push_back(uvObj); 198 | } 199 | 200 | void HttpQueryViewTransaction::unpackRestart() { 201 | bool goStart = false; 202 | if (promise_) { 203 | HttpAnswer A{"transaction", prefix_, code_}; 204 | HttpAnswer::TransactionDescr transDescr; 205 | A.set_block_id(res_block_id_); 206 | A.set_account_id(addr_); 207 | auto R = vm::std_boc_deserialize_multi(std::move(data_)); 208 | if (R.is_error()) { 209 | error_ = td::Status::Error(ton::ErrorCode::error, "bad count"); 210 | } 211 | auto list = R.move_as_ok(); 212 | if (!this->firstTryToGetTransactions) { 213 | list.erase(list.begin()); 214 | } 215 | auto n = list.size(); 216 | if(n == 0) { 217 | finish_query(); 218 | } 219 | 220 | for (std::size_t i = 0; i < n && this->count > 0; i++, this->count--) { 221 | UniValue uvObj; 222 | uvObj.setObject(); 223 | transDescr = A.serializeTransactionSmall(HttpAnswer::TransactionSmall{res_block_id_, list[i]}, uvObj); 224 | uvArr.push_back(uvObj); 225 | } 226 | 227 | if (this->count == 1 && lastCountTry) { 228 | finish_query(); 229 | } else { 230 | lastCountTry = true; 231 | this->count++; 232 | } 233 | 234 | if (this->count != 0 && n != 1) { 235 | lt_ = transDescr.lt; 236 | hash_ = transDescr.hash; 237 | addr_ = transDescr.addr; 238 | this->firstTryToGetTransactions = false; 239 | goStart = true; 240 | } else { 241 | finish_query(); 242 | } 243 | if (goStart) { 244 | start_up_query(); 245 | } 246 | } else { 247 | finish_query(); 248 | } 249 | } 250 | 251 | void HttpQueryViewTransaction::finish_query() { 252 | auto page = [&]() -> std::string { 253 | HttpAnswer A{"transaction", prefix_}; 254 | if (error_.is_error()) { 255 | A.abort(error_.move_as_error()); 256 | return A.finish(); 257 | } 258 | A.set_block_id(block_id_); 259 | A.putInJson("answer", uvArr); 260 | return A.finish(); 261 | }(); 262 | auto R = MHD_create_response_from_buffer(page.length(), const_cast(page.c_str()), MHD_RESPMEM_MUST_COPY); 263 | MHD_add_response_header(R, "Content-Type", "application/json"); 264 | promise_.set_value(std::move(R)); 265 | stop(); 266 | } 267 | --------------------------------------------------------------------------------