Skip to content

Commit

Permalink
Merge pull request #19 from Akachain/merge-external-chaincode-and-fab…
Browse files Browse the repository at this point in the history
…ric-2.0

Merge external chaincode and fabric 2.0
  • Loading branch information
harisato authored Aug 31, 2020
2 parents 00b6af0 + e22e4c9 commit 01433e9
Show file tree
Hide file tree
Showing 141 changed files with 3,216 additions and 866 deletions.
110 changes: 110 additions & 0 deletions EXTERNAL_CHAINCODE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
## 1. BOOTSTRAP NETWORK
You should start network by command: ```mamba start```
## 2. Packaging chaincode
### a. Using AKC-Admin
- Deploy akc-admin in repo: https://github.com/Akachain/akc-admin
- Call api package external chaincode:
```
curl --location --request POST http://localhost:4001/api/v2/chaincodes/packageExternalCC \
--header 'content-type: application/json' \
--data-raw '{
"orgname":"akc",
"chaincodeName":"fabcar"
}'
```
### b. Manual
- You should prepare files bellow:
- ```connection.json```: Connection to the external chaincode service:
```
{
"address": "chaincode-fabcar-org1.akc:7052",
"dial_timeout": "10s",
"tls_required": false,
"client_auth_required": false,
"client_key": "-----BEGIN EC PRIVATE KEY----- ... -----END EC PRIVATE KEY-----",
"client_cert": "-----BEGIN CERTIFICATE----- ... -----END CERTIFICATE-----",
"root_cert": "-----BEGIN CERTIFICATE---- ... -----END CERTIFICATE-----"
}
```
- ```metadata.json```: Includes information of chaincode
```
{"path":"","type":"external","label":"fabcar"}
```
- Package: With the Fabric v2.0 chaincode lifecycle, chaincode is packaged and installed in a .tar.gz format.
```
tar cfz code.tar.gz connection.json
tar cfz fabcar.tgz code.tar.gz metadata.json
```

