From d85251d65abbfbfbadcf749baedcc42ba0203424 Mon Sep 17 00:00:00 2001 From: Gabor Hosszu Date: Thu, 5 Jan 2017 10:56:10 +0100 Subject: [PATCH] Use a local copy of last batch SBFT used the ledger and an iterator to get the last batch. This commit changes this and tries to use a "local copy" that is generally a field in the backend struct. There are two cases: - backend (System implementation) only knows about Genesis, we can assume that the last batch is one with an empty header, empty signatures and empty payload batch - backend's Deliver is called on (re)connection or at batch delivery - here we can use the argument (batch of type Batch) to update the last batch Change-Id: Ibd381347df832643a547a3b05bdc7331f4fb1382 Signed-off-by: Gabor Hosszu --- orderer/sbft/backend/backend.go | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/orderer/sbft/backend/backend.go b/orderer/sbft/backend/backend.go index 54aab400989..1369390f426 100644 --- a/orderer/sbft/backend/backend.go +++ b/orderer/sbft/backend/backend.go @@ -45,7 +45,6 @@ import ( "github.com/hyperledger/fabric/orderer/sbft/persist" s "github.com/hyperledger/fabric/orderer/sbft/simplebft" cb "github.com/hyperledger/fabric/protos/common" - ab "github.com/hyperledger/fabric/protos/orderer" "github.com/op/go-logging" ) @@ -69,6 +68,8 @@ type Backend struct { persistence *persist.Persist ledger rawledger.ReadWriter + + lastBatch *s.Batch } type consensusConn Backend @@ -134,6 +135,7 @@ func NewBackend(peers map[string][]byte, conn *connection.Manager, rl rawledger. } RegisterConsensusServer(conn.Server, (*consensusConn)(c)) c.persistence = persist + c.lastBatch = &s.Batch{Header: nil, Signatures: nil, Payloads: [][]byte{}} c.queue = make(chan Executable) go c.run() return c, nil @@ -313,6 +315,7 @@ func (t *Backend) Deliver(batch *s.Batch) { // SBFT needs to use Rawledger's structures and signatures over the Block. block.Metadata.Metadata[headerIndex] = batch.Header block.Metadata.Metadata[signaturesIndex] = encodeSignatures(batch.Signatures) + t.lastBatch = batch t.ledger.Append(block) } @@ -338,16 +341,7 @@ func (t *Backend) Restore(key string, out proto.Message) bool { } func (t *Backend) LastBatch() *s.Batch { - it, _ := t.ledger.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Newest{}}) - block, status := it.Next() - data := block.Data.Data - if status != cb.Status_SUCCESS { - panic("Fatal ledger error: unable to get last block.") - } - header := getHeader(block.Metadata) - sgns := decodeSignatures(getEncodedSignatures(block.Metadata)) - batch := s.Batch{Header: header, Payloads: data, Signatures: sgns} - return &batch + return t.lastBatch } func (t *Backend) Sign(data []byte) []byte {