forked from hyperledger-archives/fabric
/
validator.go
executable file
·157 lines (121 loc) · 4.21 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package crypto
import (
"github.com/openblockchain/obc-peer/openchain/crypto/utils"
"sync"
)
// Private type and variables
type validatorEntry struct {
validator Peer
counter int64
}
var (
// Map of initialized validators
validators = make(map[string]validatorEntry)
// Sync
mutex sync.Mutex
)
// Public Methods
// RegisterValidator registers a validator to the PKI infrastructure
func RegisterValidator(name string, pwd []byte, enrollID, enrollPWD string) error {
mutex.Lock()
defer mutex.Unlock()
log.Info("Registering validator [%s] with name [%s]...", enrollID, name)
if _, ok := validators[name]; ok {
log.Info("Registering validator [%s] with name [%s]...done. Already initialized.", enrollID, name)
return nil
}
validator := new(validatorImpl)
if err := validator.register(name, pwd, enrollID, enrollPWD); err != nil {
if err != utils.ErrAlreadyRegistered && err != utils.ErrAlreadyInitialized {
log.Error("Failed registering validator [%s] with name [%s] [%s].", enrollID, name, err)
return err
}
log.Info("Registering vlidator [%s] with name [%s]...done. Already registered or initiliazed.", enrollID, name)
}
err := validator.close()
if err != nil {
// It is not necessary to report this error to the caller
log.Warning("Registering validator [%s] with name [%s]. Failed closing [%s].", enrollID, name, err)
}
log.Info("Registering validator [%s] with name [%s]...done!", enrollID, name)
return nil
}
// InitValidator initializes a validator named name with password pwd
func InitValidator(name string, pwd []byte) (Peer, error) {
mutex.Lock()
defer mutex.Unlock()
log.Info("Initializing validator [%s]...", name)
if entry, ok := validators[name]; ok {
log.Info("Validator already initiliazied [%s]. Increasing counter from [%d]", name, validators[name].counter)
entry.counter++
validators[name] = entry
return validators[name].validator, nil
}
validator := new(validatorImpl)
if err := validator.init(name, pwd); err != nil {
log.Error("Failed validator initialization [%s]: [%s]", name, err)
return nil, err
}
validators[name] = validatorEntry{validator, 1}
log.Info("Initializing validator [%s]...done!", name)
return validator, nil
}
// CloseValidator releases all the resources allocated by the validator
func CloseValidator(peer Peer) error {
mutex.Lock()
defer mutex.Unlock()
return closeValidatorInternal(peer, false)
}
// CloseAllValidators closes all the validators initialized so far
func CloseAllValidators() (bool, []error) {
mutex.Lock()
defer mutex.Unlock()
log.Info("Closing all validators...")
errs := make([]error, len(validators))
for _, value := range validators {
err := closeValidatorInternal(value.validator, true)
errs = append(errs, err)
}
log.Info("Closing all validators...done!")
return len(errs) != 0, errs
}
// Private Methods
func closeValidatorInternal(peer Peer, force bool) error {
if peer == nil {
return utils.ErrNilArgument
}
name := peer.GetName()
log.Info("Closing validator [%s]...", name)
entry, ok := validators[name]
if !ok {
return utils.ErrInvalidReference
}
if entry.counter == 1 || force {
defer delete(validators, name)
err := validators[name].validator.(*validatorImpl).close()
log.Info("Closing validator [%s]...done! [%s].", name, utils.ErrToString(err))
return err
} else {
// decrease counter
entry.counter--
validators[name] = entry
log.Info("Closing validator [%s]...decreased counter at [%d].", name, validators[name].counter)
}
return nil
}