yfhk / fabric_log

logs
1 stars 0 forks source link

bw.registrar.newChain(newChannelConfig) in WriteConfigBlock #18

Open yfhk opened 5 years ago

yfhk commented 5 years ago

// WriteConfigBlock should be invoked for blocks which contain a config transaction. // This call will block until the new config has taken effect, then will return // while the block is written asynchronously to disk. func (bw BlockWriter) WriteConfigBlock(block cb.Block, encodedMetadataValue []byte) { ctx, err := utils.ExtractEnvelope(block, 0) if err != nil { logger.Panicf("Told to write a config block, but could not get configtx: %s", err) }

payload, err := utils.UnmarshalPayload(ctx.Payload)
if err != nil {
    logger.Panicf("Told to write a config block, but configtx payload is invalid: %s", err)
}

if payload.Header == nil {
    logger.Panicf("Told to write a config block, but configtx payload header is missing")
}

chdr, err := utils.UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
    logger.Panicf("Told to write a config block with an invalid channel header: %s", err)
}

switch chdr.Type {
case int32(cb.HeaderType_ORDERER_TRANSACTION):
    newChannelConfig, err := utils.UnmarshalEnvelope(payload.Data)
    if err != nil {
        logger.Panicf("Told to write a config block with new channel, but did not have config update embedded: %s", err)
    }
    bw.registrar.newChain(newChannelConfig)
case int32(cb.HeaderType_CONFIG):
    configEnvelope, err := configtx.UnmarshalConfigEnvelope(payload.Data)
    if err != nil {
        logger.Panicf("Told to write a config block with new channel, but did not have config envelope encoded: %s", err)
    }

    err = bw.support.Validate(configEnvelope)
    if err != nil {
        logger.Panicf("Told to write a config block with new config, but could not apply it: %s", err)
    }

    bundle, err := bw.support.CreateBundle(chdr.ChannelId, configEnvelope.Config)
    if err != nil {
        logger.Panicf("Told to write a config block with a new config, but could not convert it to a bundle: %s", err)
    }

    bw.support.Update(bundle)
default:
    logger.Panicf("Told to write a config block with unknown header type: %v", chdr.Type)
}

bw.WriteBlock(block, encodedMetadataValue)

}

yfhk commented 5 years ago

func (r Registrar) newChain(configtx cb.Envelope) { ledgerResources := r.newLedgerResources(configtx) ledgerResources.Append(blockledger.CreateNextBlock(ledgerResources, []*cb.Envelope{configtx}))

// Copy the map to allow concurrent reads from broadcast/deliver while the new chainSupport is
newChains := make(map[string]*ChainSupport)
for key, value := range r.chains {
    newChains[key] = value
}

cs := newChainSupport(r, ledgerResources, r.consenters, r.signer)
chainID := ledgerResources.ConfigtxValidator().ChainID()

logger.Infof("Created and starting new chain %s", chainID)

newChains[string(chainID)] = cs
cs.start()

r.chains = newChains

}