Skip to content

Commit

Permalink
Address review comments - onconflict donothing for inserts
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Zhang <[email protected]>
  • Loading branch information
jimthematrix committed Sep 18, 2024
1 parent 690278b commit b1a4d3b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 0 additions & 4 deletions go-sdk/internal/sparse-merkle-tree/smt/merkletree.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,6 @@ func (mt *sparseMerkleTree) addNode(batch core.Transaction, n core.Node) (core.N
return n.Ref(), nil
}
k := n.Ref()
// Check that the node key doesn't already exist
if _, err := batch.GetNode(k); err == nil {
return nil, ErrNodeIndexAlreadyExists
}
err := batch.InsertNode(n)
return k, err
}
Expand Down
6 changes: 5 additions & 1 deletion go-sdk/internal/sparse-merkle-tree/storage/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/hyperledger-labs/zeto/go-sdk/internal/sparse-merkle-tree/utils"
"github.com/hyperledger-labs/zeto/go-sdk/pkg/sparse-merkle-tree/core"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type sqlStorage struct {
Expand Down Expand Up @@ -164,6 +165,9 @@ func insertNode(batchOrDb *gorm.DB, nodesTableName string, n core.Node) error {
dbNode.Index = &idx
}

err := batchOrDb.Table(nodesTableName).Create(dbNode).Error
// the merkle tree nodes, whether leaf nodes or branch nodes, are constructed
// in such a way that the reference key is the hash of the node's content, so
// there's no need to do anything if a node already exists in the DB
err := batchOrDb.Table(nodesTableName).Clauses(clause.OnConflict{DoNothing: true}).Create(dbNode).Error
return err
}

0 comments on commit b1a4d3b

Please sign in to comment.