impl.go 3.46 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
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 fileledger

import (
20
	cl "github.com/hyperledger/fabric/common/ledger"
21
	"github.com/hyperledger/fabric/common/ledger/blkstorage"
22
	ledger "github.com/hyperledger/fabric/orderer/ledger"
23
24
25
26
27
	cb "github.com/hyperledger/fabric/protos/common"
	ab "github.com/hyperledger/fabric/protos/orderer"
	"github.com/op/go-logging"
)

28
var logger = logging.MustGetLogger("orderer/fileledger")
29
30
31
32
33
34
35
36
37
38
39
40
41
var closedChan chan struct{}

func init() {
	closedChan = make(chan struct{})
	close(closedChan)
}

type fileLedger struct {
	blockStore blkstorage.BlockStore
	signal     chan struct{}
}

type fileLedgerIterator struct {
42
43
44
	ledger         *fileLedger
	blockNumber    uint64
	commonIterator cl.ResultsIterator
45
46
47
48
49
50
51
}

// Next blocks until there is a new block available, or returns an error if the
// next block is no longer retrievable
func (i *fileLedgerIterator) Next() (*cb.Block, cb.Status) {
	for {
		if i.blockNumber < i.ledger.Height() {
52
			result, err := i.commonIterator.Next()
53
54
55
56
			if err != nil {
				return nil, cb.Status_SERVICE_UNAVAILABLE
			}
			i.blockNumber++
57
			return result.(*cb.Block), cb.Status_SUCCESS
58
59
60
61
62
63
64
65
66
67
68
69
70
71
		}
		<-i.ledger.signal
	}
}

// ReadyChan supplies a channel which will block until Next will not block
func (i *fileLedgerIterator) ReadyChan() <-chan struct{} {
	signal := i.ledger.signal
	if i.blockNumber > i.ledger.Height()-1 {
		return signal
	}
	return closedChan
}

72
73
74
75
76
// Close releases resources acquired by the Iterator
func (i *fileLedgerIterator) Close() {
	i.commonIterator.Close()
}

77
78
// Iterator returns an Iterator, as specified by a cb.SeekInfo message, and its
// starting block number
79
func (fl *fileLedger) Iterator(startPosition *ab.SeekPosition) (ledger.Iterator, uint64) {
80
	var startingBlockNumber uint64
81
82
	switch start := startPosition.Type.(type) {
	case *ab.SeekPosition_Oldest:
83
		startingBlockNumber = 0
84
85
86
	case *ab.SeekPosition_Newest:
		info, err := fl.blockStore.GetBlockchainInfo()
		if err != nil {
87
			logger.Panic(err)
88
89
		}
		newestBlockNumber := info.Height - 1
90
		startingBlockNumber = newestBlockNumber
91
	case *ab.SeekPosition_Specified:
92
		startingBlockNumber = start.Specified.Number
93
		height := fl.Height()
94
		if startingBlockNumber > height {
95
			return &ledger.NotFoundErrorIterator{}, 0
96
		}
97
98
	default:
		return &ledger.NotFoundErrorIterator{}, 0
99
	}
100
101
102
103
104
105
106

	iterator, err := fl.blockStore.RetrieveBlocks(startingBlockNumber)
	if err != nil {
		return &ledger.NotFoundErrorIterator{}, 0
	}

	return &fileLedgerIterator{ledger: fl, blockNumber: startingBlockNumber, commonIterator: iterator}, startingBlockNumber
107
108
109
110
111
112
}

// Height returns the number of blocks on the ledger
func (fl *fileLedger) Height() uint64 {
	info, err := fl.blockStore.GetBlockchainInfo()
	if err != nil {
113
		logger.Panic(err)
114
115
116
117
118
119
120
121
122
123
124
125
126
	}
	return info.Height
}

// Append a new block to the ledger
func (fl *fileLedger) Append(block *cb.Block) error {
	err := fl.blockStore.AddBlock(block)
	if err == nil {
		close(fl.signal)
		fl.signal = make(chan struct{})
	}
	return err
}