-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make merkle tree leaf addition operations atomic #71
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8c35ca2
Make merkle tree leaf addition operations atomic
jimthematrix 690278b
Test coverage
jimthematrix b1a4d3b
Address review comments - onconflict donothing for inserts
jimthematrix 6532fdb
Merge branch 'main' into sql-atomicity
jimthematrix dcae880
Update go-sdk/integration-test/smt_test.go
jimthematrix 2554636
Rename batch to tx
jimthematrix 7d660d5
Increase max level of the test merkle tree
jimthematrix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,7 +17,9 @@ | |||||||||
package integration_test | ||||||||||
|
||||||||||
import ( | ||||||||||
"fmt" | ||||||||||
"math/big" | ||||||||||
"math/rand" | ||||||||||
"os" | ||||||||||
"testing" | ||||||||||
|
||||||||||
|
@@ -27,65 +29,89 @@ import ( | |||||||||
"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/sirupsen/logrus" | ||||||||||
"github.com/stretchr/testify/assert" | ||||||||||
"github.com/stretchr/testify/suite" | ||||||||||
"gorm.io/driver/postgres" | ||||||||||
"gorm.io/driver/sqlite" | ||||||||||
"gorm.io/gorm" | ||||||||||
) | ||||||||||
|
||||||||||
type SqliteTestSuite struct { | ||||||||||
suite.Suite | ||||||||||
db core.Storage | ||||||||||
dbfile *os.File | ||||||||||
gormDB *gorm.DB | ||||||||||
smtName string | ||||||||||
} | ||||||||||
|
||||||||||
type testSqlProvider struct { | ||||||||||
db *gorm.DB | ||||||||||
} | ||||||||||
|
||||||||||
func (s *testSqlProvider) DB() *gorm.DB { | ||||||||||
return s.db | ||||||||||
func (p *testSqlProvider) DB() *gorm.DB { | ||||||||||
return p.db | ||||||||||
} | ||||||||||
|
||||||||||
func (s *testSqlProvider) Close() {} | ||||||||||
func (p *testSqlProvider) Close() {} | ||||||||||
|
||||||||||
func TestSqliteStorage(t *testing.T) { | ||||||||||
dbfile, err := os.CreateTemp("", "gorm.db") | ||||||||||
func newSqliteStorage(t *testing.T) (*os.File, core.Storage, *gorm.DB, string) { | ||||||||||
seq := rand.Intn(1000) | ||||||||||
testName := fmt.Sprintf("test_%d", seq) | ||||||||||
dbfile, err := os.CreateTemp("", fmt.Sprintf("gorm-%s.db", testName)) | ||||||||||
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{}) | ||||||||||
err = db.Table(core.NodesTablePrefix + testName).AutoMigrate(&core.SMTNode{}) | ||||||||||
assert.NoError(t, err) | ||||||||||
|
||||||||||
provider := &testSqlProvider{db: db} | ||||||||||
s, err := storage.NewSqlStorage(provider, "test_1") | ||||||||||
sqlStorage, err := storage.NewSqlStorage(provider, testName) | ||||||||||
assert.NoError(t, err) | ||||||||||
return dbfile, sqlStorage, db, testName | ||||||||||
} | ||||||||||
|
||||||||||
mt, err := smt.NewMerkleTree(s, MAX_HEIGHT) | ||||||||||
assert.NoError(t, err) | ||||||||||
func (s *SqliteTestSuite) SetupTest() { | ||||||||||
logrus.SetLevel(logrus.DebugLevel) | ||||||||||
s.dbfile, s.db, s.gormDB, s.smtName = newSqliteStorage(s.T()) | ||||||||||
} | ||||||||||
|
||||||||||
func (s *SqliteTestSuite) TearDownTest() { | ||||||||||
os.Remove(s.dbfile.Name()) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
func (s *SqliteTestSuite) TestSqliteStorage() { | ||||||||||
mt, err := smt.NewMerkleTree(s.db, MAX_HEIGHT) | ||||||||||
assert.NoError(s.T(), err) | ||||||||||
|
||||||||||
tokenId := big.NewInt(1001) | ||||||||||
uriString := "https://example.com/token/1001" | ||||||||||
assert.NoError(t, err) | ||||||||||
assert.NoError(s.T(), err) | ||||||||||
sender := testutils.NewKeypair() | ||||||||||
salt1 := crypto.NewSalt() | ||||||||||
|
||||||||||
utxo1 := node.NewNonFungible(tokenId, uriString, sender.PublicKey, salt1) | ||||||||||
n1, err := node.NewLeafNode(utxo1) | ||||||||||
assert.NoError(t, err) | ||||||||||
assert.NoError(s.T(), err) | ||||||||||
err = mt.AddLeaf(n1) | ||||||||||
assert.NoError(t, err) | ||||||||||
assert.NoError(s.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) | ||||||||||
dbRoot := core.SMTRoot{Name: s.smtName} | ||||||||||
err = s.gormDB.Table(core.TreeRootsTable).First(&dbRoot).Error | ||||||||||
assert.NoError(s.T(), err) | ||||||||||
assert.Equal(s.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) | ||||||||||
err = s.gormDB.Table(core.NodesTablePrefix + s.smtName).First(&dbNode).Error | ||||||||||
assert.NoError(s.T(), err) | ||||||||||
assert.Equal(s.T(), n1.Ref().Hex(), dbNode.RefKey) | ||||||||||
} | ||||||||||
|
||||||||||
func TestSqliteStorage(t *testing.T) { | ||||||||||
suite.Run(t, new(SqliteTestSuite)) | ||||||||||
} | ||||||||||
|
||||||||||
func TestPostgresStorage(t *testing.T) { | ||||||||||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error check is regressed, this will cause removal failure due to the wrong file name not being captured.