|
| 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 multichain |
| 18 | + |
| 19 | +import ( |
| 20 | + "sync" |
| 21 | + |
| 22 | + "github.com/hyperledger/fabric/orderer/common/configtx" |
| 23 | + "github.com/hyperledger/fabric/orderer/common/policies" |
| 24 | + "github.com/hyperledger/fabric/orderer/rawledger" |
| 25 | + cb "github.com/hyperledger/fabric/protos/common" |
| 26 | + ab "github.com/hyperledger/fabric/protos/orderer" |
| 27 | + |
| 28 | + "github.com/golang/protobuf/proto" |
| 29 | + "github.com/op/go-logging" |
| 30 | +) |
| 31 | + |
| 32 | +var logger = logging.MustGetLogger("orderer/multichain") |
| 33 | + |
| 34 | +// XXX This crypto helper is a stand in until we have a real crypto handler |
| 35 | +// it considers all signatures to be valid |
| 36 | +type xxxCryptoHelper struct{} |
| 37 | + |
| 38 | +func (xxx xxxCryptoHelper) VerifySignature(msg []byte, ids []byte, sigs []byte) bool { |
| 39 | + return true |
| 40 | +} |
| 41 | + |
| 42 | +func init() { |
| 43 | + logging.SetLevel(logging.DEBUG, "") |
| 44 | +} |
| 45 | + |
| 46 | +// Manager coordinates the creation and access of chains |
| 47 | +type Manager interface { |
| 48 | + // GetChain retrieves the chain support for a chain (and whether it exists) |
| 49 | + GetChain(chainID []byte) (ChainSupport, bool) |
| 50 | +} |
| 51 | + |
| 52 | +type multiLedger struct { |
| 53 | + chains map[string]*chainSupport |
| 54 | + consenters map[string]Consenter |
| 55 | + ledgerFactory rawledger.Factory |
| 56 | + mutex sync.Mutex |
| 57 | +} |
| 58 | + |
| 59 | +// getConfigTx, this should ultimately be done more intelligently, but for now, we search the whole chain for txs and pick the last config one |
| 60 | +func getConfigTx(reader rawledger.Reader) *cb.Envelope { |
| 61 | + var lastConfigTx *cb.Envelope |
| 62 | + |
| 63 | + it, _ := reader.Iterator(ab.SeekInfo_OLDEST, 0) |
| 64 | + // Iterate over the blockchain, looking for config transactions, track the most recent one encountered |
| 65 | + // this will be the transaction which is returned |
| 66 | + for { |
| 67 | + select { |
| 68 | + case <-it.ReadyChan(): |
| 69 | + block, status := it.Next() |
| 70 | + if status != cb.Status_SUCCESS { |
| 71 | + logger.Fatalf("Error parsing blockchain at startup: %v", status) |
| 72 | + } |
| 73 | + // ConfigTxs should always be by themselves |
| 74 | + if len(block.Data.Data) != 1 { |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + maybeConfigTx := &cb.Envelope{} |
| 79 | + |
| 80 | + err := proto.Unmarshal(block.Data.Data[0], maybeConfigTx) |
| 81 | + |
| 82 | + if err != nil { |
| 83 | + logger.Fatalf("Found data which was not an envelope: %s", err) |
| 84 | + } |
| 85 | + |
| 86 | + payload := &cb.Payload{} |
| 87 | + err = proto.Unmarshal(maybeConfigTx.Payload, payload) |
| 88 | + |
| 89 | + if payload.Header.ChainHeader.Type != int32(cb.HeaderType_CONFIGURATION_TRANSACTION) { |
| 90 | + continue |
| 91 | + } |
| 92 | + |
| 93 | + logger.Debugf("Found configuration transaction for chain %x at block %d", payload.Header.ChainHeader.ChainID, block.Header.Number) |
| 94 | + lastConfigTx = maybeConfigTx |
| 95 | + default: |
| 96 | + return lastConfigTx |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// NewManagerImpl produces an instance of a Manager |
| 102 | +func NewManagerImpl(ledgerFactory rawledger.Factory, consenters map[string]Consenter) Manager { |
| 103 | + ml := &multiLedger{ |
| 104 | + chains: make(map[string]*chainSupport), |
| 105 | + ledgerFactory: ledgerFactory, |
| 106 | + } |
| 107 | + |
| 108 | + existingChains := ledgerFactory.ChainIDs() |
| 109 | + for _, chainID := range existingChains { |
| 110 | + rl, err := ledgerFactory.GetOrCreate(chainID) |
| 111 | + if err != nil { |
| 112 | + logger.Fatalf("Ledger factory reported chainID %x but could not retrieve it: %s", chainID, err) |
| 113 | + } |
| 114 | + configTx := getConfigTx(rl) |
| 115 | + if configTx == nil { |
| 116 | + logger.Fatalf("Could not find configuration transaction for chain %x", chainID) |
| 117 | + } |
| 118 | + configManager, policyManager, backingLedger := ml.newResources(configTx) |
| 119 | + chainID := configManager.ChainID() |
| 120 | + ml.chains[string(chainID)] = newChainSupport(configManager, policyManager, backingLedger, consenters) |
| 121 | + } |
| 122 | + |
| 123 | + for _, cs := range ml.chains { |
| 124 | + cs.start() |
| 125 | + } |
| 126 | + |
| 127 | + return ml |
| 128 | +} |
| 129 | + |
| 130 | +// GetChain retrieves the chain support for a chain (and whether it exists) |
| 131 | +func (ml *multiLedger) GetChain(chainID []byte) (ChainSupport, bool) { |
| 132 | + cs, ok := ml.chains[string(chainID)] |
| 133 | + return cs, ok |
| 134 | +} |
| 135 | + |
| 136 | +func (ml *multiLedger) newResources(configTx *cb.Envelope) (configtx.Manager, policies.Manager, rawledger.ReadWriter) { |
| 137 | + policyManager := policies.NewManagerImpl(xxxCryptoHelper{}) |
| 138 | + configHandlerMap := make(map[cb.ConfigurationItem_ConfigurationType]configtx.Handler) |
| 139 | + for ctype := range cb.ConfigurationItem_ConfigurationType_name { |
| 140 | + rtype := cb.ConfigurationItem_ConfigurationType(ctype) |
| 141 | + switch rtype { |
| 142 | + case cb.ConfigurationItem_Policy: |
| 143 | + configHandlerMap[rtype] = policyManager |
| 144 | + default: |
| 145 | + configHandlerMap[rtype] = configtx.NewBytesHandler() |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + payload := &cb.Payload{} |
| 150 | + err := proto.Unmarshal(configTx.Payload, payload) |
| 151 | + if err != nil { |
| 152 | + logger.Fatalf("Error unmarshaling a config transaction payload: %s", err) |
| 153 | + } |
| 154 | + |
| 155 | + configEnvelope := &cb.ConfigurationEnvelope{} |
| 156 | + err = proto.Unmarshal(payload.Data, configEnvelope) |
| 157 | + if err != nil { |
| 158 | + logger.Fatalf("Error unmarshaling a config transaction to config envelope: %s", err) |
| 159 | + } |
| 160 | + |
| 161 | + configManager, err := configtx.NewConfigurationManager(configEnvelope, policyManager, configHandlerMap) |
| 162 | + if err != nil { |
| 163 | + logger.Fatalf("Error unpacking configuration transaction: %s", err) |
| 164 | + } |
| 165 | + |
| 166 | + chainID := configManager.ChainID() |
| 167 | + |
| 168 | + ledger, err := ml.ledgerFactory.GetOrCreate(chainID) |
| 169 | + if err != nil { |
| 170 | + logger.Fatalf("Error getting ledger for %x", chainID) |
| 171 | + } |
| 172 | + |
| 173 | + return configManager, policyManager, ledger |
| 174 | +} |
0 commit comments