|
| 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 ramledger |
| 18 | + |
| 19 | +import ( |
| 20 | + ab "github.com/hyperledger/fabric/orderer/atomicbroadcast" |
| 21 | + "github.com/hyperledger/fabric/orderer/rawledger" |
| 22 | + |
| 23 | + "github.com/op/go-logging" |
| 24 | +) |
| 25 | + |
| 26 | +var logger = logging.MustGetLogger("rawledger/ramledger") |
| 27 | + |
| 28 | +func init() { |
| 29 | + logging.SetLevel(logging.DEBUG, "") |
| 30 | +} |
| 31 | + |
| 32 | +type cursor struct { |
| 33 | + list *simpleList |
| 34 | +} |
| 35 | + |
| 36 | +type simpleList struct { |
| 37 | + next *simpleList |
| 38 | + signal chan struct{} |
| 39 | + block *ab.Block |
| 40 | +} |
| 41 | + |
| 42 | +type ramLedger struct { |
| 43 | + maxSize int |
| 44 | + size int |
| 45 | + oldest *simpleList |
| 46 | + newest *simpleList |
| 47 | +} |
| 48 | + |
| 49 | +// New creates a new instance of the ram ledger |
| 50 | +func New(maxSize int) rawledger.ReadWriter { |
| 51 | + rl := &ramLedger{ |
| 52 | + maxSize: maxSize, |
| 53 | + size: 1, |
| 54 | + oldest: &simpleList{ |
| 55 | + signal: make(chan struct{}), |
| 56 | + block: &ab.Block{ |
| 57 | + Number: 0, |
| 58 | + PrevHash: []byte("GENESIS"), |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + rl.newest = rl.oldest |
| 63 | + return rl |
| 64 | +} |
| 65 | + |
| 66 | +// Height returns the highest block number in the chain, plus one |
| 67 | +func (rl *ramLedger) Height() uint64 { |
| 68 | + return rl.newest.block.Number + 1 |
| 69 | +} |
| 70 | + |
| 71 | +// Iterator implements the rawledger.Reader definition |
| 72 | +func (rl *ramLedger) Iterator(startType ab.SeekInfo_StartType, specified uint64) (rawledger.Iterator, uint64) { |
| 73 | + var list *simpleList |
| 74 | + switch startType { |
| 75 | + case ab.SeekInfo_OLDEST: |
| 76 | + oldest := rl.oldest |
| 77 | + list = &simpleList{ |
| 78 | + block: &ab.Block{Number: oldest.block.Number - 1}, |
| 79 | + next: oldest, |
| 80 | + signal: make(chan struct{}), |
| 81 | + } |
| 82 | + close(list.signal) |
| 83 | + case ab.SeekInfo_NEWEST: |
| 84 | + newest := rl.newest |
| 85 | + list = &simpleList{ |
| 86 | + block: &ab.Block{Number: newest.block.Number - 1}, |
| 87 | + next: newest, |
| 88 | + signal: make(chan struct{}), |
| 89 | + } |
| 90 | + close(list.signal) |
| 91 | + case ab.SeekInfo_SPECIFIED: |
| 92 | + list = rl.oldest |
| 93 | + if specified < list.block.Number || specified > rl.newest.block.Number+1 { |
| 94 | + return &rawledger.NotFoundErrorIterator{}, 0 |
| 95 | + } |
| 96 | + |
| 97 | + for { |
| 98 | + if list.block.Number == specified-1 { |
| 99 | + break |
| 100 | + } |
| 101 | + list = list.next // No need for nil check, because of range check above |
| 102 | + } |
| 103 | + } |
| 104 | + return &cursor{list: list}, list.block.Number + 1 |
| 105 | +} |
| 106 | + |
| 107 | +// Next blocks until there is a new block available, or returns an error if the next block is no longer retrievable |
| 108 | +func (cu *cursor) Next() (*ab.Block, ab.Status) { |
| 109 | + // This only loops once, as signal reading indicates non-nil next |
| 110 | + for { |
| 111 | + if cu.list.next != nil { |
| 112 | + cu.list = cu.list.next |
| 113 | + return cu.list.block, ab.Status_SUCCESS |
| 114 | + } |
| 115 | + |
| 116 | + <-cu.list.signal |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// ReadyChan returns a channel that will close when Next is ready to be called without blocking |
| 121 | +func (cu *cursor) ReadyChan() <-chan struct{} { |
| 122 | + return cu.list.signal |
| 123 | +} |
| 124 | + |
| 125 | +// Append creates a new block and appends it to the ledger |
| 126 | +func (rl *ramLedger) Append(messages []*ab.BroadcastMessage, proof []byte) *ab.Block { |
| 127 | + block := &ab.Block{ |
| 128 | + Number: rl.newest.block.Number + 1, |
| 129 | + PrevHash: rl.newest.block.Hash(), |
| 130 | + Messages: messages, |
| 131 | + Proof: proof, |
| 132 | + } |
| 133 | + rl.appendBlock(block) |
| 134 | + return block |
| 135 | +} |
| 136 | + |
| 137 | +func (rl *ramLedger) appendBlock(block *ab.Block) { |
| 138 | + rl.newest.next = &simpleList{ |
| 139 | + signal: make(chan struct{}), |
| 140 | + block: block, |
| 141 | + } |
| 142 | + |
| 143 | + lastSignal := rl.newest.signal |
| 144 | + logger.Debugf("Sending signal that block %d has a successor", rl.newest.block.Number) |
| 145 | + rl.newest = rl.newest.next |
| 146 | + close(lastSignal) |
| 147 | + |
| 148 | + rl.size++ |
| 149 | + |
| 150 | + if rl.size > rl.maxSize { |
| 151 | + rl.oldest = rl.oldest.next |
| 152 | + rl.size-- |
| 153 | + } |
| 154 | +} |
0 commit comments