Skip to content

Commit 512d695

Browse files
committed
(kubectl-hlf) fabric v3.0 channel creation
Signed-off-by: adityajoshi12 <[email protected]>
1 parent bf7bf33 commit 512d695

File tree

3 files changed

+146
-30
lines changed

3 files changed

+146
-30
lines changed

Diff for: controllers/testutils/channel.go

+86-30
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"github.com/hyperledger/fabric-config/configtx/membership"
99
"github.com/hyperledger/fabric-config/configtx/orderer"
1010
cb "github.com/hyperledger/fabric-protos-go/common"
11+
sb "github.com/hyperledger/fabric-protos-go/orderer/smartbft"
12+
"github.com/kfsoftware/hlf-operator/controllers/utils"
13+
hlfv1alpha1 "github.com/kfsoftware/hlf-operator/pkg/apis/hlf.kungfusoftware.es/v1alpha1"
1114
"github.com/pkg/errors"
1215
"time"
1316
)
@@ -25,9 +28,11 @@ type PeerOrg struct {
2528
}
2629

2730
type Consenter struct {
28-
host string
29-
port int
30-
tlsCert *x509.Certificate
31+
host string
32+
port int
33+
tlsCert *x509.Certificate
34+
signCert *x509.Certificate
35+
mspId string
3136
}
3237
type channelStore struct {
3338
}
@@ -38,6 +43,7 @@ type CreateChannelOptions struct {
3843
name string
3944
batchSize *orderer.BatchSize
4045
batchDuration *time.Duration
46+
consensus hlfv1alpha1.OrdererConsensusType
4147
}
4248

