Skip to content

Commit

Permalink
Refactor(smt): code quality (0xPolygonHermez#1509)
Browse files Browse the repository at this point in the history
* fix(smt): handle errors in updateDepth and SetDepth methods

* refactor(smt_batch): separate functions and repeating code

* fix: progress chan size

* refactor: function params new lines

* refactor: function params on new lines
  • Loading branch information
V-Staykov authored Nov 28, 2024
1 parent cb614c8 commit ba0a89e
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 146 deletions.
11 changes: 8 additions & 3 deletions smt/pkg/smt/smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,9 @@ func (s *SMT) insert(k utils.NodeKey, v utils.NodeValue8, newValH [4]uint64, old

utils.RemoveOver(siblings, level+1)

s.updateDepth(len(siblings))
if err := s.updateDepth(len(siblings)); err != nil {
return nil, fmt.Errorf("updateDepth: %w", err)
}

for level >= 0 {
hashValueIn, err := utils.NodeValue8FromBigIntArray(siblings[level][0:8])
Expand Down Expand Up @@ -639,7 +641,7 @@ func (s *SMT) CheckOrphanedNodes(ctx context.Context) int {
return len(orphanedNodes)
}

func (s *SMT) updateDepth(newDepth int) {
func (s *SMT) updateDepth(newDepth int) error {
oldDepth, err := s.Db.GetDepth()
if err != nil {
oldDepth = 0
Expand All @@ -652,8 +654,11 @@ func (s *SMT) updateDepth(newDepth int) {

newDepthAsByte := byte(newDepth & 0xFF)
if oldDepth < newDepthAsByte {
_ = s.Db.SetDepth(newDepthAsByte)
if err := s.Db.SetDepth(newDepthAsByte); err != nil {
return fmt.Errorf("s.Db.SetDepth: %w", err)
}
}
return nil
}

/*
Expand Down
Loading

0 comments on commit ba0a89e

Please sign in to comment.