Skip to content

Commit

Permalink
Merge pull request #40 from hyperledger-labs/fix_db_setup_go
Browse files Browse the repository at this point in the history
Fix deleting DBs in test for PSQL and document setup
  • Loading branch information
jimthematrix authored Aug 16, 2024
2 parents eb4888c + ab14795 commit d74d756
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 103 deletions.
5 changes: 5 additions & 0 deletions go-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
137 changes: 137 additions & 0 deletions go-sdk/integration-test/db_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
103 changes: 0 additions & 103 deletions go-sdk/integration-test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -593,105 +589,6 @@ 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() {
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)

Expand Down

0 comments on commit d74d756

Please sign in to comment.