Skip to content

Instantly share code, notes, and snippets.

@jcbombardelli
Created June 18, 2019 10:45
Show Gist options
  • Save jcbombardelli/f06082a0f983e1d5713617ffadc8c0fb to your computer and use it in GitHub Desktop.
Save jcbombardelli/f06082a0f983e1d5713617ffadc8c0fb to your computer and use it in GitHub Desktop.
Conteúdo Compartilhado do Curso de Hyperledger Prodesp 2019 06 10
Blockchain Academy
Curso Hyperledger - Prodesp
https://pad.riseup.net/p/bahyperledger201906
i3tech --> senha da VM
Abrir Terminal
mkdir ~/Hyperledger
cd ~/Hyperldeger
git clone https://github.com/jcbombardelli/hyperledger-tuna-composer.git
Online Tools - Hash
https://www.tools4noobs.com/online_tools/hash/
Curso Blockchain Prodesp
00c436b7ab7251351af1fe4bbe46cb5c6f0a2aec9e09a7ee7b167b72c77f1940
Curso Blockchain Prodesp 2019
53dac7c08115c26cc8f1c942b44f69fca0528ed7548572fa91719075f3207cbc
proxy01.prodesp.sp.gov.br:80
Acesso a VM
https://1drv.ms/u/s!Am25_nxruoBXt7Mwzvg3Wu79IBd43Q?e=7rZd3a
Playground Online
https://composer-playground.mybluemix.net/
Criando participante
abstract participant Individual identified by id {
o String id
o String name
o Address address
}
concept Address {
o String addressLine
o String locality
o String postCode regex=/\d{4}[ ]?[A-Z]{2}/
}
participant Fisher extends Individual {
o String licenseNumber
}
participant RestaurantOwner extends Individual {
o String restaurantName
}
participant Regulator identified by id {
o String id
o String name
}
enum FishStatus {
o CAUGHT
o PURCHASED
}
asset Tuna identified by tunaId {
o String tunaId
o Integer weight range=[500, 1000000]
o FishStatus status default="CAUGHT"
o DateTime catchTime
--> Individual owner
}
transaction SellTuna {
--> Tuna tuna
--> RestaurantOwner restaurantOwner
}
event TunaSale {
o String tunaId
o String restaurantName
}
permissions.acl
rule OnlyOwnerCanTransferTuna {
description: "Allow only Tuna owners to transfer the fish"
participant(p): "org.tuna.*"
operation: CREATE
resource(r): "org.tuna.SellTuna"
condition: (r.tuna.owner.getIdentifier() != p.getIdentifier())
action: DENY
}
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH
source ~/.profile
npm install -g nodemon
sudo -i
echo "Acquire::http::Proxy \"http://proxy01.prodesp.sp.gov.br:80/\";" > /etc/apt/apt.conf.d/proxy.conf
echo "Acquire::https::Proxy \"http://proxy01.prodesp.sp.gov.br:80/\";" >> /etc/apt/apt.conf.d/proxy.conf
exit
sudo apt update
sudo apt install build-essential
# npm install -g yo@latest
Em: Hyperledger/hyperledger-tuna-composer/infrastructure, execute
./00-bootstrap.sh
Antes do próximo passo, configure o proxy para realizar acesso do servidores Docker
# Fonte: https://www.serverlab.ca/tutorials/containers/docker/how-to-set-the-proxy-for-docker-on-ubuntu/
sudo mkdir /etc/systemd/system/docker.service.d
sudo vi /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://proxy01.prodesp.sp.gov.br:80/"
Environment="HTTPS_PROXY=http://proxy01.prodesp.sp.gov.br:80/"
Environment="NO_PROXY=timon.cloud.prodesp.sp.gov.br,nexus.prodesp.sp.gov.br,10.199.101.210:8180"
sudo systemctl daemon-reload
sudo systemctl restart docker.service
Em seguida, execute o script ./01-envoriment.sh
----Voltando ao código
rule GrantReadOnlyAccessRegulator {
description: "Grant Read permissions to Regulator"
participant: "org.tuna.Regulator"
operation: CREATE, UPDATE, DELETE
resource: "**"
action: DENY
}
Logic.js
'use strict';
/**
* Defining the namespace for the business network
*/
const NS = 'org.tuna';
/**
* Transfer tuna from one owner to another
* @param {org.tuna.SellTuna} tx - The transferTuna transaction
* @transaction
*/
async function sellTuna(tx) {
// Get asset registry for Tuna
const tunaRegistry = await getAssetRegistry(NS + '.Tuna');
// Get participant registry for Individuals
const restaurantOwnerRegistry = await getParticipantRegistry(NS + '.RestaurantOwner');
const tuna = await tunaRegistry.get(tx.tuna.getIdentifier());
if (!tuna) {
throw new Error(`Tuna with id ${tx.tuna.getIdentifier()} does not exist`);
}
// Make sure the tuna status is CAUGHT and not PURCHASED
if (tuna.status !== 'CAUGHT') {
throw new Error(`Tuna with id ${tx.tuna.getIdentifier()} is not in CAUGHT status`);
}
const restaurantOwnerId = tx.restaurantOwner.getIdentifier();
// Make sure that restaurantOwner exists
const restaurantOwner = await restaurantOwnerRegistry.get(restaurantOwnerId);
if (!restaurantOwner) {
throw new Error(`RestaurantOwner with id ${restaurantOwnerId} does not exist`);
}
tx.tuna.owner = tx.restaurantOwner;
tx.tuna.status = 'PURCHASED';
await tunaRegistry.update(tx.tuna);
let tunaSaleEvent = getFactory().newEvent(NS, 'TunaSale');
tunaSaleEvent.tunaId = tx.tuna.tunaId;
tunaSaleEvent.restaurantName = tx.restaurantOwner.restaurantName;
// Emit the Event
emit(tunaSaleEvent);
}
Query File
query getTunaByParticipant {
description: "List tuna owned by specific 'owner'"
statement:
SELECT org.tuna.Tuna
WHERE (owner == _$owner)
ORDER BY [catchTime ASC]
}
rule Default {
description: "Grant all access by default"
participant: "org.hyperledger.composer.system.Participant"
operation: ALL
resource: "**"
action: ALLOW
}
(obs: incluir no .profile para não perder quando rebootar a maquina)
export PATH=~/.npm-global/bin:$PATH
Em infrastructure: execute "./02-start.sh"
Em "hyperledger-composer-tuna" vá em "composer
cd ../composer
composer archive create -t dir -n . -a mytuna@0.0.1.bna
composer network install --card PeerAdmin@hlfv1 --archiveFile mytuna@0.0.1.bna
#Inicializar o chaincode no Hyperledger Fabric peers
composer network start --card PeerAdmin@hlfv1 --networkAdmin admin --networkAdminEnrollSecret adminpw --networkName tuna-network --networkVersion 0.0.1
#Isto vai criar um card, que deve ser importado:
composer card import --file admin@tuna-network.card
#Este card é utilizado no projeto / business network.
#Para testa-lo:
composer network ping --card admin@tuna-network
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment