def deploy_chaincode(): c = Client(base_url="http://192.168.33.10:7050") deploy = c.chaincode_deploy() print "Chain code is being deployed" time.sleep(30) if deploy['result']['status']=='Ok': return deploy['result']['message'] else: return deploy['result']['status'] + "is the error"
class ChaincodeClient(object): def __init__(self, fabric_url, chaincode_name=""): self.fabric_url = fabric_url self.client = Client(base_url=fabric_url) self.chaincode_name = chaincode_name self.chaincode_type = CHAINCODE_LANG_GO def set_chaincode_name(self, chaincode_name): self.chaincode_name = chaincode_name def deploy(self, chaincode_path, function, args, type=CHAINCODE_LANG_GO): try: resp = self.client.chaincode_deploy( chaincode_path=chaincode_path, function=function, args=args, type=type, ) if resp['result']['status'] != 'OK': raise self.set_chaincode_name(resp['result']['message']) self.chaincode_type = type return True except Exception as e: print(e) return False def invoke(self, function, args): try: resp = self.client.chaincode_invoke( chaincode_name=self.chaincode_name, function=function, args=args) if resp['result']['status'] != 'OK': raise return True except Exception as e: print(e) return False def query(self, function, args): try: resp = self.client.chaincode_query( chaincode_name=self.chaincode_name, function=function, args=args) if resp['result']['status'] != 'OK': raise return resp['result']['message'] except Exception as e: print(e) return None
def __init__(self, base_url,mustlogin): super(HyperledgerClient, self).__init__() self.base_url = base_url self.client = Client(base_url=base_url) self.enrollId = "jim" print("hyperledger.client version:" + self.client._version) if mustlogin: data = { "enrollId": self.enrollId, "enrollSecret": "6avZQLwcUe9b" } user = self.client._result(self.client._post(self.client._url("/registrar"),data=json.dumps(data))) print(user)
def init_hyperledger(): print("Initializing hyperledger environment...") register_hl_user(setup_hl_creds[0], setup_hl_creds[1]) c = Client(base_url="http://127.0.0.1:7050") # enroll_cc_name = deploy_chaincode( # c, setup_hl_creds[0], "https://github.com/ajmanlove/hyperledger-sandbox/reinsurance_poc/enrollment_service", [] # ) asset_cc_name = deploy_chaincode( c, setup_hl_creds[0], "https://github.com/ajmanlove/hyperledger-sandbox/reinsurance_poc/asset_management", []) print("Asset chaincode name is " + asset_cc_name) request_cc_name = deploy_chaincode( c, setup_hl_creds[0], "https://github.com/ajmanlove/hyperledger-sandbox/reinsurance_poc/reinsurance_request", [asset_cc_name]) print("Request chaincode name is " + request_cc_name) proposal_cc_name = deploy_chaincode( c, setup_hl_creds[0], "https://github.com/ajmanlove/hyperledger-sandbox/reinsurance_poc/reinsurance_proposal", [asset_cc_name]) print("Enrolling test users...") register_hl_user(insurer1_hl_creds[0], insurer1_hl_creds[1]) register_hl_user(reinsurer1_hl_creds[0], reinsurer1_hl_creds[1]) register_hl_user(reinsurer2_hl_creds[0], reinsurer2_hl_creds[1]) register_hl_user(insurer2_hl_creds[0], insurer2_hl_creds[1]) register_hl_user(reinsurer3_hl_creds[0], reinsurer3_hl_creds[1]) register_cc(asset_cc_name, setup_hl_creds[0], request_cc_name, "reinsurance_request") register_cc(asset_cc_name, setup_hl_creds[0], proposal_cc_name, "reinsurance_proposal") print("Writing setup meta file...") f = open(".setup.ini", 'w') f.write("[CHAINCODE]\n") f.write("asset_management={0}\n".format(asset_cc_name)) f.write("reinsurance_request={0}\n".format(request_cc_name)) f.write("reinsurance_proposal={0}\n".format(proposal_cc_name)) f.close() print("Done writing setup meta file") print("") print("-----------------------------------------------------------") print("asset_management chaincode name: ", asset_cc_name) print("reinsurance_request chaincode name: ", request_cc_name) print("reinsurance_proposal chaincode name: ", proposal_cc_name) print("-----------------------------------------------------------") print("") print("Init of hyperledger environment COMPLETE")
# * python function_test.py [API_URL=http://127.0.0.1:5000] will deploy first # * python function_test.py [API_URL=http://127.0.0.1:5000] [chaincode_name] # E.g., # "f389486d91f54d1f8775940f24b1d3bd9f8a8e75d364e158ac92328ddacad629607a3c42be156fc4a7da7173adca2ac7d7eef29afc59c6f07f3ad14abee34f68" if __name__ == '__main__': if len(sys.argv) not in [2, 3]: print("Usage: python function_test.py [" "API_URL=http://127.0.0.1:5000] [chaincode_name]") exit() API_URL = sys.argv[1] chaincode_name = "" if len(sys.argv) == 3: chaincode_name = sys.argv[2] c = Client(base_url=API_URL) print("Checking cluster at {}".format(API_URL)) if not chaincode_name: print(">>>Test: deploy the default chaincode") res = c.chaincode_deploy(args=["a", "10000", "b", "20000"]) chaincode_name = res['result']['message'] assert res['result']['status'] == 'OK' print("Successfully deploy chaincode with returned name = " + chaincode_name) print("Wait 15 seconds to make sure deployment is done.") time.sleep(15) print(">>>Check the initial value: a, b") values = query_value(chaincode_name, ["a", "b"])
from hyperledger.client import Client c = Client(base_url="http://localhost") c.peer_list()
# * python function_test.py [API_URL=http://127.0.0.1:5000] [chaincode_name] # E.g., # "f389486d91f54d1f8775940f24b1d3bd9f8a8e75d364e158ac92328ddacad629607a3c42be156fc4a7da7173adca2ac7d7eef29afc59c6f07f3ad14abee34f68" if __name__ == '__main__': if len(sys.argv) not in [1, 2, 3]: print("Usage: python function_test.py [" "API_URL=http://127.0.0.1:5000] [chaincode_name]") exit() if len(sys.argv) >= 2: API_URL = sys.argv[1] chaincode_name = "" if len(sys.argv) >= 3: chaincode_name = sys.argv[2] c = Client(base_url=API_URL) print("Checking cluster at {}".format(API_URL)) if not chaincode_name: print(">>>Test: deploy the default chaincode") res = c.chaincode_deploy(args=["a", "10000", "b", "20000"]) chaincode_name = res['result']['message'] assert res['result']['status'] == 'OK' print("Successfully deploy chaincode with returned name = " + chaincode_name) print("Wait {}s to make sure deployment is done.".format(DEPLOY_WAIT)) time.sleep(DEPLOY_WAIT) print(">>>Check the initial value: a, b") values = query_value(chaincode_name, ["a", "b"], validate=True)
# * python function_test.py [API_URL=http://127.0.0.1:7050] [chaincode_name] # E.g., # "f389486d91f54d1f8775940f24b1d3bd9f8a8e75d364e158ac92328ddacad629607a3c42be156fc4a7da7173adca2ac7d7eef29afc59c6f07f3ad14abee34f68" if __name__ == '__main__': if len(sys.argv) not in [1, 2, 3]: print("Usage: python function_test.py [" "API_URL=http://127.0.0.1:7050] [chaincode_name]") exit() chaincode_name = "" if len(sys.argv) >= 2: API_URL = sys.argv[1] if len(sys.argv) >= 3: chaincode_name = sys.argv[2] c = Client(base_url=API_URL) print("Checking cluster at {}".format(API_URL)) if not chaincode_name: print(">>>Test: deploy the default chaincode") res = c.chaincode_deploy(args=["a", "10000", "b", "20000"]) chaincode_name = res['result']['message'] assert res['result']['status'] == 'OK' print("Successfully deploy chaincode with returned name = " + chaincode_name) print("Wait {}s to make sure deployment is done.".format(DEPLOY_WAIT)) time.sleep(DEPLOY_WAIT) print(">>>Check the initial value: a, b") values = query(chaincode_name, ["a", "b"], validate=True)
import time from hyperledger.client import Client current_milli_time = lambda: int(round(time.time() * 1000)) c = Client(base_url="http://127.0.0.1:7050") start = current_milli_time() count = 100 chaincode_name = "d75bfee19774951550a2f771bd9c9a11bc466405e63a853ee0ee0163db3068465ce4aa749c9ebf183227a2ad0805fc6c6a3d0f9f9dc1d902503dbca3350d3245" # print(c.chaincode_deploy()) # print(c.chaincode_query(chaincode_name=chaincode_name)) for i in range(count): result = c.chaincode_invoke(chaincode_name=chaincode_name, function="invoke", args=["a", "b", "100"]) # print(result) done = current_milli_time() elapsed = done - start print(1 / (elapsed / count / 1000), 'tps') # mkdir $GOPATH/src/chaincode_example02 && cp $GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02/chaincode_example02.go $GOPATH/src/chaincode_example02/ && cd $GOPATH/src/chaincode_example02 && go build
# * python test.py [API_URL=http://127.0.0.1:7050] will deploy first # E.g., # "f389486d91f54d1f8775940f24b1d3bd9f8a8e75d364e158ac92328ddacad629607a3c42be156fc4a7da7173adca2ac7d7eef29afc59c6f07f3ad14abee34f68" if __name__ == '__main__': if len(sys.argv) not in [2, 3]: print("Usage: python function_test.py [" "API_URL=http://127.0.0.1:7050] [chaincode_name]") exit() API_URL = sys.argv[1] chaincode_name = "" # chaincode_name = "7be1529ee16969baf9f3156247a0ee8e7eee99a6a0a816776acff65e6e1def71249f4cb1cad5e0f0b60b25dd2a6975efb282741c0e1ecc53fa8c10a9aaa31137" # noqa if len(sys.argv) == 3: chaincode_name = sys.argv[2] c = Client(base_url=API_URL) print("Checking cluster at {}".format(API_URL)) print(">>>Test: list the peers") res = c.peer_list() print(json.dumps(res, sort_keys=True, indent=4)) assert len(res['peers']) > 0 print(">>>Test: list the chain") chain_status = c.chain_list() print(json.dumps(chain_status, sort_keys=True, indent=4)) assert chain_status['height'] > 0 print("Existing block number = {0}".format(chain_status["height"])) for i in range(1, chain_status['height']):
def setUp(self): self.c = Client(base_url="http://127.0.0.1:7050")
def __init__(self, fabric_url, chaincode_name=""): self.fabric_url = fabric_url self.client = Client(base_url=fabric_url) self.chaincode_name = chaincode_name self.chaincode_type = CHAINCODE_LANG_GO
class HyperledgerClient(object): def __init__(self, base_url,mustlogin): super(HyperledgerClient, self).__init__() self.base_url = base_url self.client = Client(base_url=base_url) self.enrollId = "jim" print("hyperledger.client version:" + self.client._version) if mustlogin: data = { "enrollId": self.enrollId, "enrollSecret": "6avZQLwcUe9b" } user = self.client._result(self.client._post(self.client._url("/registrar"),data=json.dumps(data))) print(user) def deployChainCode(self, chaincode_path, chainCodeArgs): deploy = self.execMethod(method="deploy", chaincode_name=None,chaincode_path=chaincode_path, function="init", args=chainCodeArgs) return deploy ''' deploy = self.client.chaincode_deploy(chaincode_path=chaincode_path, type=1, args=chainCodeArgs, secure_context=self.enrollId) ''' # print(deploy) #self.chaincodeID = str(deploy['result']['message']) #return deploy def printQueryResult(self, chaincodeID, function ,chainCodeArgs): queryResult = self.execMethod(method="query", chaincode_name=chaincodeID, chaincode_path=None, function=function, args=chainCodeArgs) ''' queryResult = self.client.chaincode_query(chaincode_name=self.chaincodeID, type=1, function="query", args=[target], secure_context=self.enrollId, metadata=None) ''' #print(queryResult) #print("Query: \'"+target+ "\':\'"+ queryResult['result']['message'] +"\'") return queryResult def printInvokeResult(self, chaincodeID, function ,chainCodeArgs): queryResult = self.execMethod(method="invoke", chaincode_name=chaincodeID,chaincode_path=None, function=function, args=chainCodeArgs) return queryResult def printExecResult(self,command): queryResult = self.execMethod(method="query", chaincode_name=self.chaincodeID, chaincode_path=None, function="exec", args=[command]) ''' queryResult = self.client.chaincode_invoke(chaincode_name=self.chaincodeID, type=1, function="invoke", args=[a,b,value], secure_context=self.enrollId, metadata=None) ''' #print("Invoke:\'" + a + "\' transfer "+value+ " to \'" + b + "\'") print("exec:" + str(queryResult)) return queryResult #print(self.printQueryResult(command)) def execMethod(self,method,chaincode_name,chaincode_path,function,args): chaincodeID = {"name": chaincode_name} if chaincode_name else \ {"path": chaincode_path} data = { "jsonrpc": "2.0", # JSON-RPC protocol version. MUST be "2.0". "method": method, "params": { "type": 1, "chaincodeID": chaincodeID, "ctorMsg": { "function": function, "args": args }, "secureContext": self.enrollId }, "id": 1 } #data["params"]["secureContext"] = self.enrollId result = self.client._result(self.client._post(self.client._url("/chaincode"),data=json.dumps(data))) #print(json.dumps(data)) return json.loads(result);
def invoke_chaincode(chaincode_name, function, WineID, OwnerInfo): c = Client(base_url="http://192.168.33.10:7050") invoke=c.chaincode_invoke(chaincode_name=chaincode_name, function=function, args=[WineID,OwnerInfo]) return invoke['result']['status']
def query_chaincode(chaincode_name, WineID): c = Client(base_url="http://192.168.33.10:7050") query=c.chaincode_query(chaincode_name=chaincode_name,function="GetWineInfo", args=[WineID]) print query return query['result']['status'], query['result']['message']