Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit b63b116

Browse files
committed
[FAB-3059] SDK Go - Initialize chain
Change-Id: I15974024d51ec3806a59a7d65f86e1405b724faa Signed-off-by: Bob Stasyszyn <bob.stasyszyn@securekey.com>
1 parent 65db7ec commit b63b116

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+9596
-20
lines changed

fabric-client/chain.go

Lines changed: 570 additions & 0 deletions
Large diffs are not rendered by default.

fabric-client/chain_test.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func TestCreateChain(t *testing.T) {
197197
t.Fatalf(err.Error())
198198
}
199199
// Setup mock orderer
200-
orderer := mockOrderer{fmt.Sprintf("0.0.0.0:1234"), nil}
200+
orderer := mockOrderer{MockURL: fmt.Sprintf("0.0.0.0:1234")}
201201
chain.AddOrderer(&orderer)
202202
// Test with valid cofiguration
203203
err = chain.CreateChannel(&CreateChannelRequest{ConfigData: configTx})
@@ -250,7 +250,7 @@ func TestJoinChannel(t *testing.T) {
250250
chain, _ := setupTestChain()
251251
peer, _ := CreateNewPeer(testAddress, "", "")
252252
peers = append(peers, peer)
253-
orderer := &mockOrderer{}
253+
orderer := &mockOrderer{DeliverResponse: NewMockDeliverResponse(mocks.NewSimpleMockBlock())}
254254
nonce, _ := util.GenerateRandomNonce()
255255
txID, _ := util.ComputeTxID(nonce, []byte("testID"))
256256
request := &JoinChannelRequest{Targets: peers, Nonce: nonce, TxID: txID}
@@ -293,6 +293,30 @@ func TestJoinChannel(t *testing.T) {
293293
}
294294
}
295295

