Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit 19c6ad4

Browse files
NickSolanteNick Solante
andauthored
Run validator only once to prevent collision with concurrent processes (#134)
Co-authored-by: Nick Solante <nicksolante@192-168-68-104.tpgi.com.au>
1 parent f923321 commit 19c6ad4

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

pkg/userop/parse.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import (
55
"errors"
66
"math/big"
77
"reflect"
8+
"sync"
89

910
"github.com/ethereum/go-ethereum/common"
1011
"github.com/go-playground/validator/v10"
1112
"github.com/mitchellh/mapstructure"
1213
)
1314

14-
var validate *validator.Validate
15+
var (
16+
validate = validator.New()
17+
onlyOnce = sync.Once{}
18+
)
1519

1620
func exactFieldMatch(mapKey, fieldName string) bool {
1721
return mapKey == fieldName
@@ -100,9 +104,10 @@ func New(data map[string]any) (*UserOperation, error) {
100104
}
101105

102106
// Validate struct
103-
validate = validator.New()
104-
validate.RegisterCustomTypeFunc(validateAddressType, common.Address{})
105-
validate.RegisterCustomTypeFunc(validateBigIntType, big.Int{})
107+
onlyOnce.Do(func() {
108+
validate.RegisterCustomTypeFunc(validateAddressType, common.Address{})
109+
validate.RegisterCustomTypeFunc(validateBigIntType, big.Int{})
110+
})
106111
err = validate.Struct(op)
107112
if err != nil {
108113
return nil, err

0 commit comments

Comments
 (0)