|
| 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/golang/protobuf/proto" |
| 24 | + "github.com/hyperledger/fabric/core/chaincode" |
| 25 | + "github.com/hyperledger/fabric/core/committer" |
| 26 | + "github.com/hyperledger/fabric/core/ledger/kvledger" |
| 27 | + "github.com/hyperledger/fabric/protos" |
| 28 | + "github.com/hyperledger/fabric/protos/common" |
| 29 | + "github.com/hyperledger/fabric/protos/orderer" |
| 30 | + putils "github.com/hyperledger/fabric/protos/utils" |
| 31 | + "github.com/op/go-logging" |
| 32 | + "github.com/spf13/viper" |
| 33 | + "golang.org/x/net/context" |
| 34 | + "google.golang.org/grpc" |
| 35 | +) |
| 36 | + |
| 37 | +var logger *logging.Logger // package-level logger |
| 38 | + |
| 39 | +func init() { |
| 40 | + logger = logging.MustGetLogger("committer") |
| 41 | +} |
| 42 | + |
| 43 | +// DeliverService used to communicate with orderers to obtain |
| 44 | +// new block and send the to the committer service |
| 45 | +type DeliverService struct { |
| 46 | + client orderer.AtomicBroadcast_DeliverClient |
| 47 | + windowSize uint64 |
| 48 | + unAcknowledged uint64 |
| 49 | + committer *committer.LedgerCommitter |
| 50 | +} |
| 51 | + |
| 52 | +// NewDeliverService construction function to create and initilize |
| 53 | +// delivery service instance |
| 54 | +func NewDeliverService() *DeliverService { |
| 55 | + if viper.GetBool("peer.committer.enabled") { |
| 56 | + logger.Infof("Creating committer for single noops endorser") |
| 57 | + |
| 58 | + var opts []grpc.DialOption |
| 59 | + opts = append(opts, grpc.WithInsecure()) |
| 60 | + opts = append(opts, grpc.WithTimeout(3*time.Second)) |
| 61 | + opts = append(opts, grpc.WithBlock()) |
| 62 | + endpoint := viper.GetString("peer.committer.ledger.orderer") |
| 63 | + conn, err := grpc.Dial(endpoint, opts...) |
| 64 | + if err != nil { |
| 65 | + logger.Errorf("Cannot dial to %s, because of %s", endpoint, err) |
| 66 | + return nil |
| 67 | + } |
| 68 | + var abc orderer.AtomicBroadcast_DeliverClient |
| 69 | + abc, err = orderer.NewAtomicBroadcastClient(conn).Deliver(context.TODO()) |
| 70 | + if err != nil { |
| 71 | + logger.Errorf("Unable to initialize atomic broadcast, due to %s", err) |
| 72 | + return nil |
| 73 | + } |
| 74 | + |
| 75 | + deliverService := &DeliverService{ |
| 76 | + // Atomic Broadcast Deliver Clienet |
| 77 | + client: abc, |
| 78 | + // Instance of RawLedger |
| 79 | + committer: committer.NewLedgerCommitter(kvledger.GetLedger(string(chaincode.DefaultChain))), |
| 80 | + windowSize: 10, |
| 81 | + } |
| 82 | + return deliverService |
| 83 | + } |
| 84 | + logger.Infof("Committer disabled") |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// Start the delivery service to read the block via delivery |
| 89 | +// protocol from the orderers |
| 90 | +func (d *DeliverService) Start() error { |
| 91 | + if err := d.seekOldest(); err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + d.readUntilClose() |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func (d *DeliverService) seekOldest() error { |
| 100 | + return d.client.Send(&orderer.DeliverUpdate{ |
| 101 | + Type: &orderer.DeliverUpdate_Seek{ |
| 102 | + Seek: &orderer.SeekInfo{ |
| 103 | + Start: orderer.SeekInfo_OLDEST, |
| 104 | + WindowSize: d.windowSize, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }) |
| 108 | +} |
| 109 | + |
| 110 | +func (d *DeliverService) readUntilClose() { |
| 111 | + for { |
| 112 | + msg, err := d.client.Recv() |
| 113 | + if err != nil { |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + switch t := msg.Type.(type) { |
| 118 | + case *orderer.DeliverResponse_Error: |
| 119 | + if t.Error == common.Status_SUCCESS { |
| 120 | + fmt.Println("ERROR! Received success in error field") |
| 121 | + return |
| 122 | + } |
| 123 | + fmt.Println("Got error ", t) |
| 124 | + case *orderer.DeliverResponse_Block: |
| 125 | + block := &protos.Block2{} |
| 126 | + for _, d := range t.Block.Data.Data { |
| 127 | + if d != nil { |
| 128 | + if tx, err := putils.GetEndorserTxFromBlock(d); err != nil { |
| 129 | + fmt.Printf("Error getting tx from block(%s)\n", err) |
| 130 | + } else if tx != nil { |
| 131 | + if t, err := proto.Marshal(tx); err == nil { |
| 132 | + block.Transactions = append(block.Transactions, t) |
| 133 | + } else { |
| 134 | + fmt.Printf("Cannot marshal transactoins %s\n", err) |
| 135 | + } |
| 136 | + } else { |
| 137 | + fmt.Printf("Nil tx from block\n") |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + // Once block is constructed need to commit into the ledger |
| 142 | + if err = d.committer.CommitBlock(block); err != nil { |
| 143 | + fmt.Printf("Got error while committing(%s)\n", err) |
| 144 | + } else { |
| 145 | + fmt.Printf("Commit success, created a block!\n") |
| 146 | + } |
| 147 | + |
| 148 | + d.unAcknowledged++ |
| 149 | + if d.unAcknowledged >= d.windowSize/2 { |
| 150 | + fmt.Println("Sending acknowledgement") |
| 151 | + err = d.client.Send(&orderer.DeliverUpdate{ |
| 152 | + Type: &orderer.DeliverUpdate_Acknowledgement{ |
| 153 | + Acknowledgement: &orderer.Acknowledgement{ |
| 154 | + Number: t.Block.Header.Number, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }) |
| 158 | + if err != nil { |
| 159 | + return |
| 160 | + } |
| 161 | + d.unAcknowledged = 0 |
| 162 | + } |
| 163 | + default: |
| 164 | + fmt.Println("Received unknown: ", t) |
| 165 | + return |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments