温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么进行区块链中的fabric chaincode分析

发布时间:2021-12-27 14:46:03 阅读:158 作者:柒染 栏目:云计算
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇文章为大家展示了怎么进行区块链中的fabric chaincode分析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

chaincode是所有区块链项目的核心,无论是公链还是私链。

fabric 如何形成chaincode, 下图是fabric chaincode 的核心模块组成

怎么进行区块链中的fabric chaincode分析

GenerateDockerfile 是生成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
}

构建镜像函数

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)}创建容器并上传
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}

上述内容就是怎么进行区块链中的fabric chaincode分析,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://my.oschina.net/u/2896230/blog/4574051

AI

开发者交流群×