## 3. Building and deploying the External Chaincode
### a. Writing chaincode to run as an external service
- Write chaincode: See detail in [Hyperledger Fabric](https://hyperledger-fabric.readthedocs.io/en/release-2.2/cc_service.html#writing-chaincode-to-run-as-an-external-service)
- Build chaincode: Using docker to build chaincode to a image. Image tag used in ```Deploy chaincode``` step.
### b. Deploying the chaincode using AKC-Admin
- Install package:
```
curl --location --request POST http://localhost:4001/api/v2/chaincodes/install \
--header 'content-type: application/json' \
--data-raw '{
"orgname":"akc",
"chaincodeName":"fabcar",
"chaincodePath":"fabcar.tgz",
"peerIndex": "0"
}'
```
- Query Package ID:
```
curl --location --request POST http://localhost:4001/api/v2/chaincodes/queryInstalled \
--header 'content-type: application/json' \
--data-raw '{
"orgname":"akc",
"peerIndex": "0"
}'
```
- Use package ID in previous step to deploy chaincode as stateful set in kubectl by using command bellow:
```
mamba externalcc deploy --ccname fabcar --ccimage "$IMAGE_TAG" --packageid "$PACKAGE_ID"
```
- Approve the chaincode and commit it to the channel
```
curl --location --request POST http://localhost:4001/api/v2/chaincodes/approveForMyOrg \
--header 'content-type: application/json' \
--data-raw '{
"orgname":"akc",
"peerIndex": "0",
"chaincodeName": "fabcar",
"chaincodeVersion": 1,
"channelName": "akcchannel",
"packageId": "fabcar:64abc178ac22334e3c30a42af36d688e83cbe9eb428a018a2def426ec3cfd5ea",
"ordererAddress": "orderer0-orderer.akc:7050",
"initRequired": 0
}'
curl --location --request POST http://localhost:4001/api/v2/chaincodes/commitChaincodeDefinition \
--header 'content-type: application/json' \
--data-raw '{
"chaincodeName": "fabcar",
"chaincodeVersion": 1,
"channelName": "akctestchannel",
"target": "0 akc",
"ordererAddress": "orderer0-orderer.akc:7050",
"initRequired": 0
}'
```

- Invoke the chaincode
```
curl --location --request POST http://localhost:4001/api/v2/chaincodes/invokeCLI \
--header 'content-type: application/json' \
--data-raw '{
"chaincodeName": "fabcar",
"channelName": "akcchannel",
"target": "0 akc",
"ordererAddress": "orderer0-orderer.akc:7050",
"isInit": "0"
}'
- Query the chaincode
```
peer chaincode query -C akcchannel -n fabcar -c '{"Args":["queryAllCars"]}'
```
22 changes: 11 additions & 11 deletions REMOTE_ORG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
1. Start network
## 1. Start network
```bash
mamba start
```
2. Prepare merchant config in '/home/hainq/.akachain/akc-mamba/mamba/config/merchant.env'
## 2. Prepare merchant config in '/home/hainq/.akachain/akc-mamba/mamba/config/merchant.env'
- In merchant env, must fill:
- EXTERNAL_ORDERER_ADDRESSES, EXTERNAL_RCA_ADDRESSES
- ENDORSEMENT_ORG_NAME, ENDORSEMENT_ORG_ADDRESS, ENDORSEMENT_ORG_TLSCERT
Expand All @@ -15,29 +15,29 @@ cp /home/hainq/.akachain/akc-mamba/mamba/config/merchant.env /home/hainq/.akacha
```
mamba copyscripts
```
3. Copy signed cert of root ca to merchant cluster
## 3. Copy signed cert of root ca to merchant cluster
```
kubectl exec -it test-efs-7759545f7-b5ffw bash
cp /tmp/artifact/akc-network/akc-ca-data/rca-akc-cert.pem /tmp/artifact/merchant-network/akc-ca-data/
```

4. Create new org in new cluster
## 4. Create new org in new cluster
```
mamba create-org
```
-> Automation generate to merchant.json
4. Copy merchant.json to operator cluster
## 4. Copy merchant.json to operator cluster
```
cp /tmp/artifact/merchant-network/akc-ca-data/merchant.json /tmp/artifact/akc-network/akc-ca-data/
```
5. In operator cluster, add merchant to channel
## 5. In operator cluster, add merchant to channel
Must specify env: NEW_ORG_NAME="merchant" in operator.env file
```
cp /home/hainq/.akachain/akc-mamba/mamba/config/operator.env /home/hainq/.akachain/akc-mamba/mamba/config/.env
mamba channel-config auto-update
```

6. Install chaincode test
## 6. Install chaincode test
```
curl -s -X POST http://admin-rca-ica.akc:4001/chaincodes -H "content-type: application/json" -d '{
"orgname":"akc",
Expand All @@ -48,13 +48,13 @@ curl -s -X POST http://admin-rca-ica.akc:4001/chaincodes -H "content-type: a
}'
```

7. Copy signed cert of orderer and akc org to merchant cluster
## 7. Copy signed cert of orderer and akc org to merchant cluster
```
cp /tmp/artifact/akc-network/akc-ca-data/ica-orderer-ca-chain.pem /tmp/artifact/merchant-network/akc-ca-data/
cp /tmp/artifact/akc-network/akc-ca-data/ica-akc-ca-chain.pem /tmp/artifact/merchant-network/akc-ca-data/
```

8. Join merchant to channel
## 8. Join merchant to channel
```
curl -s -X POST http://admin-rca-ica.default:4001/registerUser -H "content-type: application/json" -d '{
"orgname":"merchant"
Expand All @@ -72,7 +72,7 @@ curl -s -X POST http://admin-rca-ica.default:4001/chaincodes -H "content-typ
}'
```

9. Init or upgrade chaincode on operator cluster
## 9. Init or upgrade chaincode on operator cluster
- Init
```
curl -s -X POST http://admin-rca-ica.akc:4001/initchaincodes -H "content-type: application/json" -d '{
Expand All @@ -96,7 +96,7 @@ curl -s -X POST http://admin-rca-ica.ordererhai:4001/upgradeChainCode -H "co
}'
```

10. Try invoke chaincode on merchant cluster:
## 10. Try invoke chaincode on merchant cluster:
```
curl -s -X POST http://admin-rca-ica.default:4001/invokeChainCode -H "content-type: application/json" -d '{
"orgname":"merchant",
Expand Down
31 changes: 12 additions & 19 deletions mamba/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,37 @@

## 1. Installation Instructions

### a. Edit configuration files
First, copy the content of `config/operator.env-template` to be `config/.env`. This file will contain all modifiable configuration of Mamba.
### a. Install AKC-Mamba
Install AKC-Mamba with

```
cp ./config/operator.env-template ./config/.env
```

Update configuration parameters in `config/.env`, the file content is pretty much self-explained.

### b. Install required packages
Install required Python3 modules with

```
pip3 install -r requirements.txt
pip3 install akc-mamba
```

We now can use the Mamba tool to prepare required helm and k8s components

```
find . -type f -iname "*.sh" -exec chmod +x {} \;
python3 mamba.py environment
mamba environment
```
Configuration parameters in `~/.akachain/akc-mamba/mamba/config/.env`, the file content is pretty much self-explained.

### c. Deploy and bootstrap network
### b. Deploy and bootstrap network

```
python3 mamba.py start
mamba start
```

The `mamba start` command executes a series of sub commands that installs various network components. For more information on each command for individual components, please refer to help section

