/
orderer.go
138 lines (124 loc) · 3.21 KB
/
orderer.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
package entity
import (
"fmt"
"github.com/fabric-lab/hyperledger-fabric-manager/server/pkg/store"
"github.com/fabric-lab/hyperledger-fabric-manager/server/pkg/util"
profileConfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
ordererConfig "github.com/hyperledger/fabric/orderer/common/localconfig"
yaml "gopkg.in/yaml.v2"
"path/filepath"
"strings"
"time"
)
type Orderer struct {
Name string
LedgerType string
OrdererType string
ListenAddress string
ListenPort uint16
LocalMSPID string
Consortiums []string
}
func (o *Orderer) Create() error {
Path(OrdererDir, o.Name)
return nil
}
func (o *Orderer) Exec(cmdInfo map[string]string) string {
err := o.config(cmdInfo)
if err != nil {
return err.Error()
}
return ExecOrderer(cmdInfo)
}
func (o *Orderer) config(cmdInfo map[string]string) error {
path := filepath.Join(OrdererDir, o.Name)
profileBytes, err := o.configOrderProfile(path)
if err != nil {
return err
}
err = SimpleWrite(path, configtxyml, profileBytes)
if err != nil {
return err
}
orderBytes, err := o.configOrderer(path)
if err != nil {
return err
}
SimpleWrite(path, ordereryml, orderBytes)
return nil
}
func (o *Orderer) configOrderProfile(path string) ([]byte, error) {
ordererConfig := &profileConfig.Orderer{
OrdererType: "solo",
Addresses: []string{fmt.Sprintf("%s:%d", o.ListenAddress,o.ListenPort)},
BatchTimeout: 2 * time.Second,
BatchSize: profileConfig.BatchSize{
MaxMessageCount: 10,
AbsoluteMaxBytes: 10 * 1024 * 1024,
PreferredMaxBytes: 512 * 1024,
},
}
consortiumsConfig := make(map[string]*profileConfig.Consortium)
for _, v := range o.Consortiums {
orgs, err := configConsortiumOrgs(path, v)
if err != nil {
return nil, err
}
consortiumsConfig[v] = &profileConfig.Consortium{
Organizations: orgs,
}
}
profile := &profileConfig.Profile{
Orderer: ordererConfig,
Consortiums: consortiumsConfig,
}
Profiles := make(map[string]*profileConfig.Profile)
Profiles["SampleSolo"] = profile
topLevel := &profileConfig.TopLevel{Profiles: Profiles}
b, err := yaml.Marshal(topLevel)
if err != nil {
return nil, err
}
s := string(b)
s = strings.Replace(s, "MaxMessageSize", "MaxMessageCount", 1)
return []byte(s), nil
}
func (o *Orderer) configOrderer(path string) ([]byte, error) {
msp, err := getMspByName(o.LocalMSPID)
if err != nil {
return nil, err
}
util.Copy(msp.Path, path)
general := ordererConfig.General{
LedgerType: o.LedgerType,
ListenAddress: o.ListenAddress,
ListenPort: o.ListenPort,
LocalMSPDir: defaultMspDir,
LocalMSPID: o.LocalMSPID,
SystemChannel: "system-channel",
GenesisMethod: "provisional",
GenesisProfile: "SampleSolo",
LogLevel: "debug",
}
blockPath := filepath.Join(path, blockDir)
fileLedger := ordererConfig.FileLedger{
Location: blockPath,
}
topLevel := &ordererConfig.TopLevel{
General: general,
FileLedger: fileLedger,
}
return yaml.Marshal(topLevel)
}
func getOrderByName(oName string) (*Orderer, error) {
var o *Orderer
i, err := store.Bt.ViewByKey(orderers, oName)
if err != nil {
return o, err
}
i = MapToEntity(i, orderers)
if o, ok := i.(*Orderer); ok {
return o, nil
}
return o, nil
}