committer.go 5.7 KB
Newer Older
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

                 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package noopssinglechain

import (
	"fmt"
	"time"

	"github.com/op/go-logging"
	"github.com/spf13/viper"

	"github.com/golang/protobuf/proto"
	"github.com/hyperledger/fabric/core/chaincode"
	"github.com/hyperledger/fabric/core/committer"
	"github.com/hyperledger/fabric/core/ledgernext/kvledger"
	ab "github.com/hyperledger/fabric/orderer/atomicbroadcast"
	"golang.org/x/net/context"
	"google.golang.org/grpc"

	pb "github.com/hyperledger/fabric/protos"
)

//--------!!!IMPORTANT!!-!!IMPORTANT!!-!!IMPORTANT!!---------
// This Orderer is based off fabric/orderer/sample_clients/
// deliver_stdout/client.go. This is used merely to complete
// the loop for the "skeleton" path so we can reason about and
// modify committer component more effectively using code.

var logger *logging.Logger // package-level logger

func init() {
	logger = logging.MustGetLogger("noopssinglechain")
}

type deliverClient struct {
	client         ab.AtomicBroadcast_DeliverClient
	windowSize     uint64
	unAcknowledged uint64
	solo           *solo
}

func newDeliverClient(client ab.AtomicBroadcast_DeliverClient, windowSize uint64, solo *solo) *deliverClient {
	return &deliverClient{client: client, windowSize: windowSize, solo: solo}
}

func (r *deliverClient) seekOldest() error {
	return r.client.Send(&ab.DeliverUpdate{
		Type: &ab.DeliverUpdate_Seek{
			Seek: &ab.SeekInfo{
				Start:      ab.SeekInfo_OLDEST,
				WindowSize: r.windowSize,
			},
		},
	})
}

func (r *deliverClient) seekNewest() error {
	return r.client.Send(&ab.DeliverUpdate{
		Type: &ab.DeliverUpdate_Seek{
			Seek: &ab.SeekInfo{
				Start:      ab.SeekInfo_NEWEST,
				WindowSize: r.windowSize,
			},
		},
	})
}

func (r *deliverClient) seek(blockNumber uint64) error {
	return r.client.Send(&ab.DeliverUpdate{
		Type: &ab.DeliverUpdate_Seek{
			Seek: &ab.SeekInfo{
				Start:           ab.SeekInfo_SPECIFIED,
				SpecifiedNumber: blockNumber,
				WindowSize:      r.windowSize,
			},
		},
	})
}

// constructBlock constructs a block from a list of transactions
func (r *deliverClient) constructBlock(transactions []*pb.Transaction2) *pb.Block2 {
	block := &pb.Block2{}
	for _, tx := range transactions {
		txBytes, _ := proto.Marshal(tx)
		block.Transactions = append(block.Transactions, txBytes)
	}
	return block
}

// commit the received transaction
func (r *deliverClient) commit(txs []*pb.Transaction2) error {
	rawblock := r.constructBlock(txs)

	lgr := kvledger.GetLedger(r.solo.ledger)

	var err error
	if _, _, err = lgr.RemoveInvalidTransactionsAndPrepare(rawblock); err != nil {
		return err
	}
	if err = lgr.Commit(); err != nil {
		return err
	}
	return err
}

func (r *deliverClient) readUntilClose() {
	for {
		msg, err := r.client.Recv()
		if err != nil {
			return
		}

		switch t := msg.Type.(type) {
		case *ab.DeliverResponse_Error:
			if t.Error == ab.Status_SUCCESS {
				fmt.Println("ERROR! Received success in error field")
				return
			}
			fmt.Println("Got error ", t)
		case *ab.DeliverResponse_Block:
			txs := []*pb.Transaction2{}
			for _, d := range t.Block.Messages {
				if d != nil && d.Data != nil {
					tx := &pb.Transaction2{}
					if err = proto.Unmarshal(d.Data, tx); err != nil {
						fmt.Printf("Error getting tx(%s)...dropping block\n", err)
						continue
					}
					txs = append(txs, tx)
				}
			}
			if err = r.commit(txs); err != nil {
				fmt.Printf("Got error while committing(%s)\n", err)
			} else {
				fmt.Printf("Commit success, created a block!\n", err)
			}

			r.unAcknowledged++
			if r.unAcknowledged >= r.windowSize/2 {
				fmt.Println("Sending acknowledgement")
				err = r.client.Send(&ab.DeliverUpdate{Type: &ab.DeliverUpdate_Acknowledgement{Acknowledgement: &ab.Acknowledgement{Number: t.Block.Number}}})
				if err != nil {
					return
				}
				r.unAcknowledged = 0
			}
		default:
			fmt.Println("Received unknown: ", t)
			return
		}
	}
}

type solo struct {
	//ledger to commit to
	ledger string

	//orderer to connect to
	orderer string

	//client of the orderer
	client *deliverClient
}

const defaultTimeout = time.Second * 3

//Start establishes communication with an orders
func (s *solo) Start() error {
	if s.client != nil {
		return fmt.Errorf("Client to (%s) exists", s.orderer)
	}

	var opts []grpc.DialOption
	opts = append(opts, grpc.WithInsecure())
	opts = append(opts, grpc.WithTimeout(defaultTimeout))
	opts = append(opts, grpc.WithBlock())
	conn, err := grpc.Dial(s.orderer, opts...)
	if err != nil {
		return err
	}
	var abc ab.AtomicBroadcast_DeliverClient
	abc, err = ab.NewAtomicBroadcastClient(conn).Deliver(context.TODO())
	if err != nil {
		return err
	}

	s.client = newDeliverClient(abc, 10, s)
	if err = s.client.seekOldest(); err != nil {
		return err
	}

	s.client.readUntilClose()

	return err
}

// NewCommitter constructs a committer object if not already present
func NewCommitter() committer.Committer {
	if viper.GetBool("peer.committer.enabled") {
		//TODO ledger needs to be configured, for now just the default
		ledger := string(chaincode.DefaultChain)
		orderer := viper.GetString("peer.committer.ledger.orderer")
		logger.Infof("Creating committer for single noops endorser")
		return &solo{ledger: ledger, orderer: orderer}
	}
	logger.Infof("Committer disabled")
	return nil
}