Skip to content

Commit

Permalink
dbft: return an error from constructor if config is invalid
Browse files Browse the repository at this point in the history
It's not user-friendly to retyrn an empty instance without any error.

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
  • Loading branch information
AnnaShaleva committed Mar 21, 2024
1 parent 0f6ea3f commit 0227f09
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions dbft.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dbft

import (
"fmt"
"sync"
"time"

Expand All @@ -26,15 +27,15 @@ type (
// using provided options or nil if some of the options are missing or invalid.
// H and A generic parameters are used as hash and address representation for
// dBFT consensus messages, blocks and transactions.
func New[H Hash](options ...func(config *Config[H])) *DBFT[H] {
func New[H Hash](options ...func(config *Config[H])) (*DBFT[H], error) {
cfg := defaultConfig[H]()

for _, option := range options {
option(cfg)
}

if err := checkConfig(cfg); err != nil {
return nil
return nil, fmt.Errorf("invalid config: %w")

Check failure on line 38 in dbft.go

View workflow job for this annotation

GitHub Actions / Test (1.20)

fmt.Errorf format %w reads arg #1, but call has 0 args

Check failure on line 38 in dbft.go

View workflow job for this annotation

GitHub Actions / Test (1.21)

fmt.Errorf format %w reads arg #1, but call has 0 args

Check failure on line 38 in dbft.go

View workflow job for this annotation

GitHub Actions / Test (1.22)

fmt.Errorf format %w reads arg #1, but call has 0 args
}

d := &DBFT[H]{
Expand All @@ -45,7 +46,7 @@ func New[H Hash](options ...func(config *Config[H])) *DBFT[H] {
},
}

return d
return d, nil
}

func (d *DBFT[H]) addTransaction(tx Transaction[H]) {
Expand Down

0 comments on commit 0227f09

Please sign in to comment.