Skip to content
This repository was archived by the owner on Jan 27, 2022. It is now read-only.

Commit d202b0d

Browse files
rranjan3Ram-srini
authored andcommitted
SDK extension to support Ethereum
- New implementations for work_order & worker_registry - Add contract specific configs in tcf_connector.toml Signed-off-by: Rajeev Ranjan <[email protected]>
1 parent d9a922c commit d202b0d

File tree

6 files changed

+390
-140
lines changed

6 files changed

+390
-140
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# Copyright 2020 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import binascii
16+
import logging
17+
import json
18+
from os import environ
19+
20+
from utility.hex_utils import is_valid_hex_str
21+
22+
from avalon_sdk.worker.worker_details import WorkerStatus, WorkerType
23+
from avalon_sdk.ethereum.ethereum_wrapper import EthereumWrapper
24+
from avalon_sdk.interfaces.work_order_proxy \
25+
import WorkOrderClient
26+
27+
logging.basicConfig(
28+
format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO)
29+
30+
# Return codes
31+
SUCCESS = 0
32+
ERROR = 1
33+
34+
35+
class EthereumWorkOrderProxyImpl(WorkOrderProxy):
36+
37+
"""
38+
This class is meant to write work-order related data to Ethereum
39+
blockchain. Detailed method description is available in the interface
40+
"""
41+
42+
def __init__(self, config):
43+
if self.__validate(config) is True:
44+
self.__initialize(config)
45+
else:
46+
raise Exception("Invalid configuration parameter")
47+
48+
def __validate(self, config):
49+
"""
50+
Validates config parameters for existence.
51+
Returns false if validation fails and true if it succeeds
52+
"""
53+
if config["ethereum"]["proxy_work_order_contract_file"] is None:
54+
logging.error("Missing work order contract file path!!")
55+
return False
56+
if config["ethereum"]["proxy_work_order_contract_address"] is None:
57+
logging.error("Missing work order contract address!!")
58+
return False
59+
return True
60+
61+
def __initialize(self, config):
62+
"""
63+
Initialize the parameters from config to instance variables.
64+
"""
65+
self.__eth_client = EthereumWrapper(config)
66+
tcf_home = environ.get("TCF_HOME", "../../../")
67+
contract_file_name = tcf_home + "/" + \
68+
config["ethereum"]["proxy_work_order_contract_file"]
69+
contract_address = \
70+
config["ethereum"]["proxy_work_order_contract_address"]
71+
self.__contract_instance = self.__eth_client.get_contract_instance(
72+
contract_file_name, contract_address
73+
)
74+
75+
def _is_valid_work_order_json(self, work_order_id, worker_id, requester_id,
76+
work_order_request):
77+
"""
78+
Validate following fields in JSON request against the ones
79+
provided outside the JSON - workOrderId, workerId, requesterId
80+
"""
81+
json_request = json.load(work_order_request)
82+
if (work_order_id == json_request.get("workOrderId")
83+
and worker_id == json_request.get("workerId")
84+
and requester_id == json_request.get("requesterId")):
85+
return True
86+
else:
87+
return False
88+
89+
def work_order_submit(self, work_order_id, worker_id, requester_id,
90+
work_order_request, id=None):
91+
"""
92+
Submit work order request
93+
work_order_id is a unique id to identify the work order request
94+
worker_id is the identifier for the worker
95+
requester_id is a unique id to identify the requester
96+
work_order_request is a json string(Complete definition at
97+
work_order.py interface file)
98+
Returns
99+
An error code, 0 - success, otherwise an error.
100+
"""
101+
if (self.__contract_instance is not None):
102+
if not is_valid_hex_str(
103+
binascii.hexlify(work_order_id).decode("utf8")):
104+
logging.error("Invalid work order id {}".format(work_order_id))
105+
return ERROR
106+
107+
if not is_valid_hex_str(
108+
binascii.hexlify(worker_id).decode("utf8")):
109+
logging.error("Invalid worker id {}".format(worker_id))
110+
return ERROR
111+
112+
if not is_valid_hex_str(
113+
binascii.hexlify(requester_id).decode("utf8")):
114+
logging.error("Invalid requester id {}".format(requester_id))
115+
return ERROR
116+
117+
if not _is_valid_work_order_json(work_order_id, worker_id,
118+
requester_id, work_order_request):
119+
logging.error("Invalid request string {}"
120+
.format(work_order_request))
121+
return ERROR
122+
123+
txn_dict = self.__contract_instance.functions.workOrderSubmit(
124+
work_order_id, worker_id, requester_id, work_order_request
125+
).buildTransaction(
126+
self.__eth_client.get_transaction_params()
127+
)
128+
try:
129+
txn_receipt = self.__eth_client.execute_transaction(txn_dict)
130+
return SUCCESS
131+
except Execption as e:
132+
logging.error(
133+
"execption occured when trying to execute workOrderSubmit \
134+
transaction on chain"+str(e))
135+
return ERROR
136+
else:
137+
logging.error(
138+
"work order contract instance is not initialized")
139+
return ERROR
140+
141+
def work_order_complete(self, work_order_id, work_order_response):
142+
"""
143+
This function is called by the Worker Service to
144+
complete a Work Order successfully or in error.
145+
This API is for proxy model.
146+
params
147+
work_order_id is unique id to identify the work order request
148+
work_order_response is the Work Order response data in string
149+
Returns
150+
An error code, 0 - success, otherwise an error.
151+
"""
152+
if (self.__contract_instance is not None):
153+
if not is_valid_hex_str(
154+
binascii.hexlify(work_order_id).decode("utf8")):
155+
logging.error("Invalid work order id {}".format(work_order_id))
156+
return ERROR
157+
txn_dict = self.__contract_instance.functions.workOrderComplete(
158+
work_order_id, work_order_response).buildTransaction(
159+
self.__eth_client.get_transaction_params()
160+
)
161+
try:
162+
txn_receipt = self.__eth_client.execute_transaction(txn_dict)
163+
return SUCCESS
164+
except Execption as e:
165+
logging.error(
166+
"execption occured when trying to execute \
167+
workOrderComplete transaction on chain"+str(e))
168+
return ERROR
169+
else:
170+
logging.error(
171+
"work order contract instance is not initialized")
172+
return ERROR
173+
174+
def encryption_key_retrieve(self, worker_id, last_used_key_nonce, tag,
175+
requester_id, signature_nonce=None,
176+
signature=None, id=None):
177+
"""
178+
Get Encryption Key Request Payload
179+
"""
180+
pass
181+
182+
def encryption_key_start(self, tag, id=None):
183+
"""
184+
Function to inform the Worker that it should start
185+
encryption key generation for this requester.
186+
"""
187+
pass
188+
189+
def encryption_key_set(self, worker_id, encryption_key,
190+
encryption_nonce, tag, signature, id=None):
191+
"""
192+
Set Encryption Key Request Payload
193+
"""
194+
pass

0 commit comments

Comments
 (0)