-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
153 lines (131 loc) · 4.22 KB
/
client.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
// Copyright IBM Corp. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"flag"
"fmt"
"math"
"os"
"github.com/hyperledger/fabric/common/crypto"
"github.com/hyperledger/fabric/common/localmsp"
"github.com/hyperledger/fabric/common/tools/protolator"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/orderer/common/localconfig"
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"
"github.com/hyperledger/fabric/protos/utils"
"google.golang.org/grpc"
)
var (
oldest = &ab.SeekPosition{Type: &ab.SeekPosition_Oldest{Oldest: &ab.SeekOldest{}}}
newest = &ab.SeekPosition{Type: &ab.SeekPosition_Newest{Newest: &ab.SeekNewest{}}}
maxStop = &ab.SeekPosition{Type: &ab.SeekPosition_Specified{Specified: &ab.SeekSpecified{Number: math.MaxUint64}}}
)
type deliverClient struct {
client ab.AtomicBroadcast_DeliverClient
channelID string
signer crypto.LocalSigner
quiet bool
}
func newDeliverClient(client ab.AtomicBroadcast_DeliverClient, channelID string, signer crypto.LocalSigner, quiet bool) *deliverClient {
return &deliverClient{client: client, channelID: channelID, signer: signer, quiet: quiet}
}
func (r *deliverClient) seekHelper(start *ab.SeekPosition, stop *ab.SeekPosition) *cb.Envelope {
env, err := utils.CreateSignedEnvelope(cb.HeaderType_DELIVER_SEEK_INFO, r.channelID, r.signer, &ab.SeekInfo{
Start: start,
Stop: stop,
Behavior: ab.SeekInfo_BLOCK_UNTIL_READY,
}, 0, 0)
if err != nil {
panic(err)
}
return env
}
func (r *deliverClient) seekOldest() error {
return r.client.Send(r.seekHelper(oldest, maxStop))
}
func (r *deliverClient) seekNewest() error {
return r.client.Send(r.seekHelper(newest, maxStop))
}
func (r *deliverClient) seekSingle(blockNumber uint64) error {
specific := &ab.SeekPosition{Type: &ab.SeekPosition_Specified{Specified: &ab.SeekSpecified{Number: blockNumber}}}
return r.client.Send(r.seekHelper(specific, specific))
}
func (r *deliverClient) readUntilClose() {
for {
msg, err := r.client.Recv()
if err != nil {
fmt.Println("Error receiving:", err)
return
}
switch t := msg.Type.(type) {
case *ab.DeliverResponse_Status:
fmt.Println("Got status ", t)
return
case *ab.DeliverResponse_Block:
if !r.quiet {
fmt.Println("Received block: ")
err := protolator.DeepMarshalJSON(os.Stdout, t.Block)
if err != nil {
fmt.Printf(" Error pretty printing block: %s", err)
}
} else {
fmt.Println("Received block: ", t.Block.Header.Number)
}
}
}
}
func main() {
conf, err := localconfig.Load()
if err != nil {
fmt.Println("failed to load config:", err)
os.Exit(1)
}
// Load local MSP
err = mspmgmt.LoadLocalMsp(conf.General.LocalMSPDir, conf.General.BCCSP, conf.General.LocalMSPID)
if err != nil { // Handle errors reading the config file
fmt.Println("Failed to initialize local MSP:", err)
os.Exit(0)
}
signer := localmsp.NewSigner()
var channelID string
var serverAddr string
var seek int
var quiet bool
flag.StringVar(&serverAddr, "server", fmt.Sprintf("%s:%d", conf.General.ListenAddress, conf.General.ListenPort), "The RPC server to connect to.")
flag.StringVar(&channelID, "channelID", localconfig.Defaults.General.SystemChannel, "The channel ID to deliver from.")
flag.BoolVar(&quiet, "quiet", false, "Only print the block number, will not attempt to print its block contents.")
flag.IntVar(&seek, "seek", -2, "Specify the range of requested blocks."+
"Acceptable values:"+
"-2 (or -1) to start from oldest (or newest) and keep at it indefinitely."+
"N >= 0 to fetch block N only.")
flag.Parse()
if seek < -2 {
fmt.Println("Wrong seek value.")
flag.PrintDefaults()
}
conn, err := grpc.Dial(serverAddr, grpc.WithInsecure())
if err != nil {
fmt.Println("Error connecting:", err)
return
}
client, err := ab.NewAtomicBroadcastClient(conn).Deliver(context.TODO())
if err != nil {
fmt.Println("Error connecting:", err)
return
}
s := newDeliverClient(client, channelID, signer, quiet)
switch seek {
case -2:
err = s.seekOldest()
case -1:
err = s.seekNewest()
default:
err = s.seekSingle(uint64(seek))
}
if err != nil {
fmt.Println("Received error:", err)
}
s.readUntilClose()
}