From 7177bfbd7144b98d9825ffda96637319b2fb06d8 Mon Sep 17 00:00:00 2001 From: Enrique Lacal Date: Fri, 16 Aug 2024 12:30:41 +0200 Subject: [PATCH 1/3] Fix deleting DBs in test for PSQL and document setup Signed-off-by: Enrique Lacal --- go-sdk/README.md | 5 +++++ go-sdk/integration-test/e2e_test.go | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/go-sdk/README.md b/go-sdk/README.md index c565588..bbf6247 100644 --- a/go-sdk/README.md +++ b/go-sdk/README.md @@ -23,6 +23,11 @@ Once the proving keys and verification keys are generated, set the following env - `CIRCUITS_ROOT`: the folder that contains the WASM runtime for the circuits that are generated by the circom compiler. If you do not want to compile the circtuis yourself, a copy of them are included in the `js/lib` folder - `PROVING_KEYS_ROOT`: the folder that contains the proving keys and verification keys +Note: you need to be running a postgres database locally before running the tests, you can run it in Docker with: +```console +docker run -d --name postgres -e POSTGRES_PASSWORD=my-secret -p 5432:5432 postgres +``` + ```console $ go test -v ./integration-test/ === RUN TestZeto_1_SuccessfulProving diff --git a/go-sdk/integration-test/e2e_test.go b/go-sdk/integration-test/e2e_test.go index 23a1577..88dfa63 100644 --- a/go-sdk/integration-test/e2e_test.go +++ b/go-sdk/integration-test/e2e_test.go @@ -657,8 +657,9 @@ func TestPostgresStorage(t *testing.T) { assert.NoError(t, err) defer func() { - db.Exec("DROP TABLE " + core.TreeRootsTable) - db.Exec("DROP TABLE " + core.NodesTablePrefix + "test_1") + // Table name needs to be wrapped in quotes if not it will be lowercased + db.Exec("DROP TABLE " + "\"" + core.TreeRootsTable + "\"") + db.Exec("DROP TABLE " + "\"" + core.NodesTablePrefix + "test_1\"") }() provider := &testSqlProvider{db: db} From 5023d1e7b9de1ded3eab514461ad08aad8674cc4 Mon Sep 17 00:00:00 2001 From: Enrique Lacal Date: Fri, 16 Aug 2024 14:59:02 +0200 Subject: [PATCH 2/3] Move db tests to different file and check for error on delete Signed-off-by: Enrique Lacal --- go-sdk/integration-test/db_test.go | 137 ++++++++++++++++++++++++++++ go-sdk/integration-test/e2e_test.go | 115 ----------------------- 2 files changed, 137 insertions(+), 115 deletions(-) create mode 100644 go-sdk/integration-test/db_test.go diff --git a/go-sdk/integration-test/db_test.go b/go-sdk/integration-test/db_test.go new file mode 100644 index 0000000..dfb4a9f --- /dev/null +++ b/go-sdk/integration-test/db_test.go @@ -0,0 +1,137 @@ +// Copyright © 2024 Kaleido, Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package integration_test + +import ( + "math/big" + "os" + "testing" + + "github.com/hyperledger-labs/zeto/go-sdk/internal/testutils" + "github.com/hyperledger-labs/zeto/go-sdk/pkg/sparse-merkle-tree/core" + "github.com/hyperledger-labs/zeto/go-sdk/pkg/sparse-merkle-tree/node" + "github.com/hyperledger-labs/zeto/go-sdk/pkg/sparse-merkle-tree/smt" + "github.com/hyperledger-labs/zeto/go-sdk/pkg/sparse-merkle-tree/storage" + "github.com/hyperledger-labs/zeto/go-sdk/pkg/utxo" + "github.com/stretchr/testify/assert" + "gorm.io/driver/postgres" + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +type testSqlProvider struct { + db *gorm.DB +} + +func (s *testSqlProvider) DB() *gorm.DB { + return s.db +} + +func (s *testSqlProvider) Close() {} + +func TestSqliteStorage(t *testing.T) { + dbfile, err := os.CreateTemp("", "gorm.db") + assert.NoError(t, err) + defer func() { + err := os.Remove(dbfile.Name()) + assert.NoError(t, err) + }() + db, err := gorm.Open(sqlite.Open(dbfile.Name()), &gorm.Config{}) + assert.NoError(t, err) + err = db.Table(core.TreeRootsTable).AutoMigrate(&core.SMTRoot{}) + assert.NoError(t, err) + err = db.Table(core.NodesTablePrefix + "test_1").AutoMigrate(&core.SMTNode{}) + assert.NoError(t, err) + + provider := &testSqlProvider{db: db} + s, err := storage.NewSqlStorage(provider, "test_1") + assert.NoError(t, err) + + mt, err := smt.NewMerkleTree(s, MAX_HEIGHT) + assert.NoError(t, err) + + tokenId := big.NewInt(1001) + uriString := "https://example.com/token/1001" + assert.NoError(t, err) + sender := testutils.NewKeypair() + salt1 := utxo.NewSalt() + + utxo1 := node.NewNonFungible(tokenId, uriString, sender.PublicKey, salt1) + n1, err := node.NewLeafNode(utxo1) + assert.NoError(t, err) + err = mt.AddLeaf(n1) + assert.NoError(t, err) + + root := mt.Root() + dbRoot := core.SMTRoot{Name: "test_1"} + err = db.Table(core.TreeRootsTable).First(&dbRoot).Error + assert.NoError(t, err) + assert.Equal(t, root.Hex(), dbRoot.RootIndex) + + dbNode := core.SMTNode{RefKey: n1.Ref().Hex()} + err = db.Table(core.NodesTablePrefix + "test_1").First(&dbNode).Error + assert.NoError(t, err) + assert.Equal(t, n1.Ref().Hex(), dbNode.RefKey) +} + +func TestPostgresStorage(t *testing.T) { + dsn := "host=localhost user=postgres password=my-secret dbname=postgres port=5432 sslmode=disable" + db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) + assert.NoError(t, err) + err = db.Table(core.TreeRootsTable).AutoMigrate(&core.SMTRoot{}) + assert.NoError(t, err) + err = db.Table(core.NodesTablePrefix + "test_1").AutoMigrate(&core.SMTNode{}) + assert.NoError(t, err) + + defer func() { + // Table name needs to be wrapped in quotes if not it will be lowercased + tx := db.Exec("DROP TABLE " + "\"" + core.TreeRootsTable + "\"") + assert.NoError(t, tx.Error) + tx = db.Exec("DROP TABLE " + "\"" + core.NodesTablePrefix + "test_1\"") + assert.NoError(t, tx.Error) + }() + + provider := &testSqlProvider{db: db} + s, err := storage.NewSqlStorage(provider, "test_1") + assert.NoError(t, err) + + mt, err := smt.NewMerkleTree(s, MAX_HEIGHT) + assert.NoError(t, err) + + tokenId := big.NewInt(1001) + tokenUri := "https://example.com/token/1001" + assert.NoError(t, err) + sender := testutils.NewKeypair() + salt1 := utxo.NewSalt() + + utxo1 := node.NewNonFungible(tokenId, tokenUri, sender.PublicKey, salt1) + n1, err := node.NewLeafNode(utxo1) + assert.NoError(t, err) + err = mt.AddLeaf(n1) + assert.NoError(t, err) + + root := mt.Root() + dbRoot := core.SMTRoot{Name: "test_1"} + err = db.Table(core.TreeRootsTable).First(&dbRoot).Error + assert.NoError(t, err) + assert.Equal(t, root.Hex(), dbRoot.RootIndex) + + dbNode := core.SMTNode{RefKey: n1.Ref().Hex()} + err = db.Table(core.NodesTablePrefix + "test_1").First(&dbNode).Error + assert.NoError(t, err) + assert.Equal(t, n1.Ref().Hex(), dbNode.RefKey) +} diff --git a/go-sdk/integration-test/e2e_test.go b/go-sdk/integration-test/e2e_test.go index 88dfa63..780ae48 100644 --- a/go-sdk/integration-test/e2e_test.go +++ b/go-sdk/integration-test/e2e_test.go @@ -28,7 +28,6 @@ import ( "github.com/hyperledger-labs/zeto/go-sdk/internal/testutils" keyscore "github.com/hyperledger-labs/zeto/go-sdk/pkg/key-manager/core" "github.com/hyperledger-labs/zeto/go-sdk/pkg/key-manager/key" - "github.com/hyperledger-labs/zeto/go-sdk/pkg/sparse-merkle-tree/core" "github.com/hyperledger-labs/zeto/go-sdk/pkg/sparse-merkle-tree/node" "github.com/hyperledger-labs/zeto/go-sdk/pkg/sparse-merkle-tree/smt" "github.com/hyperledger-labs/zeto/go-sdk/pkg/sparse-merkle-tree/storage" @@ -41,9 +40,6 @@ import ( "github.com/iden3/go-rapidsnark/witness/v2" "github.com/iden3/go-rapidsnark/witness/wasmer" "github.com/stretchr/testify/assert" - "gorm.io/driver/postgres" - "gorm.io/driver/sqlite" - "gorm.io/gorm" ) const MAX_HEIGHT = 64 @@ -592,114 +588,3 @@ func testConcurrentInsertion(t *testing.T, alice *babyjub.PublicKey, values []in assert.Equal(t, "abacf46f5217552ee28fe50b8fd7ca6aa46daeb9acf9f60928654c3b1a472f23", mt.Root().Hex()) } - -type testSqlProvider struct { - db *gorm.DB -} - -func (s *testSqlProvider) DB() *gorm.DB { - return s.db -} - -func (s *testSqlProvider) Close() {} - -func TestSqliteStorage(t *testing.T) { - dbfile, err := os.CreateTemp("", "gorm.db") - assert.NoError(t, err) - defer func() { - os.Remove(dbfile.Name()) - }() - db, err := gorm.Open(sqlite.Open(dbfile.Name()), &gorm.Config{}) - assert.NoError(t, err) - err = db.Table(core.TreeRootsTable).AutoMigrate(&core.SMTRoot{}) - assert.NoError(t, err) - err = db.Table(core.NodesTablePrefix + "test_1").AutoMigrate(&core.SMTNode{}) - assert.NoError(t, err) - - provider := &testSqlProvider{db: db} - s, err := storage.NewSqlStorage(provider, "test_1") - assert.NoError(t, err) - - mt, err := smt.NewMerkleTree(s, MAX_HEIGHT) - assert.NoError(t, err) - - tokenId := big.NewInt(1001) - uriString := "https://example.com/token/1001" - assert.NoError(t, err) - sender := testutils.NewKeypair() - salt1 := utxo.NewSalt() - - utxo1 := node.NewNonFungible(tokenId, uriString, sender.PublicKey, salt1) - n1, err := node.NewLeafNode(utxo1) - assert.NoError(t, err) - err = mt.AddLeaf(n1) - assert.NoError(t, err) - - root := mt.Root() - dbRoot := core.SMTRoot{Name: "test_1"} - err = db.Table(core.TreeRootsTable).First(&dbRoot).Error - assert.NoError(t, err) - assert.Equal(t, root.Hex(), dbRoot.RootIndex) - - dbNode := core.SMTNode{RefKey: n1.Ref().Hex()} - err = db.Table(core.NodesTablePrefix + "test_1").First(&dbNode).Error - assert.NoError(t, err) - assert.Equal(t, n1.Ref().Hex(), dbNode.RefKey) -} - -func TestPostgresStorage(t *testing.T) { - dsn := "host=localhost user=postgres password=my-secret dbname=postgres port=5432 sslmode=disable" - db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) - assert.NoError(t, err) - err = db.Table(core.TreeRootsTable).AutoMigrate(&core.SMTRoot{}) - assert.NoError(t, err) - err = db.Table(core.NodesTablePrefix + "test_1").AutoMigrate(&core.SMTNode{}) - assert.NoError(t, err) - - defer func() { - // Table name needs to be wrapped in quotes if not it will be lowercased - db.Exec("DROP TABLE " + "\"" + core.TreeRootsTable + "\"") - db.Exec("DROP TABLE " + "\"" + core.NodesTablePrefix + "test_1\"") - }() - - provider := &testSqlProvider{db: db} - s, err := storage.NewSqlStorage(provider, "test_1") - assert.NoError(t, err) - - mt, err := smt.NewMerkleTree(s, MAX_HEIGHT) - assert.NoError(t, err) - - tokenId := big.NewInt(1001) - tokenUri := "https://example.com/token/1001" - assert.NoError(t, err) - sender := testutils.NewKeypair() - salt1 := utxo.NewSalt() - - utxo1 := node.NewNonFungible(tokenId, tokenUri, sender.PublicKey, salt1) - n1, err := node.NewLeafNode(utxo1) - assert.NoError(t, err) - err = mt.AddLeaf(n1) - assert.NoError(t, err) - - root := mt.Root() - dbRoot := core.SMTRoot{Name: "test_1"} - err = db.Table(core.TreeRootsTable).First(&dbRoot).Error - assert.NoError(t, err) - assert.Equal(t, root.Hex(), dbRoot.RootIndex) - - dbNode := core.SMTNode{RefKey: n1.Ref().Hex()} - err = db.Table(core.NodesTablePrefix + "test_1").First(&dbNode).Error - assert.NoError(t, err) - assert.Equal(t, n1.Ref().Hex(), dbNode.RefKey) -} - -func TestKeyManager(t *testing.T) { - keypair := decryptKeyStorev3(t) - - keyEntry := key.NewKeyEntryFromPrivateKeyBytes([32]byte(keypair.PrivateKeyBytes())) - assert.NotNil(t, keyEntry) - - assert.NotNil(t, keyEntry.PrivateKey) - assert.NotNil(t, keyEntry.PublicKey) - assert.NotNil(t, keyEntry.PrivateKeyForZkp) -} From ab147951b008677eff26528f646c29989040bf26 Mon Sep 17 00:00:00 2001 From: Enrique Lacal Date: Fri, 16 Aug 2024 16:00:42 +0200 Subject: [PATCH 3/3] Add missed test Signed-off-by: Enrique Lacal --- go-sdk/integration-test/e2e_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/go-sdk/integration-test/e2e_test.go b/go-sdk/integration-test/e2e_test.go index 780ae48..b498a9b 100644 --- a/go-sdk/integration-test/e2e_test.go +++ b/go-sdk/integration-test/e2e_test.go @@ -588,3 +588,14 @@ func testConcurrentInsertion(t *testing.T, alice *babyjub.PublicKey, values []in assert.Equal(t, "abacf46f5217552ee28fe50b8fd7ca6aa46daeb9acf9f60928654c3b1a472f23", mt.Root().Hex()) } + +func TestKeyManager(t *testing.T) { + keypair := decryptKeyStorev3(t) + + keyEntry := key.NewKeyEntryFromPrivateKeyBytes([32]byte(keypair.PrivateKeyBytes())) + assert.NotNil(t, keyEntry) + + assert.NotNil(t, keyEntry.PrivateKey) + assert.NotNil(t, keyEntry.PublicKey) + assert.NotNil(t, keyEntry.PrivateKeyForZkp) +}