@@ -20,7 +20,9 @@ import (
20
20
"testing"
21
21
22
22
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
23
+ "github.com/hyperledger/fabric/orderer/ledger"
23
24
cb "github.com/hyperledger/fabric/protos/common"
25
+ ab "github.com/hyperledger/fabric/protos/orderer"
24
26
25
27
logging "github.com/op/go-logging"
26
28
)
@@ -106,3 +108,103 @@ func TestTruncationSafety(t *testing.T) {
106
108
t .Fatalf ("The iterator should have found %d new blocks but found %d" , newBlocks , count )
107
109
}
108
110
}
111
+
112
+ func TestRetrieval (t * testing.T ) {
113
+ rl := newTestChain (3 )
114
+ rl .Append (ledger .CreateNextBlock (rl , []* cb.Envelope {& cb.Envelope {Payload : []byte ("My Data" )}}))
115
+ it , num := rl .Iterator (& ab.SeekPosition {Type : & ab.SeekPosition_Oldest {}})
116
+ if num != 0 {
117
+ t .Fatalf ("Expected genesis block iterator, but got %d" , num )
118
+ }
119
+ signal := it .ReadyChan ()
120
+ select {
121
+ case <- signal :
122
+ default :
123
+ t .Fatalf ("Should be ready for block read" )
124
+ }
125
+ block , status := it .Next ()
126
+ if status != cb .Status_SUCCESS {
127
+ t .Fatalf ("Expected to successfully read the genesis block" )
128
+ }
129
+ if block .Header .Number != 0 {
130
+ t .Fatalf ("Expected to successfully retrieve the genesis block" )
131
+ }
132
+ signal = it .ReadyChan ()
133
+ select {
134
+ case <- signal :
135
+ default :
136
+ t .Fatalf ("Should still be ready for block read" )
137
+ }
138
+ block , status = it .Next ()
139
+ if status != cb .Status_SUCCESS {
140
+ t .Fatalf ("Expected to successfully read the second block" )
141
+ }
142
+ if block .Header .Number != 1 {
143
+ t .Fatalf ("Expected to successfully retrieve the second block but got block number %d" , block .Header .Number )
144
+ }
145
+ }
146
+
147
+ func TestBlockedRetrieval (t * testing.T ) {
148
+ rl := newTestChain (3 )
149
+ it , num := rl .Iterator (& ab.SeekPosition {Type : & ab.SeekPosition_Specified {Specified : & ab.SeekSpecified {Number : 1 }}})
150
+ if num != 1 {
151
+ t .Fatalf ("Expected block iterator at 1, but got %d" , num )
152
+ }
153
+ signal := it .ReadyChan ()
154
+ select {
155
+ case <- signal :
156
+ t .Fatalf ("Should not be ready for block read" )
157
+ default :
158
+ }
159
+ rl .Append (ledger .CreateNextBlock (rl , []* cb.Envelope {& cb.Envelope {Payload : []byte ("My Data" )}}))
160
+ select {
161
+ case <- signal :
162
+ default :
163
+ t .Fatalf ("Should now be ready for block read" )
164
+ }
165
+ block , status := it .Next ()
166
+ if status != cb .Status_SUCCESS {
167
+ t .Fatalf ("Expected to successfully read the second block" )
168
+ }
169
+ if block .Header .Number != 1 {
170
+ t .Fatalf ("Expected to successfully retrieve the second block" )
171
+ }
172
+ }
173
+
174
+ func TestIteratorPastEnd (t * testing.T ) {
175
+ rl := newTestChain (3 )
176
+ it , _ := rl .Iterator (& ab.SeekPosition {Type : & ab.SeekPosition_Specified {Specified : & ab.SeekSpecified {Number : 2 }}})
177
+ if _ , status := it .Next (); status != cb .Status_NOT_FOUND {
178
+ t .Fatalf ("Expected block with status NOT_FOUND, but got %d" , status )
179
+ }
180
+ }
181
+
182
+ func TestIteratorOldest (t * testing.T ) {
183
+ rl := newTestChain (3 )
184
+ // add enough block to roll off the genesis block
185
+ rl .Append (ledger .CreateNextBlock (rl , []* cb.Envelope {& cb.Envelope {Payload : []byte ("My Data" )}}))
186
+ rl .Append (ledger .CreateNextBlock (rl , []* cb.Envelope {& cb.Envelope {Payload : []byte ("My Data" )}}))
187
+ rl .Append (ledger .CreateNextBlock (rl , []* cb.Envelope {& cb.Envelope {Payload : []byte ("My Data" )}}))
188
+ _ , num := rl .Iterator (& ab.SeekPosition {Type : & ab.SeekPosition_Specified {Specified : & ab.SeekSpecified {Number : 1 }}})
189
+ if num != 1 {
190
+ t .Fatalf ("Expected block iterator at 1, but got %d" , num )
191
+ }
192
+ }
193
+
194
+ func TestAppendBadBLock (t * testing.T ) {
195
+ rl := newTestChain (3 )
196
+ t .Run ("BadBlockNumber" , func (t * testing.T ) {
197
+ nextBlock := ledger .CreateNextBlock (rl , []* cb.Envelope {& cb.Envelope {Payload : []byte ("My Data" )}})
198
+ nextBlock .Header .Number = nextBlock .Header .Number + 1
199
+ if err := rl .Append (nextBlock ); err == nil {
200
+ t .Fatalf ("Expected Append to fail." )
201
+ }
202
+ })
203
+ t .Run ("BadPreviousHash" , func (t * testing.T ) {
204
+ nextBlock := ledger .CreateNextBlock (rl , []* cb.Envelope {& cb.Envelope {Payload : []byte ("My Data" )}})
205
+ nextBlock .Header .PreviousHash = []byte ("bad hash" )
206
+ if err := rl .Append (nextBlock ); err == nil {
207
+ t .Fatalf ("Expected Append to fail." )
208
+ }
209
+ })
210
+ }
0 commit comments