```
python3 mamba.py help
mamba --help
```

To terminate the network, just run

```
python3 mamba.py terminate
mamba terminate
```

## 2. Development Guide
Expand All @@ -59,13 +51,14 @@ Mamba makes use of [Click_](http://click.palletsprojects.com/en/7.x/), an elegan
│ ├── kube.py
├── mamba.py
├── settings.py
├── settings
├ ├──settings.py
```

There are 4 main components:
- mamba.py : The bootstrap instance module of Mamba
- settings.py : Contains global variables that are shared accross all sub modules
- settings/settings.py : Contains global variables that are shared accross all sub modules
- command_group : Each command group is separated into its own directory.
- utils : helper functions that must be initialized via settings.py

Expand Down
60 changes: 38 additions & 22 deletions mamba/blockchain/admin/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@
from utils import hiss, util
from settings import settings

def setup_admin():
def setup_admin(org):

print('settings.ORDERER_DOMAINS: ', settings.ORDERER_DOMAINS)
domains = settings.ORDERER_DOMAINS.split(' ')
if len(domains) == 0:
domains = settings.PEER_DOMAINS.split(' ')
# Get domain
domain = util.get_domain(org)

# Get orderer information
orderer_names = settings.ORDERER_ORGS.split(' ')
orderer_domains = settings.ORDERER_DOMAINS.split(' ')

# Create application artifact folder
hiss.echo('Create application artifact folder')
hiss.echo('Create wallet folder')
## Find efs pod
pods = settings.k8s.find_pod(namespace="default", keyword="test-efs")
if not pods:
return hiss.hiss('cannot find tiller pod')

mkdir_cmd = ('mkdir -p '+settings.EFS_ROOT+'/admin/crypto-path;'
'mkdir -p '+settings.EFS_ROOT+'/admin/crypto-store;')
mkdir_cmd = ('mkdir -p '+settings.EFS_ROOT+'/admin-v2/wallet;')

## Exec command
# Exec command
exec_command = [
'/bin/bash',
'-c',
Expand All @@ -34,9 +35,12 @@ def setup_admin():
hiss.sub_echo(result_get_folder.data)

# Create temp folder & namespace
settings.k8s.prereqs(domains[0])
settings.k8s.prereqs(domain)
dict_env = {
'ORDERER_DOMAIN': domains[0],
'ORG_NAME': org,
'ORG_DOMAIN': domain,
'ORDERER_DOMAIN': orderer_domains[0],
'ORGDERER_NAME': orderer_names[0],
'EFS_SERVER': settings.EFS_SERVER,
'EFS_PATH': settings.EFS_PATH,
'EFS_EXTEND': settings.EFS_EXTEND,
Expand All @@ -46,21 +50,33 @@ def setup_admin():
# Apply deployment
k8s_template_file = '%s/admin/admin-deployment.yaml' % util.get_k8s_template_path()
settings.k8s.apply_yaml_from_template(
namespace=domains[0], k8s_template_file=k8s_template_file, dict_env=dict_env)
namespace=domain, k8s_template_file=k8s_template_file, dict_env=dict_env)

# Apply service
k8s_template_file = '%s/admin/admin-service.yaml' % util.get_k8s_template_path()
settings.k8s.apply_yaml_from_template(
namespace=domains[0], k8s_template_file=k8s_template_file, dict_env=dict_env)
namespace=domain, k8s_template_file=k8s_template_file, dict_env=dict_env)

def del_admin():
def del_admin(org):

domains = settings.ORDERER_DOMAINS.split(' ')
jobname = 'admin-rca-ica'
# Get domain
domain = util.get_domain(org)
name = 'admin-v2-%s' % org

# Delete job pod
return settings.k8s.delete_stateful(name=jobname, namespace=domains[0])

return settings.k8s.delete_stateful(name=name, namespace=domain)

def setup_all_admin():
orgs = settings.PEER_ORGS.split(' ')
# TODO: Multiprocess
for org in orgs:
setup_admin(org)

def delete_all_admin():
orgs = settings.PEER_ORGS.split(' ')
# TODO: Multiprocess
for org in orgs:
del_admin(org)

@click.group()
def admin():
Expand All @@ -69,12 +85,12 @@ def admin():

@admin.command('setup', short_help='Create new a new Admin service')
def setup():
hiss.rattle('Create new a new Admin service')
hiss.rattle('Create new Admin service in all org')

setup_admin()
setup_all_admin()

@admin.command('delete', short_help='Delete the Admin service')
def delete():
hiss.rattle('Delete the Admin service')
hiss.rattle('Delete the Admin service in all org')

del_admin()
delete_all_admin()
Loading

0 comments on commit 01433e9

Please sign in to comment.