github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/sbft/simplebft/connection.go (about) 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 simplebft 18 19 // Connection is an event from system to notify a new connection with 20 // replica. 21 // On connection, we send our latest (weak) checkpoint, and we expect 22 // to receive one from replica. 23 func (s *SBFT) Connection(replica uint64) { 24 batch := *s.sys.LastBatch(s.chainId) 25 batch.Payloads = nil // don't send the big payload 26 hello := &Hello{Batch: &batch} 27 if s.isPrimary() && s.activeView && s.lastNewViewSent != nil { 28 hello.NewView = s.lastNewViewSent 29 } 30 s.sys.Send(s.chainId, &Msg{&Msg_Hello{hello}}, replica) 31 32 svc := s.replicaState[s.id].signedViewchange 33 if svc != nil { 34 s.sys.Send(s.chainId, &Msg{&Msg_ViewChange{svc}}, replica) 35 } 36 37 // A reconnecting replica can play forward its blockchain to 38 // the batches listed in the hello message. However, the 39 // currently in-flight batches will not be reflected in the 40 // Hello message, nor will all messages be present to actually 41 // commit the in-flight batches at the reconnecting replica. 42 // 43 // Therefore we also send the most recent (pre)prepare, 44 // commit, checkpoint so that the reconnecting replica can 45 // catch up on the in-flight batches. 46 47 batchheader, err := s.checkBatch(&batch, false, true) 48 if err != nil { 49 panic(err) 50 } 51 52 if s.cur.subject.Seq.Seq > batchheader.Seq && s.activeView { 53 if s.isPrimary() { 54 s.sys.Send(s.chainId, &Msg{&Msg_Preprepare{s.cur.preprep}}, replica) 55 } else { 56 s.sys.Send(s.chainId, &Msg{&Msg_Prepare{&s.cur.subject}}, replica) 57 } 58 if s.cur.prepared { 59 s.sys.Send(s.chainId, &Msg{&Msg_Commit{&s.cur.subject}}, replica) 60 } 61 if s.cur.committed { 62 s.sys.Send(s.chainId, &Msg{&Msg_Checkpoint{s.makeCheckpoint()}}, replica) 63 } 64 } 65 } 66 67 func (s *SBFT) handleHello(h *Hello, src uint64) { 68 bh, err := s.checkBatch(h.Batch, false, true) 69 log.Debugf("replica %d: got hello for batches %d from replica %d", s.id, bh.Seq, src) 70 71 if err != nil { 72 log.Warningf("replica %d: invalid hello batches from %d: %s", s.id, src, err) 73 return 74 } 75 76 if s.sys.LastBatch(s.chainId).DecodeHeader().Seq < bh.Seq { 77 log.Debugf("replica %d: delivering batches %d after hello from replica %d", s.id, bh.Seq, src) 78 blockOK, committers := s.getCommittersFromBatch(h.Batch) 79 if blockOK { 80 s.deliverBatch(h.Batch, committers) 81 } else { 82 log.Debugf("replica %d: we got a hello from %d with an erroneous block", s.id, src) 83 } 84 } 85 86 s.handleNewView(h.NewView, src) 87 88 s.replicaState[src].hello = h 89 s.discardBacklog(src) 90 s.processBacklog() 91 }