18
18
19
19
from api .config import CELLO_HOME
20
20
from api .common .serializers import PageQuerySerializer
21
- from api .utils .common import with_common_response , parse_block_file , to_dict
21
+ from api .utils .common import with_common_response , parse_block_file , to_dict , json_filter , json_add_anchor_peer , json_create_envelope
22
22
from api .lib .configtxgen import ConfigTX , ConfigTxGen
23
23
from api .lib .jq .jq import JQ
24
24
from api .lib .peer .channel import Channel as PeerChannel
@@ -148,7 +148,8 @@ def create(self, request):
148
148
peer_channel_join (name , peers , org )
149
149
150
150
# set anchor peer
151
- set_anchor_peer (name , org , peers , ordering_node )
151
+ anchor_peer = Node .objects .get (id = peers [0 ])
152
+ set_anchor_peer (name , org , anchor_peer , ordering_node )
152
153
153
154
# save channel to db
154
155
channel = Channel (
@@ -426,33 +427,55 @@ def peer_channel_join(name, peers, org):
426
427
CELLO_HOME , org .network .name , name )
427
428
)
428
429
429
- def set_anchor_peer (name , org , peers , ordering_node ):
430
+ def set_anchor_peer (name , org , anchor_peer , ordering_node ):
430
431
"""
431
432
Set anchor peer for the channel.
432
433
:param org: Organization object.
433
- :param peers: list of Node objects
434
+ :param anchor_peer: Anchor peer node
435
+ :param ordering_node: Orderer node
434
436
:return: none
435
437
"""
436
- org_msp = '{}MSP ' .format (org .name .split ("." , 1 )[0 ].capitalize ())
438
+ org_msp = '{}' .format (org .name .split ("." , 1 )[0 ].capitalize ())
437
439
channel_artifacts_path = "{}/{}" .format (CELLO_HOME , org .network .name )
438
- peer_channel_fetch (name , org , peers , ordering_node )
440
+
441
+ # Fetch the channel block from the orderer
442
+ peer_channel_fetch (name , org , anchor_peer , ordering_node )
439
443
440
- ConfigTxLator ().proto_encode (
444
+ # Decode block to JSON
445
+ ConfigTxLator ().proto_decode (
441
446
input = "{}/config_block.pb" .format (channel_artifacts_path ),
442
447
type = "common.Block" ,
443
448
output = "{}/config_block.json" .format (channel_artifacts_path ),
444
449
)
445
-
446
- JQ ().filter (
450
+
451
+ # Get the config data from the block
452
+ json_filter (
447
453
input = "{}/config_block.json" .format (channel_artifacts_path ),
448
454
output = "{}/config.json" .format (channel_artifacts_path ),
449
455
expression = ".data.data[0].payload.data.config"
450
456
)
451
457
452
- JQ ().filter (
458
+ # add anchor peer config
459
+ anchor_peer_config = {
460
+ "AnchorPeers" : {
461
+ "mod_policy" : "Admins" ,
462
+ "value" : {
463
+ "anchor_peers" : [
464
+ {
465
+ "host" : anchor_peer .name + "." + org .name ,
466
+ "port" : 7051
467
+ }
468
+ ]
469
+ },
470
+ "version" : 0
471
+ }
472
+ }
473
+
474
+ json_add_anchor_peer (
453
475
input = "{}/config.json" .format (channel_artifacts_path ),
454
476
output = "{}/modified_config.json" .format (channel_artifacts_path ),
455
- expression = ".channel_group.groups.Application.groups.{}.values += {{AnchorPeers:{{mod_policy: Admins,value:{{anchor_peers:[{{host: {},port: {}}}]}},version: 0}}}}" .format (org_msp , peers [0 ].name , str (7051 ))
477
+ anchor_peer_config = anchor_peer_config ,
478
+ org_msp = org_msp
456
479
)
457
480
458
481
ConfigTxLator ().proto_encode (
@@ -480,10 +503,11 @@ def set_anchor_peer(name, org, peers, ordering_node):
480
503
output = "{}/config_update.json" .format (channel_artifacts_path ),
481
504
)
482
505
483
- JQ ().filter (
484
- input = "." ,
506
+ # Create config update envelope
507
+ json_create_envelope (
508
+ input = "{}/config_update.json" .format (channel_artifacts_path ),
485
509
output = "{}/config_update_in_envelope.json" .format (channel_artifacts_path ),
486
- expression = ""
510
+ channel = name
487
511
)
488
512
489
513
ConfigTxLator ().proto_encode (
@@ -492,31 +516,41 @@ def set_anchor_peer(name, org, peers, ordering_node):
492
516
output = "{}/config_update_in_envelope.pb" .format (channel_artifacts_path ),
493
517
)
494
518
495
- envs = init_env_vars (ordering_node , org )
496
- peer_channel_cli = PeerChannel (** envs )
497
- peer_channel_cli .update (
498
- channel = name ,
499
- channel_tx = "{}/config_update_in_envelope.pb" .format (channel_artifacts_path ),
500
- orderer_url = "{}.{}:{}" .format (
501
- ordering_node .name , org .name .split ("." , 1 )[1 ], str (7050 )),
502
- )
519
+ # Update the channel of anchor peer
520
+ peer_channel_update (name , org , anchor_peer , ordering_node , channel_artifacts_path )
503
521
504
522
505
- def peer_channel_fetch (name , org , peers , ordering_node ):
523
+ def peer_channel_fetch (name , org , anchor_peer , ordering_node ):
506
524
"""
507
525
Fetch the channel block from the orderer.
508
- :param peers: list of Node objects
526
+ :param anchor_peer: Anchor peer node
509
527
:param org: Organization object.
510
528
:param channel_name: Name of the channel.
511
529
:return: none
512
530
"""
513
- peer_node = Node .objects .get (id = peers [0 ])
514
- envs = init_env_vars (peer_node , org )
531
+ envs = init_env_vars (anchor_peer , org )
515
532
peer_channel_cli = PeerChannel (** envs )
516
533
peer_channel_cli .fetch (block_path = "{}/{}/config_block.pb" .format (CELLO_HOME , org .network .name ),
517
534
channel = name , orderer_general_url = "{}.{}:{}" .format (
518
535
ordering_node .name , org .name .split ("." , 1 )[1 ], str (7050 )))
519
536
537
+ def peer_channel_update (name , org , anchor_peer , ordering_node , channel_artifacts_path ):
538
+ """
539
+ Update the channel.
540
+ :param anchor_peer: Anchor peer node
541
+ :param org: Organization object.
542
+ :param channel_name: Name of the channel.
543
+ :return: none
544
+ """
545
+ envs = init_env_vars (anchor_peer , org )
546
+ peer_channel_cli = PeerChannel (** envs )
547
+ peer_channel_cli .update (
548
+ channel = name ,
549
+ channel_tx = "{}/config_update_in_envelope.pb" .format (channel_artifacts_path ),
550
+ orderer_url = "{}.{}:{}" .format (
551
+ ordering_node .name , org .name .split ("." , 1 )[1 ], str (7050 )),
552
+ )
553
+
520
554
521
555
def init_env_vars (node , org ):
522
556
"""
0 commit comments