├── .github └── workflows │ ├── lint.yml │ └── main.yml ├── .golangci.yml ├── LICENSE ├── README.md ├── db ├── db.go ├── leveldb │ ├── leveldb.go │ └── leveldb_test.go ├── memory │ ├── memory.go │ └── memory_test.go ├── pebble │ ├── pebble.go │ └── pebble_test.go └── test │ └── test.go ├── go.mod ├── go.sum ├── merkletree.go ├── merkletree_test.go ├── node.go └── utils.go /.github/workflows/lint.yml: -------------------------------------------------------------------------------- 1 | name: Lint 2 | on: [ push, pull_request ] 3 | jobs: 4 | lint: 5 | runs-on: ubuntu-latest 6 | steps: 7 | - name: Install Go 8 | uses: actions/setup-go@v1 9 | with: 10 | go-version: 1.14.x 11 | - name: Checkout code 12 | uses: actions/checkout@v2 13 | - name: Lint 14 | run: | 15 | curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.30.0 16 | $(go env GOPATH)/bin/golangci-lint run --timeout=5m -c .golangci.yml 17 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | on: [push, pull_request] 3 | jobs: 4 | test: 5 | # matrix strategy from: https://github.com/mvdan/github-actions-golang/blob/master/.github/workflows/test.yml 6 | strategy: 7 | matrix: 8 | go-version: [1.13.x, 1.14.x] 9 | platform: [ubuntu-latest] 10 | runs-on: ${{ matrix.platform }} 11 | steps: 12 | - name: Install Go 13 | uses: actions/setup-go@v1 14 | with: 15 | go-version: ${{ matrix.go-version }} 16 | - name: Checkout code 17 | uses: actions/checkout@v2 18 | - name: Run tests 19 | run: go test ./... 20 | -------------------------------------------------------------------------------- /.golangci.yml: -------------------------------------------------------------------------------- 1 | issues: 2 | max-same-issues: 0 3 | exclude-use-default: false 4 | linters: 5 | enable: 6 | - whitespace 7 | - gosec 8 | - gci 9 | - misspell 10 | - gomnd 11 | - gofmt 12 | - goimports 13 | - lll 14 | - golint 15 | linters-settings: 16 | lll: 17 | line-length: 100 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-merkletree [![GoDoc](https://godoc.org/github.com/iden3/go-merkletree?status.svg)](https://godoc.org/github.com/iden3/go-merkletree) [![Go Report Card](https://goreportcard.com/badge/github.com/iden3/go-merkletree)](https://goreportcard.com/report/github.com/iden3/go-merkletree) [![Test](https://github.com/iden3/go-merkletree/workflows/Test/badge.svg)](https://github.com/iden3/go-merkletree/actions?query=workflow%3ATest) 2 | 3 | MerkleTree compatible with version from [circomlib](https://github.com/iden3/circomlib). 4 | 5 | Adaptation of the merkletree from https://github.com/iden3/go-iden3-core/tree/v0.0.8 with several changes and more functionalities. 6 | 7 | ## Usage 8 | More detailed examples can be found at the [tests](https://github.com/iden3/go-merkletree/blob/master/merkletree_test.go), and in the [documentation](https://godoc.org/github.com/iden3/go-merkletree). 9 | 10 | ```go 11 | import ( 12 | "fmt" 13 | "math/big" 14 | "testing" 15 | 16 | "github.com/iden3/go-iden3-core/db" 17 | "github.com/stretchr/testify/assert" 18 | ) 19 | 20 | [...] 21 | 22 | func TestExampleMerkleTree(t *testing.T) { 23 | mt, err := NewMerkleTree(db.NewMemoryStorage(), 10) 24 | assert.Nil(t, err) 25 | 26 | key := big.NewInt(1) 27 | value := big.NewInt(2) 28 | err = mt.Add(key, value) 29 | assert.Nil(t, err) 30 | fmt.Println(mt.Root().String()) 31 | 32 | v, err := mt.Get(key) 33 | asseert.Equal(t, value, v) 34 | 35 | value = big.NewInt(3) 36 | err = mt.Update(key, value) 37 | 38 | proof, err := mt.GenerateProof(key, nil) 39 | assert.Nil(t, err) 40 | 41 | assert.True(t, VerifyProof(mt.Root(), proof, key, value)) 42 | 43 | err := mt.Delete(big.NewInt(1)) // delete the leaf of key=1 44 | assert.Nil(t, err) 45 | } 46 | ``` 47 | -------------------------------------------------------------------------------- /db/db.go: -------------------------------------------------------------------------------- 1 | package db 2 | 3 | import ( 4 | "bytes" 5 | "crypto/sha256" 6 | "errors" 7 | ) 8 | 9 | // ErrNotFound is used by the implementations of the interface db.Storage for 10 | // when a key is not found in the storage 11 | var ErrNotFound = errors.New("key not found") 12 | 13 | // Storage is the interface that defines the methods for the storage used in 14 | // the merkletree. Examples of the interface implementation can be found at 15 | // db/memory and db/leveldb directories. 16 | type Storage interface { 17 | NewTx() (Tx, error) 18 | WithPrefix(prefix []byte) Storage 19 | Get([]byte) ([]byte, error) 20 | List(int) ([]KV, error) 21 | Close() 22 | Iterate(func([]byte, []byte) (bool, error)) error 23 | } 24 | 25 | // Tx is the interface that defines the methods for the db transaction used in 26 | // the merkletree storage. Examples of the interface implementation can be 27 | // found at db/memory and db/leveldb directories. 28 | type Tx interface { 29 | // Get retreives the value for the given key 30 | // looking first in the content of the Tx, and 31 | // then into the content of the Storage 32 | Get([]byte) ([]byte, error) 33 | // Put sets the key & value into the Tx 34 | Put(k, v []byte) error 35 | // Add adds the given Tx into the Tx 36 | Add(Tx) error 37 | Commit() error 38 | Close() 39 | } 40 | 41 | // KV contains a key (K) and a value (V) 42 | type KV struct { 43 | K []byte 44 | V []byte 45 | } 46 | 47 | // KvMap is a key-value map between a sha256 byte array hash, and a KV struct 48 | type KvMap map[[sha256.Size]byte]KV 49 | 50 | // Get retreives the value respective to a key from the KvMap 51 | func (m KvMap) Get(k []byte) ([]byte, bool) { 52 | v, ok := m[sha256.Sum256(k)] 53 | return v.V, ok 54 | } 55 | 56 | // Put stores a key and a value in the KvMap 57 | func (m KvMap) Put(k, v []byte) { 58 | m[sha256.Sum256(k)] = KV{k, v} 59 | } 60 | 61 | // Concat concatenates arrays of bytes 62 | func Concat(vs ...[]byte) []byte { 63 | var b bytes.Buffer 64 | for _, v := range vs { 65 | b.Write(v) 66 | } 67 | return b.Bytes() 68 | } 69 | 70 | // Clone clones a byte array into a new byte array 71 | func Clone(b0 []byte) []byte { 72 | b1 := make([]byte, len(b0)) 73 | copy(b1, b0) 74 | return b1 75 | } 76 | -------------------------------------------------------------------------------- /db/leveldb/leveldb.go: -------------------------------------------------------------------------------- 1 | package leveldb 2 | 3 | import ( 4 | "github.com/iden3/go-merkletree/db" 5 | "github.com/syndtr/goleveldb/leveldb" 6 | "github.com/syndtr/goleveldb/leveldb/errors" 7 | "github.com/syndtr/goleveldb/leveldb/opt" 8 | "github.com/syndtr/goleveldb/leveldb/util" 9 | ) 10 | 11 | // Storage implements the db.Storage interface 12 | type Storage struct { 13 | ldb *leveldb.DB 14 | prefix []byte 15 | } 16 | 17 | // StorageTx implements the db.Tx interface 18 | type StorageTx struct { 19 | *Storage 20 | cache db.KvMap 21 | } 22 | 23 | // NewLevelDbStorage returns a new Storage 24 | func NewLevelDbStorage(path string, errorIfMissing bool) (*Storage, error) { 25 | o := &opt.Options{ 26 | ErrorIfMissing: errorIfMissing, 27 | } 28 | ldb, err := leveldb.OpenFile(path, o) 29 | if err != nil { 30 | return nil, err 31 | } 32 | return &Storage{ldb, []byte{}}, nil 33 | } 34 | 35 | // WithPrefix implements the method WithPrefix of the interface db.Storage 36 | func (l *Storage) WithPrefix(prefix []byte) db.Storage { 37 | return &Storage{l.ldb, db.Concat(l.prefix, prefix)} 38 | } 39 | 40 | // NewTx implements the method NewTx of the interface db.Storage 41 | func (l *Storage) NewTx() (db.Tx, error) { 42 | return &StorageTx{l, make(db.KvMap)}, nil 43 | } 44 | 45 | // Get retreives a value from a key in the db.Storage 46 | func (l *Storage) Get(key []byte) ([]byte, error) { 47 | v, err := l.ldb.Get(db.Concat(l.prefix, key[:]), nil) 48 | if err == errors.ErrNotFound { 49 | return nil, db.ErrNotFound 50 | } 51 | return v, err 52 | } 53 | 54 | // Iterate implements the method Iterate of the interface db.Storage 55 | func (l *Storage) Iterate(f func([]byte, []byte) (bool, error)) error { 56 | // FIXME: Use the prefix! 57 | snapshot, err := l.ldb.GetSnapshot() 58 | if err != nil { 59 | return err 60 | } 61 | iter := snapshot.NewIterator(util.BytesPrefix(l.prefix), nil) 62 | defer iter.Release() 63 | for iter.Next() { 64 | localKey := iter.Key()[len(l.prefix):] 65 | if cont, err := f(localKey, iter.Value()); err != nil { 66 | return err 67 | } else if !cont { 68 | break 69 | } 70 | } 71 | iter.Release() 72 | return iter.Error() 73 | } 74 | 75 | // Get retreives a value from a key in the interface db.Tx 76 | func (tx *StorageTx) Get(key []byte) ([]byte, error) { 77 | var err error 78 | 79 | fullkey := db.Concat(tx.prefix, key) 80 | 81 | if value, ok := tx.cache.Get(fullkey); ok { 82 | return value, nil 83 | } 84 | 85 | value, err := tx.ldb.Get(fullkey, nil) 86 | if err == errors.ErrNotFound { 87 | return nil, db.ErrNotFound 88 | } 89 | 90 | return value, err 91 | } 92 | 93 | // Put saves a key:value into the db.Storage 94 | func (tx *StorageTx) Put(k, v []byte) error { 95 | tx.cache.Put(db.Concat(tx.prefix, k[:]), v) 96 | return nil 97 | } 98 | 99 | // Add implements the method Add of the interface db.Tx 100 | func (tx *StorageTx) Add(atx db.Tx) error { 101 | ldbtx := atx.(*StorageTx) 102 | for _, v := range ldbtx.cache { 103 | tx.cache.Put(v.K, v.V) 104 | } 105 | return nil 106 | } 107 | 108 | // Commit implements the method Commit of the interface db.Tx 109 | func (tx *StorageTx) Commit() error { 110 | var batch leveldb.Batch 111 | for _, v := range tx.cache { 112 | batch.Put(v.K, v.V) 113 | } 114 | 115 | tx.cache = nil 116 | return tx.ldb.Write(&batch, nil) 117 | } 118 | 119 | // Close implements the method Close of the interface db.Tx 120 | func (tx *StorageTx) Close() { 121 | tx.cache = nil 122 | } 123 | 124 | // Close implements the method Close of the interface db.Storage 125 | func (l *Storage) Close() { 126 | if err := l.ldb.Close(); err != nil { 127 | panic(err) 128 | } 129 | } 130 | 131 | // LevelDB is an extra method that returns the *leveldb.DB 132 | func (l *Storage) LevelDB() *leveldb.DB { 133 | return l.ldb 134 | } 135 | 136 | // List implements the method List of the interface db.Storage 137 | func (l *Storage) List(limit int) ([]db.KV, error) { 138 | ret := []db.KV{} 139 | err := l.Iterate(func(key []byte, value []byte) (bool, error) { 140 | ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)}) 141 | if len(ret) == limit { 142 | return false, nil 143 | } 144 | return true, nil 145 | }) 146 | return ret, err 147 | } 148 | -------------------------------------------------------------------------------- /db/leveldb/leveldb_test.go: -------------------------------------------------------------------------------- 1 | package leveldb 2 | 3 | import ( 4 | "io/ioutil" 5 | "os" 6 | "testing" 7 | 8 | "github.com/iden3/go-merkletree/db" 9 | "github.com/iden3/go-merkletree/db/test" 10 | "github.com/stretchr/testify/require" 11 | ) 12 | 13 | var rmDirs []string 14 | 15 | func levelDbStorage(t *testing.T) db.Storage { 16 | dir, err := ioutil.TempDir("", "db") 17 | rmDirs = append(rmDirs, dir) 18 | if err != nil { 19 | t.Fatal(err) 20 | return nil 21 | } 22 | sto, err := NewLevelDbStorage(dir, false) 23 | if err != nil { 24 | t.Fatal(err) 25 | return nil 26 | } 27 | return sto 28 | } 29 | 30 | func TestLevelDb(t *testing.T) { 31 | test.TestReturnKnownErrIfNotExists(t, levelDbStorage(t)) 32 | test.TestStorageInsertGet(t, levelDbStorage(t)) 33 | test.TestStorageWithPrefix(t, levelDbStorage(t)) 34 | test.TestConcatTx(t, levelDbStorage(t)) 35 | test.TestList(t, levelDbStorage(t)) 36 | test.TestIterate(t, levelDbStorage(t)) 37 | } 38 | 39 | func TestLevelDbInterface(t *testing.T) { 40 | var db db.Storage //nolint:gosimple 41 | 42 | dir, err := ioutil.TempDir("", "db") 43 | require.Nil(t, err) 44 | rmDirs = append(rmDirs, dir) 45 | sto, err := NewLevelDbStorage(dir, false) 46 | require.Nil(t, err) 47 | db = sto 48 | require.NotNil(t, db) 49 | } 50 | 51 | func TestMain(m *testing.M) { 52 | result := m.Run() 53 | for _, dir := range rmDirs { 54 | os.RemoveAll(dir) //nolint:errcheck,gosec 55 | } 56 | os.Exit(result) 57 | } 58 | -------------------------------------------------------------------------------- /db/memory/memory.go: -------------------------------------------------------------------------------- 1 | package memory 2 | 3 | import ( 4 | "bytes" 5 | "sort" 6 | 7 | "github.com/iden3/go-merkletree/db" 8 | ) 9 | 10 | // Storage implements the db.Storage interface 11 | type Storage struct { 12 | prefix []byte 13 | kv db.KvMap 14 | } 15 | 16 | // StorageTx implements the db.Tx interface 17 | type StorageTx struct { 18 | s *Storage 19 | kv db.KvMap 20 | } 21 | 22 | // NewMemoryStorage returns a new Storage 23 | func NewMemoryStorage() *Storage { 24 | kvmap := make(db.KvMap) 25 | return &Storage{[]byte{}, kvmap} 26 | } 27 | 28 | // WithPrefix implements the method WithPrefix of the interface db.Storage 29 | func (m *Storage) WithPrefix(prefix []byte) db.Storage { 30 | return &Storage{db.Concat(m.prefix, prefix), m.kv} 31 | } 32 | 33 | // NewTx implements the method NewTx of the interface db.Storage 34 | func (m *Storage) NewTx() (db.Tx, error) { 35 | return &StorageTx{m, make(db.KvMap)}, nil 36 | } 37 | 38 | // Get retreives a value from a key in the db.Storage 39 | func (m *Storage) Get(key []byte) ([]byte, error) { 40 | if v, ok := m.kv.Get(db.Concat(m.prefix, key[:])); ok { 41 | return v, nil 42 | } 43 | return nil, db.ErrNotFound 44 | } 45 | 46 | // Iterate implements the method Iterate of the interface db.Storage 47 | func (m *Storage) Iterate(f func([]byte, []byte) (bool, error)) error { 48 | kvs := make([]db.KV, 0) 49 | for _, v := range m.kv { 50 | if len(v.K) < len(m.prefix) || 51 | !bytes.Equal(v.K[:len(m.prefix)], m.prefix) { 52 | continue 53 | } 54 | localkey := v.K[len(m.prefix):] 55 | kvs = append(kvs, db.KV{K: localkey, V: v.V}) 56 | } 57 | sort.SliceStable(kvs, func(i, j int) bool { 58 | return bytes.Compare(kvs[i].K, kvs[j].K) < 0 59 | }) 60 | 61 | for _, kv := range kvs { 62 | if cont, err := f(kv.K, kv.V); err != nil { 63 | return err 64 | } else if !cont { 65 | break 66 | } 67 | } 68 | return nil 69 | } 70 | 71 | // Get implements the method Get of the interface db.Tx 72 | func (tx *StorageTx) Get(key []byte) ([]byte, error) { 73 | if v, ok := tx.kv.Get(db.Concat(tx.s.prefix, key)); ok { 74 | return v, nil 75 | } 76 | if v, ok := tx.s.kv.Get(db.Concat(tx.s.prefix, key)); ok { 77 | return v, nil 78 | } 79 | 80 | return nil, db.ErrNotFound 81 | } 82 | 83 | // Put implements the method Put of the interface db.Tx 84 | func (tx *StorageTx) Put(k, v []byte) error { 85 | tx.kv.Put(db.Concat(tx.s.prefix, k), v) 86 | return nil 87 | } 88 | 89 | // Commit implements the method Commit of the interface db.Tx 90 | func (tx *StorageTx) Commit() error { 91 | for _, v := range tx.kv { 92 | tx.s.kv.Put(v.K, v.V) 93 | } 94 | tx.kv = nil 95 | return nil 96 | } 97 | 98 | // Add implements the method Add of the interface db.Tx 99 | func (tx *StorageTx) Add(atx db.Tx) error { 100 | mstx := atx.(*StorageTx) 101 | for _, v := range mstx.kv { 102 | tx.kv.Put(v.K, v.V) 103 | } 104 | return nil 105 | } 106 | 107 | // Close implements the method Close of the interface db.Tx 108 | func (tx *StorageTx) Close() { 109 | tx.kv = nil 110 | } 111 | 112 | // Close implements the method Close of the interface db.Storage 113 | func (m *Storage) Close() { 114 | } 115 | 116 | // List implements the method List of the interface db.Storage 117 | func (m *Storage) List(limit int) ([]db.KV, error) { 118 | ret := []db.KV{} 119 | err := m.Iterate(func(key []byte, value []byte) (bool, error) { 120 | ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)}) 121 | if len(ret) == limit { 122 | return false, nil 123 | } 124 | return true, nil 125 | }) 126 | return ret, err 127 | } 128 | -------------------------------------------------------------------------------- /db/memory/memory_test.go: -------------------------------------------------------------------------------- 1 | package memory 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/iden3/go-merkletree/db" 7 | "github.com/iden3/go-merkletree/db/test" 8 | "github.com/stretchr/testify/require" 9 | ) 10 | 11 | func TestMemoryStorageInterface(t *testing.T) { 12 | var db db.Storage //nolint:gosimple 13 | 14 | db = NewMemoryStorage() 15 | require.NotNil(t, db) 16 | } 17 | 18 | func TestMemory(t *testing.T) { 19 | test.TestReturnKnownErrIfNotExists(t, NewMemoryStorage()) 20 | test.TestStorageInsertGet(t, NewMemoryStorage()) 21 | test.TestStorageWithPrefix(t, NewMemoryStorage()) 22 | test.TestConcatTx(t, NewMemoryStorage()) 23 | test.TestList(t, NewMemoryStorage()) 24 | test.TestIterate(t, NewMemoryStorage()) 25 | } 26 | -------------------------------------------------------------------------------- /db/pebble/pebble.go: -------------------------------------------------------------------------------- 1 | package pebble 2 | 3 | import ( 4 | "github.com/cockroachdb/pebble" 5 | "github.com/iden3/go-merkletree/db" 6 | ) 7 | 8 | // Storage implements the db.Storage interface 9 | type Storage struct { 10 | pdb *pebble.DB 11 | prefix []byte 12 | } 13 | 14 | // StorageTx implements the db.Tx interface 15 | type StorageTx struct { 16 | *Storage 17 | batch *pebble.Batch 18 | } 19 | 20 | // NewPebbleStorage returns a new Storage 21 | func NewPebbleStorage(path string, errorIfMissing bool) (*Storage, error) { 22 | o := &pebble.Options{ 23 | ErrorIfNotExists: errorIfMissing, 24 | } 25 | rdb, err := pebble.Open(path, o) 26 | if err != nil { 27 | return nil, err 28 | } 29 | return &Storage{rdb, []byte{}}, nil 30 | } 31 | 32 | // WithPrefix implements the method WithPrefix of the interface db.Storage 33 | func (p *Storage) WithPrefix(prefix []byte) db.Storage { 34 | return &Storage{p.pdb, db.Concat(p.prefix, prefix)} 35 | } 36 | 37 | // NewTx implements the method NewTx of the interface db.Storage 38 | func (p *Storage) NewTx() (db.Tx, error) { 39 | return &StorageTx{p, p.pdb.NewIndexedBatch()}, nil 40 | } 41 | 42 | // Get retreives a value from a key in the db.Storage 43 | func (p *Storage) Get(key []byte) ([]byte, error) { 44 | v, closer, err := p.pdb.Get(db.Concat(p.prefix, key[:])) 45 | if err == pebble.ErrNotFound { 46 | return nil, db.ErrNotFound 47 | } 48 | if err != nil { 49 | return nil, err 50 | } 51 | err = closer.Close() 52 | return v, err 53 | } 54 | 55 | //nolint:lll 56 | // https://github.com/cockroachdb/pebble/pull/923/files#diff-c2ade2f386c41794d5ebc57ee49b57a5fca8082e03255e5bff13977cbc061287R39 57 | func keyUpperBound(b []byte) []byte { 58 | end := make([]byte, len(b)) 59 | copy(end, b) 60 | for i := len(end) - 1; i >= 0; i-- { 61 | end[i] = end[i] + 1 62 | if end[i] != 0 { 63 | return end[:i+1] 64 | } 65 | } 66 | return nil // no upper-bound 67 | } 68 | func prefixIterOptions(prefix []byte) *pebble.IterOptions { 69 | return &pebble.IterOptions{ 70 | LowerBound: prefix, 71 | UpperBound: keyUpperBound(prefix), 72 | } 73 | } 74 | 75 | // Iterate implements the method Iterate of the interface db.Storage 76 | func (p *Storage) Iterate(f func([]byte, []byte) (bool, error)) (err error) { 77 | // NewIter already provides a point-in-time view of the current DB 78 | // state, but if is used for long term (is not the case), should use an 79 | // iterator over an snapshot: 80 | // snapshot := p.pdb.NewSnapshot() 81 | // defer snapshot.Close() 82 | // iter := snapshot.NewIter(nil) 83 | iter := p.pdb.NewIter(prefixIterOptions(p.prefix)) 84 | defer func() { 85 | err1 := iter.Close() 86 | if err != nil { 87 | return 88 | } 89 | err = err1 90 | }() 91 | 92 | for iter.First(); iter.Valid(); iter.Next() { 93 | localKey := iter.Key()[len(p.prefix):] 94 | if cont, err := f(localKey, iter.Value()); err != nil { 95 | return err 96 | } else if !cont { 97 | break 98 | } 99 | } 100 | return iter.Error() 101 | } 102 | 103 | // Get retreives a value from a key in the interface db.Tx 104 | func (tx *StorageTx) Get(key []byte) ([]byte, error) { 105 | var err error 106 | 107 | fullkey := db.Concat(tx.prefix, key) 108 | 109 | v, closer, err := tx.batch.Get(fullkey) 110 | if err == pebble.ErrNotFound { 111 | return nil, db.ErrNotFound 112 | } 113 | if err != nil { 114 | return nil, err 115 | } 116 | err = closer.Close() 117 | return v, err 118 | } 119 | 120 | // Put saves a key:value into the db.Storage 121 | func (tx *StorageTx) Put(k, v []byte) error { 122 | return tx.batch.Set(db.Concat(tx.prefix, k[:]), v, nil) 123 | } 124 | 125 | // Add implements the method Add of the interface db.Tx 126 | func (tx *StorageTx) Add(atx db.Tx) error { 127 | patx := atx.(*StorageTx) 128 | return tx.batch.Apply(patx.batch, nil) 129 | } 130 | 131 | // Commit implements the method Commit of the interface db.Tx 132 | func (tx *StorageTx) Commit() error { 133 | return tx.batch.Commit(nil) 134 | } 135 | 136 | // Close implements the method Close of the interface db.Tx 137 | func (tx *StorageTx) Close() { 138 | _ = tx.batch.Close() 139 | } 140 | 141 | // Close implements the method Close of the interface db.Storage 142 | func (p *Storage) Close() { 143 | if err := p.pdb.Close(); err != nil { 144 | panic(err) 145 | } 146 | } 147 | 148 | // Pebble is an extra method that returns the *pebble.DB 149 | func (p *Storage) Pebble() *pebble.DB { 150 | return p.pdb 151 | } 152 | 153 | // List implements the method List of the interface db.Storage 154 | func (p *Storage) List(limit int) ([]db.KV, error) { 155 | ret := []db.KV{} 156 | err := p.Iterate(func(key []byte, value []byte) (bool, error) { 157 | ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)}) 158 | if len(ret) == limit { 159 | return false, nil 160 | } 161 | return true, nil 162 | }) 163 | return ret, err 164 | } 165 | -------------------------------------------------------------------------------- /db/pebble/pebble_test.go: -------------------------------------------------------------------------------- 1 | package pebble 2 | 3 | import ( 4 | "io/ioutil" 5 | "os" 6 | "testing" 7 | 8 | "github.com/iden3/go-merkletree/db" 9 | "github.com/iden3/go-merkletree/db/test" 10 | "github.com/stretchr/testify/require" 11 | ) 12 | 13 | var rmDirs []string 14 | 15 | func pebbleStorage(t *testing.T) db.Storage { 16 | dir, err := ioutil.TempDir("", "db") 17 | rmDirs = append(rmDirs, dir) 18 | if err != nil { 19 | t.Fatal(err) 20 | return nil 21 | } 22 | sto, err := NewPebbleStorage(dir, false) 23 | if err != nil { 24 | t.Fatal(err) 25 | return nil 26 | } 27 | return sto 28 | } 29 | 30 | func TestPebble(t *testing.T) { 31 | test.TestReturnKnownErrIfNotExists(t, pebbleStorage(t)) 32 | test.TestStorageInsertGet(t, pebbleStorage(t)) 33 | test.TestStorageWithPrefix(t, pebbleStorage(t)) 34 | test.TestConcatTx(t, pebbleStorage(t)) 35 | test.TestIterate(t, pebbleStorage(t)) 36 | test.TestList(t, pebbleStorage(t)) 37 | } 38 | 39 | func TestPebbleInterface(t *testing.T) { 40 | var db db.Storage //nolint:gosimple 41 | 42 | dir, err := ioutil.TempDir("", "db") 43 | require.Nil(t, err) 44 | rmDirs = append(rmDirs, dir) 45 | sto, err := NewPebbleStorage(dir, false) 46 | require.Nil(t, err) 47 | db = sto 48 | require.NotNil(t, db) 49 | } 50 | 51 | func TestMain(m *testing.M) { 52 | result := m.Run() 53 | for _, dir := range rmDirs { 54 | os.RemoveAll(dir) //nolint:errcheck,gosec 55 | } 56 | os.Exit(result) 57 | } 58 | -------------------------------------------------------------------------------- /db/test/test.go: -------------------------------------------------------------------------------- 1 | //nolint:gomnd,golint 2 | package test 3 | 4 | import ( 5 | "testing" 6 | 7 | "github.com/iden3/go-merkletree/db" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | // TestReturnKnownErrIfNotExists checks that the implementation of the 12 | // db.Storage interface returns the expected error in the case that the value 13 | // is not found 14 | func TestReturnKnownErrIfNotExists(t *testing.T, sto db.Storage) { 15 | k := []byte("key") 16 | 17 | tx, err := sto.NewTx() 18 | assert.Nil(t, err) 19 | _, err = tx.Get(k) 20 | assert.EqualError(t, err, db.ErrNotFound.Error()) 21 | } 22 | 23 | // TestStorageInsertGet checks that the implementation of the db.Storage 24 | // interface behaves as expected 25 | func TestStorageInsertGet(t *testing.T, sto db.Storage) { 26 | key := []byte("key") 27 | value := []byte("data") 28 | 29 | tx, err := sto.NewTx() 30 | assert.Nil(t, err) 31 | err = tx.Put(key, value) 32 | assert.Nil(t, err) 33 | v, err := tx.Get(key) 34 | assert.Nil(t, err) 35 | assert.Equal(t, value, v) 36 | assert.Nil(t, tx.Commit()) 37 | 38 | tx, err = sto.NewTx() 39 | assert.Nil(t, err) 40 | v, err = tx.Get(key) 41 | assert.Nil(t, err) 42 | assert.Equal(t, value, v) 43 | } 44 | 45 | // TestStorageWithPrefix checks that the implementation of the db.Storage 46 | // interface behaves as expected for the WithPrefix method 47 | func TestStorageWithPrefix(t *testing.T, sto db.Storage) { 48 | k := []byte{9} 49 | 50 | sto1 := sto.WithPrefix([]byte{1}) 51 | sto2 := sto.WithPrefix([]byte{2}) 52 | 53 | // check within tx 54 | 55 | sto1tx, err := sto1.NewTx() 56 | assert.Nil(t, err) 57 | err = sto1tx.Put(k, []byte{4, 5, 6}) 58 | assert.Nil(t, err) 59 | v1, err := sto1tx.Get(k) 60 | assert.Nil(t, err) 61 | assert.Equal(t, v1, []byte{4, 5, 6}) 62 | assert.Nil(t, sto1tx.Commit()) 63 | 64 | sto2tx, err := sto2.NewTx() 65 | assert.Nil(t, err) 66 | err = sto2tx.Put(k, []byte{8, 9}) 67 | assert.Nil(t, err) 68 | v2, err := sto2tx.Get(k) 69 | assert.Nil(t, err) 70 | assert.Equal(t, v2, []byte{8, 9}) 71 | assert.Nil(t, sto2tx.Commit()) 72 | 73 | // check outside tx 74 | 75 | v1, err = sto1.Get(k) 76 | assert.Nil(t, err) 77 | assert.Equal(t, v1, []byte{4, 5, 6}) 78 | 79 | v2, err = sto2.Get(k) 80 | assert.Nil(t, err) 81 | assert.Equal(t, v2, []byte{8, 9}) 82 | } 83 | 84 | // TestIterate checks that the implementation of the db.Storage interface 85 | // behaves as expected for the Iterate method 86 | func TestIterate(t *testing.T, sto db.Storage) { 87 | r := []db.KV{} 88 | lister := func(k []byte, v []byte) (bool, error) { 89 | r = append(r, db.KV{K: db.Clone(k), V: db.Clone(v)}) 90 | return true, nil 91 | } 92 | 93 | sto1 := sto.WithPrefix([]byte{1}) 94 | err := sto1.Iterate(lister) 95 | assert.Nil(t, err) 96 | assert.Equal(t, 0, len(r)) 97 | 98 | sto1tx, _ := sto1.NewTx() 99 | err = sto1tx.Put([]byte{1}, []byte{4}) 100 | assert.Nil(t, err) 101 | err = sto1tx.Put([]byte{2}, []byte{5}) 102 | assert.Nil(t, err) 103 | err = sto1tx.Put([]byte{3}, []byte{6}) 104 | assert.Nil(t, err) 105 | assert.Nil(t, sto1tx.Commit()) 106 | 107 | sto2 := sto.WithPrefix([]byte{2}) 108 | sto2tx, _ := sto2.NewTx() 109 | err = sto2tx.Put([]byte{1}, []byte{7}) 110 | assert.Nil(t, err) 111 | err = sto2tx.Put([]byte{2}, []byte{8}) 112 | assert.Nil(t, err) 113 | err = sto2tx.Put([]byte{3}, []byte{9}) 114 | assert.Nil(t, err) 115 | assert.Nil(t, sto2tx.Commit()) 116 | 117 | r = []db.KV{} 118 | err = sto1.Iterate(lister) 119 | assert.Nil(t, err) 120 | assert.Equal(t, 3, len(r)) 121 | assert.Equal(t, db.KV{K: []byte{1}, V: []byte{4}}, r[0]) 122 | assert.Equal(t, db.KV{K: []byte{2}, V: []byte{5}}, r[1]) 123 | assert.Equal(t, db.KV{K: []byte{3}, V: []byte{6}}, r[2]) 124 | 125 | r = []db.KV{} 126 | err = sto2.Iterate(lister) 127 | assert.Nil(t, err) 128 | assert.Equal(t, 3, len(r)) 129 | assert.Equal(t, db.KV{K: []byte{1}, V: []byte{7}}, r[0]) 130 | assert.Equal(t, db.KV{K: []byte{2}, V: []byte{8}}, r[1]) 131 | assert.Equal(t, db.KV{K: []byte{3}, V: []byte{9}}, r[2]) 132 | } 133 | 134 | // TestConcatTx checks that the implementation of the db.Storage interface 135 | // behaves as expected 136 | func TestConcatTx(t *testing.T, sto db.Storage) { 137 | k := []byte{9} 138 | 139 | sto1 := sto.WithPrefix([]byte{1}) 140 | sto2 := sto.WithPrefix([]byte{2}) 141 | 142 | // check within tx 143 | 144 | sto1tx, err := sto1.NewTx() 145 | if err != nil { 146 | panic(err) 147 | } 148 | err = sto1tx.Put(k, []byte{4, 5, 6}) 149 | assert.Nil(t, err) 150 | sto2tx, err := sto2.NewTx() 151 | if err != nil { 152 | panic(err) 153 | } 154 | err = sto2tx.Put(k, []byte{8, 9}) 155 | assert.Nil(t, err) 156 | 157 | err = sto1tx.Add(sto2tx) 158 | assert.Nil(t, err) 159 | assert.Nil(t, sto1tx.Commit()) 160 | 161 | // check outside tx 162 | 163 | v1, err := sto1.Get(k) 164 | assert.Nil(t, err) 165 | assert.Equal(t, v1, []byte{4, 5, 6}) 166 | 167 | v2, err := sto2.Get(k) 168 | assert.Nil(t, err) 169 | assert.Equal(t, v2, []byte{8, 9}) 170 | } 171 | 172 | // TestList checks that the implementation of the db.Storage interface behaves 173 | // as expected 174 | func TestList(t *testing.T, sto db.Storage) { 175 | sto1 := sto.WithPrefix([]byte{1}) 176 | r1, err := sto1.List(100) 177 | assert.Nil(t, err) 178 | assert.Equal(t, 0, len(r1)) 179 | 180 | sto1tx, _ := sto1.NewTx() 181 | err = sto1tx.Put([]byte{1}, []byte{4}) 182 | assert.Nil(t, err) 183 | err = sto1tx.Put([]byte{2}, []byte{5}) 184 | assert.Nil(t, err) 185 | err = sto1tx.Put([]byte{3}, []byte{6}) 186 | assert.Nil(t, err) 187 | assert.Nil(t, sto1tx.Commit()) 188 | 189 | sto2 := sto.WithPrefix([]byte{2}) 190 | sto2tx, _ := sto2.NewTx() 191 | err = sto2tx.Put([]byte{1}, []byte{7}) 192 | assert.Nil(t, err) 193 | err = sto2tx.Put([]byte{2}, []byte{8}) 194 | assert.Nil(t, err) 195 | err = sto2tx.Put([]byte{3}, []byte{9}) 196 | assert.Nil(t, err) 197 | assert.Nil(t, sto2tx.Commit()) 198 | 199 | r, err := sto1.List(100) 200 | assert.Nil(t, err) 201 | assert.Equal(t, 3, len(r)) 202 | assert.Equal(t, r[0], db.KV{K: []byte{1}, V: []byte{4}}) 203 | assert.Equal(t, r[1], db.KV{K: []byte{2}, V: []byte{5}}) 204 | assert.Equal(t, r[2], db.KV{K: []byte{3}, V: []byte{6}}) 205 | 206 | r, err = sto1.List(2) 207 | assert.Nil(t, err) 208 | assert.Equal(t, 2, len(r)) 209 | assert.Equal(t, r[0], db.KV{K: []byte{1}, V: []byte{4}}) 210 | assert.Equal(t, r[1], db.KV{K: []byte{2}, V: []byte{5}}) 211 | } 212 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/iden3/go-merkletree 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/cockroachdb/pebble v0.0.0-20201229190758-9e27ae169fdd 7 | github.com/iden3/go-iden3-crypto v0.0.6-0.20210308142348-8f85683b2cef 8 | github.com/stretchr/testify v1.6.1 9 | github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d 10 | ) 11 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= 3 | github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8= 4 | github.com/Azure/azure-pipeline-go v0.2.1/go.mod h1:UGSo8XybXnIGZ3epmeBw7Jdz+HiUVpqIlpz/HKHylF4= 5 | github.com/Azure/azure-pipeline-go v0.2.2/go.mod h1:4rQ/NZncSvGqNkkOsNpOU1tgoNuIlp9AfUH5G1tvCHc= 6 | github.com/Azure/azure-storage-blob-go v0.7.0/go.mod h1:f9YQKtsG1nMisotuTPpO0tjNuEjKRYAcJU8/ydDI++4= 7 | github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI= 8 | github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0= 9 | github.com/Azure/go-autorest/autorest/adal v0.8.0/go.mod h1:Z6vX6WXXuyieHAXwMj0S6HY6e6wcHn37qQMBQlvY3lc= 10 | github.com/Azure/go-autorest/autorest/date v0.1.0/go.mod h1:plvfp3oPSKwf2DNjlBjWF/7vwR+cUD/ELuzDCXwHUVA= 11 | github.com/Azure/go-autorest/autorest/date v0.2.0/go.mod h1:vcORJHLJEh643/Ioh9+vPmf1Ij9AEBM5FuBIXLmIy0g= 12 | github.com/Azure/go-autorest/autorest/mocks v0.1.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0= 13 | github.com/Azure/go-autorest/autorest/mocks v0.2.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0= 14 | github.com/Azure/go-autorest/autorest/mocks v0.3.0/go.mod h1:a8FDP3DYzQ4RYfVAxAN3SVSiiO77gL2j2ronKKP0syM= 15 | github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc= 16 | github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk= 17 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 18 | github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= 19 | github.com/CloudyKit/fastprinter v0.0.0-20170127035650-74b38d55f37a/go.mod h1:EFZQ978U7x8IRnstaskI3IysnWY5Ao3QgZUKOXlsAdw= 20 | github.com/CloudyKit/jet v2.1.3-0.20180809161101-62edd43e4f88+incompatible/go.mod h1:HPYO+50pSWkPoj9Q/eq0aRGByCL6ScRlUmiEX5Zgm+w= 21 | github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY= 22 | github.com/Joker/jade v1.0.1-0.20190614124447-d475f43051e7/go.mod h1:6E6s8o2AE4KhCrqr6GRJjdC/gNfTdxkIXvuGZZda2VM= 23 | github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= 24 | github.com/OneOfOne/xxhash v1.2.5/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q= 25 | github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0= 26 | github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= 27 | github.com/VictoriaMetrics/fastcache v1.5.3/go.mod h1:+jv9Ckb+za/P1ZRg/sulP5Ni1v49daAVERr0H3CuscE= 28 | github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= 29 | github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= 30 | github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= 31 | github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= 32 | github.com/aristanetworks/goarista v0.0.0-20170210015632-ea17b1a17847/go.mod h1:D/tb0zPVXnP7fmsLZjtdUhSsumbK/ij54UXjjVgMGxQ= 33 | github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= 34 | github.com/aws/aws-sdk-go v1.25.48/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= 35 | github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g= 36 | github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= 37 | github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6/go.mod h1:Dmm/EzmjnCiweXmzRIAiUWCInVmPgjkzgv5k4tVyXiQ= 38 | github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= 39 | github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s= 40 | github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= 41 | github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= 42 | github.com/cespare/xxhash/v2 v2.0.1-0.20190104013014-3767db7a7e18/go.mod h1:HD5P3vAIAh+Y2GAxg0PrPN1P8WkepXGpjbUPDHJqqKM= 43 | github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= 44 | github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= 45 | github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= 46 | github.com/cloudflare/cloudflare-go v0.10.2-0.20190916151808-a80f83b9add9/go.mod h1:1MxXX1Ux4x6mqPmjkUgTP1CdXIBXKX7T+Jk9Gxrmx+U= 47 | github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= 48 | github.com/cockroachdb/datadriven v1.0.0/go.mod h1:5Ib8Meh+jk1RlHIXej6Pzevx/NLlNvQB9pmSBZErGA4= 49 | github.com/cockroachdb/errors v1.6.1/go.mod h1:tm6FTP5G81vwJ5lC0SizQo374JNCOPrHyXGitRJoDqM= 50 | github.com/cockroachdb/errors v1.8.1 h1:A5+txlVZfOqFBDa4mGz2bUWSp0aHElvHX2bKkdbQu+Y= 51 | github.com/cockroachdb/errors v1.8.1/go.mod h1:qGwQn6JmZ+oMjuLwjWzUNqblqk0xl4CVV3SQbGwK7Ac= 52 | github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f h1:o/kfcElHqOiXqcou5a3rIlMc7oJbMQkeLk0VQJ7zgqY= 53 | github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI= 54 | github.com/cockroachdb/pebble v0.0.0-20201229190758-9e27ae169fdd h1:skK6H2F9tTmNCDxePTMtJbEZ+hqqCtlzNK4BEcVxsvY= 55 | github.com/cockroachdb/pebble v0.0.0-20201229190758-9e27ae169fdd/go.mod h1:c3G8ud5zF3+nYHCWmVmtsA8eEtjrDSa6qeLtcRZyevE= 56 | github.com/cockroachdb/redact v1.0.8 h1:8QG/764wK+vmEYoOlfobpe12EQcS81ukx/a4hdVMxNw= 57 | github.com/cockroachdb/redact v1.0.8/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg= 58 | github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2 h1:IKgmqgMQlVJIZj19CdocBeSfSaiCbEBZGKODaixqtHM= 59 | github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2/go.mod h1:8BT+cPK6xvFOcRlk0R8eg+OTkcqI6baNH4xAkpiYVvQ= 60 | github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= 61 | github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM= 62 | github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= 63 | github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= 64 | github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= 65 | github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= 66 | github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= 67 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 68 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 69 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 70 | github.com/dchest/blake512 v1.0.0/go.mod h1:FV1x7xPPLWukZlpDpWQ88rF/SFwZ5qbskrzhLMB92JI= 71 | github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ= 72 | github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4= 73 | github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= 74 | github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= 75 | github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= 76 | github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= 77 | github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= 78 | github.com/dop251/goja v0.0.0-20200219165308-d1232e640a87/go.mod h1:Mw6PkjjMXWbTj+nnj4s3QPXq1jaT0s5pC0iFD4+BOAA= 79 | github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= 80 | github.com/edsrzf/mmap-go v0.0.0-20160512033002-935e0e8a636c/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= 81 | github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM= 82 | github.com/elastic/gosigar v0.8.1-0.20180330100440-37f05ff46ffa/go.mod h1:cdorVVzy1fhmEqmtgqkoE3bYtCfSCkVyjTyCIo22xvs= 83 | github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 84 | github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= 85 | github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= 86 | github.com/etcd-io/bbolt v1.3.3/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw= 87 | github.com/ethereum/go-ethereum v1.9.12/go.mod h1:PvsVkQmhZFx92Y+h2ylythYlheEDt/uBgFbl61Js/jo= 88 | github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8= 89 | github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= 90 | github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= 91 | github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0= 92 | github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4/go.mod h1:T9YF2M40nIgbVgp3rreNmTged+9HrbNTIQf1PsaIiTA= 93 | github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= 94 | github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 95 | github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= 96 | github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= 97 | github.com/gavv/httpexpect v2.0.0+incompatible/go.mod h1:x+9tiU1YnrOvnB725RkpoLv1M62hOWzwo5OXotisrKc= 98 | github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww= 99 | github.com/ghemawat/stream v0.0.0-20171120220530-696b145b53b9/go.mod h1:106OIgooyS7OzLDOpUGgm9fA3bQENb/cFSyyBmMoJDs= 100 | github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= 101 | github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM= 102 | github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98= 103 | github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w= 104 | github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= 105 | github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= 106 | github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= 107 | github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= 108 | github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AEU963A2AYjv4d1V5eVL1CQbEJq6aCNHDDjibzu8= 109 | github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8= 110 | github.com/go-sourcemap/sourcemap v2.1.2+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg= 111 | github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= 112 | github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= 113 | github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= 114 | github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= 115 | github.com/gogo/googleapis v0.0.0-20180223154316-0cd9801be74a/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= 116 | github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= 117 | github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= 118 | github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= 119 | github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= 120 | github.com/gogo/status v1.1.0/go.mod h1:BFv9nrluPLmrS0EmGVvLaPNmRosr9KapBYd5/hpY1WM= 121 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= 122 | github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= 123 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 124 | github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 125 | github.com/golang/protobuf v1.3.2-0.20190517061210-b285ee9cfc6c h1:zqAKixg3cTcIasAMJV+EcfVbWwLpOZ7LeoWJvcuD/5Q= 126 | github.com/golang/protobuf v1.3.2-0.20190517061210-b285ee9cfc6c/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 127 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 128 | github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= 129 | github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= 130 | github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= 131 | github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= 132 | github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= 133 | github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= 134 | github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= 135 | github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= 136 | github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= 137 | github.com/golang/snappy v0.0.2-0.20190904063534-ff6b7dc882cf h1:gFVkHXmVAhEbxZVDln5V9GKrLaluNoFHDbrZwAWZgws= 138 | github.com/golang/snappy v0.0.2-0.20190904063534-ff6b7dc882cf/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= 139 | github.com/gomodule/redigo v1.7.1-0.20190724094224-574c33c3df38/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= 140 | github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= 141 | github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 142 | github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 143 | github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= 144 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 145 | github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= 146 | github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= 147 | github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= 148 | github.com/gorilla/websocket v1.4.1-0.20190629185528-ae1634f6a989/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= 149 | github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc= 150 | github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= 151 | github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= 152 | github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= 153 | github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= 154 | github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= 155 | github.com/huin/goupnp v0.0.0-20161224104101-679507af18f3/go.mod h1:MZ2ZmwcBpvOoJ22IJsc7va19ZwoheaBk43rKg12SKag= 156 | github.com/hydrogen18/memlistener v0.0.0-20141126152155-54553eb933fb/go.mod h1:qEIFzExnS6016fRpRfxrExeVn2gbClQA99gQhnIcdhE= 157 | github.com/iden3/go-iden3-crypto v0.0.6-0.20210308142348-8f85683b2cef h1:72PG9b2eDlLqKszJVLrsoJbpt4CtgJLhKOjH1MJqCVY= 158 | github.com/iden3/go-iden3-crypto v0.0.6-0.20210308142348-8f85683b2cef/go.mod h1:oBgthFLboAWi9feaBUFy7OxEcyn9vA1khHSL/WwWFyg= 159 | github.com/imkira/go-interpol v1.1.0/go.mod h1:z0h2/2T3XF8kyEPpRgJ3kmNv+C43p+I/CoI+jC3w2iA= 160 | github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= 161 | github.com/influxdata/influxdb v1.2.3-0.20180221223340-01288bdb0883/go.mod h1:qZna6X/4elxqT3yI9iZYdZrWWdeFOOprn86kgg4+IzY= 162 | github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbkg6SAB4att1aAwTmVIx/5gCVqeyUdI= 163 | github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0= 164 | github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62A0xJL6I+umB2YTlFRwWXaDFA0jy+5HzGiJjqI= 165 | github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw= 166 | github.com/jackpal/go-nat-pmp v1.0.2-0.20160603034137-1fa385a6f458/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= 167 | github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= 168 | github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= 169 | github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= 170 | github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5/go.mod h1:W54LbzXuIE0boCoNJfwqpmkKJ1O4TCTZMetAt6jGk7Q= 171 | github.com/juju/loggo v0.0.0-20180524022052-584905176618/go.mod h1:vgyd7OREkbtVEN/8IXZe5Ooef3LQePvuBm9UWj6ZL8U= 172 | github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073/go.mod h1:63prj8cnj0tU0S9OHjGJn+b1h0ZghCndfnbQolrYTwA= 173 | github.com/julienschmidt/httprouter v1.1.1-0.20170430222011-975b5c4c7c21/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= 174 | github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k= 175 | github.com/karalabe/usb v0.0.0-20190919080040-51dc0efba356/go.mod h1:Od972xHfMJowv7NGVDiWVxk2zxnWgjLlJzE+F4F7AGU= 176 | github.com/kataras/golog v0.0.9/go.mod h1:12HJgwBIZFNGL0EJnMRhmvGA0PQGx8VFwrZtM4CqbAk= 177 | github.com/kataras/iris/v12 v12.0.1/go.mod h1:udK4vLQKkdDqMGJJVd/msuMtN6hpYJhg/lSzuxjhO+U= 178 | github.com/kataras/neffos v0.0.10/go.mod h1:ZYmJC07hQPW67eKuzlfY7SO3bC0mw83A3j6im82hfqw= 179 | github.com/kataras/pio v0.0.0-20190103105442-ea782b38602d/go.mod h1:NV88laa9UiiDuX9AhMbDPkGYSPugBOV6yTZB1l2K9Z0= 180 | github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= 181 | github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= 182 | github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= 183 | github.com/klauspost/compress v1.9.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= 184 | github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= 185 | github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= 186 | github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 187 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 188 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 189 | github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= 190 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 191 | github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= 192 | github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g= 193 | github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= 194 | github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= 195 | github.com/mattn/go-colorable v0.1.0/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= 196 | github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= 197 | github.com/mattn/go-ieproxy v0.0.0-20190610004146-91bb50d98149/go.mod h1:31jz6HNzdxOmlERGGEc4v/dMssOfmp2p5bT/okiKFFc= 198 | github.com/mattn/go-ieproxy v0.0.0-20190702010315-6dee0af9227d/go.mod h1:31jz6HNzdxOmlERGGEc4v/dMssOfmp2p5bT/okiKFFc= 199 | github.com/mattn/go-isatty v0.0.5-0.20180830101745-3fb116b82035/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= 200 | github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= 201 | github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= 202 | github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= 203 | github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= 204 | github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= 205 | github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw= 206 | github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= 207 | github.com/mediocregopher/mediocre-go-lib v0.0.0-20181029021733-cb65787f37ed/go.mod h1:dSsfyI2zABAdhcbvkXqgxOxrCsbYeHCPgrZkku60dSg= 208 | github.com/mediocregopher/radix/v3 v3.3.0/go.mod h1:EmfVyvspXz1uZEyPBMyGK+kjWiKQGvsUt6O3Pj+LDCQ= 209 | github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc= 210 | github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= 211 | github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= 212 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 213 | github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 214 | github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ= 215 | github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0= 216 | github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E= 217 | github.com/nats-io/nats.go v1.8.1/go.mod h1:BrFz9vVn0fU3AcH9Vn4Kd7W0NpJ651tD5omQ3M8LwxM= 218 | github.com/nats-io/nkeys v0.0.2/go.mod h1:dab7URMsZm6Z/jp9Z5UGa87Uutgc2mVpXLC4B7TDb/4= 219 | github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= 220 | github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= 221 | github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= 222 | github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= 223 | github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= 224 | github.com/olekukonko/tablewriter v0.0.2-0.20190409134802-7e037d187b0c/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= 225 | github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 226 | github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= 227 | github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 228 | github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= 229 | github.com/onsi/ginkgo v1.13.0 h1:M76yO2HkZASFjXL0HSoZJ1AYEmQxNJmY41Jx1zNUq1Y= 230 | github.com/onsi/ginkgo v1.13.0/go.mod h1:+REjRxOmWfHCjfv9TTWB1jD1Frx4XydAD3zm1lskyM0= 231 | github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= 232 | github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= 233 | github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= 234 | github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= 235 | github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= 236 | github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= 237 | github.com/pborman/uuid v0.0.0-20170112150404-1b00554d8222/go.mod h1:VyrYX9gd7irzKovcSS6BIIEwPRkP2Wm2m9ufcdFSJ34= 238 | github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= 239 | github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0= 240 | github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= 241 | github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= 242 | github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 243 | github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 244 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 245 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 246 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 247 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 248 | github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= 249 | github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= 250 | github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 251 | github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= 252 | github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= 253 | github.com/prometheus/tsdb v0.6.2-0.20190402121629-4f204dcbc150/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= 254 | github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho= 255 | github.com/rs/cors v0.0.0-20160617231935-a62a804a8a00/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= 256 | github.com/rs/xhandler v0.0.0-20160618193221-ed27b6fd6521/go.mod h1:RvLn4FgxWubrpZHtQLnOf6EwhN2hEMusxZOhcW9H3UQ= 257 | github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= 258 | github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 259 | github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= 260 | github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZKJ48R1S7H23Ji7oFO5Bw= 261 | github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= 262 | github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 263 | github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= 264 | github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= 265 | github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= 266 | github.com/spaolacci/murmur3 v1.0.1-0.20190317074736-539464a789e9/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= 267 | github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= 268 | github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= 269 | github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= 270 | github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= 271 | github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= 272 | github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= 273 | github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q= 274 | github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570/go.mod h1:8OR4w3TdeIHIh1g6EMY5p0gVNOovcWC+1vpc7naMuAw= 275 | github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3/go.mod h1:hpGUWaI9xL8pRQCTXQgocU38Qw1g0Us7n5PxxTwTCYU= 276 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 277 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 278 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 279 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 280 | github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= 281 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 282 | github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d h1:gZZadD8H+fF+n9CmNhYL1Y0dJB+kLOmKd7FbPJLeGHs= 283 | github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA= 284 | github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs= 285 | github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= 286 | github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= 287 | github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= 288 | github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= 289 | github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 290 | github.com/valyala/fasthttp v1.6.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w= 291 | github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= 292 | github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio= 293 | github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208/go.mod h1:IotVbo4F+mw0EzQ08zFqg7pK3FebNXpaMsRy2RT+Ees= 294 | github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= 295 | github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= 296 | github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= 297 | github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= 298 | github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0/go.mod h1:/LWChgwKmvncFJFHJ7Gvn9wZArjbV5/FppcK2fKk/tI= 299 | github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg= 300 | github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDfVJdfcVVdX+jpBxNmX4rDAzaS45IcYoM= 301 | github.com/yudai/pp v2.0.1+incompatible/go.mod h1:PuxR/8QJ7cyCkFp/aUDS+JY727OFEZkTdatxwunjIkc= 302 | golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 303 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 304 | golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 305 | golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 306 | golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 h1:QmwruyY+bKbDDL0BaglrbZABEali68eoMFhTZpCjYVA= 307 | golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 308 | golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= 309 | golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= 310 | golang.org/x/exp v0.0.0-20200513190911-00229845015e h1:rMqLP+9XLy+LdbCXHjJHAmTfXCr93W7oruWA6Hq1Alc= 311 | golang.org/x/exp v0.0.0-20200513190911-00229845015e/go.mod h1:4M0jN8W1tt0AVLNr8HDosyJCDCDuyL9N9+3m7wDWgKw= 312 | golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= 313 | golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= 314 | golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= 315 | golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= 316 | golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 317 | golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= 318 | golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= 319 | golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= 320 | golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 321 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 322 | golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 323 | golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 324 | golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 325 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 326 | golang.org/x/net v0.0.0-20190327091125-710a502c58a2/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 327 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 328 | golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 329 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 330 | golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 331 | golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0= 332 | golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 333 | golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 h1:AeiKBIuRw3UomYXSbLy0Mc2dDLfdtbT/IVn4keq83P0= 334 | golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= 335 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 336 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 337 | golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 338 | golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 339 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 340 | golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck= 341 | golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 342 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 343 | golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 344 | golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 345 | golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 346 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 347 | golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 348 | golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 349 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 350 | golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 351 | golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 352 | golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 353 | golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 354 | golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 355 | golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 356 | golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So= 357 | golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 358 | golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 359 | golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4= 360 | golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 361 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 362 | golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= 363 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 364 | golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= 365 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 366 | golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 367 | golang.org/x/tools v0.0.0-20181221001348-537d06c36207/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 368 | golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 369 | golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= 370 | golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 371 | golang.org/x/tools v0.0.0-20190327201419-c70d86f8b7cf/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 372 | golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 373 | golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= 374 | golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= 375 | golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 376 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 377 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 378 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 379 | google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= 380 | google.golang.org/genproto v0.0.0-20180518175338-11a468237815/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= 381 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= 382 | google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= 383 | google.golang.org/grpc v1.12.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= 384 | google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= 385 | google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= 386 | google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= 387 | google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= 388 | google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= 389 | google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= 390 | google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= 391 | google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= 392 | google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= 393 | google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= 394 | google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 395 | gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= 396 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 397 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= 398 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 399 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= 400 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 401 | gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= 402 | gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= 403 | gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= 404 | gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= 405 | gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= 406 | gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c= 407 | gopkg.in/olebedev/go-duktape.v3 v3.0.0-20190213234257-ec84240a7772/go.mod h1:uAJfkITjFhyEEuUfm7bsmCZRbW5WRq8s9EY8HZ6hCns= 408 | gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= 409 | gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= 410 | gopkg.in/urfave/cli.v1 v1.20.0/go.mod h1:vuBzUtMdQeixQj8LVd+/98pzhxNGQoyuPBlsXHOQNO0= 411 | gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 412 | gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= 413 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 414 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 415 | gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= 416 | gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 417 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= 418 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 419 | gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= 420 | honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 421 | honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 422 | -------------------------------------------------------------------------------- /merkletree.go: -------------------------------------------------------------------------------- 1 | package merkletree 2 | 3 | import ( 4 | "bytes" 5 | "encoding/hex" 6 | "errors" 7 | "fmt" 8 | "io" 9 | "math/big" 10 | "strings" 11 | "sync" 12 | 13 | cryptoUtils "github.com/iden3/go-iden3-crypto/utils" 14 | "github.com/iden3/go-merkletree/db" 15 | ) 16 | 17 | const ( 18 | // proofFlagsLen is the byte length of the flags in the proof header 19 | // (first 32 bytes). 20 | proofFlagsLen = 2 21 | // ElemBytesLen is the length of the Hash byte array 22 | ElemBytesLen = 32 23 | 24 | numCharPrint = 8 25 | ) 26 | 27 | var ( 28 | // ErrNodeKeyAlreadyExists is used when a node key already exists. 29 | ErrNodeKeyAlreadyExists = errors.New("key already exists") 30 | // ErrKeyNotFound is used when a key is not found in the MerkleTree. 31 | ErrKeyNotFound = errors.New("Key not found in the MerkleTree") 32 | // ErrNodeBytesBadSize is used when the data of a node has an incorrect 33 | // size and can't be parsed. 34 | ErrNodeBytesBadSize = errors.New("node data has incorrect size in the DB") 35 | // ErrReachedMaxLevel is used when a traversal of the MT reaches the 36 | // maximum level. 37 | ErrReachedMaxLevel = errors.New("reached maximum level of the merkle tree") 38 | // ErrInvalidNodeFound is used when an invalid node is found and can't 39 | // be parsed. 40 | ErrInvalidNodeFound = errors.New("found an invalid node in the DB") 41 | // ErrInvalidProofBytes is used when a serialized proof is invalid. 42 | ErrInvalidProofBytes = errors.New("the serialized proof is invalid") 43 | // ErrInvalidDBValue is used when a value in the key value DB is 44 | // invalid (for example, it doen't contain a byte header and a []byte 45 | // body of at least len=1. 46 | ErrInvalidDBValue = errors.New("the value in the DB is invalid") 47 | // ErrEntryIndexAlreadyExists is used when the entry index already 48 | // exists in the tree. 49 | ErrEntryIndexAlreadyExists = errors.New("the entry index already exists in the tree") 50 | // ErrNotWritable is used when the MerkleTree is not writable and a 51 | // write function is called 52 | ErrNotWritable = errors.New("Merkle Tree not writable") 53 | 54 | dbKeyRootNode = []byte("currentroot") 55 | // HashZero is used at Empty nodes 56 | HashZero = Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 58 | ) 59 | 60 | // Hash is the generic type stored in the MerkleTree 61 | type Hash [32]byte 62 | 63 | // MarshalText implements the marshaler for the Hash type 64 | func (h Hash) MarshalText() ([]byte, error) { 65 | return []byte(h.BigInt().String()), nil 66 | } 67 | 68 | // UnmarshalText implements the unmarshaler for the Hash type 69 | func (h *Hash) UnmarshalText(b []byte) error { 70 | ha, err := NewHashFromString(string(b)) 71 | copy(h[:], ha[:]) 72 | return err 73 | } 74 | 75 | // String returns decimal representation in string format of the Hash 76 | func (h Hash) String() string { 77 | s := h.BigInt().String() 78 | if len(s) < numCharPrint { 79 | return s 80 | } 81 | return s[0:numCharPrint] + "..." 82 | } 83 | 84 | // Hex returns the hexadecimal representation of the Hash 85 | func (h Hash) Hex() string { 86 | return hex.EncodeToString(h[:]) 87 | // alternatively equivalent, but with too extra steps: 88 | // bRaw := h.BigInt().Bytes() 89 | // b := [32]byte{} 90 | // copy(b[:], SwapEndianness(bRaw[:])) 91 | // return hex.EncodeToString(b[:]) 92 | } 93 | 94 | // BigInt returns the *big.Int representation of the *Hash 95 | func (h *Hash) BigInt() *big.Int { 96 | if new(big.Int).SetBytes(SwapEndianness(h[:])) == nil { 97 | return big.NewInt(0) 98 | } 99 | return new(big.Int).SetBytes(SwapEndianness(h[:])) 100 | } 101 | 102 | // Bytes returns the []byte representation of the *Hash, which always is 32 103 | // bytes length. 104 | func (h *Hash) Bytes() []byte { 105 | bi := new(big.Int).SetBytes(h[:]).Bytes() 106 | b := [32]byte{} 107 | copy(b[:], SwapEndianness(bi[:])) 108 | return b[:] 109 | } 110 | 111 | // NewBigIntFromHashBytes returns a *big.Int from a byte array, swapping the 112 | // endianness in the process. This is the intended method to get a *big.Int 113 | // from a byte array that previously has ben generated by the Hash.Bytes() 114 | // method. 115 | func NewBigIntFromHashBytes(b []byte) (*big.Int, error) { 116 | if len(b) != ElemBytesLen { 117 | return nil, fmt.Errorf("Expected 32 bytes, found %d bytes", len(b)) 118 | } 119 | bi := new(big.Int).SetBytes(b[:ElemBytesLen]) 120 | if !cryptoUtils.CheckBigIntInField(bi) { 121 | return nil, fmt.Errorf("NewBigIntFromHashBytes: Value not inside the Finite Field") 122 | } 123 | return bi, nil 124 | } 125 | 126 | // NewHashFromBigInt returns a *Hash representation of the given *big.Int 127 | func NewHashFromBigInt(b *big.Int) *Hash { 128 | r := &Hash{} 129 | copy(r[:], SwapEndianness(b.Bytes())) 130 | return r 131 | } 132 | 133 | // NewHashFromBytes returns a *Hash from a byte array, swapping the endianness 134 | // in the process. This is the intended method to get a *Hash from a byte array 135 | // that previously has ben generated by the Hash.Bytes() method. 136 | func NewHashFromBytes(b []byte) (*Hash, error) { 137 | if len(b) != ElemBytesLen { 138 | return nil, fmt.Errorf("Expected 32 bytes, found %d bytes", len(b)) 139 | } 140 | var h Hash 141 | copy(h[:], SwapEndianness(b)) 142 | return &h, nil 143 | } 144 | 145 | // NewHashFromHex returns a *Hash representation of the given hex string 146 | func NewHashFromHex(h string) (*Hash, error) { 147 | h = strings.TrimPrefix(h, "0x") 148 | b, err := hex.DecodeString(h) 149 | if err != nil { 150 | return nil, err 151 | } 152 | return NewHashFromBytes(SwapEndianness(b[:])) 153 | } 154 | 155 | // NewHashFromString returns a *Hash representation of the given decimal string 156 | func NewHashFromString(s string) (*Hash, error) { 157 | bi, ok := new(big.Int).SetString(s, 10) 158 | if !ok { 159 | return nil, fmt.Errorf("Can not parse string to Hash") 160 | } 161 | return NewHashFromBigInt(bi), nil 162 | } 163 | 164 | // MerkleTree is the struct with the main elements of the MerkleTree 165 | type MerkleTree struct { 166 | sync.RWMutex 167 | db db.Storage 168 | rootKey *Hash 169 | writable bool 170 | maxLevels int 171 | } 172 | 173 | // NewMerkleTree loads a new Merkletree. If in the sotrage already exists one 174 | // will open that one, if not, will create a new one. 175 | func NewMerkleTree(storage db.Storage, maxLevels int) (*MerkleTree, error) { 176 | mt := MerkleTree{db: storage, maxLevels: maxLevels, writable: true} 177 | 178 | root, err := mt.dbGetRoot() 179 | if err == db.ErrNotFound { 180 | tx, err := mt.db.NewTx() 181 | if err != nil { 182 | return nil, err 183 | } 184 | mt.rootKey = &HashZero 185 | err = tx.Put(dbKeyRootNode, mt.rootKey[:]) 186 | if err != nil { 187 | return nil, err 188 | } 189 | err = tx.Commit() 190 | if err != nil { 191 | return nil, err 192 | } 193 | return &mt, nil 194 | } else if err != nil { 195 | return nil, err 196 | } 197 | mt.rootKey = root 198 | return &mt, nil 199 | } 200 | 201 | func (mt *MerkleTree) dbGetRoot() (*Hash, error) { 202 | v, err := mt.db.Get(dbKeyRootNode) 203 | if err != nil { 204 | return nil, err 205 | } 206 | var root Hash 207 | // Skip first byte which contains the NodeType 208 | copy(root[:], v[1:]) 209 | return &root, nil 210 | } 211 | 212 | // DB returns the MerkleTree.DB() 213 | func (mt *MerkleTree) DB() db.Storage { 214 | return mt.db 215 | } 216 | 217 | // Root returns the MerkleRoot 218 | func (mt *MerkleTree) Root() *Hash { 219 | return mt.rootKey 220 | } 221 | 222 | // MaxLevels returns the MT maximum level 223 | func (mt *MerkleTree) MaxLevels() int { 224 | return mt.maxLevels 225 | } 226 | 227 | // Snapshot returns a read-only copy of the MerkleTree 228 | func (mt *MerkleTree) Snapshot(rootKey *Hash) (*MerkleTree, error) { 229 | mt.RLock() 230 | defer mt.RUnlock() 231 | _, err := mt.GetNode(rootKey) 232 | if err != nil { 233 | return nil, err 234 | } 235 | return &MerkleTree{db: mt.db, maxLevels: mt.maxLevels, rootKey: rootKey, writable: false}, nil 236 | } 237 | 238 | // Add adds a Key & Value into the MerkleTree. Where the `k` determines the 239 | // path from the Root to the Leaf. 240 | func (mt *MerkleTree) Add(k, v *big.Int) error { 241 | // verify that the MerkleTree is writable 242 | if !mt.writable { 243 | return ErrNotWritable 244 | } 245 | 246 | // verfy that k & v are valid and fit inside the Finite Field. 247 | if !cryptoUtils.CheckBigIntInField(k) { 248 | return errors.New("Key not inside the Finite Field") 249 | } 250 | if !cryptoUtils.CheckBigIntInField(v) { 251 | return errors.New("Value not inside the Finite Field") 252 | } 253 | 254 | tx, err := mt.db.NewTx() 255 | if err != nil { 256 | return err 257 | } 258 | mt.Lock() 259 | defer mt.Unlock() 260 | 261 | kHash := NewHashFromBigInt(k) 262 | vHash := NewHashFromBigInt(v) 263 | newNodeLeaf := NewNodeLeaf(kHash, vHash) 264 | path := getPath(mt.maxLevels, kHash[:]) 265 | 266 | newRootKey, err := mt.addLeaf(tx, newNodeLeaf, mt.rootKey, 0, path) 267 | if err != nil { 268 | return err 269 | } 270 | mt.rootKey = newRootKey 271 | err = mt.dbInsert(tx, dbKeyRootNode, DBEntryTypeRoot, mt.rootKey[:]) 272 | if err != nil { 273 | return err 274 | } 275 | 276 | if err := tx.Commit(); err != nil { 277 | return err 278 | } 279 | 280 | return nil 281 | } 282 | 283 | // AddAndGetCircomProof does an Add, and returns a CircomProcessorProof 284 | func (mt *MerkleTree) AddAndGetCircomProof(k, 285 | v *big.Int) (*CircomProcessorProof, error) { 286 | var cp CircomProcessorProof 287 | cp.Fnc = 2 288 | cp.OldRoot = mt.rootKey 289 | gettedK, gettedV, _, err := mt.Get(k) 290 | if err != nil && err != ErrKeyNotFound { 291 | return nil, err 292 | } 293 | cp.OldKey = NewHashFromBigInt(gettedK) 294 | cp.OldValue = NewHashFromBigInt(gettedV) 295 | if bytes.Equal(cp.OldKey[:], HashZero[:]) { 296 | cp.IsOld0 = true 297 | } 298 | _, _, siblings, err := mt.Get(k) 299 | if err != nil && err != ErrKeyNotFound { 300 | return nil, err 301 | } 302 | cp.Siblings = CircomSiblingsFromSiblings(siblings, mt.maxLevels) 303 | 304 | err = mt.Add(k, v) 305 | if err != nil { 306 | return nil, err 307 | } 308 | 309 | cp.NewKey = NewHashFromBigInt(k) 310 | cp.NewValue = NewHashFromBigInt(v) 311 | cp.NewRoot = mt.rootKey 312 | 313 | return &cp, nil 314 | } 315 | 316 | // pushLeaf recursively pushes an existing oldLeaf down until its path diverges 317 | // from newLeaf, at which point both leafs are stored, all while updating the 318 | // path. 319 | func (mt *MerkleTree) pushLeaf(tx db.Tx, newLeaf *Node, oldLeaf *Node, lvl int, 320 | pathNewLeaf []bool, pathOldLeaf []bool) (*Hash, error) { 321 | if lvl > mt.maxLevels-2 { 322 | return nil, ErrReachedMaxLevel 323 | } 324 | var newNodeMiddle *Node 325 | if pathNewLeaf[lvl] == pathOldLeaf[lvl] { // We need to go deeper! 326 | nextKey, err := mt.pushLeaf(tx, newLeaf, oldLeaf, lvl+1, pathNewLeaf, pathOldLeaf) 327 | if err != nil { 328 | return nil, err 329 | } 330 | if pathNewLeaf[lvl] { // go right 331 | newNodeMiddle = NewNodeMiddle(&HashZero, nextKey) 332 | } else { // go left 333 | newNodeMiddle = NewNodeMiddle(nextKey, &HashZero) 334 | } 335 | return mt.addNode(tx, newNodeMiddle) 336 | } 337 | oldLeafKey, err := oldLeaf.Key() 338 | if err != nil { 339 | return nil, err 340 | } 341 | newLeafKey, err := newLeaf.Key() 342 | if err != nil { 343 | return nil, err 344 | } 345 | 346 | if pathNewLeaf[lvl] { 347 | newNodeMiddle = NewNodeMiddle(oldLeafKey, newLeafKey) 348 | } else { 349 | newNodeMiddle = NewNodeMiddle(newLeafKey, oldLeafKey) 350 | } 351 | // We can add newLeaf now. We don't need to add oldLeaf because it's 352 | // already in the tree. 353 | _, err = mt.addNode(tx, newLeaf) 354 | if err != nil { 355 | return nil, err 356 | } 357 | return mt.addNode(tx, newNodeMiddle) 358 | } 359 | 360 | // addLeaf recursively adds a newLeaf in the MT while updating the path. 361 | func (mt *MerkleTree) addLeaf(tx db.Tx, newLeaf *Node, key *Hash, 362 | lvl int, path []bool) (*Hash, error) { 363 | var err error 364 | var nextKey *Hash 365 | if lvl > mt.maxLevels-1 { 366 | return nil, ErrReachedMaxLevel 367 | } 368 | n, err := mt.GetNode(key) 369 | if err != nil { 370 | return nil, err 371 | } 372 | switch n.Type { 373 | case NodeTypeEmpty: 374 | // We can add newLeaf now 375 | return mt.addNode(tx, newLeaf) 376 | case NodeTypeLeaf: 377 | nKey := n.Entry[0] 378 | // Check if leaf node found contains the leaf node we are 379 | // trying to add 380 | newLeafKey := newLeaf.Entry[0] 381 | if bytes.Equal(nKey[:], newLeafKey[:]) { 382 | return nil, ErrEntryIndexAlreadyExists 383 | } 384 | pathOldLeaf := getPath(mt.maxLevels, nKey[:]) 385 | // We need to push newLeaf down until its path diverges from 386 | // n's path 387 | return mt.pushLeaf(tx, newLeaf, n, lvl, path, pathOldLeaf) 388 | case NodeTypeMiddle: 389 | // We need to go deeper, continue traversing the tree, left or 390 | // right depending on path 391 | var newNodeMiddle *Node 392 | if path[lvl] { // go right 393 | nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildR, lvl+1, path) 394 | newNodeMiddle = NewNodeMiddle(n.ChildL, nextKey) 395 | } else { // go left 396 | nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildL, lvl+1, path) 397 | newNodeMiddle = NewNodeMiddle(nextKey, n.ChildR) 398 | } 399 | if err != nil { 400 | return nil, err 401 | } 402 | // Update the node to reflect the modified child 403 | return mt.addNode(tx, newNodeMiddle) 404 | default: 405 | return nil, ErrInvalidNodeFound 406 | } 407 | } 408 | 409 | // addNode adds a node into the MT. Empty nodes are not stored in the tree; 410 | // they are all the same and assumed to always exist. 411 | func (mt *MerkleTree) addNode(tx db.Tx, n *Node) (*Hash, error) { 412 | // verify that the MerkleTree is writable 413 | if !mt.writable { 414 | return nil, ErrNotWritable 415 | } 416 | if n.Type == NodeTypeEmpty { 417 | return n.Key() 418 | } 419 | k, err := n.Key() 420 | if err != nil { 421 | return nil, err 422 | } 423 | v := n.Value() 424 | // Check that the node key doesn't already exist 425 | if _, err := tx.Get(k[:]); err == nil { 426 | return nil, ErrNodeKeyAlreadyExists 427 | } 428 | err = tx.Put(k[:], v) 429 | return k, err 430 | } 431 | 432 | // updateNode updates an existing node in the MT. Empty nodes are not stored 433 | // in the tree; they are all the same and assumed to always exist. 434 | func (mt *MerkleTree) updateNode(tx db.Tx, n *Node) (*Hash, error) { 435 | // verify that the MerkleTree is writable 436 | if !mt.writable { 437 | return nil, ErrNotWritable 438 | } 439 | if n.Type == NodeTypeEmpty { 440 | return n.Key() 441 | } 442 | k, err := n.Key() 443 | if err != nil { 444 | return nil, err 445 | } 446 | v := n.Value() 447 | err = tx.Put(k[:], v) 448 | return k, err 449 | } 450 | 451 | // Get returns the value of the leaf for the given key 452 | func (mt *MerkleTree) Get(k *big.Int) (*big.Int, *big.Int, []*Hash, error) { 453 | // verfy that k is valid and fit inside the Finite Field. 454 | if !cryptoUtils.CheckBigIntInField(k) { 455 | return nil, nil, nil, errors.New("Key not inside the Finite Field") 456 | } 457 | 458 | kHash := NewHashFromBigInt(k) 459 | path := getPath(mt.maxLevels, kHash[:]) 460 | 461 | nextKey := mt.rootKey 462 | siblings := []*Hash{} 463 | for i := 0; i < mt.maxLevels; i++ { 464 | n, err := mt.GetNode(nextKey) 465 | if err != nil { 466 | return nil, nil, nil, err 467 | } 468 | switch n.Type { 469 | case NodeTypeEmpty: 470 | return big.NewInt(0), big.NewInt(0), siblings, ErrKeyNotFound 471 | case NodeTypeLeaf: 472 | if bytes.Equal(kHash[:], n.Entry[0][:]) { 473 | return n.Entry[0].BigInt(), n.Entry[1].BigInt(), siblings, nil 474 | } 475 | return n.Entry[0].BigInt(), n.Entry[1].BigInt(), siblings, ErrKeyNotFound 476 | case NodeTypeMiddle: 477 | if path[i] { 478 | nextKey = n.ChildR 479 | siblings = append(siblings, n.ChildL) 480 | } else { 481 | nextKey = n.ChildL 482 | siblings = append(siblings, n.ChildR) 483 | } 484 | default: 485 | return nil, nil, nil, ErrInvalidNodeFound 486 | } 487 | } 488 | 489 | return nil, nil, nil, ErrReachedMaxLevel 490 | } 491 | 492 | // Update updates the value of a specified key in the MerkleTree, and updates 493 | // the path from the leaf to the Root with the new values. Returns the 494 | // CircomProcessorProof. 495 | func (mt *MerkleTree) Update(k, v *big.Int) (*CircomProcessorProof, error) { 496 | // verify that the MerkleTree is writable 497 | if !mt.writable { 498 | return nil, ErrNotWritable 499 | } 500 | 501 | // verfy that k & are valid and fit inside the Finite Field. 502 | if !cryptoUtils.CheckBigIntInField(k) { 503 | return nil, errors.New("Key not inside the Finite Field") 504 | } 505 | if !cryptoUtils.CheckBigIntInField(v) { 506 | return nil, errors.New("Key not inside the Finite Field") 507 | } 508 | tx, err := mt.db.NewTx() 509 | if err != nil { 510 | return nil, err 511 | } 512 | mt.Lock() 513 | defer mt.Unlock() 514 | 515 | kHash := NewHashFromBigInt(k) 516 | vHash := NewHashFromBigInt(v) 517 | path := getPath(mt.maxLevels, kHash[:]) 518 | 519 | var cp CircomProcessorProof 520 | cp.Fnc = 1 521 | cp.OldRoot = mt.rootKey 522 | cp.OldKey = kHash 523 | cp.NewKey = kHash 524 | cp.NewValue = vHash 525 | 526 | nextKey := mt.rootKey 527 | siblings := []*Hash{} 528 | for i := 0; i < mt.maxLevels; i++ { 529 | n, err := mt.GetNode(nextKey) 530 | if err != nil { 531 | return nil, err 532 | } 533 | switch n.Type { 534 | case NodeTypeEmpty: 535 | return nil, ErrKeyNotFound 536 | case NodeTypeLeaf: 537 | if bytes.Equal(kHash[:], n.Entry[0][:]) { 538 | cp.OldValue = n.Entry[1] 539 | cp.Siblings = CircomSiblingsFromSiblings(siblings, mt.maxLevels) 540 | // update leaf and upload to the root 541 | newNodeLeaf := NewNodeLeaf(kHash, vHash) 542 | _, err := mt.updateNode(tx, newNodeLeaf) 543 | if err != nil { 544 | return nil, err 545 | } 546 | newRootKey, err := 547 | mt.recalculatePathUntilRoot(tx, path, newNodeLeaf, siblings) 548 | if err != nil { 549 | return nil, err 550 | } 551 | mt.rootKey = newRootKey 552 | err = mt.dbInsert(tx, dbKeyRootNode, DBEntryTypeRoot, mt.rootKey[:]) 553 | if err != nil { 554 | return nil, err 555 | } 556 | cp.NewRoot = newRootKey 557 | if err := tx.Commit(); err != nil { 558 | return nil, err 559 | } 560 | return &cp, nil 561 | } 562 | return nil, ErrKeyNotFound 563 | case NodeTypeMiddle: 564 | if path[i] { 565 | nextKey = n.ChildR 566 | siblings = append(siblings, n.ChildL) 567 | } else { 568 | nextKey = n.ChildL 569 | siblings = append(siblings, n.ChildR) 570 | } 571 | default: 572 | return nil, ErrInvalidNodeFound 573 | } 574 | } 575 | 576 | return nil, ErrKeyNotFound 577 | } 578 | 579 | // Delete removes the specified Key from the MerkleTree and updates the path 580 | // from the deleted key to the Root with the new values. This method removes 581 | // the key from the MerkleTree, but does not remove the old nodes from the 582 | // key-value database; this means that if the tree is accessed by an old Root 583 | // where the key was not deleted yet, the key will still exist. If is desired 584 | // to remove the key-values from the database that are not under the current 585 | // Root, an option could be to dump all the leafs (using mt.DumpLeafs) and 586 | // import them in a new MerkleTree in a new database (using 587 | // mt.ImportDumpedLeafs), but this will loose all the Root history of the 588 | // MerkleTree 589 | func (mt *MerkleTree) Delete(k *big.Int) error { 590 | // verify that the MerkleTree is writable 591 | if !mt.writable { 592 | return ErrNotWritable 593 | } 594 | 595 | // verfy that k is valid and fit inside the Finite Field. 596 | if !cryptoUtils.CheckBigIntInField(k) { 597 | return errors.New("Key not inside the Finite Field") 598 | } 599 | tx, err := mt.db.NewTx() 600 | if err != nil { 601 | return err 602 | } 603 | mt.Lock() 604 | defer mt.Unlock() 605 | 606 | kHash := NewHashFromBigInt(k) 607 | path := getPath(mt.maxLevels, kHash[:]) 608 | 609 | nextKey := mt.rootKey 610 | siblings := []*Hash{} 611 | for i := 0; i < mt.maxLevels; i++ { 612 | n, err := mt.GetNode(nextKey) 613 | if err != nil { 614 | return err 615 | } 616 | switch n.Type { 617 | case NodeTypeEmpty: 618 | return ErrKeyNotFound 619 | case NodeTypeLeaf: 620 | if bytes.Equal(kHash[:], n.Entry[0][:]) { 621 | // remove and go up with the sibling 622 | err = mt.rmAndUpload(tx, path, kHash, siblings) 623 | return err 624 | } 625 | return ErrKeyNotFound 626 | case NodeTypeMiddle: 627 | if path[i] { 628 | nextKey = n.ChildR 629 | siblings = append(siblings, n.ChildL) 630 | } else { 631 | nextKey = n.ChildL 632 | siblings = append(siblings, n.ChildR) 633 | } 634 | default: 635 | return ErrInvalidNodeFound 636 | } 637 | } 638 | 639 | return ErrKeyNotFound 640 | } 641 | 642 | // rmAndUpload removes the key, and goes up until the root updating all the 643 | // nodes with the new values. 644 | func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings []*Hash) error { 645 | if len(siblings) == 0 { 646 | mt.rootKey = &HashZero 647 | err := mt.dbInsert(tx, dbKeyRootNode, DBEntryTypeRoot, mt.rootKey[:]) 648 | if err != nil { 649 | return err 650 | } 651 | return tx.Commit() 652 | } 653 | 654 | toUpload := siblings[len(siblings)-1] 655 | if len(siblings) < 2 { //nolint:gomnd 656 | mt.rootKey = siblings[0] 657 | err := mt.dbInsert(tx, dbKeyRootNode, DBEntryTypeRoot, mt.rootKey[:]) 658 | if err != nil { 659 | return err 660 | } 661 | return tx.Commit() 662 | } 663 | for i := len(siblings) - 2; i >= 0; i-- { //nolint:gomnd 664 | if !bytes.Equal(siblings[i][:], HashZero[:]) { 665 | var newNode *Node 666 | if path[i] { 667 | newNode = NewNodeMiddle(siblings[i], toUpload) 668 | } else { 669 | newNode = NewNodeMiddle(toUpload, siblings[i]) 670 | } 671 | _, err := mt.addNode(tx, newNode) 672 | if err != ErrNodeKeyAlreadyExists && err != nil { 673 | return err 674 | } 675 | // go up until the root 676 | newRootKey, err := mt.recalculatePathUntilRoot(tx, path, newNode, 677 | siblings[:i]) 678 | if err != nil { 679 | return err 680 | } 681 | mt.rootKey = newRootKey 682 | err = mt.dbInsert(tx, dbKeyRootNode, DBEntryTypeRoot, mt.rootKey[:]) 683 | if err != nil { 684 | return err 685 | } 686 | break 687 | } 688 | // if i==0 (root position), stop and store the sibling of the 689 | // deleted leaf as root 690 | if i == 0 { 691 | mt.rootKey = toUpload 692 | err := mt.dbInsert(tx, dbKeyRootNode, DBEntryTypeRoot, mt.rootKey[:]) 693 | if err != nil { 694 | return err 695 | } 696 | break 697 | } 698 | } 699 | if err := tx.Commit(); err != nil { 700 | return err 701 | } 702 | 703 | return nil 704 | } 705 | 706 | // recalculatePathUntilRoot recalculates the nodes until the Root 707 | func (mt *MerkleTree) recalculatePathUntilRoot(tx db.Tx, path []bool, node *Node, 708 | siblings []*Hash) (*Hash, error) { 709 | for i := len(siblings) - 1; i >= 0; i-- { 710 | nodeKey, err := node.Key() 711 | if err != nil { 712 | return nil, err 713 | } 714 | if path[i] { 715 | node = NewNodeMiddle(siblings[i], nodeKey) 716 | } else { 717 | node = NewNodeMiddle(nodeKey, siblings[i]) 718 | } 719 | _, err = mt.addNode(tx, node) 720 | if err != ErrNodeKeyAlreadyExists && err != nil { 721 | return nil, err 722 | } 723 | } 724 | 725 | // return last node added, which is the root 726 | nodeKey, err := node.Key() 727 | return nodeKey, err 728 | } 729 | 730 | // dbInsert is a helper function to insert a node into a key in an open db 731 | // transaction. 732 | func (mt *MerkleTree) dbInsert(tx db.Tx, k []byte, t NodeType, data []byte) error { 733 | v := append([]byte{byte(t)}, data...) 734 | return tx.Put(k, v) 735 | } 736 | 737 | // GetNode gets a node by key from the MT. Empty nodes are not stored in the 738 | // tree; they are all the same and assumed to always exist. 739 | func (mt *MerkleTree) GetNode(key *Hash) (*Node, error) { 740 | if bytes.Equal(key[:], HashZero[:]) { 741 | return NewNodeEmpty(), nil 742 | } 743 | nBytes, err := mt.db.Get(key[:]) 744 | if err != nil { 745 | return nil, err 746 | } 747 | return NewNodeFromBytes(nBytes) 748 | } 749 | 750 | // getPath returns the binary path, from the root to the leaf. 751 | func getPath(numLevels int, k []byte) []bool { 752 | path := make([]bool, numLevels) 753 | for n := 0; n < numLevels; n++ { 754 | path[n] = TestBit(k[:], uint(n)) 755 | } 756 | return path 757 | } 758 | 759 | // NodeAux contains the auxiliary node used in a non-existence proof. 760 | type NodeAux struct { 761 | Key *Hash 762 | Value *Hash 763 | } 764 | 765 | // Proof defines the required elements for a MT proof of existence or 766 | // non-existence. 767 | type Proof struct { 768 | // existence indicates wether this is a proof of existence or 769 | // non-existence. 770 | Existence bool 771 | // depth indicates how deep in the tree the proof goes. 772 | depth uint 773 | // notempties is a bitmap of non-empty Siblings found in Siblings. 774 | notempties [ElemBytesLen - proofFlagsLen]byte 775 | // Siblings is a list of non-empty sibling keys. 776 | Siblings []*Hash 777 | NodeAux *NodeAux 778 | } 779 | 780 | // NewProofFromBytes parses a byte array into a Proof. 781 | func NewProofFromBytes(bs []byte) (*Proof, error) { 782 | if len(bs) < ElemBytesLen { 783 | return nil, ErrInvalidProofBytes 784 | } 785 | p := &Proof{} 786 | if (bs[0] & 0x01) == 0 { 787 | p.Existence = true 788 | } 789 | p.depth = uint(bs[1]) 790 | copy(p.notempties[:], bs[proofFlagsLen:ElemBytesLen]) 791 | siblingBytes := bs[ElemBytesLen:] 792 | sibIdx := 0 793 | for i := uint(0); i < p.depth; i++ { 794 | if TestBitBigEndian(p.notempties[:], i) { 795 | if len(siblingBytes) < (sibIdx+1)*ElemBytesLen { 796 | return nil, ErrInvalidProofBytes 797 | } 798 | var sib Hash 799 | copy(sib[:], siblingBytes[sibIdx*ElemBytesLen:(sibIdx+1)*ElemBytesLen]) 800 | p.Siblings = append(p.Siblings, &sib) 801 | sibIdx++ 802 | } 803 | } 804 | 805 | if !p.Existence && ((bs[0] & 0x02) != 0) { 806 | p.NodeAux = &NodeAux{Key: &Hash{}, Value: &Hash{}} 807 | nodeAuxBytes := siblingBytes[len(p.Siblings)*ElemBytesLen:] 808 | if len(nodeAuxBytes) != 2*ElemBytesLen { 809 | return nil, ErrInvalidProofBytes 810 | } 811 | copy(p.NodeAux.Key[:], nodeAuxBytes[:ElemBytesLen]) 812 | copy(p.NodeAux.Value[:], nodeAuxBytes[ElemBytesLen:2*ElemBytesLen]) 813 | } 814 | return p, nil 815 | } 816 | 817 | // Bytes serializes a Proof into a byte array. 818 | func (p *Proof) Bytes() []byte { 819 | bsLen := proofFlagsLen + len(p.notempties) + ElemBytesLen*len(p.Siblings) 820 | if p.NodeAux != nil { 821 | bsLen += 2 * ElemBytesLen //nolint:gomnd 822 | } 823 | bs := make([]byte, bsLen) 824 | 825 | if !p.Existence { 826 | bs[0] |= 0x01 827 | } 828 | bs[1] = byte(p.depth) 829 | copy(bs[proofFlagsLen:len(p.notempties)+proofFlagsLen], p.notempties[:]) 830 | siblingsBytes := bs[len(p.notempties)+proofFlagsLen:] 831 | for i, k := range p.Siblings { 832 | copy(siblingsBytes[i*ElemBytesLen:(i+1)*ElemBytesLen], k[:]) 833 | } 834 | if p.NodeAux != nil { 835 | bs[0] |= 0x02 836 | copy(bs[len(bs)-2*ElemBytesLen:], p.NodeAux.Key[:]) 837 | copy(bs[len(bs)-1*ElemBytesLen:], p.NodeAux.Value[:]) 838 | } 839 | return bs 840 | } 841 | 842 | // SiblingsFromProof returns all the siblings of the proof. 843 | func SiblingsFromProof(proof *Proof) []*Hash { 844 | sibIdx := 0 845 | siblings := []*Hash{} 846 | for lvl := 0; lvl < int(proof.depth); lvl++ { 847 | if TestBitBigEndian(proof.notempties[:], uint(lvl)) { 848 | siblings = append(siblings, proof.Siblings[sibIdx]) 849 | sibIdx++ 850 | } else { 851 | siblings = append(siblings, &HashZero) 852 | } 853 | } 854 | return siblings 855 | } 856 | 857 | // AllSiblings returns all the siblings of the proof. 858 | func (p *Proof) AllSiblings() []*Hash { 859 | return SiblingsFromProof(p) 860 | } 861 | 862 | // CircomSiblingsFromSiblings returns the full siblings compatible with circom 863 | func CircomSiblingsFromSiblings(siblings []*Hash, levels int) []*Hash { 864 | // Add the rest of empty levels to the siblings 865 | for i := len(siblings); i < levels+1; i++ { 866 | siblings = append(siblings, &HashZero) 867 | } 868 | return siblings 869 | } 870 | 871 | // CircomProcessorProof defines the ProcessorProof compatible with circom. Is 872 | // the data of the proof between the transition from one state to another. 873 | type CircomProcessorProof struct { 874 | OldRoot *Hash `json:"oldRoot"` 875 | NewRoot *Hash `json:"newRoot"` 876 | Siblings []*Hash `json:"siblings"` 877 | OldKey *Hash `json:"oldKey"` 878 | OldValue *Hash `json:"oldValue"` 879 | NewKey *Hash `json:"newKey"` 880 | NewValue *Hash `json:"newValue"` 881 | IsOld0 bool `json:"isOld0"` 882 | // 0: NOP, 1: Update, 2: Insert, 3: Delete 883 | Fnc int `json:"fnc"` 884 | } 885 | 886 | // String returns a human readable string representation of the 887 | // CircomProcessorProof 888 | func (p CircomProcessorProof) String() string { 889 | buf := bytes.NewBufferString("{") 890 | fmt.Fprintf(buf, " OldRoot: %v,\n", p.OldRoot) 891 | fmt.Fprintf(buf, " NewRoot: %v,\n", p.NewRoot) 892 | fmt.Fprintf(buf, " Siblings: [\n ") 893 | for _, s := range p.Siblings { 894 | fmt.Fprintf(buf, "%v, ", s) 895 | } 896 | fmt.Fprintf(buf, "\n ],\n") 897 | fmt.Fprintf(buf, " OldKey: %v,\n", p.OldKey) 898 | fmt.Fprintf(buf, " OldValue: %v,\n", p.OldValue) 899 | fmt.Fprintf(buf, " NewKey: %v,\n", p.NewKey) 900 | fmt.Fprintf(buf, " NewValue: %v,\n", p.NewValue) 901 | fmt.Fprintf(buf, " IsOld0: %v,\n", p.IsOld0) 902 | fmt.Fprintf(buf, "}\n") 903 | 904 | return buf.String() 905 | } 906 | 907 | // CircomVerifierProof defines the VerifierProof compatible with circom. Is the 908 | // data of the proof that a certain leaf exists in the MerkleTree. 909 | type CircomVerifierProof struct { 910 | Root *Hash `json:"root"` 911 | Siblings []*Hash `json:"siblings"` 912 | OldKey *Hash `json:"oldKey"` 913 | OldValue *Hash `json:"oldValue"` 914 | IsOld0 bool `json:"isOld0"` 915 | Key *Hash `json:"key"` 916 | Value *Hash `json:"value"` 917 | Fnc int `json:"fnc"` // 0: inclusion, 1: non inclusion 918 | } 919 | 920 | // GenerateCircomVerifierProof returns the CircomVerifierProof for a certain 921 | // key in the MerkleTree. If the rootKey is nil, the current merkletree root 922 | // is used. 923 | func (mt *MerkleTree) GenerateCircomVerifierProof(k *big.Int, 924 | rootKey *Hash) (*CircomVerifierProof, error) { 925 | cp, err := mt.GenerateSCVerifierProof(k, rootKey) 926 | if err != nil { 927 | return nil, err 928 | } 929 | cp.Siblings = CircomSiblingsFromSiblings(cp.Siblings, mt.maxLevels) 930 | return cp, nil 931 | } 932 | 933 | // GenerateSCVerifierProof returns the CircomVerifierProof for a certain key in 934 | // the MerkleTree with the Siblings without the extra 0 needed at the circom 935 | // circuits, which makes it straight forward to verifiy inside a Smart 936 | // Contract. If the rootKey is nil, the current merkletree root is used. 937 | func (mt *MerkleTree) GenerateSCVerifierProof(k *big.Int, 938 | rootKey *Hash) (*CircomVerifierProof, error) { 939 | if rootKey == nil { 940 | rootKey = mt.Root() 941 | } 942 | p, v, err := mt.GenerateProof(k, rootKey) 943 | if err != nil && err != ErrKeyNotFound { 944 | return nil, err 945 | } 946 | var cp CircomVerifierProof 947 | cp.Root = rootKey 948 | cp.Siblings = p.AllSiblings() 949 | if p.NodeAux != nil { 950 | cp.OldKey = p.NodeAux.Key 951 | cp.OldValue = p.NodeAux.Value 952 | } else { 953 | cp.OldKey = &HashZero 954 | cp.OldValue = &HashZero 955 | } 956 | cp.Key = NewHashFromBigInt(k) 957 | cp.Value = NewHashFromBigInt(v) 958 | if p.Existence { 959 | cp.Fnc = 0 // inclusion 960 | } else { 961 | cp.Fnc = 1 // non inclusion 962 | } 963 | 964 | return &cp, nil 965 | } 966 | 967 | // GenerateProof generates the proof of existence (or non-existence) of an 968 | // Entry's hash Index for a Merkle Tree given the root. 969 | // If the rootKey is nil, the current merkletree root is used 970 | func (mt *MerkleTree) GenerateProof(k *big.Int, rootKey *Hash) (*Proof, 971 | *big.Int, error) { 972 | p := &Proof{} 973 | var siblingKey *Hash 974 | 975 | kHash := NewHashFromBigInt(k) 976 | path := getPath(mt.maxLevels, kHash[:]) 977 | if rootKey == nil { 978 | rootKey = mt.Root() 979 | } 980 | nextKey := rootKey 981 | for p.depth = 0; p.depth < uint(mt.maxLevels); p.depth++ { 982 | n, err := mt.GetNode(nextKey) 983 | if err != nil { 984 | return nil, nil, err 985 | } 986 | switch n.Type { 987 | case NodeTypeEmpty: 988 | return p, big.NewInt(0), nil 989 | case NodeTypeLeaf: 990 | if bytes.Equal(kHash[:], n.Entry[0][:]) { 991 | p.Existence = true 992 | return p, n.Entry[1].BigInt(), nil 993 | } 994 | // We found a leaf whose entry didn't match hIndex 995 | p.NodeAux = &NodeAux{Key: n.Entry[0], Value: n.Entry[1]} 996 | return p, n.Entry[1].BigInt(), nil 997 | case NodeTypeMiddle: 998 | if path[p.depth] { 999 | nextKey = n.ChildR 1000 | siblingKey = n.ChildL 1001 | } else { 1002 | nextKey = n.ChildL 1003 | siblingKey = n.ChildR 1004 | } 1005 | default: 1006 | return nil, nil, ErrInvalidNodeFound 1007 | } 1008 | if !bytes.Equal(siblingKey[:], HashZero[:]) { 1009 | SetBitBigEndian(p.notempties[:], uint(p.depth)) 1010 | p.Siblings = append(p.Siblings, siblingKey) 1011 | } 1012 | } 1013 | return nil, nil, ErrKeyNotFound 1014 | } 1015 | 1016 | // VerifyProof verifies the Merkle Proof for the entry and root. 1017 | func VerifyProof(rootKey *Hash, proof *Proof, k, v *big.Int) bool { 1018 | rootFromProof, err := RootFromProof(proof, k, v) 1019 | if err != nil { 1020 | return false 1021 | } 1022 | return bytes.Equal(rootKey[:], rootFromProof[:]) 1023 | } 1024 | 1025 | // RootFromProof calculates the root that would correspond to a tree whose 1026 | // siblings are the ones in the proof with the leaf hashing to hIndex and 1027 | // hValue. 1028 | func RootFromProof(proof *Proof, k, v *big.Int) (*Hash, error) { 1029 | kHash := NewHashFromBigInt(k) 1030 | vHash := NewHashFromBigInt(v) 1031 | sibIdx := len(proof.Siblings) - 1 1032 | var err error 1033 | var midKey *Hash 1034 | if proof.Existence { 1035 | midKey, err = LeafKey(kHash, vHash) 1036 | if err != nil { 1037 | return nil, err 1038 | } 1039 | } else { 1040 | if proof.NodeAux == nil { 1041 | midKey = &HashZero 1042 | } else { 1043 | if bytes.Equal(kHash[:], proof.NodeAux.Key[:]) { 1044 | return nil, 1045 | fmt.Errorf("Non-existence proof being checked against hIndex equal to nodeAux") 1046 | } 1047 | midKey, err = LeafKey(proof.NodeAux.Key, proof.NodeAux.Value) 1048 | if err != nil { 1049 | return nil, err 1050 | } 1051 | } 1052 | } 1053 | path := getPath(int(proof.depth), kHash[:]) 1054 | var siblingKey *Hash 1055 | for lvl := int(proof.depth) - 1; lvl >= 0; lvl-- { 1056 | if TestBitBigEndian(proof.notempties[:], uint(lvl)) { 1057 | siblingKey = proof.Siblings[sibIdx] 1058 | sibIdx-- 1059 | } else { 1060 | siblingKey = &HashZero 1061 | } 1062 | if path[lvl] { 1063 | midKey, err = NewNodeMiddle(siblingKey, midKey).Key() 1064 | if err != nil { 1065 | return nil, err 1066 | } 1067 | } else { 1068 | midKey, err = NewNodeMiddle(midKey, siblingKey).Key() 1069 | if err != nil { 1070 | return nil, err 1071 | } 1072 | } 1073 | } 1074 | return midKey, nil 1075 | } 1076 | 1077 | // walk is a helper recursive function to iterate over all tree branches 1078 | func (mt *MerkleTree) walk(key *Hash, f func(*Node)) error { 1079 | n, err := mt.GetNode(key) 1080 | if err != nil { 1081 | return err 1082 | } 1083 | switch n.Type { 1084 | case NodeTypeEmpty: 1085 | f(n) 1086 | case NodeTypeLeaf: 1087 | f(n) 1088 | case NodeTypeMiddle: 1089 | f(n) 1090 | if err := mt.walk(n.ChildL, f); err != nil { 1091 | return err 1092 | } 1093 | if err := mt.walk(n.ChildR, f); err != nil { 1094 | return err 1095 | } 1096 | default: 1097 | return ErrInvalidNodeFound 1098 | } 1099 | return nil 1100 | } 1101 | 1102 | // Walk iterates over all the branches of a MerkleTree with the given rootKey 1103 | // if rootKey is nil, it will get the current RootKey of the current state of 1104 | // the MerkleTree. For each node, it calls the f function given in the 1105 | // parameters. See some examples of the Walk function usage in the 1106 | // merkletree.go and merkletree_test.go 1107 | func (mt *MerkleTree) Walk(rootKey *Hash, f func(*Node)) error { 1108 | if rootKey == nil { 1109 | rootKey = mt.Root() 1110 | } 1111 | err := mt.walk(rootKey, f) 1112 | return err 1113 | } 1114 | 1115 | // GraphViz uses Walk function to generate a string GraphViz representation of 1116 | // the tree and writes it to w 1117 | func (mt *MerkleTree) GraphViz(w io.Writer, rootKey *Hash) error { 1118 | fmt.Fprintf(w, `digraph hierarchy { 1119 | node [fontname=Monospace,fontsize=10,shape=box] 1120 | `) 1121 | cnt := 0 1122 | var errIn error 1123 | err := mt.Walk(rootKey, func(n *Node) { 1124 | k, err := n.Key() 1125 | if err != nil { 1126 | errIn = err 1127 | } 1128 | switch n.Type { 1129 | case NodeTypeEmpty: 1130 | case NodeTypeLeaf: 1131 | fmt.Fprintf(w, "\"%v\" [style=filled];\n", k.String()) 1132 | case NodeTypeMiddle: 1133 | lr := [2]string{n.ChildL.String(), n.ChildR.String()} 1134 | emptyNodes := "" 1135 | for i := range lr { 1136 | if lr[i] == "0" { 1137 | lr[i] = fmt.Sprintf("empty%v", cnt) 1138 | emptyNodes += fmt.Sprintf("\"%v\" [style=dashed,label=0];\n", lr[i]) 1139 | cnt++ 1140 | } 1141 | } 1142 | fmt.Fprintf(w, "\"%v\" -> {\"%v\" \"%v\"}\n", k.String(), lr[0], lr[1]) 1143 | fmt.Fprint(w, emptyNodes) 1144 | default: 1145 | } 1146 | }) 1147 | fmt.Fprintf(w, "}\n") 1148 | if errIn != nil { 1149 | return errIn 1150 | } 1151 | return err 1152 | } 1153 | 1154 | // PrintGraphViz prints directly the GraphViz() output 1155 | func (mt *MerkleTree) PrintGraphViz(rootKey *Hash) error { 1156 | if rootKey == nil { 1157 | rootKey = mt.Root() 1158 | } 1159 | w := bytes.NewBufferString("") 1160 | fmt.Fprintf(w, 1161 | "--------\nGraphViz of the MerkleTree with RootKey "+rootKey.BigInt().String()+"\n") 1162 | err := mt.GraphViz(w, nil) 1163 | if err != nil { 1164 | return err 1165 | } 1166 | fmt.Fprintf(w, 1167 | "End of GraphViz of the MerkleTree with RootKey "+rootKey.BigInt().String()+"\n--------\n") 1168 | 1169 | fmt.Println(w) 1170 | return nil 1171 | } 1172 | 1173 | // DumpLeafs returns all the Leafs that exist under the given Root. If no Root 1174 | // is given (nil), it uses the current Root of the MerkleTree. 1175 | func (mt *MerkleTree) DumpLeafs(rootKey *Hash) ([]byte, error) { 1176 | var b []byte 1177 | err := mt.Walk(rootKey, func(n *Node) { 1178 | if n.Type == NodeTypeLeaf { 1179 | l := n.Entry[0].Bytes() 1180 | r := n.Entry[1].Bytes() 1181 | b = append(b, append(l[:], r[:]...)...) 1182 | } 1183 | }) 1184 | return b, err 1185 | } 1186 | 1187 | // ImportDumpedLeafs parses and adds to the MerkleTree the dumped list of leafs 1188 | // from the DumpLeafs function. 1189 | func (mt *MerkleTree) ImportDumpedLeafs(b []byte) error { 1190 | for i := 0; i < len(b); i += 64 { 1191 | lr := b[i : i+64] 1192 | lB, err := NewBigIntFromHashBytes(lr[:32]) 1193 | if err != nil { 1194 | return err 1195 | } 1196 | rB, err := NewBigIntFromHashBytes(lr[32:]) 1197 | if err != nil { 1198 | return err 1199 | } 1200 | err = mt.Add(lB, rB) 1201 | if err != nil { 1202 | return err 1203 | } 1204 | } 1205 | return nil 1206 | } 1207 | -------------------------------------------------------------------------------- /merkletree_test.go: -------------------------------------------------------------------------------- 1 | package merkletree 2 | 3 | import ( 4 | "bytes" 5 | "encoding/hex" 6 | "encoding/json" 7 | "fmt" 8 | "math/big" 9 | "testing" 10 | 11 | "github.com/iden3/go-iden3-crypto/constants" 12 | cryptoUtils "github.com/iden3/go-iden3-crypto/utils" 13 | "github.com/iden3/go-merkletree/db/memory" 14 | "github.com/stretchr/testify/assert" 15 | "github.com/stretchr/testify/require" 16 | ) 17 | 18 | var debug = false 19 | 20 | type Fatalable interface { 21 | Fatal(args ...interface{}) 22 | } 23 | 24 | func newTestingMerkle(f Fatalable, numLevels int) *MerkleTree { 25 | mt, err := NewMerkleTree(memory.NewMemoryStorage(), numLevels) 26 | if err != nil { 27 | f.Fatal(err) 28 | return nil 29 | } 30 | return mt 31 | } 32 | 33 | func TestHashParsers(t *testing.T) { 34 | h0 := NewHashFromBigInt(big.NewInt(0)) 35 | assert.Equal(t, "0", h0.String()) 36 | h1 := NewHashFromBigInt(big.NewInt(1)) 37 | assert.Equal(t, "1", h1.String()) 38 | h10 := NewHashFromBigInt(big.NewInt(10)) 39 | assert.Equal(t, "10", h10.String()) 40 | 41 | h7l := NewHashFromBigInt(big.NewInt(1234567)) 42 | assert.Equal(t, "1234567", h7l.String()) 43 | h8l := NewHashFromBigInt(big.NewInt(12345678)) 44 | assert.Equal(t, "12345678...", h8l.String()) 45 | 46 | b, ok := new(big.Int).SetString("4932297968297298434239270129193057052722409868268166443802652458940273154854", 10) //nolint:lll 47 | assert.True(t, ok) 48 | h := NewHashFromBigInt(b) 49 | assert.Equal(t, "4932297968297298434239270129193057052722409868268166443802652458940273154854", h.BigInt().String()) //nolint:lll 50 | assert.Equal(t, "49322979...", h.String()) 51 | assert.Equal(t, "265baaf161e875c372d08e50f52abddc01d32efc93e90290bb8b3d9ceb94e70a", h.Hex()) 52 | 53 | b1, err := NewBigIntFromHashBytes(b.Bytes()) 54 | assert.Nil(t, err) 55 | assert.Equal(t, new(big.Int).SetBytes(b.Bytes()).String(), b1.String()) 56 | 57 | b2, err := NewHashFromBytes(b.Bytes()) 58 | assert.Nil(t, err) 59 | assert.Equal(t, b.String(), b2.BigInt().String()) 60 | 61 | h2, err := NewHashFromHex(h.Hex()) 62 | assert.Nil(t, err) 63 | assert.Equal(t, h, h2) 64 | _, err = NewHashFromHex("0x12") 65 | assert.NotNil(t, err) 66 | 67 | // check limits 68 | a := new(big.Int).Sub(constants.Q, big.NewInt(1)) 69 | testHashParsers(t, a) 70 | a = big.NewInt(int64(1)) 71 | testHashParsers(t, a) 72 | } 73 | 74 | func testHashParsers(t *testing.T, a *big.Int) { 75 | require.True(t, cryptoUtils.CheckBigIntInField(a)) 76 | h := NewHashFromBigInt(a) 77 | assert.Equal(t, a, h.BigInt()) 78 | hFromBytes, err := NewHashFromBytes(h.Bytes()) 79 | assert.Nil(t, err) 80 | assert.Equal(t, h, hFromBytes) 81 | assert.Equal(t, a, hFromBytes.BigInt()) 82 | assert.Equal(t, a.String(), hFromBytes.BigInt().String()) 83 | hFromHex, err := NewHashFromHex(h.Hex()) 84 | assert.Nil(t, err) 85 | assert.Equal(t, h, hFromHex) 86 | 87 | aBIFromHBytes, err := NewBigIntFromHashBytes(h.Bytes()) 88 | assert.Nil(t, err) 89 | assert.Equal(t, a, aBIFromHBytes) 90 | assert.Equal(t, new(big.Int).SetBytes(a.Bytes()).String(), aBIFromHBytes.String()) 91 | } 92 | 93 | func TestNewTree(t *testing.T) { 94 | mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10) 95 | assert.Nil(t, err) 96 | assert.Equal(t, "0", mt.Root().String()) 97 | 98 | // test vectors generated using https://github.com/iden3/circomlib smt.js 99 | err = mt.Add(big.NewInt(1), big.NewInt(2)) 100 | assert.Nil(t, err) 101 | assert.Equal(t, "13578938674299138072471463694055224830892726234048532520316387704878000008795", mt.Root().BigInt().String()) //nolint:lll 102 | 103 | err = mt.Add(big.NewInt(33), big.NewInt(44)) 104 | assert.Nil(t, err) 105 | assert.Equal(t, "5412393676474193513566895793055462193090331607895808993925969873307089394741", mt.Root().BigInt().String()) //nolint:lll 106 | 107 | err = mt.Add(big.NewInt(1234), big.NewInt(9876)) 108 | assert.Nil(t, err) 109 | assert.Equal(t, "14204494359367183802864593755198662203838502594566452929175967972147978322084", mt.Root().BigInt().String()) //nolint:lll 110 | 111 | dbRoot, err := mt.dbGetRoot() 112 | require.Nil(t, err) 113 | assert.Equal(t, mt.Root(), dbRoot) 114 | 115 | proof, v, err := mt.GenerateProof(big.NewInt(33), nil) 116 | assert.Nil(t, err) 117 | assert.Equal(t, big.NewInt(44), v) 118 | 119 | assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(33), big.NewInt(44))) 120 | assert.True(t, !VerifyProof(mt.Root(), proof, big.NewInt(33), big.NewInt(45))) 121 | } 122 | 123 | func TestAddDifferentOrder(t *testing.T) { 124 | mt1 := newTestingMerkle(t, 140) 125 | defer mt1.db.Close() 126 | for i := 0; i < 16; i++ { 127 | k := big.NewInt(int64(i)) 128 | v := big.NewInt(0) 129 | if err := mt1.Add(k, v); err != nil { 130 | t.Fatal(err) 131 | } 132 | } 133 | 134 | mt2 := newTestingMerkle(t, 140) 135 | defer mt2.db.Close() 136 | for i := 16 - 1; i >= 0; i-- { 137 | k := big.NewInt(int64(i)) 138 | v := big.NewInt(0) 139 | if err := mt2.Add(k, v); err != nil { 140 | t.Fatal(err) 141 | } 142 | } 143 | 144 | assert.Equal(t, mt1.Root().Hex(), mt2.Root().Hex()) 145 | assert.Equal(t, "3b89100bec24da9275c87bc188740389e1d5accfc7d88ba5688d7fa96a00d82f", mt1.Root().Hex()) //nolint:lll 146 | } 147 | 148 | func TestAddRepeatedIndex(t *testing.T) { 149 | mt := newTestingMerkle(t, 140) 150 | defer mt.db.Close() 151 | k := big.NewInt(int64(3)) 152 | v := big.NewInt(int64(12)) 153 | if err := mt.Add(k, v); err != nil { 154 | t.Fatal(err) 155 | } 156 | err := mt.Add(k, v) 157 | assert.NotNil(t, err) 158 | assert.Equal(t, err, ErrEntryIndexAlreadyExists) 159 | } 160 | 161 | func TestGet(t *testing.T) { 162 | mt := newTestingMerkle(t, 140) 163 | defer mt.db.Close() 164 | 165 | for i := 0; i < 16; i++ { 166 | k := big.NewInt(int64(i)) 167 | v := big.NewInt(int64(i * 2)) 168 | if err := mt.Add(k, v); err != nil { 169 | t.Fatal(err) 170 | } 171 | } 172 | k, v, _, err := mt.Get(big.NewInt(10)) 173 | assert.Nil(t, err) 174 | assert.Equal(t, big.NewInt(10), k) 175 | assert.Equal(t, big.NewInt(20), v) 176 | 177 | k, v, _, err = mt.Get(big.NewInt(15)) 178 | assert.Nil(t, err) 179 | assert.Equal(t, big.NewInt(15), k) 180 | assert.Equal(t, big.NewInt(30), v) 181 | 182 | k, v, _, err = mt.Get(big.NewInt(16)) 183 | assert.NotNil(t, err) 184 | assert.Equal(t, ErrKeyNotFound, err) 185 | assert.Equal(t, "0", k.String()) 186 | assert.Equal(t, "0", v.String()) 187 | } 188 | 189 | func TestUpdate(t *testing.T) { 190 | mt := newTestingMerkle(t, 140) 191 | defer mt.db.Close() 192 | 193 | for i := 0; i < 16; i++ { 194 | k := big.NewInt(int64(i)) 195 | v := big.NewInt(int64(i * 2)) 196 | if err := mt.Add(k, v); err != nil { 197 | t.Fatal(err) 198 | } 199 | } 200 | _, v, _, err := mt.Get(big.NewInt(10)) 201 | assert.Nil(t, err) 202 | assert.Equal(t, big.NewInt(20), v) 203 | 204 | _, err = mt.Update(big.NewInt(10), big.NewInt(1024)) 205 | assert.Nil(t, err) 206 | _, v, _, err = mt.Get(big.NewInt(10)) 207 | assert.Nil(t, err) 208 | assert.Equal(t, big.NewInt(1024), v) 209 | 210 | _, err = mt.Update(big.NewInt(1000), big.NewInt(1024)) 211 | assert.Equal(t, ErrKeyNotFound, err) 212 | 213 | dbRoot, err := mt.dbGetRoot() 214 | require.Nil(t, err) 215 | assert.Equal(t, mt.Root(), dbRoot) 216 | } 217 | 218 | func TestUpdate2(t *testing.T) { 219 | mt1 := newTestingMerkle(t, 140) 220 | defer mt1.db.Close() 221 | mt2 := newTestingMerkle(t, 140) 222 | defer mt2.db.Close() 223 | 224 | err := mt1.Add(big.NewInt(1), big.NewInt(119)) 225 | assert.Nil(t, err) 226 | err = mt1.Add(big.NewInt(2), big.NewInt(229)) 227 | assert.Nil(t, err) 228 | err = mt1.Add(big.NewInt(9876), big.NewInt(6789)) 229 | assert.Nil(t, err) 230 | 231 | err = mt2.Add(big.NewInt(1), big.NewInt(11)) 232 | assert.Nil(t, err) 233 | err = mt2.Add(big.NewInt(2), big.NewInt(22)) 234 | assert.Nil(t, err) 235 | err = mt2.Add(big.NewInt(9876), big.NewInt(10)) 236 | assert.Nil(t, err) 237 | 238 | _, err = mt1.Update(big.NewInt(1), big.NewInt(11)) 239 | assert.Nil(t, err) 240 | _, err = mt1.Update(big.NewInt(2), big.NewInt(22)) 241 | assert.Nil(t, err) 242 | _, err = mt2.Update(big.NewInt(9876), big.NewInt(6789)) 243 | assert.Nil(t, err) 244 | 245 | assert.Equal(t, mt1.Root(), mt2.Root()) 246 | } 247 | 248 | func TestGenerateAndVerifyProof128(t *testing.T) { 249 | mt, err := NewMerkleTree(memory.NewMemoryStorage(), 140) 250 | require.Nil(t, err) 251 | defer mt.db.Close() 252 | 253 | for i := 0; i < 128; i++ { 254 | k := big.NewInt(int64(i)) 255 | v := big.NewInt(0) 256 | if err := mt.Add(k, v); err != nil { 257 | t.Fatal(err) 258 | } 259 | } 260 | proof, v, err := mt.GenerateProof(big.NewInt(42), nil) 261 | assert.Nil(t, err) 262 | assert.Equal(t, "0", v.String()) 263 | assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(42), big.NewInt(0))) 264 | } 265 | 266 | func TestTreeLimit(t *testing.T) { 267 | mt, err := NewMerkleTree(memory.NewMemoryStorage(), 5) 268 | require.Nil(t, err) 269 | defer mt.db.Close() 270 | 271 | for i := 0; i < 16; i++ { 272 | err = mt.Add(big.NewInt(int64(i)), big.NewInt(int64(i))) 273 | assert.Nil(t, err) 274 | } 275 | 276 | // here the tree is full, should not allow to add more data as reaches the maximum number of levels 277 | err = mt.Add(big.NewInt(int64(16)), big.NewInt(int64(16))) 278 | assert.NotNil(t, err) 279 | assert.Equal(t, ErrReachedMaxLevel, err) 280 | } 281 | 282 | func TestSiblingsFromProof(t *testing.T) { 283 | mt, err := NewMerkleTree(memory.NewMemoryStorage(), 140) 284 | require.Nil(t, err) 285 | defer mt.db.Close() 286 | 287 | for i := 0; i < 64; i++ { 288 | k := big.NewInt(int64(i)) 289 | v := big.NewInt(0) 290 | if err := mt.Add(k, v); err != nil { 291 | t.Fatal(err) 292 | } 293 | } 294 | 295 | proof, _, err := mt.GenerateProof(big.NewInt(4), nil) 296 | if err != nil { 297 | t.Fatal(err) 298 | } 299 | 300 | siblings := SiblingsFromProof(proof) 301 | assert.Equal(t, 6, len(siblings)) 302 | assert.Equal(t, 303 | "d6e368bda90c5ee3e910222c1fc1c0d9e23f2d350dbc47f4a92de30f1be3c60b", 304 | siblings[0].Hex()) 305 | assert.Equal(t, 306 | "9dbd03b1bcd580e0f3e6668d80d55288f04464126feb1624ec8ee30be8df9c16", 307 | siblings[1].Hex()) 308 | assert.Equal(t, 309 | "de866af9545dcd1c5bb7811e7f27814918e037eb9fead40919e8f19525896e27", 310 | siblings[2].Hex()) 311 | assert.Equal(t, 312 | "5f4182212a84741d1174ba7c42e369f2e3ad8ade7d04eea2d0f98e3ed8b7a317", 313 | siblings[3].Hex()) 314 | assert.Equal(t, 315 | "77639098d513f7aef9730fdb1d1200401af5fe9da91b61772f4dd142ac89a122", 316 | siblings[4].Hex()) 317 | assert.Equal(t, 318 | "943ee501f4ba2137c79b54af745dfc5f105f539fcc449cd2a356eb5c030e3c07", 319 | siblings[5].Hex()) 320 | } 321 | 322 | func TestVerifyProofCases(t *testing.T) { 323 | mt := newTestingMerkle(t, 140) 324 | defer mt.DB().Close() 325 | 326 | for i := 0; i < 8; i++ { 327 | if err := mt.Add(big.NewInt(int64(i)), big.NewInt(0)); err != nil { 328 | t.Fatal(err) 329 | } 330 | } 331 | 332 | // Existence proof 333 | proof, _, err := mt.GenerateProof(big.NewInt(4), nil) 334 | if err != nil { 335 | t.Fatal(err) 336 | } 337 | assert.Equal(t, proof.Existence, true) 338 | assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(4), big.NewInt(0))) 339 | assert.Equal(t, "0003000000000000000000000000000000000000000000000000000000000007529cbedbda2bdd25fd6455551e55245fa6dc11a9d0c27dc0cd38fca44c17e40344ad686a18ba78b502c0b6f285c5c8393bde2f7a3e2abe586515e4d84533e3037b062539bde2d80749746986cf8f0001fd2cdbf9a89fcbf981a769daef49df06", hex.EncodeToString(proof.Bytes())) //nolint:lll 340 | 341 | for i := 8; i < 32; i++ { 342 | proof, _, err = mt.GenerateProof(big.NewInt(int64(i)), nil) 343 | assert.Nil(t, err) 344 | if debug { 345 | fmt.Println(i, proof) 346 | } 347 | } 348 | // Non-existence proof, empty aux 349 | proof, _, err = mt.GenerateProof(big.NewInt(12), nil) 350 | if err != nil { 351 | t.Fatal(err) 352 | } 353 | assert.Equal(t, proof.Existence, false) 354 | // assert.True(t, proof.nodeAux == nil) 355 | assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(12), big.NewInt(0))) 356 | assert.Equal(t, "0303000000000000000000000000000000000000000000000000000000000007529cbedbda2bdd25fd6455551e55245fa6dc11a9d0c27dc0cd38fca44c17e40344ad686a18ba78b502c0b6f285c5c8393bde2f7a3e2abe586515e4d84533e3037b062539bde2d80749746986cf8f0001fd2cdbf9a89fcbf981a769daef49df0604000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(proof.Bytes())) //nolint:lll 357 | 358 | // Non-existence proof, diff. node aux 359 | proof, _, err = mt.GenerateProof(big.NewInt(10), nil) 360 | if err != nil { 361 | t.Fatal(err) 362 | } 363 | assert.Equal(t, proof.Existence, false) 364 | assert.True(t, proof.NodeAux != nil) 365 | assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(10), big.NewInt(0))) 366 | assert.Equal(t, "0303000000000000000000000000000000000000000000000000000000000007529cbedbda2bdd25fd6455551e55245fa6dc11a9d0c27dc0cd38fca44c17e4030acfcdd2617df9eb5aef744c5f2e03eb8c92c61f679007dc1f2707fd908ea41a9433745b469c101edca814c498e7f388100d497b24f1d2ac935bced3572f591d02000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(proof.Bytes())) //nolint:lll 367 | } 368 | 369 | func TestVerifyProofFalse(t *testing.T) { 370 | mt := newTestingMerkle(t, 140) 371 | defer mt.DB().Close() 372 | 373 | for i := 0; i < 8; i++ { 374 | if err := mt.Add(big.NewInt(int64(i)), big.NewInt(0)); err != nil { 375 | t.Fatal(err) 376 | } 377 | } 378 | 379 | // Invalid existence proof (node used for verification doesn't 380 | // correspond to node in the proof) 381 | proof, _, err := mt.GenerateProof(big.NewInt(int64(4)), nil) 382 | if err != nil { 383 | t.Fatal(err) 384 | } 385 | assert.Equal(t, proof.Existence, true) 386 | assert.True(t, !VerifyProof(mt.Root(), proof, big.NewInt(int64(5)), big.NewInt(int64(5)))) 387 | 388 | // Invalid non-existence proof (Non-existence proof, diff. node aux) 389 | proof, _, err = mt.GenerateProof(big.NewInt(int64(4)), nil) 390 | if err != nil { 391 | t.Fatal(err) 392 | } 393 | assert.Equal(t, proof.Existence, true) 394 | // Now we change the proof from existence to non-existence, and add e's 395 | // data as auxiliary node. 396 | proof.Existence = false 397 | proof.NodeAux = &NodeAux{Key: NewHashFromBigInt(big.NewInt(int64(4))), 398 | Value: NewHashFromBigInt(big.NewInt(4))} 399 | assert.True(t, !VerifyProof(mt.Root(), proof, big.NewInt(int64(4)), big.NewInt(0))) 400 | } 401 | 402 | func TestGraphViz(t *testing.T) { 403 | mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10) 404 | assert.Nil(t, err) 405 | 406 | _ = mt.Add(big.NewInt(1), big.NewInt(0)) 407 | _ = mt.Add(big.NewInt(2), big.NewInt(0)) 408 | _ = mt.Add(big.NewInt(3), big.NewInt(0)) 409 | _ = mt.Add(big.NewInt(4), big.NewInt(0)) 410 | _ = mt.Add(big.NewInt(5), big.NewInt(0)) 411 | _ = mt.Add(big.NewInt(100), big.NewInt(0)) 412 | 413 | // mt.PrintGraphViz(nil) 414 | 415 | expected := `digraph hierarchy { 416 | node [fontname=Monospace,fontsize=10,shape=box] 417 | "56332309..." -> {"18483622..." "20902180..."} 418 | "18483622..." -> {"75768243..." "16893244..."} 419 | "75768243..." -> {"empty0" "21857056..."} 420 | "empty0" [style=dashed,label=0]; 421 | "21857056..." -> {"51072523..." "empty1"} 422 | "empty1" [style=dashed,label=0]; 423 | "51072523..." -> {"17311038..." "empty2"} 424 | "empty2" [style=dashed,label=0]; 425 | "17311038..." -> {"69499803..." "21008290..."} 426 | "69499803..." [style=filled]; 427 | "21008290..." [style=filled]; 428 | "16893244..." [style=filled]; 429 | "20902180..." -> {"12496585..." "18055627..."} 430 | "12496585..." -> {"19374975..." "15739329..."} 431 | "19374975..." [style=filled]; 432 | "15739329..." [style=filled]; 433 | "18055627..." [style=filled]; 434 | } 435 | ` 436 | w := bytes.NewBufferString("") 437 | err = mt.GraphViz(w, nil) 438 | assert.Nil(t, err) 439 | assert.Equal(t, []byte(expected), w.Bytes()) 440 | } 441 | 442 | func TestDelete(t *testing.T) { 443 | mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10) 444 | assert.Nil(t, err) 445 | assert.Equal(t, "0", mt.Root().String()) 446 | 447 | // test vectors generated using https://github.com/iden3/circomlib smt.js 448 | err = mt.Add(big.NewInt(1), big.NewInt(2)) 449 | assert.Nil(t, err) 450 | assert.Equal(t, "13578938674299138072471463694055224830892726234048532520316387704878000008795", mt.Root().BigInt().String()) //nolint:lll 451 | 452 | err = mt.Add(big.NewInt(33), big.NewInt(44)) 453 | assert.Nil(t, err) 454 | assert.Equal(t, "5412393676474193513566895793055462193090331607895808993925969873307089394741", mt.Root().BigInt().String()) //nolint:lll 455 | 456 | err = mt.Add(big.NewInt(1234), big.NewInt(9876)) 457 | assert.Nil(t, err) 458 | assert.Equal(t, "14204494359367183802864593755198662203838502594566452929175967972147978322084", mt.Root().BigInt().String()) //nolint:lll 459 | 460 | // mt.PrintGraphViz(nil) 461 | 462 | err = mt.Delete(big.NewInt(33)) 463 | // mt.PrintGraphViz(nil) 464 | assert.Nil(t, err) 465 | assert.Equal(t, "15550352095346187559699212771793131433118240951738528922418613687814377955591", mt.Root().BigInt().String()) //nolint:lll 466 | 467 | err = mt.Delete(big.NewInt(1234)) 468 | assert.Nil(t, err) 469 | err = mt.Delete(big.NewInt(1)) 470 | assert.Nil(t, err) 471 | assert.Equal(t, "0", mt.Root().String()) 472 | 473 | dbRoot, err := mt.dbGetRoot() 474 | require.Nil(t, err) 475 | assert.Equal(t, mt.Root(), dbRoot) 476 | } 477 | 478 | func TestDelete2(t *testing.T) { 479 | mt := newTestingMerkle(t, 140) 480 | defer mt.db.Close() 481 | for i := 0; i < 8; i++ { 482 | k := big.NewInt(int64(i)) 483 | v := big.NewInt(0) 484 | if err := mt.Add(k, v); err != nil { 485 | t.Fatal(err) 486 | } 487 | } 488 | 489 | expectedRoot := mt.Root() 490 | 491 | k := big.NewInt(8) 492 | v := big.NewInt(0) 493 | err := mt.Add(k, v) 494 | require.Nil(t, err) 495 | 496 | err = mt.Delete(big.NewInt(8)) 497 | assert.Nil(t, err) 498 | assert.Equal(t, expectedRoot, mt.Root()) 499 | 500 | mt2 := newTestingMerkle(t, 140) 501 | defer mt2.db.Close() 502 | for i := 0; i < 8; i++ { 503 | k := big.NewInt(int64(i)) 504 | v := big.NewInt(0) 505 | if err := mt2.Add(k, v); err != nil { 506 | t.Fatal(err) 507 | } 508 | } 509 | assert.Equal(t, mt2.Root(), mt.Root()) 510 | } 511 | 512 | func TestDelete3(t *testing.T) { 513 | mt := newTestingMerkle(t, 140) 514 | defer mt.db.Close() 515 | 516 | err := mt.Add(big.NewInt(1), big.NewInt(1)) 517 | assert.Nil(t, err) 518 | 519 | err = mt.Add(big.NewInt(2), big.NewInt(2)) 520 | assert.Nil(t, err) 521 | 522 | assert.Equal(t, "19060075022714027595905950662613111880864833370144986660188929919683258088314", mt.Root().BigInt().String()) //nolint:lll 523 | err = mt.Delete(big.NewInt(1)) 524 | assert.Nil(t, err) 525 | assert.Equal(t, "849831128489032619062850458217693666094013083866167024127442191257793527951", mt.Root().BigInt().String()) //nolint:lll 526 | 527 | mt2 := newTestingMerkle(t, 140) 528 | defer mt2.db.Close() 529 | err = mt2.Add(big.NewInt(2), big.NewInt(2)) 530 | assert.Nil(t, err) 531 | assert.Equal(t, mt2.Root(), mt.Root()) 532 | } 533 | 534 | func TestDelete4(t *testing.T) { 535 | mt := newTestingMerkle(t, 140) 536 | defer mt.db.Close() 537 | 538 | err := mt.Add(big.NewInt(1), big.NewInt(1)) 539 | assert.Nil(t, err) 540 | 541 | err = mt.Add(big.NewInt(2), big.NewInt(2)) 542 | assert.Nil(t, err) 543 | 544 | err = mt.Add(big.NewInt(3), big.NewInt(3)) 545 | assert.Nil(t, err) 546 | 547 | assert.Equal(t, "14109632483797541575275728657193822866549917334388996328141438956557066918117", mt.Root().BigInt().String()) //nolint:lll 548 | err = mt.Delete(big.NewInt(1)) 549 | assert.Nil(t, err) 550 | assert.Equal(t, "159935162486187606489815340465698714590556679404589449576549073038844694972", mt.Root().BigInt().String()) //nolint:lll 551 | 552 | mt2 := newTestingMerkle(t, 140) 553 | defer mt2.db.Close() 554 | err = mt2.Add(big.NewInt(2), big.NewInt(2)) 555 | assert.Nil(t, err) 556 | err = mt2.Add(big.NewInt(3), big.NewInt(3)) 557 | assert.Nil(t, err) 558 | assert.Equal(t, mt2.Root(), mt.Root()) 559 | } 560 | 561 | func TestDelete5(t *testing.T) { 562 | mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10) 563 | assert.Nil(t, err) 564 | 565 | err = mt.Add(big.NewInt(1), big.NewInt(2)) 566 | assert.Nil(t, err) 567 | err = mt.Add(big.NewInt(33), big.NewInt(44)) 568 | assert.Nil(t, err) 569 | assert.Equal(t, "5412393676474193513566895793055462193090331607895808993925969873307089394741", mt.Root().BigInt().String()) //nolint:lll 570 | 571 | err = mt.Delete(big.NewInt(1)) 572 | assert.Nil(t, err) 573 | assert.Equal(t, "18869260084287237667925661423624848342947598951870765316380602291081195309822", mt.Root().BigInt().String()) //nolint:lll 574 | 575 | mt2 := newTestingMerkle(t, 140) 576 | defer mt2.db.Close() 577 | err = mt2.Add(big.NewInt(33), big.NewInt(44)) 578 | assert.Nil(t, err) 579 | assert.Equal(t, mt2.Root(), mt.Root()) 580 | } 581 | 582 | func TestDeleteNonExistingKeys(t *testing.T) { 583 | mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10) 584 | assert.Nil(t, err) 585 | 586 | err = mt.Add(big.NewInt(1), big.NewInt(2)) 587 | assert.Nil(t, err) 588 | err = mt.Add(big.NewInt(33), big.NewInt(44)) 589 | assert.Nil(t, err) 590 | 591 | err = mt.Delete(big.NewInt(33)) 592 | assert.Nil(t, err) 593 | err = mt.Delete(big.NewInt(33)) 594 | assert.Equal(t, ErrKeyNotFound, err) 595 | 596 | err = mt.Delete(big.NewInt(1)) 597 | assert.Nil(t, err) 598 | 599 | assert.Equal(t, "0", mt.Root().String()) 600 | 601 | err = mt.Delete(big.NewInt(33)) 602 | assert.Equal(t, ErrKeyNotFound, err) 603 | } 604 | 605 | func TestDumpLeafsImportLeafs(t *testing.T) { 606 | mt, err := NewMerkleTree(memory.NewMemoryStorage(), 140) 607 | require.Nil(t, err) 608 | defer mt.db.Close() 609 | 610 | q1 := new(big.Int).Sub(constants.Q, big.NewInt(1)) 611 | for i := 0; i < 10; i++ { 612 | // use numbers near under Q 613 | k := new(big.Int).Sub(q1, big.NewInt(int64(i))) 614 | v := big.NewInt(0) 615 | err = mt.Add(k, v) 616 | require.Nil(t, err) 617 | 618 | // use numbers near above 0 619 | k = big.NewInt(int64(i)) 620 | err = mt.Add(k, v) 621 | require.Nil(t, err) 622 | } 623 | 624 | d, err := mt.DumpLeafs(nil) 625 | assert.Nil(t, err) 626 | 627 | mt2, err := NewMerkleTree(memory.NewMemoryStorage(), 140) 628 | require.Nil(t, err) 629 | defer mt2.db.Close() 630 | err = mt2.ImportDumpedLeafs(d) 631 | assert.Nil(t, err) 632 | 633 | assert.Equal(t, mt.Root(), mt2.Root()) 634 | } 635 | 636 | func TestAddAndGetCircomProof(t *testing.T) { 637 | mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10) 638 | assert.Nil(t, err) 639 | assert.Equal(t, "0", mt.Root().String()) 640 | 641 | // test vectors generated using https://github.com/iden3/circomlib smt.js 642 | cpp, err := mt.AddAndGetCircomProof(big.NewInt(1), big.NewInt(2)) 643 | assert.Nil(t, err) 644 | assert.Equal(t, "0", cpp.OldRoot.String()) 645 | assert.Equal(t, "13578938...", cpp.NewRoot.String()) 646 | assert.Equal(t, "0", cpp.OldKey.String()) 647 | assert.Equal(t, "0", cpp.OldValue.String()) 648 | assert.Equal(t, "1", cpp.NewKey.String()) 649 | assert.Equal(t, "2", cpp.NewValue.String()) 650 | assert.Equal(t, true, cpp.IsOld0) 651 | assert.Equal(t, "[0 0 0 0 0 0 0 0 0 0 0]", fmt.Sprintf("%v", cpp.Siblings)) 652 | assert.Equal(t, mt.maxLevels+1, len(cpp.Siblings)) 653 | 654 | cpp, err = mt.AddAndGetCircomProof(big.NewInt(33), big.NewInt(44)) 655 | assert.Nil(t, err) 656 | assert.Equal(t, "13578938...", cpp.OldRoot.String()) 657 | assert.Equal(t, "54123936...", cpp.NewRoot.String()) 658 | assert.Equal(t, "1", cpp.OldKey.String()) 659 | assert.Equal(t, "2", cpp.OldValue.String()) 660 | assert.Equal(t, "33", cpp.NewKey.String()) 661 | assert.Equal(t, "44", cpp.NewValue.String()) 662 | assert.Equal(t, false, cpp.IsOld0) 663 | assert.Equal(t, "[0 0 0 0 0 0 0 0 0 0 0]", fmt.Sprintf("%v", cpp.Siblings)) 664 | assert.Equal(t, mt.maxLevels+1, len(cpp.Siblings)) 665 | 666 | cpp, err = mt.AddAndGetCircomProof(big.NewInt(55), big.NewInt(66)) 667 | assert.Nil(t, err) 668 | assert.Equal(t, "54123936...", cpp.OldRoot.String()) 669 | assert.Equal(t, "50943640...", cpp.NewRoot.String()) 670 | assert.Equal(t, "0", cpp.OldKey.String()) 671 | assert.Equal(t, "0", cpp.OldValue.String()) 672 | assert.Equal(t, "55", cpp.NewKey.String()) 673 | assert.Equal(t, "66", cpp.NewValue.String()) 674 | assert.Equal(t, true, cpp.IsOld0) 675 | assert.Equal(t, "[0 21312042... 0 0 0 0 0 0 0 0 0]", fmt.Sprintf("%v", cpp.Siblings)) 676 | assert.Equal(t, mt.maxLevels+1, len(cpp.Siblings)) 677 | } 678 | 679 | func TestUpdateCircomProcessorProof(t *testing.T) { 680 | mt := newTestingMerkle(t, 10) 681 | defer mt.db.Close() 682 | 683 | for i := 0; i < 16; i++ { 684 | k := big.NewInt(int64(i)) 685 | v := big.NewInt(int64(i * 2)) 686 | if err := mt.Add(k, v); err != nil { 687 | t.Fatal(err) 688 | } 689 | } 690 | _, v, _, err := mt.Get(big.NewInt(10)) 691 | assert.Nil(t, err) 692 | assert.Equal(t, big.NewInt(20), v) 693 | 694 | // test vectors generated using https://github.com/iden3/circomlib smt.js 695 | cpp, err := mt.Update(big.NewInt(10), big.NewInt(1024)) 696 | assert.Nil(t, err) 697 | assert.Equal(t, "39010880...", cpp.OldRoot.String()) 698 | assert.Equal(t, "18587862...", cpp.NewRoot.String()) 699 | assert.Equal(t, "10", cpp.OldKey.String()) 700 | assert.Equal(t, "20", cpp.OldValue.String()) 701 | assert.Equal(t, "10", cpp.NewKey.String()) 702 | assert.Equal(t, "1024", cpp.NewValue.String()) 703 | assert.Equal(t, false, cpp.IsOld0) 704 | assert.Equal(t, 705 | "[34930557... 20201609... 18790542... 15930030... 0 0 0 0 0 0 0]", 706 | fmt.Sprintf("%v", cpp.Siblings)) 707 | } 708 | 709 | func TestSmtVerifier(t *testing.T) { 710 | mt, err := NewMerkleTree(memory.NewMemoryStorage(), 4) 711 | assert.Nil(t, err) 712 | 713 | err = mt.Add(big.NewInt(1), big.NewInt(11)) 714 | assert.Nil(t, err) 715 | 716 | cvp, err := mt.GenerateSCVerifierProof(big.NewInt(1), nil) 717 | assert.Nil(t, err) 718 | jCvp, err := json.Marshal(cvp) 719 | assert.Nil(t, err) 720 | // expect siblings to be '[]', instead of 'null' 721 | expected := `{"root":"6525056641794203554583616941316772618766382307684970171204065038799368146416","siblings":[],"oldKey":"0","oldValue":"0","isOld0":false,"key":"1","value":"11","fnc":0}` //nolint:lll 722 | 723 | assert.Equal(t, expected, string(jCvp)) 724 | err = mt.Add(big.NewInt(2), big.NewInt(22)) 725 | assert.Nil(t, err) 726 | err = mt.Add(big.NewInt(3), big.NewInt(33)) 727 | assert.Nil(t, err) 728 | err = mt.Add(big.NewInt(4), big.NewInt(44)) 729 | assert.Nil(t, err) 730 | 731 | cvp, err = mt.GenerateCircomVerifierProof(big.NewInt(2), nil) 732 | assert.Nil(t, err) 733 | 734 | jCvp, err = json.Marshal(cvp) 735 | assert.Nil(t, err) 736 | // Test vectors generated using https://github.com/iden3/circomlib smt.js 737 | // Expect siblings with the extra 0 that the circom circuits need 738 | expected = `{"root":"13558168455220559042747853958949063046226645447188878859760119761585093422436","siblings":["11620130507635441932056895853942898236773847390796721536119314875877874016518","5158240518874928563648144881543092238925265313977134167935552944620041388700","0","0","0"],"oldKey":"0","oldValue":"0","isOld0":false,"key":"2","value":"22","fnc":0}` //nolint:lll 739 | assert.Equal(t, expected, string(jCvp)) 740 | 741 | cvp, err = mt.GenerateSCVerifierProof(big.NewInt(2), nil) 742 | assert.Nil(t, err) 743 | 744 | jCvp, err = json.Marshal(cvp) 745 | assert.Nil(t, err) 746 | // Test vectors generated using https://github.com/iden3/circomlib smt.js 747 | // Without the extra 0 that the circom circuits need, but that are not 748 | // needed at a smart contract verification 749 | expected = `{"root":"13558168455220559042747853958949063046226645447188878859760119761585093422436","siblings":["11620130507635441932056895853942898236773847390796721536119314875877874016518","5158240518874928563648144881543092238925265313977134167935552944620041388700"],"oldKey":"0","oldValue":"0","isOld0":false,"key":"2","value":"22","fnc":0}` //nolint:lll 750 | assert.Equal(t, expected, string(jCvp)) 751 | } 752 | 753 | func TestTypesMarshalers(t *testing.T) { 754 | // test Hash marshalers 755 | h, err := NewHashFromString("42") 756 | assert.Nil(t, err) 757 | s, err := json.Marshal(h) 758 | assert.Nil(t, err) 759 | var h2 *Hash 760 | err = json.Unmarshal(s, &h2) 761 | assert.Nil(t, err) 762 | assert.Equal(t, h, h2) 763 | 764 | // create CircomProcessorProof 765 | mt := newTestingMerkle(t, 10) 766 | defer mt.db.Close() 767 | for i := 0; i < 16; i++ { 768 | k := big.NewInt(int64(i)) 769 | v := big.NewInt(int64(i * 2)) 770 | if err := mt.Add(k, v); err != nil { 771 | t.Fatal(err) 772 | } 773 | } 774 | _, v, _, err := mt.Get(big.NewInt(10)) 775 | assert.Nil(t, err) 776 | assert.Equal(t, big.NewInt(20), v) 777 | cpp, err := mt.Update(big.NewInt(10), big.NewInt(1024)) 778 | assert.Nil(t, err) 779 | 780 | // test CircomProcessorProof marshalers 781 | b, err := json.Marshal(&cpp) 782 | assert.Nil(t, err) 783 | 784 | var cpp2 *CircomProcessorProof 785 | err = json.Unmarshal(b, &cpp2) 786 | assert.Nil(t, err) 787 | assert.Equal(t, cpp, cpp2) 788 | } 789 | -------------------------------------------------------------------------------- /node.go: -------------------------------------------------------------------------------- 1 | package merkletree 2 | 3 | import ( 4 | "fmt" 5 | "math/big" 6 | ) 7 | 8 | // NodeType defines the type of node in the MT. 9 | type NodeType byte 10 | 11 | const ( 12 | // NodeTypeMiddle indicates the type of middle Node that has children. 13 | NodeTypeMiddle NodeType = 0 14 | // NodeTypeLeaf indicates the type of a leaf Node that contains a key & 15 | // value. 16 | NodeTypeLeaf NodeType = 1 17 | // NodeTypeEmpty indicates the type of an empty Node. 18 | NodeTypeEmpty NodeType = 2 19 | 20 | // DBEntryTypeRoot indicates the type of a DB entry that indicates the 21 | // current Root of a MerkleTree 22 | DBEntryTypeRoot NodeType = 3 23 | ) 24 | 25 | // Node is the struct that represents a node in the MT. The node should not be 26 | // modified after creation because the cached key won't be updated. 27 | type Node struct { 28 | // Type is the type of node in the tree. 29 | Type NodeType 30 | // ChildL is the left child of a middle node. 31 | ChildL *Hash 32 | // ChildR is the right child of a middle node. 33 | ChildR *Hash 34 | // Entry is the data stored in a leaf node. 35 | Entry [2]*Hash 36 | // key is a cache used to avoid recalculating key 37 | key *Hash 38 | } 39 | 40 | // NewNodeLeaf creates a new leaf node. 41 | func NewNodeLeaf(k, v *Hash) *Node { 42 | return &Node{Type: NodeTypeLeaf, Entry: [2]*Hash{k, v}} 43 | } 44 | 45 | // NewNodeMiddle creates a new middle node. 46 | func NewNodeMiddle(childL *Hash, childR *Hash) *Node { 47 | return &Node{Type: NodeTypeMiddle, ChildL: childL, ChildR: childR} 48 | } 49 | 50 | // NewNodeEmpty creates a new empty node. 51 | func NewNodeEmpty() *Node { 52 | return &Node{Type: NodeTypeEmpty} 53 | } 54 | 55 | // NewNodeFromBytes creates a new node by parsing the input []byte. 56 | func NewNodeFromBytes(b []byte) (*Node, error) { 57 | if len(b) < 1 { 58 | return nil, ErrNodeBytesBadSize 59 | } 60 | n := Node{Type: NodeType(b[0])} 61 | b = b[1:] 62 | switch n.Type { 63 | case NodeTypeMiddle: 64 | if len(b) != 2*ElemBytesLen { 65 | return nil, ErrNodeBytesBadSize 66 | } 67 | n.ChildL, n.ChildR = &Hash{}, &Hash{} 68 | copy(n.ChildL[:], b[:ElemBytesLen]) 69 | copy(n.ChildR[:], b[ElemBytesLen:ElemBytesLen*2]) 70 | case NodeTypeLeaf: 71 | if len(b) != 2*ElemBytesLen { 72 | return nil, ErrNodeBytesBadSize 73 | } 74 | n.Entry = [2]*Hash{{}, {}} 75 | copy(n.Entry[0][:], b[0:32]) 76 | copy(n.Entry[1][:], b[32:64]) 77 | case NodeTypeEmpty: 78 | break 79 | default: 80 | return nil, ErrInvalidNodeFound 81 | } 82 | return &n, nil 83 | } 84 | 85 | // LeafKey computes the key of a leaf node given the hIndex and hValue of the 86 | // entry of the leaf. 87 | func LeafKey(k, v *Hash) (*Hash, error) { 88 | return HashElemsKey(big.NewInt(1), k.BigInt(), v.BigInt()) 89 | } 90 | 91 | // Key computes the key of the node by hashing the content in a specific way 92 | // for each type of node. This key is used as the hash of the merklee tree for 93 | // each node. 94 | func (n *Node) Key() (*Hash, error) { 95 | if n.key == nil { // Cache the key to avoid repeated hash computations. 96 | // NOTE: We are not using the type to calculate the hash! 97 | switch n.Type { 98 | case NodeTypeMiddle: // H(ChildL || ChildR) 99 | var err error 100 | n.key, err = HashElems(n.ChildL.BigInt(), n.ChildR.BigInt()) 101 | if err != nil { 102 | return nil, err 103 | } 104 | case NodeTypeLeaf: 105 | var err error 106 | n.key, err = LeafKey(n.Entry[0], n.Entry[1]) 107 | if err != nil { 108 | return nil, err 109 | } 110 | case NodeTypeEmpty: // Zero 111 | n.key = &HashZero 112 | default: 113 | n.key = &HashZero 114 | } 115 | } 116 | return n.key, nil 117 | } 118 | 119 | // Value returns the value of the node. This is the content that is stored in 120 | // the backend database. 121 | func (n *Node) Value() []byte { 122 | switch n.Type { 123 | case NodeTypeMiddle: // {Type || ChildL || ChildR} 124 | return append([]byte{byte(n.Type)}, append(n.ChildL[:], n.ChildR[:]...)...) 125 | case NodeTypeLeaf: // {Type || Data...} 126 | return append([]byte{byte(n.Type)}, append(n.Entry[0][:], n.Entry[1][:]...)...) 127 | case NodeTypeEmpty: // {} 128 | return []byte{} 129 | default: 130 | return []byte{} 131 | } 132 | } 133 | 134 | // String outputs a string representation of a node (different for each type). 135 | func (n *Node) String() string { 136 | switch n.Type { 137 | case NodeTypeMiddle: // {Type || ChildL || ChildR} 138 | return fmt.Sprintf("Middle L:%s R:%s", n.ChildL, n.ChildR) 139 | case NodeTypeLeaf: // {Type || Data...} 140 | return fmt.Sprintf("Leaf I:%v D:%v", n.Entry[0], n.Entry[1]) 141 | case NodeTypeEmpty: // {} 142 | return "Empty" 143 | default: 144 | return "Invalid Node" 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /utils.go: -------------------------------------------------------------------------------- 1 | package merkletree 2 | 3 | import ( 4 | "math/big" 5 | 6 | "github.com/iden3/go-iden3-crypto/poseidon" 7 | ) 8 | 9 | // HashElems performs a poseidon hash over the array of ElemBytes, currently we 10 | // are using 2 elements. Uses poseidon.Hash to be compatible with the circom 11 | // circuits implementations. 12 | func HashElems(elems ...*big.Int) (*Hash, error) { 13 | poseidonHash, err := poseidon.Hash(elems) 14 | if err != nil { 15 | return nil, err 16 | } 17 | return NewHashFromBigInt(poseidonHash), nil 18 | } 19 | 20 | // HashElemsKey performs a poseidon hash over the array of ElemBytes, currently 21 | // we are using 2 elements. 22 | func HashElemsKey(key *big.Int, elems ...*big.Int) (*Hash, error) { 23 | if key == nil { 24 | key = new(big.Int).SetInt64(0) 25 | } 26 | bi := make([]*big.Int, 3) 27 | copy(bi[:], elems) 28 | bi[2] = key 29 | poseidonHash, err := poseidon.Hash(bi) 30 | if err != nil { 31 | return nil, err 32 | } 33 | return NewHashFromBigInt(poseidonHash), nil 34 | } 35 | 36 | // SetBitBigEndian sets the bit n in the bitmap to 1, in Big Endian. 37 | func SetBitBigEndian(bitmap []byte, n uint) { 38 | bitmap[uint(len(bitmap))-n/8-1] |= 1 << (n % 8) 39 | } 40 | 41 | // TestBit tests whether the bit n in bitmap is 1. 42 | func TestBit(bitmap []byte, n uint) bool { 43 | return bitmap[n/8]&(1<<(n%8)) != 0 44 | } 45 | 46 | // TestBitBigEndian tests whether the bit n in bitmap is 1, in Big Endian. 47 | func TestBitBigEndian(bitmap []byte, n uint) bool { 48 | return bitmap[uint(len(bitmap))-n/8-1]&(1<<(n%8)) != 0 49 | } 50 | 51 | // SwapEndianness swaps the order of the bytes in the slice. 52 | func SwapEndianness(b []byte) []byte { 53 | o := make([]byte, len(b)) 54 | for i := range b { 55 | o[len(b)-1-i] = b[i] 56 | } 57 | return o 58 | } 59 | --------------------------------------------------------------------------------