This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ccchecker.go
179 lines (151 loc) · 4.4 KB
/
ccchecker.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
/*
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 main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sync"
"time"
"golang.org/x/net/context"
"github.com/hyperledger/fabric/examples/ccchecker/chaincodes"
"github.com/hyperledger/fabric/peer/common"
)
//global ccchecker params
var ccchecker *CCChecker
//CCChecker encapsulates ccchecker properties and runtime
type CCChecker struct {
//Chaincodes to do ccchecker over (see ccchecker.json for defaults)
Chaincodes []*chaincodes.CCClient
//TimeoutToAbortSecs abort deadline
TimeoutToAbortSecs int
//ChainName name of the chain
ChainName string
}
//LoadCCCheckerParams read the ccchecker params from a file
func LoadCCCheckerParams(file string) error {
var b []byte
var err error
if b, err = ioutil.ReadFile(file); err != nil {
return fmt.Errorf("Cannot read config file %s\n", err)
}
sp := &CCChecker{}
err = json.Unmarshal(b, &sp)
if err != nil {
return fmt.Errorf("error unmarshalling ccchecker: %s\n", err)
}
ccchecker = &CCChecker{}
id := 0
for _, scc := range sp.Chaincodes {
//concurrency <=0 will be dropped
if scc.Concurrency > 0 {
for i := 0; i < scc.Concurrency; i++ {
tmp := &chaincodes.CCClient{}
*tmp = *scc
tmp.ID = id
id = id + 1
ccchecker.Chaincodes = append(ccchecker.Chaincodes, tmp)
}
}
}
ccchecker.TimeoutToAbortSecs = sp.TimeoutToAbortSecs
ccchecker.ChainName = sp.ChainName
return nil
}
//CCCheckerInit assigns shadow chaincode to each of the CCClient from registered shadow chaincodes
func CCCheckerInit() {
if ccchecker == nil {
fmt.Printf("LoadCCCheckerParams needs to be called before init\n")
os.Exit(1)
}
if err := chaincodes.RegisterCCClients(ccchecker.Chaincodes); err != nil {
panic(fmt.Sprintf("%s", err))
}
}
//CCCheckerRun main loops that will run the tests and cleanup
func CCCheckerRun(orderingEndpoint string, report bool, verbose bool) error {
//connect with Broadcast client
bc, err := common.GetBroadcastClient(orderingEndpoint, false, "")
if err != nil {
return err
}
defer bc.Close()
ec, err := common.GetEndorserClient()
if err != nil {
return err
}
signer, err := common.GetDefaultSigner()
if err != nil {
return err
}
//when the wait's timeout and get out of ccchecker, we
//cancel and release all goroutines
ctxt, cancel := context.WithCancel(context.Background())
defer cancel()
var ccsWG sync.WaitGroup
ccsWG.Add(len(ccchecker.Chaincodes))
//an anonymous struct to hold failures
var failures struct {
sync.Mutex
failedCCClients int
}
//run the invokes
ccerrs := make([]error, len(ccchecker.Chaincodes))
for _, cc := range ccchecker.Chaincodes {
go func(cc2 *chaincodes.CCClient) {
if ccerrs[cc2.ID] = cc2.Run(ctxt, ccchecker.ChainName, bc, ec, signer, &ccsWG); ccerrs[cc2.ID] != nil {
failures.Lock()
failures.failedCCClients = failures.failedCCClients + 1
failures.Unlock()
}
}(cc)
}
//wait or timeout
err = ccchecker.wait(&ccsWG)
//verify results
if err == nil && failures.failedCCClients < len(ccchecker.Chaincodes) {
ccsWG = sync.WaitGroup{}
ccsWG.Add(len(ccchecker.Chaincodes) - failures.failedCCClients)
for _, cc := range ccchecker.Chaincodes {
go func(cc2 *chaincodes.CCClient) {
if ccerrs[cc2.ID] == nil {
ccerrs[cc2.ID] = cc2.Validate(ctxt, ccchecker.ChainName, bc, ec, signer, &ccsWG)
} else {
fmt.Printf("Ignoring [%v] for validation as it returned err %s\n", cc2, ccerrs[cc2.ID])
}
}(cc)
}
//wait or timeout
err = ccchecker.wait(&ccsWG)
}
if report {
for _, cc := range ccchecker.Chaincodes {
cc.Report(verbose, ccchecker.ChainName)
}
}
return err
}
func (s *CCChecker) wait(ccsWG *sync.WaitGroup) error {
done := make(chan struct{})
go func() {
ccsWG.Wait()
done <- struct{}{}
}()
select {
case <-done:
return nil
case <-time.After(time.Duration(s.TimeoutToAbortSecs) * time.Second):
return fmt.Errorf("Aborting due to timeoutout!!")
}
}