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