Skip to content

Commit fd47bea

Browse files
committed
[FAB-2578] Move utility functions to proper file
https://jira.hyperledger.org/browse/FAB-2578 `CreateNextBlock` and `GetBlock` are utility functions and as such, they belong to `util.go`. Change-Id: I74567bdbb508f8d8be00defdfee06e303b3f6379 Signed-off-by: Kostas Christidis <[email protected]>
1 parent 1ce056a commit fd47bea

File tree

2 files changed

+65
-54
lines changed

2 files changed

+65
-54
lines changed

orderer/ledger/ordererledger.go

-53
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package ordererledger
1919
import (
2020
cb "github.com/hyperledger/fabric/protos/common"
2121
ab "github.com/hyperledger/fabric/protos/orderer"
22-
23-
"github.com/golang/protobuf/proto"
2422
)
2523

2624
// Factory retrieves or creates new ledgers by chainID
@@ -62,54 +60,3 @@ type ReadWriter interface {
6260
Reader
6361
Writer
6462
}
65-
66-
// CreateNextBlock provides a utility way to construct the next block from contents and metadata for a given ledger
67-
// XXX this will need to be modified to accept marshaled envelopes to accomodate non-deterministic marshaling
68-
func CreateNextBlock(rl Reader, messages []*cb.Envelope) *cb.Block {
69-
var nextBlockNumber uint64
70-
var previousBlockHash []byte
71-
72-
if rl.Height() > 0 {
73-
it, _ := rl.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Newest{&ab.SeekNewest{}}})
74-
<-it.ReadyChan() // Should never block, but just in case
75-
block, status := it.Next()
76-
if status != cb.Status_SUCCESS {
77-
panic("Error seeking to newest block for chain with non-zero height")
78-
}
79-
nextBlockNumber = block.Header.Number + 1
80-
previousBlockHash = block.Header.Hash()
81-
}
82-
83-
data := &cb.BlockData{
84-
Data: make([][]byte, len(messages)),
85-
}
86-
87-
var err error
88-
for i, msg := range messages {
89-
data.Data[i], err = proto.Marshal(msg)
90-
if err != nil {
91-
panic(err)
92-
}
93-
}
94-
95-
block := cb.NewBlock(nextBlockNumber, previousBlockHash)
96-
block.Header.DataHash = data.Hash()
97-
block.Data = data
98-
99-
return block
100-
}
101-
102-
// GetBlock is a utility method for retrieving a single block
103-
func GetBlock(rl Reader, index uint64) *cb.Block {
104-
i, _ := rl.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Specified{Specified: &ab.SeekSpecified{Number: index}}})
105-
select {
106-
case <-i.ReadyChan():
107-
block, status := i.Next()
108-
if status != cb.Status_SUCCESS {
109-
return nil
110-
}
111-
return block
112-
default:
113-
return nil
114-
}
115-
}

orderer/ledger/util.go

+65-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package ordererledger
1818

1919
import (
20+
"github.com/golang/protobuf/proto"
2021
cb "github.com/hyperledger/fabric/protos/common"
22+
ab "github.com/hyperledger/fabric/protos/orderer"
2123
)
2224

2325
var closedChan chan struct{}
@@ -27,7 +29,8 @@ func init() {
2729
close(closedChan)
2830
}
2931

30-
// NotFoundErrorIterator simply always returns an error of cb.Status_NOT_FOUND, and is generally useful for implementations of the Reader interface
32+
// NotFoundErrorIterator simply always returns an error of cb.Status_NOT_FOUND,
33+
// and is generally useful for implementations of the Reader interface
3134
type NotFoundErrorIterator struct{}
3235

3336
// Next returns nil, cb.Status_NOT_FOUND
@@ -39,3 +42,64 @@ func (nfei *NotFoundErrorIterator) Next() (*cb.Block, cb.Status) {
3942
func (nfei *NotFoundErrorIterator) ReadyChan() <-chan struct{} {
4043
return closedChan
4144
}
45+
46+
// CreateNextBlock provides a utility way to construct the next block from
47+
// contents and metadata for a given ledger
48+
// XXX This will need to be modified to accept marshaled envelopes
49+
// to accomodate non-deterministic marshaling
50+
func CreateNextBlock(rl Reader, messages []*cb.Envelope) *cb.Block {
51+
var nextBlockNumber uint64
52+
var previousBlockHash []byte
53+
54+
if rl.Height() > 0 {
55+
it, _ := rl.Iterator(&ab.SeekPosition{
56+
Type: &ab.SeekPosition_Newest{
57+
&ab.SeekNewest{},
58+
},
59+
})
60+
<-it.ReadyChan() // Should never block, but just in case
61+
block, status := it.Next()
62+
if status != cb.Status_SUCCESS {
63+
panic("Error seeking to newest block for chain with non-zero height")
64+
}
65+
nextBlockNumber = block.Header.Number + 1
66+
previousBlockHash = block.Header.Hash()
67+
}
68+
69+
data := &cb.BlockData{
70+
Data: make([][]byte, len(messages)),
71+
}
72+
73+
var err error
74+
for i, msg := range messages {
75+
data.Data[i], err = proto.Marshal(msg)
76+
if err != nil {
77+
panic(err)
78+
}
79+
}
80+
81+
block := cb.NewBlock(nextBlockNumber, previousBlockHash)
82+
block.Header.DataHash = data.Hash()
83+
block.Data = data
84+
85+
return block
86+
}
87+
88+
// GetBlock is a utility method for retrieving a single block
89+
func GetBlock(rl Reader, index uint64) *cb.Block {
90+
i, _ := rl.Iterator(&ab.SeekPosition{
91+
Type: &ab.SeekPosition_Specified{
92+
Specified: &ab.SeekSpecified{Number: index},
93+
},
94+
})
95+
select {
96+
case <-i.ReadyChan():
97+
block, status := i.Next()
98+
if status != cb.Status_SUCCESS {
99+
return nil
100+
}
101+
return block
102+
default:
103+
return nil
104+
}
105+
}

0 commit comments

Comments
 (0)