Skip to content

Commit

Permalink
Update HLF2.5.9 channel creation
Browse files Browse the repository at this point in the history
Signed-off-by: YoungHypo <haiboyang@smail.nju.edu.cn>
  • Loading branch information
YoungHypo committed Sep 19, 2024
1 parent 90f8f16 commit 65b467e
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 134 deletions.
1 change: 0 additions & 1 deletion build_image/docker/cello-hlf/scripts/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ fi
# Read each file from env and store under the ${cfg_path}
for name in HLF_NODE_MSP \
HLF_NODE_TLS \
HLF_NODE_BOOTSTRAP_BLOCK \
HLF_NODE_PEER_CONFIG \
HLF_NODE_ORDERER_CONFIG
do
Expand Down
14 changes: 10 additions & 4 deletions src/agent/docker-rest-agent/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def create_node():
env = {
'HLF_NODE_MSP': request.form.get('msp'),
'HLF_NODE_TLS':request.form.get('tls'),
'HLF_NODE_BOOTSTRAP_BLOCK':request.form.get('bootstrap_block'),
# 'HLF_NODE_BOOTSTRAP_BLOCK':request.form.get('bootstrap_block'),
'HLF_NODE_PEER_CONFIG':request.form.get('peer_config_file'),
'HLF_NODE_ORDERER_CONFIG':request.form.get('orderer_config_file'),
'platform': 'linux/amd64',
Expand Down Expand Up @@ -72,16 +72,22 @@ def create_node():
'FABRIC_LOGGING_SPEC':'DEBUG',
'ORDERER_GENERAL_LISTENADDRESS': '0.0.0.0',
'ORDERER_GENERAL_LISTENPORT': '7050',
'ORDERER_GENERAL_GENESISMETHOD':'file',
'ORDERER_GENERAL_LOCALMSPDIR': '/etc/hyperledger/fabric/msp',
'ORDERER_GENERAL_GENESISFILE': '/etc/hyperledger/fabric/genesis.block',
'ORDERER_GENERAL_TLS_ENABLED': 'true',
'ORDERER_GENERAL_TLS_PRIVATEKEY':'/etc/hyperledger/fabric/tls/server.key',
'ORDERER_GENERAL_TLS_CERTIFICATE':'/etc/hyperledger/fabric/tls/server.crt',
'ORDERER_GENERAL_TLS_ROOTCAS': '[/etc/hyperledger/fabric/tls/ca.crt]',
'ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE': '/etc/hyperledger/fabric/tls/server.crt',
'ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY': '/etc/hyperledger/fabric/tls/server.key',
'ORDERER_GENERAL_CLUSTER_ROOTCAS': '[/etc/hyperledger/fabric/tls/ca.crt]',
'ORDERER_GENERAL_LOCALMSPDIR': '/etc/hyperledger/fabric/msp',
'ORDERER_GENERAL_LOCALMSPID': 'OrdererMSP',

"ORDERER_ADMIN_LISTENADDRESS": "0.0.0.0:7053",
"ORDERER_ADMIN_TLS_ENABLED": "true",
"ORDERER_ADMIN_TLS_CERTIFICATE": "/etc/hyperledger/fabric/tls/server.crt",
"ORDERER_ADMIN_TLS_PRIVATEKEY": "/etc/hyperledger/fabric/tls/server.key",
"ORDERER_ADMIN_TLS_CLIENTROOTCAS": "[/etc/hyperledger/fabric/tls/ca.crt]",
"ORDERER_ADMIN_TLS_CLIENTAUTHREQUIRED": "true"
}
env.update(order_envs)
try:
Expand Down
4 changes: 2 additions & 2 deletions src/api-engine/api/lib/agent/docker/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def create(self, info):
data = {
'msp': info.get("msp")[2:-1],
'tls': info.get("tls")[2:-1],
'bootstrap_block': info.get("bootstrap_block")[2:-1],
# 'bootstrap_block': info.get("bootstrap_block")[2:-1],
'peer_config_file': info.get("config_file")[2:-1],
'orderer_config_file': info.get("config_file")[2:-1],
'img': 'yeasy/hyperledger-fabric:2.2.0',
'img': 'hyperledger/fabric:2.5.9',
'cmd': 'bash /tmp/init.sh "peer node start"' if info.get("type") == "peer" else 'bash /tmp/init.sh "orderer"',
'name': info.get("name"),
'type': info.get("type"),
Expand Down
50 changes: 19 additions & 31 deletions src/api-engine/api/lib/configtxgen/configtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,9 @@ def __init__(self, network, filepath=CELLO_HOME, orderer=None, raft_option=None,
self.filepath = filepath
self.network = network
self.template = load_configtx(template_path)
# self.orderer = {'BatchTimeout': '2s',
# 'OrdererType': "etcdraft",
# 'BatchSize': {'AbsoluteMaxBytes': '98 MB',
# 'MaxMessageCount': 2000,
# 'PreferredMaxBytes': '10 MB'}} if not orderer else orderer
# self.raft_option = {'TickInterval': "600ms",
# 'ElectionTick': 10,
# 'HeartbeatTick': 1,
# 'MaxInflightBlocks': 5,
# 'SnapshotIntervalSize': "20 MB"} if not raft_option else raft_option

def create(self, consensus, orderers, peers, orderer_cfg=None, application=None, option=None):
"""create the cryptotx.yaml

def create(self, name, consensus, orderers, peers, orderer_cfg=None, application=None, option=None):
"""create the configtx.yaml
param:
consensus:consensus
orderers:the list of orderer
Expand Down Expand Up @@ -101,24 +91,24 @@ def create(self, consensus, orderers, peers, orderer_cfg=None, application=None,
Application=ApplicationCapabilities
)
Application = deepcopy(ApplicationDefaults)
Application["Capabilities"] = Capabilities["Application"]
Orderer = deepcopy(OrdererDefaults)
Orderer["Addresses"] = deepcopy(OrdererAddress)
Orderer["Policies"] = dict(
Readers=dict(Type="ImplicitMeta", Rule="ANY Readers"),
Writers=dict(Type="ImplicitMeta", Rule="ANY Writers"),
Admins=dict(Type="ImplicitMeta", Rule="MAJORITY Admins"),
BlockValidation=dict(Type="ImplicitMeta", Rule="ANY Writers")
)
Orderer["EtcdRaft"]["Consenters"] = deepcopy(Consenters)
Channel = deepcopy(ChannelDefaults)
Application["Capabilities"] = Capabilities["Application"]
Channel["Capabilities"] = Capabilities["Channel"]
Orderer["Capabilities"] = Capabilities["Orderer"]
Orderer["OrdererType"] = consensus
Orderer["EtcdRaft"]["Consenters"] = deepcopy(Consenters)

Profiles = {}
Profiles["TwoOrgsOrdererGenesis"] = deepcopy(Channel)
Profiles["TwoOrgsOrdererGenesis"]["Orderer"] = deepcopy(Orderer)
Profiles["TwoOrgsOrdererGenesis"]["Orderer"]["Organizations"] = OrdererOrganizations
Profiles["TwoOrgsOrdererGenesis"]["Orderer"]["Capabilities"] = Capabilities["Orderer"]
Profiles["TwoOrgsOrdererGenesis"]["Consortiums"] = {'SampleConsortium': {'Organizations': deepcopy(PeerOrganizations)}}
Profiles[name] = deepcopy(Channel)
Profiles[name]["Orderer"] = deepcopy(Orderer)
Profiles[name]["Application"] = deepcopy(Application)
Profiles[name]["Capabilities"] = Capabilities["Channel"]
Profiles[name]["Orderer"]["Capabilities"] = Capabilities["Orderer"]
Profiles[name]["Application"]["Capabilities"] = Capabilities["Application"]
Profiles[name]["Orderer"]["Organizations"] = OrdererOrganizations
Profiles[name]["Application"]["Organizations"] = PeerOrganizations

configtx = dict(
Organizations=Organizations,
Expand All @@ -145,8 +135,8 @@ def createChannel(self, name, organizations):
configtx = yaml.load(f, Loader=yaml.FullLoader)
Profiles = configtx["Profiles"]
Channel = configtx["Channel"]
Orderer = configtx["Orderer"]
Application = configtx["Application"]
Capabilities = configtx["Capabilities"]["Application"]
PeerOrganizations = []
for org in configtx["Organizations"]:
for item in organizations:
Expand All @@ -155,10 +145,8 @@ def createChannel(self, name, organizations):
if PeerOrganizations == []:
raise Exception("can't find organnization")
Profiles[name] = deepcopy(Channel)
Profiles[name]["Consortium"] = "SampleConsortium"
Profiles[name]["Application"] = deepcopy(Application)
Profiles[name]["Application"]["Organizations"] = deepcopy(PeerOrganizations)
Profiles[name]["Application"]["Capabilities"] = deepcopy(Capabilities)
Profiles[name]["Orderer"] = Orderer
Profiles[name]["Application"] = Application

with open('{}/{}/{}'.format(self.filepath, self.network, "configtx.yaml"), 'w', encoding='utf-8') as f:
yaml.safe_dump(configtx, f, sort_keys=False)
Expand Down
37 changes: 16 additions & 21 deletions src/api-engine/api/lib/configtxgen/configtxgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from subprocess import call
from api.config import CELLO_HOME, FABRIC_TOOL, FABRIC_VERSION

import logging
LOG = logging.getLogger(__name__)


class ConfigTxGen:
"""Class represents cryptotxgen."""
Expand All @@ -22,7 +25,7 @@ def __init__(self, network, filepath=CELLO_HOME, configtxgen=FABRIC_TOOL, versio
self.filepath = filepath
self.version = version

def genesis(self, profile="TwoOrgsOrdererGenesis", channelid="testchainid", outputblock="genesis.block"):
def genesis(self, profile="", channelid="", outputblock="genesis.block"):
"""generate gensis
param:
profile: profile
Expand All @@ -31,31 +34,23 @@ def genesis(self, profile="TwoOrgsOrdererGenesis", channelid="testchainid", outp
return:
"""
try:
call([self.configtxgen, "-configPath", "{}/{}/".format(self.filepath, self.network),
"-profile", "{}".format(profile),
"-outputBlock", "{}/{}/{}".format(self.filepath, self.network, outputblock),
"-channelID", "{}".format(channelid)])
except Exception as e:
err_msg = "configtxgen genesis fail! "
raise Exception(err_msg + str(e))
command = [
self.configtxgen,
"-configPath", "{}/{}/".format(self.filepath, self.network),
"-profile", "{}".format(profile),
"-outputBlock", "{}/{}/{}".format(self.filepath, self.network, outputblock),
"-channelID", "{}".format(channelid)
]

LOG.info("Running command: " + " ".join(command))

call(command)

def channeltx(self, profile, channelid, outputCreateChannelTx="channel.tx"):
"""generate anchorpeer
param:
profile: profile
channelid: channelid
outputblock: outputblock
return:
"""
try:
call([self.configtxgen, "-configPath", "{}/{}/".format(self.filepath, self.network),
"-profile", "{}".format(profile),
"-outputCreateChannelTx", "{}/{}/{}".format(self.filepath, self.network, "channel-artifacts/" + outputCreateChannelTx),
"-channelID", "{}".format(channelid)])
except Exception as e:
err_msg = "configtxgen genesis fail! "
raise Exception(err_msg + str(e))


def anchorpeer(self, profile, channelid, outputblock):
"""set anchorpeer
param:
Expand Down
35 changes: 22 additions & 13 deletions src/api-engine/api/lib/peer/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,31 @@
import subprocess
from api.lib.peer.command import Command
from api.config import FABRIC_TOOL, FABRIC_VERSION
import logging

LOG = logging.getLogger(__name__)

class Channel(Command):
"""Call CMD to perform channel create, join and other related operations"""

def __init__(self, version=FABRIC_VERSION, peer=FABRIC_TOOL, **kwargs):
self.peer = peer + "/peer"
self.osnadmin = peer + "/osnadmin"
super(Channel, self).__init__(version, **kwargs)

def create(self, channel, orderer_url, channel_tx, output_block, time_out="90s"):
def create(self, channel, orderer_url, block_path, time_out="90s"):
try:
res = 0x100
if os.getenv("CORE_PEER_TLS_ENABLED") == "false" or os.getenv("CORE_PEER_TLS_ENABLED") is None:
res = os.system("{} channel create -c {} -o {} -f {} --outputBlock {} --timeout {}"
.format(self.peer, channel, orderer_url, channel_tx, output_block, time_out))
else:
ORDERER_CA = os.getenv("ORDERER_CA")
res = os.system("{} channel create -c {} -o {} -f {} --outputBlock {} --timeout {} --tls --cafile {}"
.format(self.peer, channel, orderer_url, channel_tx, output_block, time_out, ORDERER_CA))

ORDERER_CA = os.getenv("ORDERER_CA")
ORDERER_ADMIN_TLS_SIGN_CERT = os.getenv("ORDERER_ADMIN_TLS_SIGN_CERT")
ORDERER_ADMIN_TLS_PRIVATE_KEY = os.getenv("ORDERER_ADMIN_TLS_PRIVATE_KEY")

command = "{} channel join --channelID {} --config-block {} -o {} --ca-file {} --client-cert {} --client-key {}".format(self.osnadmin, channel, block_path, orderer_url, ORDERER_CA, ORDERER_ADMIN_TLS_SIGN_CERT, ORDERER_ADMIN_TLS_PRIVATE_KEY)

LOG.info(f"Running command: {command}")

res = os.system(command)

# The return value of os.system is not the result of executing the program. It is a 16 bit number,
# and its high bit is the return code
Expand Down Expand Up @@ -106,16 +112,19 @@ def signconfigtx(self, channel_tx):
res = res >> 8
return res

def join(self, block_file):
def join(self, block_path):
"""
Joins the peer to a channel.
params:
block_file: Path to file containing genesis block.
block_path: Path to file containing genesis block.
"""
try:
res = os.system(
"{} channel join -b {} ".format(self.peer, block_file)
)
command = "{} channel join -b {} ".format(self.peer, block_path)

LOG.info(f"Running command: {command}")

res = os.system(command)

except Exception as e:
err_msg = "join the peer to a channel failed. {}".format(e)
raise Exception(err_msg)
Expand Down
64 changes: 40 additions & 24 deletions src/api-engine/api/routes/channel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,34 @@ def create(self, request):
if p.status != "running":
raise NoResource

ConfigTX(org.network.name).createChannel(name, [org.name])
ConfigTxGen(org.network.name).channeltx(
profile=name, channelid=name, outputCreateChannelTx="{}.tx".format(name))
tx_path = "{}/{}/channel-artifacts/{}.tx".format(
CELLO_HOME, org.network.name, name)
block_path = "{}/{}/channel-artifacts/{}.block".format(
_orderers = []
_peers = []
_orderers.append({"name": org.name, "hosts": []})
_peers.append({"name": org.name, "hosts": []})
nodes = Node.objects.filter(organization=org)
for node in nodes:
if node.type == "peer":
_peers[0]["hosts"].append({"name": node.name})
elif node.type == "orderer":
_orderers[0]["hosts"].append({"name": node.name})

ConfigTX(org.network.name).create(name, org.network.consensus, _orderers, _peers)
ConfigTxGen(org.network.name).genesis(profile=name, channelid=name, outputblock="{}.block".format(name))

block_path = "{}/{}/{}.block".format(
CELLO_HOME, org.network.name, name)
ordering_node = Node.objects.get(id=orderers[0])
peer_node = Node.objects.get(id=peers[0])
envs = init_env_vars(peer_node, org)
envs = init_env_vars(ordering_node, org)
peer_channel_cli = PeerChannel(**envs)
peer_channel_cli.create(
channel=name,
orderer_url="{}.{}:{}".format(
ordering_node.name, org.name.split(".", 1)[1], str(7050)),
channel_tx=tx_path,
output_block=block_path
ordering_node.name, org.name.split(".", 1)[1], str(7053)),
block_path=block_path
)
for i in range(len(peers)):
peer_node = Node.objects.get(id=peers[i])
envs = init_env_vars(peer_node, org)
# envs["CORE_PEER_LOCALMSPID"] = '{}MSP'.format(peer_node.name.split(".")[0].capitalize()) #Org1MSP
join_peers(envs, block_path)

channel = Channel(
Expand Down Expand Up @@ -385,18 +391,28 @@ def init_env_vars(node, org):
CELLO_HOME, org_name, org_domain)
dir_node = "{}/{}/crypto-config/peerOrganizations".format(
CELLO_HOME, org_name)

envs = {}

if(node.type == "orderer"):
envs = {
"ORDERER_CA": "{}/msp/tlscacerts/tlsca.{}-cert.pem".format(dir_certificate, org_domain),
"ORDERER_ADMIN_TLS_SIGN_CERT": "{}/orderers/{}/tls/server.crt".format(dir_certificate, node.name + "." + org_domain),
"ORDERER_ADMIN_TLS_PRIVATE_KEY": "{}/orderers/{}/tls/server.key".format(dir_certificate, node.name + "." + org_domain)
}
elif(node.type == "peer"):
envs = {
"CORE_PEER_TLS_ENABLED": "true",
"CORE_PEER_LOCALMSPID": "{}MSP".format(org_name.capitalize()),
"CORE_PEER_TLS_ROOTCERT_FILE": "{}/{}/peers/{}/tls/ca.crt".format(dir_node, org_name, node.name + "." + org_name),
"CORE_PEER_MSPCONFIGPATH": "{}/{}/users/Admin@{}/msp".format(dir_node, org_name, org_name),
"CORE_PEER_ADDRESS": "{}:{}".format(
node.name + "." + org_name, str(7051)),

"FABRIC_CFG_PATH": "{}/{}/peers/{}/".format(dir_node, org_name, node.name + "." + org_name)
}
LOG.info("Peer envs: {}".format(envs))

envs = {
"CORE_PEER_TLS_ENABLED": "true",
# "Org1.cello.comMSP"
"CORE_PEER_LOCALMSPID": "{}MSP".format(org_name.capitalize()),
"CORE_PEER_TLS_ROOTCERT_FILE": "{}/{}/peers/{}/tls/ca.crt".format(dir_node, org_name, node.name + "." + org_name),
"CORE_PEER_ADDRESS": "{}:{}".format(
node.name + "." + org_name, str(7051)),
"CORE_PEER_MSPCONFIGPATH": "{}/{}/users/Admin@{}/msp".format(dir_node, org_name, org_name),
"FABRIC_CFG_PATH": "{}/{}/peers/{}/".format(dir_node, org_name, node.name + "." + org_name),
"ORDERER_CA": "{}/msp/tlscacerts/tlsca.{}-cert.pem".format(dir_certificate, org_domain)
}
return envs


Expand All @@ -409,5 +425,5 @@ def join_peers(envs, block_path):
# Join the peers to the channel.
peer_channel_cli = PeerChannel(**envs)
peer_channel_cli.join(
block_file=block_path
block_path=block_path
)
Loading

0 comments on commit 65b467e

Please sign in to comment.