Browse Source

Tests migrate from stretchr/testify to frankban/quicktest

master
arnaucube 3 years ago
parent
commit
f8ac4be02b
5 changed files with 85 additions and 70 deletions
  1. +1
    -1
      go.mod
  2. +8
    -5
      go.sum
  3. +9
    -7
      hash_test.go
  4. +7
    -1
      tree.go
  5. +60
    -56
      tree_test.go

+ 1
- 1
go.mod

@ -3,7 +3,7 @@ module github.com/arnaucube/arbo
go 1.14 go 1.14
require ( require (
github.com/frankban/quicktest v1.11.3
github.com/iden3/go-iden3-crypto v0.0.6-0.20210308142348-8f85683b2cef github.com/iden3/go-iden3-crypto v0.0.6-0.20210308142348-8f85683b2cef
github.com/iden3/go-merkletree v0.0.0-20210308143313-8b63ca866189 github.com/iden3/go-merkletree v0.0.0-20210308143313-8b63ca866189
github.com/stretchr/testify v1.7.0
) )

+ 8
- 5
go.sum

@ -58,7 +58,6 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/blake512 v1.0.0/go.mod h1:FV1x7xPPLWukZlpDpWQ88rF/SFwZ5qbskrzhLMB92JI= github.com/dchest/blake512 v1.0.0/go.mod h1:FV1x7xPPLWukZlpDpWQ88rF/SFwZ5qbskrzhLMB92JI=
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ= github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ=
@ -83,6 +82,8 @@ github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0= github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0=
github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4/go.mod h1:T9YF2M40nIgbVgp3rreNmTged+9HrbNTIQf1PsaIiTA= github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4/go.mod h1:T9YF2M40nIgbVgp3rreNmTged+9HrbNTIQf1PsaIiTA=
github.com/frankban/quicktest v1.11.3 h1:8sXhOn0uLys67V8EsXLc6eszDs8VXWxL3iRvebPhedY=
github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/gavv/httpexpect v2.0.0+incompatible/go.mod h1:x+9tiU1YnrOvnB725RkpoLv1M62hOWzwo5OXotisrKc= github.com/gavv/httpexpect v2.0.0+incompatible/go.mod h1:x+9tiU1YnrOvnB725RkpoLv1M62hOWzwo5OXotisrKc=
@ -127,6 +128,8 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
@ -170,7 +173,10 @@ github.com/klauspost/compress v1.9.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g= github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g=
@ -220,7 +226,6 @@ github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTw
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
@ -255,8 +260,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA= github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA=
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs= github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
@ -342,6 +345,7 @@ golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
@ -375,7 +379,6 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

+ 9
- 7
hash_test.go

