Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yfhk opened this issue Nov 23, 2018 · 1 comment
Open

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

yfhk opened this issue Nov 23, 2018 · 1 comment

Comments

@yfhk
Copy link
Owner

yfhk commented Nov 23, 2018

// 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
Copy link
Owner Author

yfhk commented Nov 23, 2018

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

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant