Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to do fabric chaincode Analysis in Block chain

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you how to carry out fabric chaincode analysis in the blockchain, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Chaincode is the core of all blockchain projects, whether public or private.

How does fabric form chaincode? the following is the core module composition of fabric chaincode.

GenerateDockerfile is the function that generates chaincode dockerfile

Func (carPlatform Platform) GenerateDockerfile (cds pb.ChaincodeDeploymentSpec) (string, error) {

Var buf [] string

/ / let the executable's name be chaincode ID's name

Buf = append (buf, "FROM" + cutil.GetDockerfileFromConfig ("chaincode.car.runtime"))

Buf = append (buf, "ADD binpackage.tar / usr/local/bin")

DockerFileContents: = strings.Join (buf, "\ n")

Return dockerFileContents, nil

}

Build a mirror function

Func (carPlatform * Platform) GenerateDockerBuild (cds * pb.ChaincodeDeploymentSpec, tw * tar.Writer) error {

/ / Bundle the .car file into a tar stream so it may be transferred to the builder container

Codepackage, output: = io.Pipe ()

Go func () {

Tw: = tar.NewWriter (output)

Err: = cutil.WriteBytesToPackage ("codepackage.car", cds.CodePackage, tw)

Tw.Close ()

Output.CloseWithError (err)

} ()

Binpackage: = bytes.NewBuffer (nil)

Err: = util.DockerBuild (util.DockerBuildOptions {

Cmd: "java-jar / usr/local/bin/chaintool buildcar / chaincode/input/codepackage.car-o / chaincode/output/chaincode"

InputStream: codepackage

OutputStream: binpackage

})

If err! = nil {

Return fmt.Errorf ("Error building CAR:% s", err)

}

Return cutil.WriteBytesToPackage ("binpackage.tar", binpackage.Bytes (), tw)

}

Create a container and upload

Func DockerBuild (opts DockerBuildOptions) error {

Client, err: = cutil.NewDockerClient ()

If err! = nil {

Return fmt.Errorf ("Error creating docker client:% s", err)

}

If opts.Image = "" {

Opts.Image = cutil.GetDockerfileFromConfig ("chaincode.builder")

If opts.Image = "" {

Return fmt.Errorf ("No image provided and\" chaincode.builder\ "default does not exist")

}

}

Logger.Debugf ("Attempting build with image% s", opts.Image)

/ /-

/ / Ensure the image exists locally, or pull it from a registry if it doesn't

/ /-

_, err = client.InspectImage (opts.Image)

If err! = nil {

Logger.Debugf ("Image% s does not exist locally, attempt pull", opts.Image)

Err = client.PullImage (docker.PullImageOptions {Repository: opts.Image}, docker.AuthConfiguration {})

If err! = nil {

Return fmt.Errorf ("Failed to pull% s:% s", opts.Image, err)

}

}

/ /-

/ / Create an ephemeral container, armed with our Env/Cmd

/ /-

Container, err: = client.CreateContainer (docker.CreateContainerOptions {

Config: & docker.Config {

Image: opts.Image

Env: opts.Env

Cmd: [] string {"/ bin/sh", "- c", opts.Cmd}

AttachStdout: true

AttachStderr: true

}

})

If err! = nil {

Return fmt.Errorf ("Error creating container:% s", err)

}

Defer client.RemoveContainer (docker.RemoveContainerOptions {ID: container.ID})

/ /-

/ / Upload our input stream

/ /-

Err = client.UploadToContainer (container.ID, docker.UploadToContainerOptions {

Path: "/ chaincode/input"

InputStream: opts.InputStream

})

If err! = nil {

Return fmt.Errorf ("Error uploading input to container:% s", err)

}

/ /-

/ / Attach stdout buffer to capture possible compilation errors

/ /-

Stdout: = bytes.NewBuffer (nil)

_, err = client.AttachToContainerNonBlocking (docker.AttachToContainerOptions {

Container: container.ID

OutputStream: stdout

ErrorStream: stdout

Logs: true

Stdout: true

Stderr: true

Stream: true

})

If err! = nil {

Return fmt.Errorf ("Error attaching to container:% s", err)

}

/ /-

/ / Launch the actual build, realizing the Env/Cmd specified at container creation

/ /-

Err = client.StartContainer (container.ID, nil)

If err! = nil {

Return fmt.Errorf ("Error executing build:% s\"% s\ ", err, stdout.String ())

}

/ /-

/ / Wait for the build to complete and gather the return value

/ /-

Retval, err: = client.WaitContainer (container.ID)

If err! = nil {

Return fmt.Errorf ("Error waiting for container to complete:% s", err)

}

If retval > 0 {

Return fmt.Errorf ("Error returned from build:% d\"% s\ ", retval, stdout.String ())

}

/ /-

/ / Finally, download the result

/ /-

Err = client.DownloadFromContainer (container.ID, docker.DownloadFromContainerOptions {

Path: "/ chaincode/output/."

OutputStream: opts.OutputStream

})

If err! = nil {

Return fmt.Errorf ("Error downloading output:% s", err)

}

Return nil

} the above is how to carry out the fabric chaincode analysis in the blockchain. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report