@ -5,7 +5,7 @@ import (
"math/big" "math/big"
"testing" "testing"
"github.com/stretchr/testify/assert"
qt "github.com/frankban/quicktest"
) )
func TestHashSha256(t *testing.T) { func TestHashSha256(t *testing.T) {
@ -16,9 +16,10 @@ func TestHashSha256(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
assert.Equal(t,
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
hex.EncodeToString(h))
c := qt.New(t)
c.Assert(hex.EncodeToString(h),
qt.Equals,
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")
} }
func TestHashPoseidon(t *testing.T) { func TestHashPoseidon(t *testing.T) {
@ -32,7 +33,8 @@ func TestHashPoseidon(t *testing.T) {
} }
hBI := BytesToBigInt(h) hBI := BytesToBigInt(h)
// value checked with circomlib // value checked with circomlib
assert.Equal(t,
"7853200120776062878684798364095072458815029376092732009249414926327459813530",
hBI.String())
c := qt.New(t)
c.Assert(hBI.String(),
qt.Equals,
"7853200120776062878684798364095072458815029376092732009249414926327459813530")
} }

+ 7
- 1
tree.go

@ -57,8 +57,8 @@ type Tree struct {
// will load it. // will load it.
func NewTree(storage db.Storage, maxLevels int, hash HashFunction) (*Tree, error) { func NewTree(storage db.Storage, maxLevels int, hash HashFunction) (*Tree, error) {
t := Tree{db: storage, maxLevels: maxLevels, hashFunction: hash} t := Tree{db: storage, maxLevels: maxLevels, hashFunction: hash}
t.updateAccessTime() t.updateAccessTime()
root, err := t.dbGet(nil, dbKeyRoot) root, err := t.dbGet(nil, dbKeyRoot)
if err == db.ErrNotFound { if err == db.ErrNotFound {
// store new root 0 // store new root 0
@ -99,6 +99,7 @@ func (t *Tree) Root() []byte {
// optimized to do some internal parallelization. Returns an array containing // optimized to do some internal parallelization. Returns an array containing
// the indexes of the keys failed to add. // the indexes of the keys failed to add.
func (t *Tree) AddBatch(keys, values [][]byte) ([]int, error) { func (t *Tree) AddBatch(keys, values [][]byte) ([]int, error) {
t.updateAccessTime()
if len(keys) != len(values) { if len(keys) != len(values) {
return nil, fmt.Errorf("len(keys)!=len(values) (%d!=%d)", return nil, fmt.Errorf("len(keys)!=len(values) (%d!=%d)",
len(keys), len(values)) len(keys), len(values))
@ -131,6 +132,7 @@ func (t *Tree) AddBatch(keys, values [][]byte) ([]int, error) {
// is expected that are represented by a Little-Endian byte array (for circom // is expected that are represented by a Little-Endian byte array (for circom
// compatibility). // compatibility).
func (t *Tree) Add(k, v []byte) error { func (t *Tree) Add(k, v []byte) error {
t.updateAccessTime()
tx, err := t.db.NewTx() tx, err := t.db.NewTx()
if err != nil { if err != nil {
return err return err
@ -373,6 +375,7 @@ func getPath(numLevels int, k []byte) []bool {
// the Tree, the proof will be of existence, if the key does not exist in the // the Tree, the proof will be of existence, if the key does not exist in the
// tree, the proof will be of non-existence. // tree, the proof will be of non-existence.
func (t *Tree) GenProof(k []byte) ([]byte, error) { func (t *Tree) GenProof(k []byte) ([]byte, error) {
t.updateAccessTime()
keyPath := make([]byte, t.hashFunction.Len()) keyPath := make([]byte, t.hashFunction.Len())
copy(keyPath[:], k) copy(keyPath[:], k)
@ -540,6 +543,7 @@ func (t *Tree) dbGet(tx db.Tx, k []byte) ([]byte, error) {
// Iterate iterates through the full Tree, executing the given function on each // Iterate iterates through the full Tree, executing the given function on each
// node of the Tree. // node of the Tree.
func (t *Tree) Iterate(f func([]byte, []byte)) error { func (t *Tree) Iterate(f func([]byte, []byte)) error {
t.updateAccessTime()
return t.iter(t.root, f) return t.iter(t.root, f)
} }
@ -575,6 +579,7 @@ func (t *Tree) iter(k []byte, f func([]byte, []byte)) error {
// [ len(k) | len(v) | key | value ] // [ len(k) | len(v) | key | value ]
// Where S is the size of the output of the hash function used for the Tree. // Where S is the size of the output of the hash function used for the Tree.
func (t *Tree) Dump() ([]byte, error) { func (t *Tree) Dump() ([]byte, error) {
t.updateAccessTime()
// WARNING current encoding only supports key & values of 255 bytes each // WARNING current encoding only supports key & values of 255 bytes each
// (due using only 1 byte for the length headers). // (due using only 1 byte for the length headers).
var b []byte var b []byte
@ -596,6 +601,7 @@ func (t *Tree) Dump() ([]byte, error) {
// ImportDump imports the leafs (that have been exported with the ExportLeafs // ImportDump imports the leafs (that have been exported with the ExportLeafs
// method) in the Tree. // method) in the Tree.
func (t *Tree) ImportDump(b []byte) error { func (t *Tree) ImportDump(b []byte) error {
t.updateAccessTime()
r := bytes.NewReader(b) r := bytes.NewReader(b)
for { for {
l := make([]byte, 2) l := make([]byte, 2)

+ 60
- 56
tree_test.go

@ -2,16 +2,16 @@ package arbo
import ( import (
"encoding/hex" "encoding/hex"
"fmt"
"math/big" "math/big"
"testing" "testing"
qt "github.com/frankban/quicktest"
"github.com/iden3/go-merkletree/db/memory" "github.com/iden3/go-merkletree/db/memory"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestAddTestVectors(t *testing.T) { func TestAddTestVectors(t *testing.T) {
c := qt.New(t)
// Poseidon test vectors generated using https://github.com/iden3/circomlib smt.js // Poseidon test vectors generated using https://github.com/iden3/circomlib smt.js
testVectorsPoseidon := []string{ testVectorsPoseidon := []string{
"0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000000",
@ -19,7 +19,7 @@ func TestAddTestVectors(t *testing.T) {
"5412393676474193513566895793055462193090331607895808993925969873307089394741", "5412393676474193513566895793055462193090331607895808993925969873307089394741",
"14204494359367183802864593755198662203838502594566452929175967972147978322084", "14204494359367183802864593755198662203838502594566452929175967972147978322084",
} }
testAdd(t, HashFunctionPoseidon, testVectorsPoseidon)
testAdd(c, HashFunctionPoseidon, testVectorsPoseidon)
testVectorsSha256 := []string{ testVectorsSha256 := []string{
"0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000000",
@ -27,41 +27,42 @@ func TestAddTestVectors(t *testing.T) {
"59481735341404520835410489183267411392292882901306595567679529387376287440550", "59481735341404520835410489183267411392292882901306595567679529387376287440550",
"20573794434149960984975763118181266662429997821552560184909083010514790081771", "20573794434149960984975763118181266662429997821552560184909083010514790081771",
} }
testAdd(t, HashFunctionSha256, testVectorsSha256)
testAdd(c, HashFunctionSha256, testVectorsSha256)
} }
func testAdd(t *testing.T, hashFunc HashFunction, testVectors []string) {
func testAdd(c *qt.C, hashFunc HashFunction, testVectors []string) {
tree, err := NewTree(memory.NewMemoryStorage(), 10, hashFunc) tree, err := NewTree(memory.NewMemoryStorage(), 10, hashFunc)
assert.Nil(t, err)
c.Assert(err, qt.IsNil)
defer tree.db.Close() defer tree.db.Close()
assert.Equal(t, testVectors[0], hex.EncodeToString(tree.Root()))
c.Check(hex.EncodeToString(tree.Root()), qt.Equals, testVectors[0])
err = tree.Add( err = tree.Add(
BigIntToBytes(big.NewInt(1)), BigIntToBytes(big.NewInt(1)),
BigIntToBytes(big.NewInt(2))) BigIntToBytes(big.NewInt(2)))
assert.Nil(t, err)
c.Assert(err, qt.IsNil)
rootBI := BytesToBigInt(tree.Root()) rootBI := BytesToBigInt(tree.Root())
assert.Equal(t, testVectors[1], rootBI.String())
c.Check(rootBI.String(), qt.Equals, testVectors[1])
err = tree.Add( err = tree.Add(
BigIntToBytes(big.NewInt(33)), BigIntToBytes(big.NewInt(33)),
BigIntToBytes(big.NewInt(44))) BigIntToBytes(big.NewInt(44)))
assert.Nil(t, err)
c.Assert(err, qt.IsNil)
rootBI = BytesToBigInt(tree.Root()) rootBI = BytesToBigInt(tree.Root())
assert.Equal(t, testVectors[2], rootBI.String())
c.Check(rootBI.String(), qt.Equals, testVectors[2])
err = tree.Add( err = tree.Add(
BigIntToBytes(big.NewInt(1234)), BigIntToBytes(big.NewInt(1234)),
BigIntToBytes(big.NewInt(9876))) BigIntToBytes(big.NewInt(9876)))
assert.Nil(t, err)
c.Assert(err, qt.IsNil)
rootBI = BytesToBigInt(tree.Root()) rootBI = BytesToBigInt(tree.Root())
assert.Equal(t, testVectors[3], rootBI.String())
c.Check(rootBI.String(), qt.Equals, testVectors[3])
} }
func TestAddBatch(t *testing.T) { func TestAddBatch(t *testing.T) {
c := qt.New(t)
tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon) tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
require.Nil(t, err)
c.Assert(err, qt.IsNil)
defer tree.db.Close() defer tree.db.Close()
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
@ -73,12 +74,11 @@ func TestAddBatch(t *testing.T) {
} }
rootBI := BytesToBigInt(tree.Root()) rootBI := BytesToBigInt(tree.Root())
assert.Equal(t,
"296519252211642170490407814696803112091039265640052570497930797516015811235",
rootBI.String())
c.Check(rootBI.String(), qt.Equals,
"296519252211642170490407814696803112091039265640052570497930797516015811235")
tree2, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon) tree2, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
require.Nil(t, err)
c.Assert(err, qt.IsNil)
defer tree2.db.Close() defer tree2.db.Close()
var keys, values [][]byte var keys, values [][]byte
@ -89,18 +89,18 @@ func TestAddBatch(t *testing.T) {
values = append(values, v) values = append(values, v)
} }
indexes, err := tree2.AddBatch(keys, values) indexes, err := tree2.AddBatch(keys, values)
assert.Nil(t, err)
assert.Equal(t, 0, len(indexes))
c.Assert(err, qt.IsNil)
c.Check(len(indexes), qt.Equals, 0)
rootBI = BytesToBigInt(tree2.Root()) rootBI = BytesToBigInt(tree2.Root())
assert.Equal(t,
"296519252211642170490407814696803112091039265640052570497930797516015811235",
rootBI.String())
c.Check(rootBI.String(), qt.Equals,
"296519252211642170490407814696803112091039265640052570497930797516015811235")
} }
func TestAddDifferentOrder(t *testing.T) { func TestAddDifferentOrder(t *testing.T) {
c := qt.New(t)
tree1, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon) tree1, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
require.Nil(t, err)
c.Assert(err, qt.IsNil)
defer tree1.db.Close() defer tree1.db.Close()
for i := 0; i < 16; i++ { for i := 0; i < 16; i++ {
@ -112,7 +112,7 @@ func TestAddDifferentOrder(t *testing.T) {
} }
tree2, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon) tree2, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
require.Nil(t, err)
c.Assert(err, qt.IsNil)
defer tree2.db.Close() defer tree2.db.Close()
for i := 16 - 1; i >= 0; i-- { for i := 16 - 1; i >= 0; i-- {
@ -123,15 +123,15 @@ func TestAddDifferentOrder(t *testing.T) {
} }
} }
assert.Equal(t, hex.EncodeToString(tree1.Root()), hex.EncodeToString(tree2.Root()))
assert.Equal(t,
"3b89100bec24da9275c87bc188740389e1d5accfc7d88ba5688d7fa96a00d82f",
hex.EncodeToString(tree1.Root()))
c.Check(hex.EncodeToString(tree2.Root()), qt.Equals, hex.EncodeToString(tree1.Root()))
c.Check(hex.EncodeToString(tree1.Root()), qt.Equals,
"3b89100bec24da9275c87bc188740389e1d5accfc7d88ba5688d7fa96a00d82f")
} }
func TestAddRepeatedIndex(t *testing.T) { func TestAddRepeatedIndex(t *testing.T) {
c := qt.New(t)
tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon) tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
require.Nil(t, err)
c.Assert(err, qt.IsNil)
defer tree.db.Close() defer tree.db.Close()
k := big.NewInt(int64(3)).Bytes() k := big.NewInt(int64(3)).Bytes()
@ -140,38 +140,40 @@ func TestAddRepeatedIndex(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = tree.Add(k, v) err = tree.Add(k, v)
assert.NotNil(t, err)
assert.Equal(t, fmt.Errorf("max virtual level 100"), err)
c.Assert(err, qt.Not(qt.IsNil))
c.Check(err, qt.ErrorMatches, "max virtual level 100")
} }
func TestAux(t *testing.T) { func TestAux(t *testing.T) {
c := qt.New(t)
tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon) tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
require.Nil(t, err)
c.Assert(err, qt.IsNil)
defer tree.db.Close() defer tree.db.Close()
k := BigIntToBytes(big.NewInt(int64(1))) k := BigIntToBytes(big.NewInt(int64(1)))
v := BigIntToBytes(big.NewInt(int64(0))) v := BigIntToBytes(big.NewInt(int64(0)))
err = tree.Add(k, v) err = tree.Add(k, v)
assert.Nil(t, err)
c.Assert(err, qt.IsNil)
k = BigIntToBytes(big.NewInt(int64(256))) k = BigIntToBytes(big.NewInt(int64(256)))
err = tree.Add(k, v) err = tree.Add(k, v)
assert.Nil(t, err)
c.Assert(err, qt.IsNil)
k = BigIntToBytes(big.NewInt(int64(257))) k = BigIntToBytes(big.NewInt(int64(257)))
err = tree.Add(k, v) err = tree.Add(k, v)
assert.Nil(t, err)
c.Assert(err, qt.IsNil)
k = BigIntToBytes(big.NewInt(int64(515))) k = BigIntToBytes(big.NewInt(int64(515)))
err = tree.Add(k, v) err = tree.Add(k, v)
assert.Nil(t, err)
c.Assert(err, qt.IsNil)
k = BigIntToBytes(big.NewInt(int64(770))) k = BigIntToBytes(big.NewInt(int64(770)))
err = tree.Add(k, v) err = tree.Add(k, v)
assert.Nil(t, err)
c.Assert(err, qt.IsNil)
} }
func TestGet(t *testing.T) { func TestGet(t *testing.T) {
c := qt.New(t)
tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon) tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
require.Nil(t, err)
c.Assert(err, qt.IsNil)
defer tree.db.Close() defer tree.db.Close()
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
@ -184,14 +186,15 @@ func TestGet(t *testing.T) {
k := BigIntToBytes(big.NewInt(int64(7))) k := BigIntToBytes(big.NewInt(int64(7)))
gettedKey, gettedValue, err := tree.Get(k) gettedKey, gettedValue, err := tree.Get(k)
assert.Nil(t, err)
assert.Equal(t, k, gettedKey)
assert.Equal(t, BigIntToBytes(big.NewInt(int64(7*2))), gettedValue)
c.Assert(err, qt.IsNil)
c.Check(gettedKey, qt.DeepEquals, k)
c.Check(gettedValue, qt.DeepEquals, BigIntToBytes(big.NewInt(int64(7*2))))
} }
func TestGenProofAndVerify(t *testing.T) { func TestGenProofAndVerify(t *testing.T) {
c := qt.New(t)
tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon) tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
require.Nil(t, err)
c.Assert(err, qt.IsNil)
defer tree.db.Close() defer tree.db.Close()
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
@ -204,18 +207,19 @@ func TestGenProofAndVerify(t *testing.T) {
k := BigIntToBytes(big.NewInt(int64(7))) k := BigIntToBytes(big.NewInt(int64(7)))
siblings, err := tree.GenProof(k) siblings, err := tree.GenProof(k)
assert.Nil(t, err)
c.Assert(err, qt.IsNil)
k = BigIntToBytes(big.NewInt(int64(7))) k = BigIntToBytes(big.NewInt(int64(7)))
v := BigIntToBytes(big.NewInt(int64(14))) v := BigIntToBytes(big.NewInt(int64(14)))
verif, err := CheckProof(tree.hashFunction, k, v, tree.Root(), siblings) verif, err := CheckProof(tree.hashFunction, k, v, tree.Root(), siblings)
require.Nil(t, err)
assert.True(t, verif)
c.Assert(err, qt.IsNil)
c.Check(verif, qt.IsTrue)
} }
func TestDumpAndImportDump(t *testing.T) { func TestDumpAndImportDump(t *testing.T) {
c := qt.New(t)
tree1, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon) tree1, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
require.Nil(t, err)
c.Assert(err, qt.IsNil)
defer tree1.db.Close() defer tree1.db.Close()
for i := 0; i < 16; i++ { for i := 0; i < 16; i++ {
@ -227,17 +231,16 @@ func TestDumpAndImportDump(t *testing.T) {
} }
e, err := tree1.Dump() e, err := tree1.Dump()
require.Nil(t, err)
c.Assert(err, qt.IsNil)
tree2, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon) tree2, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
require.Nil(t, err)
c.Assert(err, qt.IsNil)
defer tree2.db.Close() defer tree2.db.Close()
err = tree2.ImportDump(e) err = tree2.ImportDump(e)
require.Nil(t, err)
assert.Equal(t, tree1.Root(), tree2.Root())
assert.Equal(t,
"0d93aaa3362b2f999f15e15728f123087c2eee716f01c01f56e23aae07f09f08",
hex.EncodeToString(tree2.Root()))
c.Assert(err, qt.IsNil)
c.Check(tree2.Root(), qt.DeepEquals, tree1.Root())
c.Check(hex.EncodeToString(tree2.Root()), qt.Equals,
"0d93aaa3362b2f999f15e15728f123087c2eee716f01c01f56e23aae07f09f08")
} }
func BenchmarkAdd(b *testing.B) { func BenchmarkAdd(b *testing.B) {
@ -259,8 +262,9 @@ func BenchmarkAdd(b *testing.B) {
} }
func benchmarkAdd(b *testing.B, hashFunc HashFunction, ks, vs [][]byte) { func benchmarkAdd(b *testing.B, hashFunc HashFunction, ks, vs [][]byte) {
c := qt.New(b)
tree, err := NewTree(memory.NewMemoryStorage(), 140, hashFunc) tree, err := NewTree(memory.NewMemoryStorage(), 140, hashFunc)
require.Nil(b, err)
c.Assert(err, qt.IsNil)
defer tree.db.Close() defer tree.db.Close()
for i := 0; i < len(ks); i++ { for i := 0; i < len(ks); i++ {

Loading…
Cancel
Save