Blockchain tutorial fabric1.0 Source Code Analysis configuration transaction-generate Channel Configuration 2

Source: Internet
Author: User
Blockchain tutorialFabric1.0 Source Code Analysis configuration transaction-generate Channel Configuration 2. Generator interface implementation, that is, bootstrapper.

Type bootstrapper struct {???? Channelgroups [] * CB. configgroup ???? Orderergroups [] * CB. configgroup ???? Applicationgroups [] * CB. configgroup ???? Consortiumsgroups [] * CB. configgroup} func new (CONF * genesisconfig. profile) generatorfunc (BS * bootstrapper) channeltemplate () configtx. templatefunc (BS * bootstrapper) genesisblock () * CB. blockfunc (BS * bootstrapper) genesisblockforchannel (channelid string) * CB. block // code in common/configtx/tool/Provisional. go

The generator code of func new (CONF * genesisconfig. Profile) is as follows:

Func new (CONF * genesisconfig. Profile) generator {???? BS: = & bootstrapper {???????? Channelgroups: [] * CB. configgroup {???????????? Config. defaulthashingalgorithm (), // default hash function ???????????? Config. defablockblockdatahashingstructure (), // default block data hash structure ???????????????????????? // Default channel policy, including read policy, write policy, and management policy ???????????? // Readerspolicykey = "readers", implicitmetapoicy_any, arbitrary ???????????? Policies. templateimplicitmetaanypolicy ([] string {}, configvaluesmsp. readerspolicykey ),???????????? // Writerspolicykey = "writers", implicitmetapoicy_any, arbitrary ???????????? Policies. templateimplicitmetaanypolicy ([] string {}, configvaluesmsp. writerspolicykey ),???????????? // Adminspolicykey = "Admins", implicitmetapoicy_majority, the majority ???????????? Policies. templateimplicitmetamajoritypolicy ([] string {}, configvaluesmsp. adminspolicykey ),????????},????}???? If Conf. orderer! = Nil {// system Channel Configuration ???????? OA: = config. templateordereraddresses (Conf. orderer. addresses) // set the orderer address ???????? Oa. Values [config. ordereraddresseskey]. modpolicy = ordereradminspolicy // ordereradminspolicy = "/channel/orderer/Admins "???????? BS. orderergroups = [] * CB. configgroup {???????????? Oa ,???????????? Config. templateconsensustype (Conf. orderer. orderertype), // set the consensus plug-in type ???????????? Config. templatebatchsize (& AB. batchsize {// set the batch size ???????????????? Maxmessagecount: Conf. orderer. batchsize. maxmessagecount ,???????????????? Absolutemaxbytes: Conf. orderer. batchsize. absolutemaxbytes ,???????????????? Preferredmaxbytes: Conf. orderer. batchsize. preferredmaxbytes ,????????????}),???????????? Config. templatebatchtimeout (Conf. orderer. batchtimeout. String (), // set the batch processing timeout ???????????? Config. templatechannelrestrictions (Conf. orderer. maxchannels), // set the maximum number of channels ???????????? // Initialize the read, write, and management policies of orderer ???????????? Policies. Keys ([] string {config. orderergroupkey}, blockvalidationpolicykey, configvaluesmsp. writerspolicykey, CB. implicitmetapoicy_any ),???????????? Policies. templateimplicitmetaanypolicy ([] string {config. orderergroupkey}, configvaluesmsp. readerspolicykey ),???????????? Policies. templateimplicitmetaanypolicy ([] string {config. orderergroupkey}, configvaluesmsp. writerspolicykey ),???????????? Policies. templateimplicitmetamajoritypolicy ([] string {config. orderergroupkey}, configvaluesmsp. adminspolicykey ),????????}???????? For _, org: = range Conf. orderer. Organizations {???????????? Mspconfig, err: = MSP. getverifyingmspconfig (Org. mspdir, org. ID )???????????? BS. orderergroups = append (BS. orderergroups ,???????????????? Configvaluesmsp. templategroupmspwithadminroleprincipal ([] string {config. orderergroupkey, org. name },???????????????????? Mspconfig, org. adminprincipal = genesisconfig. adminroleadminprincipal ,????????????????),????????????)??????? ?}???????? Switch Conf. orderer. orderertype {???????? Case consensustypesolo :???????? Case consensustypekafka :???????????? BS. orderergroups = append (BS. orderergroups, config. templatekafkabrokers (Conf. orderer. Kafka. Brokers) // set Kafka ???????? Default :????????}????}???? If Conf. application! = Nil {// application channel configuration ???????? BS. applicationgroups = [] * CB. configgroup {???????????? Policies. templateimplicitmetaanypolicy ([] string {config. applicationgroupkey}, configvaluesmsp. readerspolicykey ),???????????? Policies. templateimplicitmetaanypolicy ([] string {config. applicationgroupkey}, configvaluesmsp. writerspolicykey ),???????????? Policies. templateimplicitmetamajoritypolicy ([] string {config. applicationgroupkey}, configvaluesmsp. adminspolicykey ),????????}???????? For _, org: = range Conf. application. Organizations {???????????? Mspconfig, err: = MSP. getverifyingmspconfig (Org. mspdir, org. ID )???????????? BS. applicationgroups = append (BS. applicationgroups ,???????????????? Configvaluesmsp. templategroupmspwithadminroleprincipal ([] string {config. applicationgroupkey, org. name },???????????????????? Mspconfig, org. adminprincipal = genesisconfig. adminroleadminprincipal ,????????????????),????????????)??????? ????? VaR anchorprotos [] * pb. anchorpeer ???????????? For _, anchorpeer: = range org. anchorpeers {???????????????? Anchorprotos = append (anchorprotos, & Pb. anchorpeer {???????????????????? HOST: anchorpeer. Host ,???????????????????? Port: int32 (anchorpeer. port ),????????????????})????????????}?????? ?????? BS. applicationgroups = append (BS. applicationgroups, config. templateanchorpeers (Org. Name, anchorprotos ))????????}????}???? If Conf. consortiums! = Nil {// consortium related ???????? TCG: = config. templateconsortiumsgroup ()???????? TCG. Groups [config. consortiumsgroupkey]. modpolicy = ordereradminspolicy ???????? TCG. Groups [config. consortiumsgroupkey]. Policies [configvaluesmsp. adminspolicykey] = & CB. configpolicy {???????????? Policy: & CB. Policy {???????????????? Type: int32 (CB. policy_signature ),???????????????? Value: utils. marshalorpanic (cauthdsl. acceptallpolicy ),????????????},???????????? Modpolicy: ordereradminspolicy ,????????}???????? BS. consortiumsgroups = append (BS. consortiumsgroups, TCG )???????? For consortiumname, consorparts: = range Conf. consortiums {???????????? CG: = config. templateconsortiumchannelcreationpolicy (consortiumname, policies. implicitmetapoicywithsubpolicy (???????????????? Configvaluesmsp. adminspolicykey ,???????????????? CB. implicitmetapoicy_any ,????????????). Policy )???????????? CG. Groups [config. consortiumsgroupkey]. Groups [consortiumname]. modpolicy = ordereradminspolicy ???????????? CG. Groups [config. consortiumsgroupkey]. Groups [consortiumname]. Values [config. channelcreationpolicykey]. modpolicy = ordereradminspolicy ???????????? BS. consortiumsgroups = append (BS. consortiumsgroups, CG )???????????? For _, org: = range consorations. Organizations {???????????????? Mspconfig, err: = MSP. getverifyingmspconfig (Org. mspdir, org. ID )???????????????? BS. consortiumsgroups = append (BS. consortiumsgroups ,???????????????????? Configvaluesmsp. templategroupmspwithadminroleprincipal (???????????????????????? [] String {config. consortiumsgroupkey, consortiumname, org. name },???????????????????????? Mspconfig, org. adminprincipal = genesisconfig. adminroleadminprincipal ,????????????????????),???????????????? )????????????}????????}????}???? Return BS} // code in common/configtx/tool/Provisional. Go

Func (BSBootstrapper) genesisblockforchannel (channelid string)The CB. Block Code is as follows:

Func (BS * bootstrapper) genesisblockforchannel (channelid string) * CB. Block {???? Block, err: = Genesis. newfactoryimpl (???????? Configtx. newmodpolicysettingtemplate (???????????? Configvaluesmsp. adminspolicykey ,???????????? Configtx. newcompositetemplate (???????????????? Configtx. newsimpletemplate (BS. consortiumsgroups ...),???????????????? BS. channeltemplate (),????????????),????????),????). Block (channelid )???? Return block} // code in common/configtx/tool/Provisional. Go
5. configtxgen command 5.1, Main Function
Func main (){???? VaR outputblock, outputchannelcreatetx, profile, channelid, inspectblock, inspectchannelcreatetx, outputanchorpeersupdate, asorg string ???? //-Outputblock: the initial block is written to the specified file ???? Flag. stringvar (& outputblock, "outputblock", "", "The path to write the Genesis block to (if set )")???? //-Channelid: Specifies the channel name ???? Flag. stringvar (& channelid, "channelid", provisional. testchainid, "the channel ID to use in the configtx ")???? //-Outputcreatechanneltx: writes the channel creation transaction to the specified file ???? Flag. stringvar (& outputchannelcreatetx, "outputcreatechanneltx", "", "The path to write a channel creation configtx to (if set )")???? //-Profile: Specifies the profile ???? Flag. stringvar (& profile, "Profile", genesisconfig. sampleinsecureprofile, "the profile from configtx. yaml to use for generation .")???? //-Inspectblock: prints the configuration information of the specified block ???? Flag. stringvar (& inspectblock, "inspectblock", "", "prints the configuration contained in the block at the specified path ")???? //-Inspectchannelcreatetx: print the configuration update information in the transaction file created by the channel ???? Flag. stringvar (& inspectchannelcreatetx, "inspectchannelcreatetx", "", "prints the configuration contained in the transaction at the specified path ")???? //-Outputanchorpeersupdate: Specifies the-asorg ???? Flag. stringvar (& outputanchorpeersupdate, "outputanchorpeersupdate", "", "creates an config update to update an anchor peer (works only with the default channel creation, and only for the first update) ")???? //-Asorg: executes the Update Configuration transaction with the specified identity, such as updating the configuration information of the anchor node ???? Flag. stringvar (& asorg, "asorg", "", "performs the config generation as a participant organization (by name), only including values in the write set that org (likely) has privilege to set ")???? Flag. parse ()???? Factory. initfactories (NiL )???? Config: = genesisconfig. Load (profile) // read the specified configuration ???? If outputblock! = "" {// Generate the initial orderer Service Startup block ???????? Err: = dooutputblock (config, channelid, outputblock )????}???? If outputchannelcreatetx! = "" {// Generate the configuration transaction ???????? Err: = dooutputchannelcreatetx (config, channelid, outputchannelcreatetx )????}???? If outputanchorpeersupdate! = "" {// Generate the anchor node configuration Update file ???????? Err: = dooutputanchorpeersupdate (config, channelid, outputanchorpeersupdate, asorg )????}} // Code in common/configtx/tool/configtxgen/Main. Go
5.2. dooutputblock (generate the initial block for the orderer service to start, that is, the initial block file of the system channel)
Func dooutputblock (config * genesisconfig. Profile, channelid string, outputblock string) error {???? Pgen: = provisional. New (config) // build a generator instance ???? Genesisblock: = pgen. genesisblockforchannel (channelid) // generate the creation block ???? Err: = ioutil. writefile (outputblock, utils. marshalorpanic (genesisblock), 0644) // write the file into the creation block ???? Return nil} // code in common/configtx/tool/configtxgen/Main. Go

For more information about Genesis, see configtx (configuration transaction) # genesis (system channel creation block) in fabric 1.0 source code notes)

5.3. dooutputchannelcreatetx (generate configuration transactions for new application channels)
Func dooutputchannelcreatetx (CONF * genesisconfig. Profile, channelid string, outputchannelcreatetx string) error {???? VaR orgnames [] string ???? For _, org: = range Conf. application. Organizations {???????? Orgnames = append (orgnames, org. Name )????}???? Configtx, err: = configtx. makechaincreationtransaction (channelid, Conf. consortings, nil, orgnames ...)???? Err = ioutil. writefile (outputchannelcreatetx, utils. financialorpanic (configtx), 0644 )???? Return nil} // code in common/configtx/tool/configtxgen/Main. Go
5.4. dooutputanchorpeersupdate (generate the anchor node configuration Update file)
Func dooutputanchorpeersupdate (CONF * genesisconfig. Profile, channelid string, outputanchorpeersupdate string, asorg string) error {???? VaR Org * genesisconfig. Organization ???? For _, iorg: = range Conf. application. Organizations {???????? If iorg. Name = asorg {???????????? Org = iorg ????????}????}???? Anchorpeers: = make ([] * pb. anchorpeer, Len (Org. anchorpeers ))???? For I, anchorpeer: = range org. anchorpeers {???????? Anchorpeers [I] = & Pb. anchorpeer {???????????? HOST: anchorpeer. Host ,???????????? Port: int32 (anchorpeer. Port ),????????}????}???? Configgroup: = config. templateanchorpeers (Org. Name, anchorpeers )???? Configgroup. Groups [config. applicationgroupkey]. Groups [org. Name]. Values [config. anchorpeerskey]. modpolicy = mspconfig. adminspolicykey ???? Configupdate: = & CB. configupdate {???????? Channelid: channelid ,???????? Writeset: configgroup ,???????? Readset: CB. newconfiggroup (),????}???? Configupdate. readset. Groups [config. applicationgroupkey] = CB. newconfiggroup ()???? Configupdate. readset. Groups [config. applicationgroupkey]. Version = 1 ???? Configupdate. readset. Groups [config. applicationgroupkey]. modpolicy = mspconfig. adminspolicykey ???? Configupdate. readset. Groups [config. applicationgroupkey]. Groups [org. Name] = CB. newconfiggroup ()???? Configupdate. readset. Groups [config. applicationgroupkey]. Groups [org. Name]. Values [config. mspkey] = & CB. configvalue {}???? Configupdate. readset. Groups [config. applicationgroupkey]. Groups [org. Name]. Policies [mspconfig. readerspolicykey] = & CB. configpolicy {}???? Configupdate. readset. Groups [config. applicationgroupkey]. Groups [org. Name]. Policies [mspconfig. writerspolicykey] = & CB. configpolicy {}???? Configupdate. readset. Groups [config. applicationgroupkey]. Groups [org. Name]. Policies [mspconfig. adminspolicykey] = & CB. configpolicy {}???? Configupdate. writeset. Groups [config. applicationgroupkey]. Version = 1 ???? Configupdate. writeset. Groups [config. applicationgroupkey]. modpolicy = mspconfig. adminspolicykey ???? Configupdate. writeset. Groups [config. applicationgroupkey]. Groups [org. Name]. Version = 1 ???? Configupdate. writeset. Groups [config. applicationgroupkey]. Groups [org. Name]. modpolicy = mspconfig. adminspolicykey ???? Configupdate. writeset. Groups [config. applicationgroupkey]. Groups [org. Name]. Values [config. mspkey] = & CB. configvalue {}???? Configupdate. writeset. Groups [config. applicationgroupkey]. Groups [org. Name]. Policies [mspconfig. readerspolicykey] = & CB. configpolicy {}???? Configupdate. writeset. Groups [config. applicationgroupkey]. Groups [org. Name]. Policies [mspconfig. writerspolicykey] = & CB. configpolicy {}???? Configupdate. writeset. Groups [config. applicationgroupkey]. Groups [org. Name]. Policies [mspconfig. adminspolicykey] = & CB. configpolicy {}???? Configupdateenvelope: = & CB. configupdateenvelope {???????? Configupdate: utils. financialorpanic (configupdate ),????}???? Update: = & CB. Envelope {???????? Payload: utils. financialorpanic (& CB. Payload {???????????? Header: & CB. header {???????????????? Channelheader: utils. financialorpanic (& CB. channelheader {???????????????????? Channelid: channelid ,???????????????????? Type: int32 (CB. headertype_config_update ),????????????????}),????????????},???? ???????? Data: utils. financialorpanic (configupdateenvelope ),????????}),????}???? Err: = ioutil. writefile (outputanchorpeersupdate, utils. financialorpanic (update), 0644 )???? Return nil} // code in common/configtx/tool/configtxgen/Main. Go

Welcome to continue watching brothersBlockchain tutorialShare!

Blockchain tutorial fabric1.0 Source Code Analysis configuration transaction-generate Channel Configuration 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.