-
Notifications
You must be signed in to change notification settings - Fork 3
/
validator.go
39 lines (32 loc) · 998 Bytes
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package runtime
import (
"context"
"github.com/artela-network/aspect-runtime/types"
"github.com/artela-network/aspect-runtime/wasmtime"
"github.com/pkg/errors"
"sync"
)
type validatorConstructor func(ctx context.Context, logger types.Logger) (types.Validator, error)
var (
validatorRegistry = map[RuntimeType]validatorConstructor{
WASM: wasmtime.NewWASMTimeValidator,
}
validatorCache sync.Map
)
// NewValidator creates a new wasm code validator
func NewValidator(ctx context.Context, logger types.Logger, runtimeType RuntimeType) (types.Validator, error) {
if validator, ok := validatorCache.Load(runtimeType); ok {
return validator.(types.Validator), nil
}
constructor := validatorRegistry[runtimeType]
if constructor == nil {
return nil, errors.New("runtime engine not support")
}
validator, err := constructor(ctx, logger)
if err != nil {
return nil, err
}
validatorCache.Store(runtimeType, validator)
logger.Debug("validator created")
return validator, nil
}