카테고리 없음

hyperledger-fabric

정원에서 2018. 1. 18. 10:28

http://hyperledger-fabric.readthedocs.io/en/release/prereqs.html

Prerequisites

Install cURL

sudo apt-get install curl

Docker and Docker Compose

remove existing component

sudo apt-get remove docker docker-engine docker.io


install required

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

add repo

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"

repo update

sudo apt-get update

install docker, new version ce

sudo apt-get install docker-ce

test

sudo docker run hello-world

verify version(Docker version 17.12.0-ce, build c97c6d6)

docker --version

verify  Docker Compose version (1.8 or greater installed)

docker-compose --version

Go Programming Language(Go programming language 1.7.x for many of its components.)

set environment variable

vi ~/.bashrc

export GOPATH=$HOME/go

export GOROOT=/usr/local/go

export PATH=$PATH:$GOPATH/bin:$GOROOT/bin


download

wget https://storage.googleapis.com/golang/go1.9.2.linux-amd64.tar.gz

extract

sudo tar -xvf go1.9.2.linux-amd64.tar.gz

sudo mv go /usr/local

Node.js Runtime and NPM(need to have version 6.9.x of Node.js installed.)

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

sudo apt-get install -y nodejs

if want to install nodejs7

curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

sudo apt-get install -y nodejs

option build tool

sudo apt-get install -y build-essential

upgrade the npm tool

sudo npm install npm@3.10.10 -g

Getting Started

Install Prerequisites ....OK!!

Install Binaries and Docker Images

Download Platform-specific Binaries(일반계정으로는 permission error 발생)

su -

curl -sSL https://goo.gl/byy2Qj | bash -s 1.0.5

Hyperledger Fabric Samples

Tutorials

Writing Your First Application

Building Your First Network

Chaincode for Developer

Chaincode for Operators.


Chaincode for Developers

mkdir -p $GOPATH/src/sacc && cd $GOPATH/src/sacc

touch sacc.go

vi sacc.go

package main


import (

"fmt"


"github.com/hyperledger/fabric/core/chaincode/shim"

"github.com/hyperledger/fabric/protos/peer"

)


// SimpleAsset implements a simple chaincode to manage an asset

type SimpleAsset struct {

}


// Init is called during chaincode instantiation to initialize any

// data. Note that chaincode upgrade also calls this function to reset

// or to migrate data.

func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response {

// Get the args from the transaction proposal

args := stub.GetStringArgs()

if len(args) != 2 {

return shim.Error("Incorrect arguments. Expecting a key and a value")

}


// Set up any variables or assets here by calling stub.PutState()


// We store the key and the value on the ledger

err := stub.PutState(args[0], []byte(args[1]))

if err != nil {

return shim.Error(fmt.Sprintf("Failed to create asset: %s", args[0]))

}

return shim.Success(nil)

}


// Invoke is called per transaction on the chaincode. Each transaction is

// either a 'get' or a 'set' on the asset created by Init function. The Set

// method may create a new asset by specifying a new key-value pair.

func (t *SimpleAsset) Invoke(stub shim.ChaincodeStubInterface) peer.Response {

// Extract the function and args from the transaction proposal

fn, args := stub.GetFunctionAndParameters()


var result string

var err error

if fn == "set" {

result, err = set(stub, args)

} else { // assume 'get' even if fn is nil

result, err = get(stub, args)

}

if err != nil {

return shim.Error(err.Error())

}


// Return the result as success payload

return shim.Success([]byte(result))

}


// Set stores the asset (both key and value) on the ledger. If the key exists,

// it will override the value with the new one

func set(stub shim.ChaincodeStubInterface, args []string) (string, error) {

if len(args) != 2 {

return "", fmt.Errorf("Incorrect arguments. Expecting a key and a value")

}


err := stub.PutState(args[0], []byte(args[1]))

if err != nil {

return "", fmt.Errorf("Failed to set asset: %s", args[0])

}

return args[1], nil

}


// Get returns the value of the specified asset key

func get(stub shim.ChaincodeStubInterface, args []string) (string, error) {

if len(args) != 1 {

return "", fmt.Errorf("Incorrect arguments. Expecting a key")

}


value, err := stub.GetState(args[0])

if err != nil {

return "", fmt.Errorf("Failed to get asset: %s with error: %s", args[0], err)

}

if value == nil {

return "", fmt.Errorf("Asset not found: %s", args[0])

}

return string(value), nil

}


// main function starts up the chaincode in the container during instantiate

func main() {

if err := shim.Start(new(SimpleAsset)); err != nil {

fmt.Printf("Error starting SimpleAsset chaincode: %s", err)

}

}

go get -u --tags nopkcs11 github.com/hyperledger/fabric/core/chaincode/shim

go build --tags nopkcs11

cd /root/fabric-samples/chaincode-docker-devmode

Terminal 1 - Start the network

docker-compose -f docker-compose-simple.yaml up

Terminal 2 - Build & start the chaincode

docker exec -it chaincode bash

cd sacc

go build

CORE_PEER_ADDRESS=peer:7051 CORE_CHAINCODE_ID_NAME=mycc:0 ./sacc

Terminal 3 - Use the chaincode

docker exec -it cli bash

peer chaincode install -p chaincodedev/chaincode/sacc -n mycc -v 0

peer chaincode instantiate -n mycc -v 0 -c '{"Args":["a","10"]}' -C myc

peer chaincode invoke -n mycc -c '{"Args":["set", "a", "20"]}' -C myc

peer chaincode query -n mycc -c '{"Args":["query","a"]}' -C myc