Skip to content

Commit

Permalink
Fix lint issues.
Browse files Browse the repository at this point in the history
Signed-off-by: txaty <txaty@proton.me>
  • Loading branch information
txaty committed Nov 25, 2023
1 parent 9cb457e commit e4881cb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 51 deletions.
2 changes: 1 addition & 1 deletion default_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import "crypto/sha256"

// sha256Digest is the reusable digest for DefaultHashFunc.
// It is used to avoid creating a new hash digest for every call to DefaultHashFunc.
var sha256Digest = sha256.New()
var sha256Digest = sha256.New() //nolint:gochecknoglobals

// DefaultHashFunc is the default hash function used when no user-specified hash function is provided.
// It implements the SHA256 hash function and reuses sha256Digest to reduce memory allocations.
Expand Down
117 changes: 67 additions & 50 deletions merkle_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,81 +127,98 @@ func New(config *Config, blocks []DataBlock) (m *MerkleTree, err error) {
}
}

// Initialize the hash function.
if m.HashFunc == nil {
if m.RunInParallel {
// Use a concurrent safe hash function for parallel execution.
m.HashFunc = DefaultHashFuncParallel
} else {
m.HashFunc = DefaultHashFunc
}
// Perform actions based on the configured mode.
// Set the mode to ModeProofGen by default if not specified.
if m.Mode == 0 {
m.Mode = ModeProofGen
}

// Configure parallelization settings.
if m.RunInParallel {
// Set NumRoutines to the number of CPU cores if not specified or invalid.
if m.NumRoutines <= 0 {
m.NumRoutines = runtime.NumCPU()
}

m.Leaves, err = m.computeLeafNodesParallel(blocks)
if err != nil {
return nil, err
}
} else {
// Generate leaves without parallelization.
m.Leaves, err = m.computeLeafNodes(blocks)
if err != nil {
if err := m.newParallel(blocks); err != nil {
return nil, err
}

return m, nil
}

// Perform actions based on the configured mode.
// Set the mode to ModeProofGen by default if not specified.
if m.Mode == 0 {
m.Mode = ModeProofGen
if err := m.new(blocks); err != nil {
return nil, err
}

// Generate proofs in ModeProofGen.
if m.Mode == ModeProofGen {
if m.RunInParallel {
err = m.proofGenParallel()
return
}
return m, nil
}

func (m *MerkleTree) new(blocks []DataBlock) error {
// Initialize the hash function.
if m.HashFunc == nil {
m.HashFunc = DefaultHashFunc
}

err = m.proofGen()
// Generate leaves.
var err error
m.Leaves, err = m.computeLeafNodes(blocks)

return
if err != nil {
return err
}

if m.Mode == ModeProofGen {
return m.proofGen()
}

// Initialize the leafMap for ModeTreeBuild and ModeProofGenAndTreeBuild.
m.leafMap = make(map[string]int)

// Build the tree in ModeTreeBuild.
if m.Mode == ModeTreeBuild {
if m.RunInParallel {
err = m.treeBuildParallel()
return
}

err = m.treeBuild()

return
return m.treeBuild()
}

// Build the tree and generate proofs in ModeProofGenAndTreeBuild.
if m.Mode == ModeProofGenAndTreeBuild {
if m.RunInParallel {
err = m.proofGenAndTreeBuildParallel()
return
}
return m.proofGenAndTreeBuild()
}

// Return an error if the configuration mode is invalid.
return ErrInvalidConfigMode
}

func (m *MerkleTree) newParallel(blocks []DataBlock) error {
// Initialize the hash function.
if m.HashFunc == nil {
m.HashFunc = DefaultHashFuncParallel
}

// Set NumRoutines to the number of CPU cores if not specified or invalid.
if m.NumRoutines <= 0 {
m.NumRoutines = runtime.NumCPU()
}

err = m.proofGenAndTreeBuild()
// Generate leaves.
var err error
m.Leaves, err = m.computeLeafNodesParallel(blocks)

return
if err != nil {
return err
}

if m.Mode == ModeProofGen {
return m.proofGenParallel()
}

// Initialize the leafMap for ModeTreeBuild and ModeProofGenAndTreeBuild.
m.leafMap = make(map[string]int)

if m.Mode == ModeTreeBuild {
return m.treeBuildParallel()
}

// Build the tree and generate proofs in ModeProofGenAndTreeBuild.
if m.Mode == ModeProofGenAndTreeBuild {
return m.proofGenAndTreeBuildParallel()
}

// Return an error if the configuration mode is invalid.
return nil, ErrInvalidConfigMode
return ErrInvalidConfigMode
}

// concatHash concatenates two byte slices, b1 and b2.
Expand Down

0 comments on commit e4881cb

Please sign in to comment.