def __init__(self, username, enodeString, addressString): threading.Thread.__init__(self, daemon=True) self.status = S_INITIAL self.username = username self.enodeString = enodeString self.addressString = addressString self.nodekey = None self.ip = None self.gethPort = None self.address = None self.udpSoc = None self.trackersList = None self.peersString = [] self.peersConnexion = False #self.tcpSoc = None # check if there is already a client instance for th in threading.enumerate(): if "[thread]_{0}-client".format(self.username) in th.name: console.error( "client instance already executed for {0}".format( self.username)) self.status = S_ERROR return self.name = "[thread]_{0}-client".format(self.username) console.info("client initialized") return
def __init__(self, username, enodeS, addressS, eigentrust, dealer): threading.Thread.__init__(self, daemon=True) self.status = S_INITIAL self.username = username self.enodeS = enodeS self.addressS = addressS self.udpGuestSoc = None self.eigentrust = eigentrust self.dealer = dealer self.guests = [] #self.tcpTrackerSoc = None #self.trackers = [] # check if there is already a node instance for th in threading.enumerate(): if "[thread]_{0}-tracker".format(self.username) in th.name: console.error( "tracker instance already executed for {0}".format( self.username)) self.status = S_ERROR return self.name = "[thread]_{0}-tracker".format(self.username) console.info("tracker initialized") return
def __init__(self, user): threading.Thread.__init__(self, daemon=True) self.status = S_INITIAL self.pid = 0 self.user = user self.log = None self.proc = None self.eigentrust = None self.eigentrustEvents = None self.dealer = None self.dealerEvents = None # check if there is already a node instance for th in threading.enumerate(): if "[thread]_{0}-geth".format(self.user[0]) in th.name: console.error("geth instance already executed for {0}".format( self.user[0])) self.status = S_ERROR return self.name = "[thread]_{0}-geth".format(self.user[0]) console.info("geth initialized") return
def __run_geth_subprocess(self): port = tools.next_free_port() cmdlist = [ "geth", "--datadir", "{0}/eth_{1}/".format(ROOT, self.user[0]), "--networkid", str(GETH_NETWORK_ID), "--port", str(port) ] + GETH_FLAGS with open( "{0}/eth_{1}/{2}".format(ROOT, self.user[0], GETH_LOG_FILENAME), 'w') as self.log: self.proc = subprocess.Popen(cmdlist, stdin=None, stdout=None, stderr=self.log, shell=False) self.log = open( "{0}/eth_{1}/{2}".format(ROOT, self.user[0], GETH_LOG_FILENAME), 'r') time.sleep(UPDATE_WAITING_TIME) if (self.proc.poll() != None): console.error( "failed to run geth subprocess, return code {0}".format( self.proc.poll())) return console.info("geth subprocess is renning on {0}".format(port)) return True
def check_addressS(addressString): if (len(addressString) == 42 and addressString[:2] == "0x"): addressString = addressString[2:] else: console.error( "invalid address : 42-length address must be start with \"0x\"") return False if not (len(addressString) == 40 and is_hex_string(addressString)): console.error("invalid address : address must be 40-length hex string") return False return True
def run(self): if (self.status == S_ERROR): self.end() return # check if the eth_username folder is initialized if not os.path.exists("{0}/eth_{1}".format(ROOT, self.user[0])): console.error("eth_{0} folder not initialized".format( self.user[0])) self.status = S_ERROR self.end() return # run geth subprocess if not self.__run_geth_subprocess(): self.status = S_ERROR self.end() return # connect to geth client with IPC file if not self.__IPC_connection(): self.status = S_ERROR self.end() return # check valid coinbase and user/password and set defaultAccount if not self.__check_coinbase(): self.status = S_ERROR self.end() return #self.enodeS = self.w3.geth.admin.node_info()["enode"] #self.addressS = self.w3.eth.defaultAccount # load eigentrust contract self.__load_eigentrust() # load dealer contract self.__load_dealer() self.status = S_VALID console.info("geth is running") while (self.status == S_VALID): self.update() self.status = S_EXIT self.end() return
def __IPC_connection(self): console.info("waiting IPC connection ...") while not os.path.exists("./eth_{0}/geth.ipc".format(self.user[0])): time.sleep(UPDATE_WAITING_TIME) # need set provider in local variable to use clique methode provider = Web3.IPCProvider("./eth_{0}/geth.ipc".format(self.user[0])) self.w3 = Web3(provider) self.w3.middleware_onion.inject(geth_poa_middleware, layer=0) if (self.w3.isConnected()): console.info( "IPC connection successful to {0} geth subprocess".format( self.user[0])) return True else: console.error("IPC connection failed") return False
def __check_coinbase(self): try: self.w3.eth.defaultAccount = self.w3.eth.coinbase except ValueError: console.error("coinbase not initialized") return False console.info("{0} setted to default account".format( self.w3.eth.coinbase)) try: self.unlock_coinbase(1) except ValueError: console.error("wrong password") return False self.lock_coinbase() return True
def __init__(self, username, contractName, w3): threading.Thread.__init__(self, daemon = True) self.username = username self.contractName = contractName self.status = S_INITIAL # check if there is already a node instance for th in threading.enumerate(): if "[thread]_{0}-{1}".format(self.username,self.contractName) in th.name : console.error("{1} already executed for {0}".format(self.username, self.contractName)) self.status = S_ERROR return self.name = "[thread]_{0}-{1}".format(self.username, self.contractName) console.info("{0} initialized".format(self.contractName)) return
def __init__(self): threading.Thread.__init__(self, daemon=True) self.status = S_INITIAL self.user = (input("Username : "******"Password : "******"[thread]_{0}-node".format(self.user[0]) in th.name: console.error("node instance already executed for {0}".format( self.user[0])) self.status = S_ERROR return self.name = "[thread]_{0}-node".format(self.user[0]) console.info("node initialized") return
def check_enodeS(enodeString): if (enodeString[:8] != "enode://"): console.error("invalid enode : doesn't start with \"enode://\"") return False if (enodeString[136] != "@"): console.error("invalid enode : 136th char must be '@'") return False if not is_hex_string(enodeString[8:136]): console.error( "invalid enode : public key must be 128-length hex string") return False return True
def run(self): if (self.status == S_ERROR): return self.geth = Geth(self.user) self.geth.start() while (self.geth.status == S_INITIAL): time.sleep(UPDATE_WAITING_TIME) if (self.geth.status != S_VALID): self.geth.stop() self.geth.join() self.geth = None console.error("node instance aborted") self.status == S_ERROR self.end() return """ #if (NODE_TYPE == "tracker"): self.tracker = Tracker(self.user[0], self.geth.enodeS, self.geth.addressS, self.geth.eigentrust, self.geth.dealer) self.tracker.start() while (self.tracker.status == S_INITIAL): time.sleep(UPDATE_WAITING_TIME) if (self.tracker.status != S_VALID): self.tracker.stop() self.tracker.join() self.tracker = None console.warn("only geth is running") elif (NODE_TYPE == "client"): self.client = Client(self.user[0], self.geth.enodeString, self.geth.addressString) self.client.start() while (self.client.status == S_INITIAL): time.sleep(UPDATE_WAITING_TIME) if (self.client.status != S_VALID): self.client.stop() self.client.join() self.client = None console.error("node instance aborted") self.status == S_ERROR self.end() return else: console.warn("no NODE_TYPE specified") console.warn("only geth is running") """ # Create an UDP based socket self.udpGuestSoc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.udpGuestSoc.settimeout(UPDATE_WAITING_TIME) port = tools.next_free_port() self.udpGuestSoc.bind(('', port)) console.info("udp socket openned, listenning on {0}".format(port)) if os.path.isfile("{0}/{1}".format(ROOT, TRACKERS_LIST_FILENAME)): with open("{0}/{1}".format(ROOT, TRACKERS_LIST_FILENAME), 'r') as fd: trackers = json.load(fd) console.info("{0} tracker address loaded".format(len(trackers))) self.status = S_VALID console.info("node is runing") # thread loop while (self.status == S_VALID): self.update_udp() self.status = S_EXIT self.end() return
def __identity(self, frame, contact): valid = False newGuest = Guest(self.username) console.print("new connexion from {0}:{1}".format( contact[0], contact[1]), who=newGuest.who) try: if (frame[0] == BC_BEGIN_IDENTITY): console.bc(frame[0], who=newGuest.who) index = 1 while (frame[index] != BC_END_IDENTITY): console.bc(frame[index], who=newGuest.who) if (frame[index] == BC_ERROR): console.warn("identity frame aborted", who=newGuest.who) valid = False return -1, [] elif (frame[index] == BC_IDENTITY_USER): index += 1 newGuest.guestType = "user" newGuest.enode = frame[index:index + 70] if ([int(contact[0].split('.')[x]) for x in range(4)] != newGuest.enode[64:68]): console.warn("guest's ip doesn't match", who=newGuest.who) valid = False return -1, [] index += 70 valid = True elif (frame[index] == BC_IDENTITY_TRACKER): index += 1 newGuest.guestType = "tracker" # # code # valid = True console.error("not implemented") return -1, [] else: valid = False console.warn("invalid balise in identity frame", who=newGuest.who) return -1, [] console.bc(frame[index], who=newGuest.who) index += 1 if valid: newGuest.udpPyPort = list(contact[1].to_bytes( 2, byteorder='big')) newGuest.update() n = self.__guest_exist(newGuest) if (n == -1): self.guests.append(newGuest) n = len(self.guests) - 1 console.print("identity completed", who=newGuest.who) return n, frame[index:] else: console.warn("incomplete identity", who=newGuest.who) return -1, [] else: raise IndexError except IndexError: console.warn("bad frame construction from {0}:{1}".format( contact[0], contact[1]), who=newGuest.who) return -1, []