|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 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. |
| 15 | +*/ |
| 16 | + |
| 17 | +package noopssinglechain |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/op/go-logging" |
| 24 | + "github.com/spf13/viper" |
| 25 | + |
| 26 | + "github.com/golang/protobuf/proto" |
| 27 | + "github.com/hyperledger/fabric/core/chaincode" |
| 28 | + "github.com/hyperledger/fabric/core/committer" |
| 29 | + "github.com/hyperledger/fabric/core/ledgernext/kvledger" |
| 30 | + ab "github.com/hyperledger/fabric/orderer/atomicbroadcast" |
| 31 | + "golang.org/x/net/context" |
| 32 | + "google.golang.org/grpc" |
| 33 | + |
| 34 | + pb "github.com/hyperledger/fabric/protos" |
| 35 | +) |
| 36 | + |
| 37 | +//--------!!!IMPORTANT!!-!!IMPORTANT!!-!!IMPORTANT!!--------- |
| 38 | +// This Orderer is based off fabric/orderer/sample_clients/ |
| 39 | +// deliver_stdout/client.go. This is used merely to complete |
| 40 | +// the loop for the "skeleton" path so we can reason about and |
| 41 | +// modify committer component more effectively using code. |
| 42 | + |
| 43 | +var logger *logging.Logger // package-level logger |
| 44 | + |
| 45 | +func init() { |
| 46 | + logger = logging.MustGetLogger("noopssinglechain") |
| 47 | +} |
| 48 | + |
| 49 | +type deliverClient struct { |
| 50 | + client ab.AtomicBroadcast_DeliverClient |
| 51 | + windowSize uint64 |
| 52 | + unAcknowledged uint64 |
| 53 | + solo *solo |
| 54 | +} |
| 55 | + |
| 56 | +func newDeliverClient(client ab.AtomicBroadcast_DeliverClient, windowSize uint64, solo *solo) *deliverClient { |
| 57 | + return &deliverClient{client: client, windowSize: windowSize, solo: solo} |
| 58 | +} |
| 59 | + |
| 60 | +func (r *deliverClient) seekOldest() error { |
| 61 | + return r.client.Send(&ab.DeliverUpdate{ |
| 62 | + Type: &ab.DeliverUpdate_Seek{ |
| 63 | + Seek: &ab.SeekInfo{ |
| 64 | + Start: ab.SeekInfo_OLDEST, |
| 65 | + WindowSize: r.windowSize, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +func (r *deliverClient) seekNewest() error { |
| 72 | + return r.client.Send(&ab.DeliverUpdate{ |
| 73 | + Type: &ab.DeliverUpdate_Seek{ |
| 74 | + Seek: &ab.SeekInfo{ |
| 75 | + Start: ab.SeekInfo_NEWEST, |
| 76 | + WindowSize: r.windowSize, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +func (r *deliverClient) seek(blockNumber uint64) error { |
| 83 | + return r.client.Send(&ab.DeliverUpdate{ |
| 84 | + Type: &ab.DeliverUpdate_Seek{ |
| 85 | + Seek: &ab.SeekInfo{ |
| 86 | + Start: ab.SeekInfo_SPECIFIED, |
| 87 | + SpecifiedNumber: blockNumber, |
| 88 | + WindowSize: r.windowSize, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +// constructBlock constructs a block from a list of transactions |
| 95 | +func (r *deliverClient) constructBlock(transactions []*pb.Transaction2) *pb.Block2 { |
| 96 | + block := &pb.Block2{} |
| 97 | + for _, tx := range transactions { |
| 98 | + txBytes, _ := proto.Marshal(tx) |
| 99 | + block.Transactions = append(block.Transactions, txBytes) |
| 100 | + } |
| 101 | + return block |
| 102 | +} |
| 103 | + |
| 104 | +// commit the received transaction |
| 105 | +func (r *deliverClient) commit(txs []*pb.Transaction2) error { |
| 106 | + rawblock := r.constructBlock(txs) |
| 107 | + |
| 108 | + lgr := kvledger.GetLedger(r.solo.ledger) |
| 109 | + |
| 110 | + var err error |
| 111 | + if _, _, err = lgr.RemoveInvalidTransactionsAndPrepare(rawblock); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + if err = lgr.Commit(); err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + return err |
| 118 | +} |
| 119 | + |
| 120 | +func (r *deliverClient) readUntilClose() { |
| 121 | + for { |
| 122 | + msg, err := r.client.Recv() |
| 123 | + if err != nil { |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + switch t := msg.Type.(type) { |
| 128 | + case *ab.DeliverResponse_Error: |
| 129 | + if t.Error == ab.Status_SUCCESS { |
| 130 | + fmt.Println("ERROR! Received success in error field") |
| 131 | + return |
| 132 | + } |
| 133 | + fmt.Println("Got error ", t) |
| 134 | + case *ab.DeliverResponse_Block: |
| 135 | + txs := []*pb.Transaction2{} |
| 136 | + for _, d := range t.Block.Messages { |
| 137 | + if d != nil && d.Data != nil { |
| 138 | + tx := &pb.Transaction2{} |
| 139 | + if err = proto.Unmarshal(d.Data, tx); err != nil { |
| 140 | + fmt.Printf("Error getting tx(%s)...dropping block\n", err) |
| 141 | + continue |
| 142 | + } |
| 143 | + txs = append(txs, tx) |
| 144 | + } |
| 145 | + } |
| 146 | + if err = r.commit(txs); err != nil { |
| 147 | + fmt.Printf("Got error while committing(%s)\n", err) |
| 148 | + } else { |
| 149 | + fmt.Printf("Commit success, created a block!\n", err) |
| 150 | + } |
| 151 | + |
| 152 | + r.unAcknowledged++ |
| 153 | + if r.unAcknowledged >= r.windowSize/2 { |
| 154 | + fmt.Println("Sending acknowledgement") |
| 155 | + err = r.client.Send(&ab.DeliverUpdate{Type: &ab.DeliverUpdate_Acknowledgement{Acknowledgement: &ab.Acknowledgement{Number: t.Block.Number}}}) |
| 156 | + if err != nil { |
| 157 | + return |
| 158 | + } |
| 159 | + r.unAcknowledged = 0 |
| 160 | + } |
| 161 | + default: |
| 162 | + fmt.Println("Received unknown: ", t) |
| 163 | + return |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +type solo struct { |
| 169 | + //ledger to commit to |
| 170 | + ledger string |
| 171 | + |
| 172 | + //orderer to connect to |
| 173 | + orderer string |
| 174 | + |
| 175 | + //client of the orderer |
| 176 | + client *deliverClient |
| 177 | +} |
| 178 | + |
| 179 | +const defaultTimeout = time.Second * 3 |
| 180 | + |
| 181 | +//Start establishes communication with an orders |
| 182 | +func (s *solo) Start() error { |
| 183 | + if s.client != nil { |
| 184 | + return fmt.Errorf("Client to (%s) exists", s.orderer) |
| 185 | + } |
| 186 | + |
| 187 | + var opts []grpc.DialOption |
| 188 | + opts = append(opts, grpc.WithInsecure()) |
| 189 | + opts = append(opts, grpc.WithTimeout(defaultTimeout)) |
| 190 | + opts = append(opts, grpc.WithBlock()) |
| 191 | + conn, err := grpc.Dial(s.orderer, opts...) |
| 192 | + if err != nil { |
| 193 | + return err |
| 194 | + } |
| 195 | + var abc ab.AtomicBroadcast_DeliverClient |
| 196 | + abc, err = ab.NewAtomicBroadcastClient(conn).Deliver(context.TODO()) |
| 197 | + if err != nil { |
| 198 | + return err |
| 199 | + } |
| 200 | + |
| 201 | + s.client = newDeliverClient(abc, 10, s) |
| 202 | + if err = s.client.seekOldest(); err != nil { |
| 203 | + return err |
| 204 | + } |
| 205 | + |
| 206 | + s.client.readUntilClose() |
| 207 | + |
| 208 | + return err |
| 209 | +} |
| 210 | + |
| 211 | +// NewCommitter constructs a committer object if not already present |
| 212 | +func NewCommitter() committer.Committer { |
| 213 | + if viper.GetBool("peer.committer.enabled") { |
| 214 | + //TODO ledger needs to be configured, for now just the default |
| 215 | + ledger := string(chaincode.DefaultChain) |
| 216 | + orderer := viper.GetString("peer.committer.ledger.orderer") |
| 217 | + logger.Infof("Creating committer for single noops endorser") |
| 218 | + return &solo{ledger: ledger, orderer: orderer} |
| 219 | + } |
| 220 | + logger.Infof("Committer disabled") |
| 221 | + return nil |
| 222 | +} |
0 commit comments