14
14
15
15
''' this Component acts as bridge between smart contract deployed in blockchain and KV storage'''
16
16
17
- import os
18
- import sys
19
- from os import urandom
20
- import argparse
21
- import time
22
- import json
23
- import logging
24
- from os .path import dirname , join , abspath
25
17
26
18
from shared_kv .shared_kv_interface import KvStorage
19
+ from connectors .ethereum .ethereum_worker_registry_list_impl import \
20
+ EthereumWorkerRegistryListImpl as registry
21
+ from utility .tcf_types import RegistryStatus
22
+
23
+ import toml
24
+ from os .path import dirname , join , abspath
25
+ import logging
26
+ import json
27
+ import time
28
+ import argparse
29
+ from os import urandom , environ
30
+ import sys
31
+ import os
27
32
28
33
sys .path .insert (0 , abspath (join (dirname (__file__ ), '..' )) + '/tcf_connector/' )
29
34
30
- import EthereumDirectRegistry as registry
31
35
32
36
logger = logging .getLogger (__name__ )
37
+ logger .setLevel (logging .DEBUG )
33
38
34
39
35
40
def str_list_to_bytes_list (str_list ):
36
41
bytes_list = []
37
42
for str in str_list :
38
- bytes_list .append (str . encode ( ))
43
+ bytes_list .append (bytes . fromhex ( str ))
39
44
return bytes_list
40
45
41
46
42
47
def bytes_list_to_str_list (bytes_list ):
43
48
str_list = []
44
49
for byte in bytes_list :
45
- str_list .append (byte .decode (). rstrip ( ' \t \r \n \0 ' ))
50
+ str_list .append (byte .hex ( ))
46
51
return str_list
47
52
48
53
49
- def create_json_object (registry_id , registry , status ):
54
+ def create_json_object (org_id , registry , status ):
55
+ logger .debug ("create_json_object id: %s, registry: %s" ,
56
+ org_id .hex (), registry )
50
57
registry_info = dict ()
51
58
# registry[0] contains registryType which symbolizes if registry present ( value equals to 1).
52
- registry_info ["orgId" ] = (registry_id .decode ()).rstrip (' \t \r \n \0 ' ) # Strip off null bytes added by decode
53
- registry_info ["uri" ] = registry [1 ]
54
- registry_info ["scAddress" ] = (registry [2 ].decode ()).rstrip (' \t \r \n \0 ' )
55
- app_ids_bytes = registry [3 ] # list of application ids
59
+ registry_info ["orgId" ] = org_id .hex ()
60
+ registry_info ["uri" ] = registry [0 ]
61
+ registry_info ["scAddress" ] = registry [1 ].hex ()
62
+ app_ids_bytes = registry [2 ] # list of application ids
63
+ logger .debug ("app_ids_bytes %s" , app_ids_bytes )
56
64
app_ids_list = bytes_list_to_str_list (app_ids_bytes )
57
65
registry_info ["appTypeIds" ] = app_ids_list
58
- registry_info ["status" ] = status
66
+ registry_info ["status" ] = status .value
67
+ logger .debug ("registry_info %s" , registry_info )
59
68
60
69
json_registry = json .dumps (registry_info )
61
- logger .debug ("JSON serialized registry is %s" , json_registry )
70
+ logger .debug ("JSON serialized registry %s is %s" , org_id . hex () , json_registry )
62
71
return json_registry
63
72
64
73
65
74
def deserialize_json_object (json_reg_info ):
66
75
reg_info = json .loads (json_reg_info )
67
76
uri = reg_info ["uri" ]
68
- sc_address = reg_info ["scAddress" ]. encode ( )
77
+ sc_address = bytes . fromhex ( reg_info ["scAddress" ])
69
78
app_ids_str = reg_info ["appTypeIds" ]
70
79
71
80
# Convert list of appTypeIds of type string to bytes
@@ -76,32 +85,37 @@ def deserialize_json_object(json_reg_info):
76
85
77
86
def sync_contract_and_lmdb (eth_direct_registry , kv_storage ):
78
87
# Check if all registries in smart contract are available in KvStorage, if not add them
79
- eth_lookup_result = eth_direct_registry .RegistryLookUp ()
80
- eth_registry_list = eth_lookup_result [- 1 ] # last value of lookup will be list of registry id's/org id's
88
+ eth_lookup_result = eth_direct_registry .registry_lookup ()
89
+ # last value of lookup will be list of registry id's/org id's
90
+ org_id_list = eth_lookup_result [- 1 ]
81
91
82
92
logger .info ("Syncing registries from Contract to KvStorage" )
83
- if not eth_registry_list :
93
+ if not org_id_list :
84
94
logger .info ("No registries available in Direct registry contract" )
85
95
86
96
else :
87
- for eth_registry_id in eth_registry_list :
88
- eth_reg_info = eth_direct_registry .RegistryRetrieve ( eth_registry_id )
89
-
97
+ for org_id in org_id_list :
98
+ eth_reg_info = eth_direct_registry .registry_retrieve ( org_id )
99
+ logger . debug ( "eth_reg_info: %s" , eth_reg_info )
90
100
# Check if registry entry present in KvStorage
91
- logger .info ("Check if registry with id %s present in KvStorage" , eth_registry_id .decode ())
92
- kv_reg_info = kv_storage .get ("registries" , eth_registry_id .decode ().rstrip (' \t \r \n \0 ' ))
101
+ logger .info (
102
+ "Check if registry with id %s present in KvStorage" , org_id .hex ())
103
+ kv_reg_info = kv_storage .get ("registries" , org_id )
93
104
if not kv_reg_info :
94
- logger .info ("No matching registry found in KvStorage, ADDING it to KvStorage" )
105
+ logger .info (
106
+ "No matching registry found in KvStorage, ADDING it to KvStorage" )
95
107
# Create JSON registry object with status 0 equivalent to SUSPENDED
96
- json_registry = create_json_object (eth_registry_id , eth_reg_info , 0 )
97
- kv_storage .set ("registries" , eth_registry_id . decode (). rstrip ( ' \t \r \n \0 ' ), json_registry )
108
+ json_registry = create_json_object (org_id , eth_reg_info , RegistryStatus . SUSPENDED )
109
+ kv_storage .set ("registries" , org_id . hex ( ), json_registry )
98
110
99
111
else :
100
- logger .info ("Matching registry found in KvStorage, hence ADDING" )
112
+ logger .info (
113
+ "Matching registry found in KvStorage, hence ADDING" )
101
114
# Set the status of registry to ACTIVE
102
115
kv_registry = json .loads (kv_reg_info )
103
116
kv_registry ["status" ] = 1 # status = 1 implies ACTIVE
104
- kv_storage .set ("registries" , eth_registry_id .decode ().rstrip (' \t \r \n \0 ' ), json .dumps (kv_registry ))
117
+ kv_storage .set ("registries" , org_id .hex (),
118
+ json .dumps (kv_registry ))
105
119
106
120
logger .info ("Syncing registries from KvStorage to Contract" )
107
121
# Check if all registries present in KvStorage are available in Smart contract, if not add them
@@ -114,21 +128,30 @@ def sync_contract_and_lmdb(eth_direct_registry, kv_storage):
114
128
for kv_registry_id in kv_registry_list :
115
129
kv_reg_info = kv_storage .get ("registries" , kv_registry_id )
116
130
# Check if registry entry present in smart contract
117
- retrieve_result = eth_direct_registry .RegistryRetrieve (kv_registry_id .encode ())
131
+ logger .debug (
132
+ "found registry_retrieve: %s from kv store" , kv_registry_id )
133
+ retrieve_result = eth_direct_registry .registry_retrieve (
134
+ bytes .fromhex (kv_registry_id ))
118
135
119
136
if retrieve_result [0 ] == 0 :
120
- logger .info ("Matching registry with id %s not found in Smart contract, hence ADDING" , kv_registry_id )
137
+ logger .info (
138
+ "Matching registry with id %s not found in Smart contract, hence ADDING" , kv_registry_id )
121
139
reg_info = deserialize_json_object (kv_reg_info )
122
140
# Add registry to smart contract and set status to ACTIVE
123
- eth_direct_registry .RegistryAdd (kv_registry_id .encode (), reg_info [0 ], reg_info [1 ], reg_info [2 ])
124
- eth_direct_registry .RegistrySetStatus (kv_registry_id .encode (), json .loads (kv_reg_info )["status" ])
141
+ eth_direct_registry .registry_add (
142
+ bytes .fromhex (kv_registry_id ), reg_info [0 ], reg_info [1 ], reg_info [2 ])
143
+ eth_direct_registry .registry_set_status (
144
+ bytes .fromhex (kv_registry_id ), RegistryStatus .ACTIVE )
125
145
126
146
else :
127
147
# Set status of registry to registry status in KvStorage
128
- logger .info ("Matching registry %s found in Smart contract, CHANGING status" , kv_registry_id )
129
- eth_direct_registry .RegistrySetStatus (kv_registry_id .encode (), json .loads (kv_reg_info )["status" ])
148
+ logger .info (
149
+ "Matching registry %s found in Smart contract, CHANGING status" , kv_registry_id )
150
+ eth_direct_registry .registry_set_status (
151
+ bytes .fromhex (kv_registry_id ), RegistryStatus .ACTIVE ) # TODO
130
152
131
- logger .info ("-------------- Direct Registry bridge flow complete ------------------- " )
153
+ logger .info (
154
+ "-------------- Direct Registry bridge flow complete ------------------- " )
132
155
return
133
156
134
157
@@ -137,9 +160,11 @@ def main(args=None):
137
160
138
161
# Smart contract address is the address where smart contract is deployed.
139
162
# TODO: Add mechanism to read the address from config file.
140
-
141
- eth_direct_registry = registry .EthereumDirectRegistry ("0x8c99670a15047248403a3E5A38eb8FBE7a12533e" , \
142
- '../connectors/contracts/WorkerRegistryList.sol' )
163
+ tcf_home = environ .get ("TCF_HOME" , "../../" )
164
+ config_file = tcf_home + "/examples/common/python/connectors/" + "tcf_connector.toml"
165
+ with open (config_file ) as fd :
166
+ config = toml .load (fd )
167
+ eth_direct_registry = registry (config )
143
168
kv_storage = KvStorage ()
144
169
kv_storage .open ("kv_storage" )
145
170
0 commit comments