4349
func (o CreateChannelOptions) validate() error {
@@ -87,11 +93,18 @@ func WithPeerOrgs(peerOrgs ...PeerOrg) ChannelOption {
8793
o.peerOrgs = peerOrgs
8894
}
8995
}
90-
func CreateConsenter(host string, port int, tlsCert *x509.Certificate) Consenter {
96+
func WithConsensus(consensus hlfv1alpha1.OrdererConsensusType) ChannelOption {
97+
return func(o *CreateChannelOptions) {
98+
o.consensus = consensus
99+
}
100+
}
101+
func CreateConsenter(host string, port int, tlsCert, signCert *x509.Certificate, mspId string) Consenter {
91102
return Consenter{
92-
host: host,
93-
port: port,
94-
tlsCert: tlsCert,
103+
host: host,
104+
port: port,
105+
tlsCert: tlsCert,
106+
signCert: signCert,
107+
mspId: mspId,
95108
}
96109
}
97110
func CreateOrdererOrg(mspID string, tlsRootCert *x509.Certificate, signRootCert *x509.Certificate, ordererUrls []string) OrdererOrg {
@@ -145,32 +158,75 @@ func (s channelStore) GetApplicationChannelBlock(ctx context.Context, opts ...Ch
145158
}
146159
peerOrgs = append(peerOrgs, genesisOrdererOrg)
147160
}
148-
var consenters []orderer.Consenter
149-
for _, consenter := range o.consenters {
150-
genesisConsenter := orderer.Consenter{
151-
Address: orderer.EtcdAddress{
152-
Host: consenter.host,
153-
Port: consenter.port,
161+
var consenters []orderer.Consenter // raft consenter
162+
consenterMapping := []cb.Consenter{} // bft consenter
163+
var etcdRaft orderer.EtcdRaft
164+
var smartBFTOptions *sb.Options
165+
var ordererType string
166+
channelCapabilities := []string{"V2_0"}
167+
if o.consensus == hlfv1alpha1.OrdererConsensusEtcdraft {
168+
ordererType = orderer.ConsensusTypeEtcdRaft
169+
for _, consenter := range o.consenters {
170+
genesisConsenter := orderer.Consenter{
171+
Address: orderer.EtcdAddress{
172+
Host: consenter.host,
173+
Port: consenter.port,
174+
},
175+
ClientTLSCert: consenter.tlsCert,
176+
ServerTLSCert: consenter.tlsCert,
177+
}
178+
consenters = append(consenters, genesisConsenter)
179+
}
180+
etcdRaft = orderer.EtcdRaft{
181+
Consenters: consenters,
182+
Options: orderer.EtcdRaftOptions{
183+
TickInterval: "500ms",
184+
ElectionTick: 10,
185+
HeartbeatTick: 1,
186+
MaxInflightBlocks: 5,
187+
SnapshotIntervalSize: 16 * 1024 * 1024, // 16 MB
154188
},
155-
ClientTLSCert: consenter.tlsCert,
156-
ServerTLSCert: consenter.tlsCert,
157189
}
158-
consenters = append(consenters, genesisConsenter)
190+
} else if o.consensus == hlfv1alpha1.OrdererConsensusBFT {
191+
ordererType = orderer.ConsensusTypeBFT
192+
channelCapabilities = append(channelCapabilities, "V3_0")
193+
for i, consenter := range o.consenters {
194+
consenterMapping = append(consenterMapping, cb.Consenter{
195+
Host: consenter.host,
196+
Port: uint32(consenter.port),
197+
MspId: consenter.mspId,
198+
Id: uint32(i + 1),
199+
Identity: utils.EncodeX509Certificate(consenter.signCert),
200+
ClientTlsCert: utils.EncodeX509Certificate(consenter.tlsCert),
201+
ServerTlsCert: utils.EncodeX509Certificate(consenter.tlsCert),
202+
})
203+
}
204+
smartBFTOptions = &sb.Options{
205+
RequestBatchMaxCount: 100,
206+
RequestBatchMaxInterval: "50ms",
207+
RequestForwardTimeout: "2s",
208+
RequestComplainTimeout: "20s",
209+
RequestAutoRemoveTimeout: "3m0s",
210+
ViewChangeResendInterval: "5s",
211+
ViewChangeTimeout: "20s",
212+
LeaderHeartbeatTimeout: "1m0s",
213+
CollectTimeout: "1s",
214+
RequestBatchMaxBytes: 10485760,
215+
IncomingMessageBufferSize: 200,
216+
RequestPoolSize: 100000,
217+
LeaderHeartbeatCount: 10,
218+
}
219+
} else {
220+
return nil, fmt.Errorf("orderer type %s not supported", ordererType)
159221
}
222+
160223
channelConfig := configtx.Channel{
161224
Orderer: configtx.Orderer{
162-
OrdererType: "etcdraft",
163-
Organizations: ordererOrgs,
164-
EtcdRaft: orderer.EtcdRaft{
165-
Consenters: consenters,
166-
Options: orderer.EtcdRaftOptions{
167-
TickInterval: "500ms",
168-
ElectionTick: 10,
169-
HeartbeatTick: 1,
170-
MaxInflightBlocks: 5,
171-
SnapshotIntervalSize: 16 * 1024 * 1024, // 16 MB
172-
},
173-
},
225+
ConsenterMapping: consenterMapping,
226+
OrdererType: ordererType,
227+
Organizations: ordererOrgs,
228+
EtcdRaft: etcdRaft,
229+
SmartBFT: smartBFTOptions,
174230
Policies: map[string]configtx.Policy{
175231
"Readers": {
176232
Type: "ImplicitMeta",
@@ -196,7 +252,7 @@ func (s channelStore) GetApplicationChannelBlock(ctx context.Context, opts ...Ch
196252
PreferredMaxBytes: 512 * 1024,
197253
},
198254
BatchTimeout: 2 * time.Second,
199-
State: "STATE_NORMAL",
255+
State: orderer.ConsensusStateNormal,
200256
},
201257
Application: configtx.Application{
202258
Organizations: peerOrgs,
@@ -225,7 +281,7 @@ func (s channelStore) GetApplicationChannelBlock(ctx context.Context, opts ...Ch
225281
},
226282
ACLs: defaultACLs(),
227283
},
228-
Capabilities: []string{"V2_0"},
284+
Capabilities: channelCapabilities,
229285
Policies: map[string]configtx.Policy{
230286
"Readers": {
231287
Type: "ImplicitMeta",

Diff for: kubectl-hlf/cmd/channel/generate.go

+17
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ package channel
22

33
import (
44
"context"
5+
"fmt"
56
"github.com/golang/protobuf/proto"
67
"github.com/hyperledger/fabric-config/configtx/orderer"
78
"github.com/kfsoftware/hlf-operator/controllers/testutils"
89
"github.com/kfsoftware/hlf-operator/controllers/utils"
910
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers"
11+
hlfv1alpha1 "github.com/kfsoftware/hlf-operator/pkg/apis/hlf.kungfusoftware.es/v1alpha1"
1012
"github.com/pkg/errors"
1113
log "github.com/sirupsen/logrus"
1214
"github.com/spf13/cobra"
1315
"io"
1416
"io/ioutil"
1517
"strings"
18+
"time"
1619
)
1720

1821
type generateChannelCmd struct {
@@ -24,8 +27,12 @@ type generateChannelCmd struct {
2427
maxMessageCount int
2528
absoluteMaxBytes int
2629
preferredMaxBytes int
30+
batchTimeout int
31+
consensus hlfv1alpha1.OrdererConsensusType
2732
}
2833

34+
var validConsensusTypes = []hlfv1alpha1.OrdererConsensusType{hlfv1alpha1.OrdererConsensusBFT, hlfv1alpha1.OrdererConsensusEtcdraft}
35+
2936
func (c generateChannelCmd) validate() error {
3037
if c.channelName == "" {
3138
return errors.Errorf("--channelName is required")
@@ -82,6 +89,10 @@ func (c generateChannelCmd) run() error {
8289
if err != nil {
8390
return err
8491
}
92+
signCert, err := utils.ParseX509Certificate([]byte(consenter.Status.SignCert))
93+
if err != nil {
94+
return err
95+
}
8596
consenterHost, consenterPort, err := helpers.GetOrdererHostAndPort(
8697
clientSet,
8798
consenter.Spec,
@@ -94,6 +105,8 @@ func (c generateChannelCmd) run() error {
94105
consenterHost,
95106
consenterPort,
96107
tlsCert,
108+
signCert,
109+
consenter.Spec.MspID,
97110
)
98111
consenters = append(consenters, createConsenter)
99112
_, ok := ordererMap[consenter.Spec.MspID]
@@ -189,6 +202,8 @@ func (c generateChannelCmd) run() error {
189202
AbsoluteMaxBytes: uint32(c.absoluteMaxBytes),
190203
PreferredMaxBytes: uint32(c.preferredMaxBytes),
191204
}),
205+
testutils.WithBatchTimeout(time.Duration(c.batchTimeout)*time.Second),
206+
testutils.WithConsensus(c.consensus),
192207
)
193208
if err != nil {
194209
return err
@@ -223,6 +238,8 @@ func newGenerateChannelCMD(io.Writer, io.Writer) *cobra.Command {
223238
persistentFlags.IntVarP(&c.maxMessageCount, "maxMessageCount", "", 100, "Max transactions per block")
224239
persistentFlags.IntVarP(&c.absoluteMaxBytes, "absoluteMaxBytes", "", 1024*1024, "Max size per block")
225240
persistentFlags.IntVarP(&c.preferredMaxBytes, "preferredMaxBytes", "", 512*1024, "Max size per block")
241+
persistentFlags.IntVarP(&c.batchTimeout, "batchTimeout", "", 2, "Batch timeout in seconds")
242+
persistentFlags.Var(hlfv1alpha1.NewConsensusTypeValue(hlfv1alpha1.OrdererConsensusEtcdraft, validConsensusTypes, &c.consensus), "consensus", fmt.Sprintf("Consensus type ( %s, %s), default etcdraft", hlfv1alpha1.OrdererConsensusBFT, hlfv1alpha1.OrdererConsensusEtcdraft))
226243
cmd.MarkPersistentFlagRequired("name")
227244
cmd.MarkPersistentFlagRequired("organizations")
228245
cmd.MarkPersistentFlagRequired("ordererOrganizations")

Diff for: pkg/apis/hlf.kungfusoftware.es/v1alpha1/hlf_types.go

+43
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1alpha1
1818

1919
import (
2020
"fmt"
21+
"strings"
2122

2223
sb "github.com/hyperledger/fabric-protos-go/orderer/smartbft"
2324
"github.com/kfsoftware/hlf-operator/pkg/status"
@@ -2579,6 +2580,48 @@ const (
25792580
ConsensusStateMaintenance FabricMainChannelConsensusState = "STATE_MAINTENANCE"
25802581
)
25812582

2583+
type ConsensusTypeValue struct {
2584+
value *OrdererConsensusType
2585+
allowed []OrdererConsensusType
2586+
}
2587+
2588+
func (c *ConsensusTypeValue) String() string {
2589+
if c.value == nil {
2590+
return ""
2591+
}
2592+
return string(*c.value)
2593+
}
2594+
2595+
func (c *ConsensusTypeValue) Set(s string) error {
2596+
for _, v := range c.allowed {
2597+
if string(v) == s {
2598+
*c.value = v
2599+
return nil
2600+
}
2601+
}
2602+
return fmt.Errorf("invalid consensus type: %s. Valid types are: %s", s, strings.Join(c.allowedStrings(), ", "))
2603+
}
2604+
2605+
func (c *ConsensusTypeValue) Type() string {
2606+
return "ConsensusType"
2607+
}
2608+
2609+
func (c *ConsensusTypeValue) allowedStrings() []string {
2610+
var allowedStrs []string
2611+
for _, v := range c.allowed {
2612+
allowedStrs = append(allowedStrs, string(v))
2613+
}
2614+
return allowedStrs
2615+
}
2616+
2617+
func NewConsensusTypeValue(val OrdererConsensusType, allowed []OrdererConsensusType, p *OrdererConsensusType) *ConsensusTypeValue {
2618+
*p = val
2619+
return &ConsensusTypeValue{
2620+
value: p,
2621+
allowed: allowed,
2622+
}
2623+
}
2624+
25822625
type FabricMainChannelOrdererBatchSize struct {
25832626
// The number of transactions that can fit in a block.
25842627
// +kubebuilder:default:=100

0 commit comments

Comments
 (0)