Skip to content

Commit

Permalink
Legacy Chaincode init (#592)
Browse files Browse the repository at this point in the history
- permit JSON string to be included on the committed chaincode
- add note to docs on why there are not general purposes invoke modules

Signed-off-by: Matthew B White <[email protected]>
  • Loading branch information
mbwhite authored Mar 10, 2022
1 parent a9cb9c9 commit 550239e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/source/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ values.
While different modules perform different tasks, their interfaces and responses
follow similar patterns.


Invoking transactions
---------------------

Ansible modules should work on the principle that they are idempotent; the same playbook
can be executed more than once safetly. Submmiting a transactions to Fabric would break this concept.

The modules here are intended for administrative purposes; for this reason and to main the modules
as being idempotent there are no modules for general purpose transaction invoking.

Module reference
----------------

Expand Down
19 changes: 19 additions & 0 deletions plugins/module_utils/peers.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,25 @@ def commit_chaincode(self, channel, msp_ids, name, version, sequence, endorsemen
else:
raise Exception(f'Failed to commit chaincode on peer: {process.stdout}')

def init_chaincode(self, channel, msp_ids, name, initJsonStr, endorsement_policy_ref, endorsement_policy, endorsement_plugin, validation_plugin, timeout, orderer):
env = self._get_environ()
args = ['peer', 'chaincode', 'invoke', '-C', channel, '-n', name, '--isInit', '--ctor', initJsonStr, '--waitForEventTimeout', str(timeout) + "s"]
if endorsement_policy_ref:
args.extend(['--channel-config-policy', endorsement_policy_ref])
elif endorsement_policy:
args.extend(['--signature-policy', endorsement_policy])
if endorsement_plugin:
args.extend(['--endorsement-plugin', endorsement_plugin])
if validation_plugin:
args.extend(['--validation-plugin', validation_plugin])
args.extend(self._get_anchor_peers(channel, msp_ids))
args.extend(self._get_ordering_service(channel, orderer))
process = self._run_command(args, env)
if process.returncode == 0:
return
else:
raise Exception(f'Failed to init legacy chaincode on peer: {process.stdout}')

def _get_environ(self):
api_url_parsed = urllib.parse.urlparse(self.peer.api_url)
env = os.environ.copy()
Expand Down
11 changes: 11 additions & 0 deletions plugins/modules/committed_chaincode.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@
- True if this chaincode definition requires called the I(Init) method before the I(Invoke) method,
false otherwise.
type: bool
init_json_str:
description:
- The JSON string to pass to the Init method.
If init_required is true, then the transaciton will be submitted immediately after the commit
completes sucessfully. Note if the transaction fails and you wish to resubmit this will need
to be done via other (non-ansible) methods
type: string
collections_config:
description:
- The path to the collections configuration file for the chaincode definition.
Expand Down Expand Up @@ -318,6 +325,7 @@ def main():
endorsement_plugin=dict(type='str', default='escc'),
validation_plugin=dict(type='str', default='vscc'),
init_required=dict(type='bool'),
init_json_str=dict(type='str'),
collections_config=dict(type='str'),
orderer_name=dict(type='str')
)
Expand Down Expand Up @@ -369,6 +377,7 @@ def main():
init_required = module.params['init_required']
collections_config = module.params['collections_config']
timeout = module.params['api_timeout']
initJsonStr = module.params['init_json_str']

# Check if this chaincode is already committed on the channel.
with peer.connect(module, identity, msp_id, hsm) as peer_connection:
Expand Down Expand Up @@ -403,6 +412,8 @@ def main():
with peer.connect(module, identity, msp_id, hsm) as peer_connection:
peer_connection.commit_chaincode(channel, msp_ids, name, version, sequence, endorsement_policy_ref, endorsement_policy, endorsement_plugin, validation_plugin, init_required, collections_config, timeout, orderer)
changed = True
if init_required:
peer_connection.init_chaincode(channel, msp_ids, name, initJsonStr, endorsement_policy_ref, endorsement_policy, endorsement_plugin, validation_plugin, timeout, orderer)

# Return the committed chaincode.
return module.exit_json(changed=changed, committed_chaincode=dict(channel=channel, name=name, version=version, sequence=sequence, endorsement_policy_ref=endorsement_policy_ref, endorsement_policy=endorsement_policy, endorsement_plugin=endorsement_plugin, validation_plugin=validation_plugin, init_required=init_required, collections_config=collections_config))
Expand Down

0 comments on commit 550239e

Please sign in to comment.