forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_tcert_pool_st.go
192 lines (138 loc) · 5.02 KB
/
client_tcert_pool_st.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed 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 (
"encoding/hex"
"fmt"
"sort"
"sync"
"github.com/hyperledger/fabric/core/crypto/primitives"
)
//TCertBlock is an object that include the generated TCert and the attributes used to generate it.
type TCertBlock struct {
tCert tCert
attributesHash string
}
//TCertDBBlock is an object used to store the TCert in the database. A raw field is used to represent the TCert and the preK0, a string field is use to the attributesHash.
type TCertDBBlock struct {
tCertDER []byte
attributesHash string
preK0 []byte
}
type tCertPoolSingleThreadImpl struct {
client *clientImpl
empty bool
length map[string]int
tCerts map[string][]*TCertBlock
m sync.Mutex
}
//Start starts the pool processing.
func (tCertPool *tCertPoolSingleThreadImpl) Start() (err error) {
tCertPool.m.Lock()
defer tCertPool.m.Unlock()
tCertPool.client.Debug("Starting TCert Pool...")
// Load unused TCerts if any
tCertDBBlocks, err := tCertPool.client.ks.loadUnusedTCerts()
if err != nil {
tCertPool.client.Errorf("Failed loading TCerts from cache: [%s]", err)
return
}
if len(tCertDBBlocks) > 0 {
tCertPool.client.Debug("TCerts in cache found! Loading them...")
for _, tCertDBBlock := range tCertDBBlocks {
tCertBlock, err := tCertPool.client.getTCertFromDER(tCertDBBlock)
if err != nil {
tCertPool.client.Errorf("Failed paring TCert [% x]: [%s]", tCertDBBlock.tCertDER, err)
continue
}
tCertPool.AddTCert(tCertBlock)
}
} //END-IF
return
}
//Stop stops the pool.
func (tCertPool *tCertPoolSingleThreadImpl) Stop() (err error) {
tCertPool.m.Lock()
defer tCertPool.m.Unlock()
for k := range tCertPool.tCerts {
certList := tCertPool.tCerts[k]
certListLen := tCertPool.length[k]
tCertPool.client.ks.storeUnusedTCerts(certList[:certListLen])
}
tCertPool.client.Debug("Store unused TCerts...done!")
return
}
//calculateAttributesHash generates a unique hash using the passed attributes.
func calculateAttributesHash(attributes []string) (attrHash string) {
keys := make([]string, len(attributes))
for _, k := range attributes {
keys = append(keys, k)
}
sort.Strings(keys)
values := make([]byte, len(keys))
for _, k := range keys {
vb := []byte(k)
for _, bval := range vb {
values = append(values, bval)
}
}
attributesHash := primitives.Hash(values)
return hex.EncodeToString(attributesHash)
}
//GetNextTCert returns a TCert from the pool valid to the passed attributes. If no TCert is available TCA is invoked to generate it.
func (tCertPool *tCertPoolSingleThreadImpl) GetNextTCerts(nCerts int, attributes ...string) ([]*TCertBlock, error) {
blocks := make([]*TCertBlock, nCerts)
for i := 0; i < nCerts; i++ {
block, err := tCertPool.getNextTCert(attributes...)
if err != nil {
return nil, err
}
blocks[i] = block
}
return blocks, nil
}
func (tCertPool *tCertPoolSingleThreadImpl) getNextTCert(attributes ...string) (tCert *TCertBlock, err error) {
tCertPool.m.Lock()
defer tCertPool.m.Unlock()
attributesHash := calculateAttributesHash(attributes)
poolLen := tCertPool.length[attributesHash]
if poolLen <= 0 {
// Reload
if err := tCertPool.client.getTCertsFromTCA(attributesHash, attributes, tCertPool.client.conf.getTCertBatchSize()); err != nil {
return nil, fmt.Errorf("Failed loading TCerts from TCA")
}
}
tCert = tCertPool.tCerts[attributesHash][tCertPool.length[attributesHash]-1]
tCertPool.length[attributesHash] = tCertPool.length[attributesHash] - 1
return tCert, nil
}
//AddTCert adds a TCert into the pool is invoked by the client after TCA is called.
func (tCertPool *tCertPoolSingleThreadImpl) AddTCert(tCertBlock *TCertBlock) (err error) {
tCertPool.client.Debugf("Adding new Cert [% x].", tCertBlock.tCert.GetCertificate().Raw)
if tCertPool.length[tCertBlock.attributesHash] <= 0 {
tCertPool.length[tCertBlock.attributesHash] = 0
}
tCertPool.length[tCertBlock.attributesHash] = tCertPool.length[tCertBlock.attributesHash] + 1
if tCertPool.tCerts[tCertBlock.attributesHash] == nil {
tCertPool.tCerts[tCertBlock.attributesHash] = make([]*TCertBlock, tCertPool.client.conf.getTCertBatchSize())
}
tCertPool.tCerts[tCertBlock.attributesHash][tCertPool.length[tCertBlock.attributesHash]-1] = tCertBlock
return nil
}
func (tCertPool *tCertPoolSingleThreadImpl) init(client *clientImpl) (err error) {
tCertPool.client = client
tCertPool.client.Debug("Init TCert Pool...")
tCertPool.tCerts = make(map[string][]*TCertBlock)
tCertPool.length = make(map[string]int)
return
}