296+
func TestChainInitializeFromOrderer(t *testing.T) {
297+
chain, _ := setupTestChain()
298+
builder := &mocks.MockConfigBlockBuilder{Index: 0, LastConfigIndex: 0}
299+
orderer := &mockOrderer{DeliverResponse: NewMockDeliverResponse(builder.Build())}
300+
chain.AddOrderer(orderer)
301+
302+
err := chain.Initialize([]byte{})
303+
if err != nil {
304+
t.Fatalf("channel Initialize failed : %v", err)
305+
}
306+
// TODO: Check data in chain
307+
}
308+
309+
func TestChainInitializeFromUpdate(t *testing.T) {
310+
chain, _ := setupTestChain()
311+
builder := &mocks.MockConfigUpdateEnvelopeBuilder{}
312+
313+
err := chain.Initialize(builder.BuildBytes())
314+
if err != nil {
315+
t.Fatalf("channel Initialize failed : %v", err)
316+
}
317+
// TODO: Check data in chain
318+
}
319+
296320
func setupTestChain() (Chain, error) {
297321
client := NewClient()
298322
user := NewUser("test")
@@ -314,7 +338,7 @@ func setupMassiveTestChain(numberOfPeers int, numberOfOrderers int) (Chain, erro
314338
}
315339

316340
for i := 0; i < numberOfOrderers; i++ {
317-
orderer := mockOrderer{fmt.Sprintf("http://mock%d.orderers.r.us", i), nil}
341+
orderer := mockOrderer{MockURL: fmt.Sprintf("http://mock%d.orderers.r.us", i)}
318342
chain.AddOrderer(&orderer)
319343
}
320344

fabric-client/mockorderer.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,11 @@ import (
2424
ab "github.com/hyperledger/fabric/protos/orderer"
2525
)
2626

27-
// TestBlock is a test block
28-
var testBlock = &ab.DeliverResponse{
29-
Type: &ab.DeliverResponse_Block{
30-
Block: &common.Block{
31-
Data: &common.BlockData{
32-
Data: [][]byte{[]byte("test")},
33-
},
34-
},
35-
},
36-
}
37-
3827
// mockOrderer is a mock fabricclient.Orderer
3928
type mockOrderer struct {
40-
MockURL string
41-
MockError error
29+
MockURL string
30+
MockError error
31+
DeliverResponse *ab.DeliverResponse
4232
}
4333

4434
// GetURL returns the mock URL of the mock Orderer
@@ -56,6 +46,13 @@ func (o *mockOrderer) SendDeliver(envelope *SignedEnvelope) (chan *common.Block,
5646
chan error) {
5747
responses := make(chan *common.Block, 1)
5848
errors := make(chan error, 1)
59-
responses <- testBlock.GetBlock()
49+
responses <- o.DeliverResponse.GetBlock()
6050
return responses, errors
6151
}
52+
53+
// NewMockDeliverResponse returns a mock DeliverResponse with the given block
54+
func NewMockDeliverResponse(block *common.Block) *ab.DeliverResponse {
55+
return &ab.DeliverResponse{
56+
Type: &ab.DeliverResponse_Block{Block: block},
57+
}
58+
}

fabric-client/mocks/mockdata.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
Copyright SecureKey Technologies Inc. All Rights Reserved.
3+
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package mocks
21+
22+
import (
23+
"github.com/hyperledger/fabric-sdk-go/fabric-client/util"
24+
"github.com/hyperledger/fabric/protos/common"
25+
)
26+
27+
// NewSimpleMockBlock returns a simple mock block
28+
func NewSimpleMockBlock() *common.Block {
29+
return &common.Block{
30+
Data: &common.BlockData{
31+
Data: [][]byte{[]byte("test")},
32+
},
33+
}
34+
}
35+
36+
// MockConfigBlockBuilder is used to build a mock Chain configuration block builder
37+
type MockConfigBlockBuilder struct {
38+
Index uint64
39+
LastConfigIndex uint64
40+
}
41+
42+
// Build will create a mock Chain configuration block
43+
func (b *MockConfigBlockBuilder) Build() *common.Block {
44+
return &common.Block{
45+
Header: &common.BlockHeader{
46+
Number: b.Index,
47+
},
48+
Metadata: &common.BlockMetadata{
49+
Metadata: b.buildMetaDataBytes(),
50+
},
51+
Data: &common.BlockData{
52+
Data: b.buildBlockEnvelopeBytes(),
53+
},
54+
}
55+
}
56+
57+
func (b *MockConfigBlockBuilder) buildMetaDataBytes() [][]byte {
58+
return [][]byte{b.buildSignaturesMetaDataBytes(), b.buildLastConfigMetaDataBytes()}
59+
}
60+
61+
func (b *MockConfigBlockBuilder) buildSignaturesMetaDataBytes() []byte {
62+
return []byte("test signatures")
63+
}
64+
65+
func (b *MockConfigBlockBuilder) buildLastConfigMetaDataBytes() []byte {
66+
return util.MarshalOrPanic(&common.Metadata{Value: b.buildLastConfigBytes()})
67+
}
68+
69+
func (b *MockConfigBlockBuilder) buildLastConfigBytes() []byte {
70+
return util.MarshalOrPanic(&common.LastConfig{Index: b.LastConfigIndex})
71+
}
72+
73+
func (b *MockConfigBlockBuilder) buildBlockEnvelopeBytes() [][]byte {
74+
return [][]byte{b.buildEnvelopeBytes()}
75+
}
76+
77+
func (b *MockConfigBlockBuilder) buildEnvelopeBytes() []byte {
78+
return util.MarshalOrPanic(&common.Envelope{Payload: b.buildPayloadBytes()})
79+
}
80+
81+
func (b *MockConfigBlockBuilder) buildPayloadBytes() []byte {
82+
return util.MarshalOrPanic(&common.Payload{Header: b.buildHeader(), Data: b.buildConfigEnvelopeBytes()})
83+
}
84+
85+
func (b *MockConfigBlockBuilder) buildHeader() *common.Header {
86+
return &common.Header{ChannelHeader: b.buildChannelHeaderBytes()}
87+
}
88+
89+
func (b *MockConfigBlockBuilder) buildChannelHeaderBytes() []byte {
90+
return util.MarshalOrPanic(&common.ChannelHeader{Type: int32(common.HeaderType_CONFIG)})
91+
}
92+
93+
func (b *MockConfigBlockBuilder) buildConfigEnvelopeBytes() []byte {
94+
return util.MarshalOrPanic(&common.ConfigEnvelope{Config: b.buildConfig()})
95+
}
96+
97+
func (b *MockConfigBlockBuilder) buildConfig() *common.Config {
98+
return &common.Config{Sequence: 0, ChannelGroup: b.buildConfigGroup()}
99+
}
100+
101+
func (b *MockConfigBlockBuilder) buildConfigGroup() *common.ConfigGroup {
102+
return &common.ConfigGroup{}
103+
}
104+
105+
type MockConfigUpdateEnvelopeBuilder struct {
106+
}
107+
108+
func (b *MockConfigUpdateEnvelopeBuilder) BuildBytes() []byte {
109+
return util.MarshalOrPanic(&common.Envelope{Payload: b.buildPayloadBytes()})
110+
}
111+
112+
func (b *MockConfigUpdateEnvelopeBuilder) buildPayloadBytes() []byte {
113+
return util.MarshalOrPanic(&common.Payload{Header: b.buildHeader(), Data: b.buildConfigEnvelopeBytes()})
114+
}
115+
116+
func (b *MockConfigUpdateEnvelopeBuilder) buildHeader() *common.Header {
117+
return &common.Header{ChannelHeader: b.buildChannelHeaderBytes()}
118+
}
119+
120+
func (b *MockConfigUpdateEnvelopeBuilder) buildChannelHeaderBytes() []byte {
121+
return util.MarshalOrPanic(&common.ChannelHeader{Type: int32(common.HeaderType_CONFIG_UPDATE)})
122+
}
123+
124+
func (b *MockConfigUpdateEnvelopeBuilder) buildConfigEnvelopeBytes() []byte {
125+
return util.MarshalOrPanic(&common.ConfigEnvelope{Config: b.buildConfig()})
126+
}
127+
128+
func (b *MockConfigUpdateEnvelopeBuilder) buildConfig() *common.Config {
129+
return &common.Config{Sequence: 0, ChannelGroup: b.buildConfigGroup()}
130+
}
131+
132+
func (b *MockConfigUpdateEnvelopeBuilder) buildConfigGroup() *common.ConfigGroup {
133+
return &common.ConfigGroup{}
134+
}

fabric-client/util/util.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ limitations under the License.
2020
package util
2121

2222
import (
23+
"fmt"
24+
"time"
25+
2326
"github.com/hyperledger/fabric/core/crypto/primitives"
2427
ab "github.com/hyperledger/fabric/protos/orderer"
2528

2629
"github.com/golang/protobuf/proto"
30+
google_protobuf "github.com/golang/protobuf/ptypes/timestamp"
2731
"github.com/hyperledger/fabric/protos/common"
2832
protos_utils "github.com/hyperledger/fabric/protos/utils"
2933
)
@@ -64,3 +68,44 @@ func GenerateRandomNonce() ([]byte, error) {
6468
func ComputeTxID(nonce []byte, creatorID []byte) (string, error) {
6569
return protos_utils.ComputeProposalTxID(nonce, creatorID)
6670
}
71+
72+
// NewNewestSeekPosition returns a SeekPosition that requests the newest block
73+
func NewNewestSeekPosition() *ab.SeekPosition {
74+
return &ab.SeekPosition{Type: &ab.SeekPosition_Newest{Newest: &ab.SeekNewest{}}}
75+
}
76+
77+
// NewSpecificSeekPosition returns a SeekPosition that requests the block at the given index
78+
func NewSpecificSeekPosition(index uint64) *ab.SeekPosition {
79+
return &ab.SeekPosition{Type: &ab.SeekPosition_Specified{Specified: &ab.SeekSpecified{Number: index}}}
80+
}
81+
82+
// GetLastConfigFromBlock returns the LastConfig data from the given block
83+
func GetLastConfigFromBlock(block *common.Block) (*common.LastConfig, error) {
84+
metadata := &common.Metadata{}
85+
err := proto.Unmarshal(block.Metadata.Metadata[common.BlockMetadataIndex_LAST_CONFIG], metadata)
86+
if err != nil {
87+
return nil, fmt.Errorf("unable to unmarshal meta data at index %d: %v", common.BlockMetadataIndex_LAST_CONFIG, err)
88+
}
89+
90+
lastConfig := &common.LastConfig{}
91+
err = proto.Unmarshal(metadata.Value, lastConfig)
92+
if err != nil {
93+
return nil, fmt.Errorf("unable to unmarshal last config from meta data: %v", err)
94+
}
95+
96+
return lastConfig, err
97+
}
98+
99+
// BuildChannelHeader builds a ChannelHeader with the given parameters
100+
func BuildChannelHeader(channelName string, headerType common.HeaderType, txID string, epoch uint64) (*common.ChannelHeader, error) {
101+
now := time.Now()
102+
channelHeader := &common.ChannelHeader{
103+
Type: int32(headerType),
104+
Version: 1,
105+
Timestamp: &google_protobuf.Timestamp{Seconds: int64(now.Second()), Nanos: int32(now.Nanosecond())},
106+
ChannelId: channelName,
107+
Epoch: epoch,
108+
TxId: txID,
109+
}
110+
return channelHeader, nil
111+
}

vendor/github.com/Knetic/govaluate/CONTRIBUTORS

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)