1
1
/*
2
- Copyright IBM Corp. 2016 All Rights Reserved.
2
+ Copyright IBM Corp. All Rights Reserved.
3
3
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
4
+ SPDX-License-Identifier: Apache-2.0
15
5
*/
16
6
17
7
package main
@@ -20,12 +10,18 @@ import (
20
10
"flag"
21
11
"fmt"
22
12
"math"
13
+ "os"
23
14
15
+ "github.com/hyperledger/fabric/common/crypto"
16
+ "github.com/hyperledger/fabric/common/localmsp"
24
17
"github.com/hyperledger/fabric/common/tools/configtxgen/provisional"
18
+ "github.com/hyperledger/fabric/common/tools/protolator"
19
+ mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
25
20
"github.com/hyperledger/fabric/orderer/common/localconfig"
26
21
cb "github.com/hyperledger/fabric/protos/common"
27
22
ab "github.com/hyperledger/fabric/protos/orderer"
28
23
"github.com/hyperledger/fabric/protos/utils"
24
+
29
25
"golang.org/x/net/context"
30
26
"google.golang.org/grpc"
31
27
)
@@ -37,44 +33,39 @@ var (
37
33
)
38
34
39
35
type deliverClient struct {
40
- client ab.AtomicBroadcast_DeliverClient
41
- chainID string
36
+ client ab.AtomicBroadcast_DeliverClient
37
+ channelID string
38
+ signer crypto.LocalSigner
39
+ quiet bool
42
40
}
43
41
44
- func newDeliverClient (client ab.AtomicBroadcast_DeliverClient , chainID string ) * deliverClient {
45
- return & deliverClient {client : client , chainID : chainID }
42
+ func newDeliverClient (client ab.AtomicBroadcast_DeliverClient , channelID string , signer crypto. LocalSigner , quiet bool ) * deliverClient {
43
+ return & deliverClient {client : client , channelID : channelID , signer : signer , quiet : quiet }
46
44
}
47
45
48
- func seekHelper (chainID string , start * ab.SeekPosition , stop * ab.SeekPosition ) * cb.Envelope {
49
- return & cb.Envelope {
50
- Payload : utils .MarshalOrPanic (& cb.Payload {
51
- Header : & cb.Header {
52
- ChannelHeader : utils .MarshalOrPanic (& cb.ChannelHeader {
53
- ChannelId : chainID ,
54
- }),
55
- SignatureHeader : utils .MarshalOrPanic (& cb.SignatureHeader {}),
56
- },
57
-
58
- Data : utils .MarshalOrPanic (& ab.SeekInfo {
59
- Start : start ,
60
- Stop : stop ,
61
- Behavior : ab .SeekInfo_BLOCK_UNTIL_READY ,
62
- }),
63
- }),
46
+ func (r * deliverClient ) seekHelper (start * ab.SeekPosition , stop * ab.SeekPosition ) * cb.Envelope {
47
+ env , err := utils .CreateSignedEnvelope (cb .HeaderType_DELIVER_SEEK_INFO , r .channelID , r .signer , & ab.SeekInfo {
48
+ Start : start ,
49
+ Stop : stop ,
50
+ Behavior : ab .SeekInfo_BLOCK_UNTIL_READY ,
51
+ }, 0 , 0 )
52
+ if err != nil {
53
+ panic (err )
64
54
}
55
+ return env
65
56
}
66
57
67
58
func (r * deliverClient ) seekOldest () error {
68
- return r .client .Send (seekHelper ( r . chainID , oldest , maxStop ))
59
+ return r .client .Send (r . seekHelper ( oldest , maxStop ))
69
60
}
70
61
71
62
func (r * deliverClient ) seekNewest () error {
72
- return r .client .Send (seekHelper ( r . chainID , newest , maxStop ))
63
+ return r .client .Send (r . seekHelper ( newest , maxStop ))
73
64
}
74
65
75
66
func (r * deliverClient ) seekSingle (blockNumber uint64 ) error {
76
67
specific := & ab.SeekPosition {Type : & ab.SeekPosition_Specified {Specified : & ab.SeekSpecified {Number : blockNumber }}}
77
- return r .client .Send (seekHelper ( r . chainID , specific , specific ))
68
+ return r .client .Send (r . seekHelper ( specific , specific ))
78
69
}
79
70
80
71
func (r * deliverClient ) readUntilClose () {
@@ -90,20 +81,39 @@ func (r *deliverClient) readUntilClose() {
90
81
fmt .Println ("Got status " , t )
91
82
return
92
83
case * ab.DeliverResponse_Block :
93
- fmt .Println ("Received block: " , t .Block )
84
+ if ! r .quiet {
85
+ fmt .Println ("Received block: " )
86
+ err := protolator .DeepMarshalJSON (os .Stdout , t .Block )
87
+ if err != nil {
88
+ fmt .Println (" Error pretty printing block: %s" , err )
89
+ }
90
+ } else {
91
+ fmt .Println ("Received block: " , t .Block .Header .Number )
92
+ }
94
93
}
95
94
}
96
95
}
97
96
98
97
func main () {
99
98
config := config .Load ()
100
99
101
- var chainID string
100
+ // Load local MSP
101
+ err := mspmgmt .LoadLocalMsp (config .General .LocalMSPDir , config .General .BCCSP , config .General .LocalMSPID )
102
+ if err != nil { // Handle errors reading the config file
103
+ fmt .Println ("Failed to initialize local MSP:" , err )
104
+ os .Exit (0 )
105
+ }
106
+
107
+ signer := localmsp .NewSigner ()
108
+
109
+ var channelID string
102
110
var serverAddr string
103
111
var seek int
112
+ var quiet bool
104
113
105
114
flag .StringVar (& serverAddr , "server" , fmt .Sprintf ("%s:%d" , config .General .ListenAddress , config .General .ListenPort ), "The RPC server to connect to." )
106
- flag .StringVar (& chainID , "chainID" , provisional .TestChainID , "The chain ID to deliver from." )
115
+ flag .StringVar (& channelID , "channelID" , provisional .TestChainID , "The channel ID to deliver from." )
116
+ flag .BoolVar (& quiet , "quiet" , false , "Only print the block number, will not attempt to print its block contents." )
107
117
flag .IntVar (& seek , "seek" , - 2 , "Specify the range of requested blocks." +
108
118
"Acceptable values:" +
109
119
"-2 (or -1) to start from oldest (or newest) and keep at it indefinitely." +
@@ -126,7 +136,7 @@ func main() {
126
136
return
127
137
}
128
138
129
- s := newDeliverClient (client , chainID )
139
+ s := newDeliverClient (client , channelID , signer , quiet )
130
140
switch seek {
131
141
case - 2 :
132
142
err = s .seekOldest ()
0 commit comments