1414
1515''' this Component acts as bridge between smart contract deployed in blockchain and KV storage'''
1616
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
2517
2618from 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
2732
2833sys .path .insert (0 , abspath (join (dirname (__file__ ), '..' )) + '/tcf_connector/' )
2934
30- import EthereumDirectRegistry as registry
3135
3236logger = logging .getLogger (__name__ )
37+ logger .setLevel (logging .DEBUG )
3338
3439
3540def str_list_to_bytes_list (str_list ):
3641 bytes_list = []
3742 for str in str_list :
38- bytes_list .append (str . encode ( ))
43+ bytes_list .append (bytes . fromhex ( str ))
3944 return bytes_list
4045
4146
4247def bytes_list_to_str_list (bytes_list ):
4348 str_list = []
4449 for byte in bytes_list :
45- str_list .append (byte .decode (). rstrip ( ' \t \r \n \0 ' ))
50+ str_list .append (byte .hex ( ))
4651 return str_list
4752
4853
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 )
5057 registry_info = dict ()
5158 # 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 )
5664 app_ids_list = bytes_list_to_str_list (app_ids_bytes )
5765 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 )
5968
6069 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 )
6271 return json_registry
6372
6473
6574def deserialize_json_object (json_reg_info ):
6675 reg_info = json .loads (json_reg_info )
6776 uri = reg_info ["uri" ]
68- sc_address = reg_info ["scAddress" ]. encode ( )
77+ sc_address = bytes . fromhex ( reg_info ["scAddress" ])
6978 app_ids_str = reg_info ["appTypeIds" ]
7079
7180 # Convert list of appTypeIds of type string to bytes
@@ -76,32 +85,37 @@ def deserialize_json_object(json_reg_info):
7685
7786def sync_contract_and_lmdb (eth_direct_registry , kv_storage ):
7887 # 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 ]
8191
8292 logger .info ("Syncing registries from Contract to KvStorage" )
83- if not eth_registry_list :
93+ if not org_id_list :
8494 logger .info ("No registries available in Direct registry contract" )
8595
8696 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 )
90100 # 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 )
93104 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" )
95107 # 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 )
98110
99111 else :
100- logger .info ("Matching registry found in KvStorage, hence ADDING" )
112+ logger .info (
113+ "Matching registry found in KvStorage, hence ADDING" )
101114 # Set the status of registry to ACTIVE
102115 kv_registry = json .loads (kv_reg_info )
103116 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 ))
105119
106120 logger .info ("Syncing registries from KvStorage to Contract" )
107121 # 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):
114128 for kv_registry_id in kv_registry_list :
115129 kv_reg_info = kv_storage .get ("registries" , kv_registry_id )
116130 # 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 ))
118135
119136 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 )
121139 reg_info = deserialize_json_object (kv_reg_info )
122140 # 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 )
125145
126146 else :
127147 # 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
130152
131- logger .info ("-------------- Direct Registry bridge flow complete ------------------- " )
153+ logger .info (
154+ "-------------- Direct Registry bridge flow complete ------------------- " )
132155 return
133156
134157
@@ -137,9 +160,11 @@ def main(args=None):
137160
138161 # Smart contract address is the address where smart contract is deployed.
139162 # 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 )
143168 kv_storage = KvStorage ()
144169 kv_storage .open ("kv_storage" )
145170
0 commit comments