Esempio n. 1
0
class RpcClient:
        
    def __init__(self):
        self.rpc_ip, self.rpc_port, self.rpc_user, self.rpc_passwd = readRPCfile()
        rpc_url = "http://%s:%s@%s:%d" % (self.rpc_user, self.rpc_passwd, self.rpc_ip, self.rpc_port)
        try:    
            self.conn = AuthServiceProxy(rpc_url, timeout=8)     
        except JSONRPCException as e:
            err_msg = 'remote or local PIVX-cli running?'
            printException(getCallerName(), getFunctionName(), err_msg, e)
        except Exception as e:
            err_msg = 'remote or local PIVX-cli running?'
            printException(getCallerName(), getFunctionName(), err_msg, e)
            
    
    
    
    def decodeRawTransaction(self, rawTx):
        try:
            return self.conn.decoderawtransaction(rawTx)    
        except Exception as e:
            err_msg = 'error in decodeRawTransaction'
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            
    
    
    
    def getAddressUtxos(self, addresses):
        try:
            return self.conn.getaddressutxos({'addresses': addresses})    
        except Exception as e:
            err_msg = "error in getAddressUtxos"
            if str(e.args[0]) != "Request-sent":
                printException(getCallerName(), getFunctionName(), err_msg, e.args)
            else:
                printException(getCallerName(), getFunctionName(), err_msg, e.args)
            raise e
    
    
    
    
    def getBlockCount(self):
        try:
            n = self.conn.getblockcount()
            return n
        except Exception as e:
            err_msg = 'remote or local PIVX-cli running?'
            if str(e.args[0]) != "Request-sent":
                printException(getCallerName(), getFunctionName(), err_msg, e.args)
            else:
                printException(getCallerName(), getFunctionName(), err_msg, e.args)
    
    
    
    
    def getBlockHash(self, blockNum):
        try:
            h = self.conn.getblockhash(blockNum)
            return h
        except Exception as e:
            err_msg = 'remote or local PIVX-cli running?'
            if str(e.args[0]) != "Request-sent":
                printException(getCallerName(), getFunctionName(), err_msg, e.args)
            else:
                printException(getCallerName(), getFunctionName(), err_msg, e.args)
    
    
    def getFeePerKb(self):
        try:
            # get transaction data from last 10 blocks
            feePerKb = float(self.conn.getfeeinfo(10)['feeperkb'])
            return (feePerKb if feePerKb > MINIMUM_FEE else MINIMUM_FEE)
        except Exception as e:
            err_msg = 'error in getFeePerKb'
            if str(e.args[0]) != "Request-sent":
                printException(getCallerName(), getFunctionName(), err_msg, e.args)
            else:
                printException(getCallerName(), getFunctionName(), err_msg, e.args)
    
    
    
    def getMNStatus(self, address):
        try:
            mnStatusList = self.conn.listmasternodes(address)
            if not mnStatusList:
                return None
            mnStatus = mnStatusList[0]
            mnStatus['mnCount'] = self.conn.getmasternodecount()['enabled']
            return mnStatus
        
        except Exception as e:
            err_msg = "error in getMNStatus"
            if str(e.args[0]) != "Request-sent":
                printException(getCallerName(), getFunctionName(), err_msg, e.args)
            else:
                printException(getCallerName(), getFunctionName(), err_msg, e.args)
    
    
    
    
    def getProtocolVersion(self):
        try:
            prot_version = self.conn.getinfo().get('protocolversion')
            return int(prot_version)
        
        except Exception as e:
            err_msg = 'error in getProtocolVersion'
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            return DEFAULT_PROTOCOL_VERSION    
     
            
    
    
    def getRawTransaction(self, txid):
        try:
            return self.conn.getrawtransaction(txid)
        except Exception as e:
            err_msg = "is Blockchain synced?"
            if str(e.args[0]) != "Request-sent":
                printException(getCallerName(), getFunctionName(), err_msg, e.args)
            return None
    
    
    
    
    def getStatus(self):
        status = False
        n = 0
        try:
            n = self.conn.getblockcount()
            if n > 0:
                status = True
                
        except Exception as e:
            # If loading block index set lastBlock=1
            if str(e.args[0]) == "Loading block index..." or str(e.args[0]) == "Verifying blocks...":
                printDbg(str(e.args[0]))
                n = 1
            #else:
                #err_msg = "Error while contacting RPC server"
                #printException(getCallerName(), getFunctionName(), err_msg, e.args)    
        return status, n
    
    
    
    
    def getStatusMess(self, status=None):
        if status == None:
            status = self.getStatus()    
        if status: 
            return "RPC status: CONNECTED!!!"
        else:
            return "RPC status: NOT CONNECTED. remote or local PIVX-cli running?"
    
    
    
    def isBlockchainSynced(self):
        try:
            return self.conn.mnsync('status').get("IsBlockchainSynced")
        
        except Exception as e:
            err_msg = "error in isBlockchainSynced"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            return False
            
            
            
    def decodemasternodebroadcast(self, work):
        try:
            return self.conn.decodemasternodebroadcast(work.strip())
        
        except Exception as e:
            err_msg = "error in decodemasternodebroadcast"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            return ""
            
            
    
    def relaymasternodebroadcast(self, work):
        try:
            return self.conn.relaymasternodebroadcast(work.strip())
        
        except Exception as e:
            err_msg = "error in relaymasternodebroadcast"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)    
            return ""


    def sendRawTransaction(self, tx_hex):
        try:
            tx_id = self.conn.sendrawtransaction(tx_hex)
            return tx_id
        except Exception as e:
            err_msg = 'error in rpcClient.sendRawTransaction'
            if str(e.args[0]) != "Request-sent":
                printException(getCallerName(), getFunctionName(), err_msg, e.args)
            else:
                printException(getCallerName(), getFunctionName(), err_msg, e.args)
    
    
    
    
    def verifyMessage(self, pivxaddress, signature, message):
        try:
            return self.conn.verifymessage(pivxaddress, signature, message)
        
        except Exception as e:
            err_msg = "error in verifyMessage"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            
Esempio n. 2
0
class RpcClient:
    def __init__(self):
        # Lock for threads
        self.lock = threading.Lock()
        self.rpc_ip, self.rpc_port, self.rpc_user, self.rpc_passwd = readRPCfile(
        )
        rpc_url = "http://%s:%s@%s:%d" % (self.rpc_user, self.rpc_passwd,
                                          self.rpc_ip, self.rpc_port)
        try:
            self.lock.acquire()
            self.conn = AuthServiceProxy(rpc_url, timeout=120)
        except JSONRPCException as e:
            err_msg = 'remote or local PIVX-cli running?'
            printException(getCallerName(), getFunctionName(), err_msg, e)
        except Exception as e:
            err_msg = 'remote or local PIVX-cli running?'
            printException(getCallerName(), getFunctionName(), err_msg, e)
        finally:
            self.lock.release()

    def decodeRawTransaction(self, rawTx):
        try:
            self.lock.acquire()
            res = self.conn.decoderawtransaction(rawTx)
        except Exception as e:
            err_msg = 'error in decodeRawTransaction'
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = None
        finally:
            self.lock.release()

        return res

    def getAddressUtxos(self, addresses):
        try:
            self.lock.acquire()
            res = self.conn.getaddressutxos({'addresses': addresses})
        except Exception as e:
            err_msg = "error in getAddressUtxos"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = None
        finally:
            self.lock.release()

        return res

    def getBlockCount(self):
        try:
            self.lock.acquire()
            n = self.conn.getblockcount()
        except Exception as e:
            err_msg = 'remote or local PIVX-cli running?'
            if str(e.args[0]) != "Request-sent":
                printException(getCallerName(), getFunctionName(), err_msg,
                               e.args)
            n = 0
        finally:
            self.lock.release()

        return n

    def getBlockHash(self, blockNum):
        try:
            self.lock.acquire()
            h = self.conn.getblockhash(blockNum)
        except Exception as e:
            err_msg = 'remote or local PIVX-cli running?'
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            h = None
        finally:
            self.lock.release()

        return h

    def getBudgetVotes(self, proposal):
        try:
            self.lock.acquire()
            votes = self.conn.getbudgetvotes(proposal)
        except Exception as e:
            err_msg = 'remote or local PIVX-cli running?'
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            votes = {}
        finally:
            self.lock.release()

        return votes

    def getFeePerKb(self):
        try:
            self.lock.acquire()
            # get transaction data from last 200 blocks
            feePerKb = float(self.conn.getfeeinfo(200)['feeperkb'])
            res = (feePerKb if feePerKb > MINIMUM_FEE else MINIMUM_FEE)
        except Exception as e:
            err_msg = 'error in getFeePerKb'
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = MINIMUM_FEE
        finally:
            self.lock.release()

        return res

    def getMNStatus(self, address):
        try:
            self.lock.acquire()
            mnStatusList = self.conn.listmasternodes(address)
            if not mnStatusList:
                return None
            mnStatus = mnStatusList[0]
            mnStatus['mnCount'] = self.conn.getmasternodecount()['enabled']
        except Exception as e:
            err_msg = "error in getMNStatus"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            mnStatus = None
        finally:
            self.lock.release()

        return mnStatus

    def getMasternodeCount(self):
        try:
            self.lock.acquire()
            ans = self.conn.getmasternodecount()
        except Exception as e:
            err_msg = "error in getMasternodeCount"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            ans = None
        finally:
            self.lock.release()

        return ans

    def getMasternodes(self):
        mnList = {}
        mnList['last_update'] = now()
        score = []
        try:
            self.lock.acquire()
            masternodes = self.conn.listmasternodes()
        except Exception as e:
            err_msg = "error in getMasternodes"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            masternodes = []
        finally:
            self.lock.release()

        for mn in masternodes:

            if mn.get('status') == 'ENABLED':
                if mn.get('lastpaid') == 0:
                    mn['score'] = mn.get('activetime')
                else:
                    lastpaid_ago = now() - mn.get('lastpaid')
                    mn['score'] = min(lastpaid_ago, mn.get('activetime'))

            else:
                mn['score'] = 0

            score.append(mn)

        score.sort(key=lambda x: x['score'], reverse=True)

        for mn in masternodes:
            mn['queue_pos'] = score.index(mn)

        mnList['masternodes'] = masternodes

        return mnList

    def getNextSuperBlock(self):
        try:
            self.lock.acquire()
            n = self.conn.getnextsuperblock()
        except Exception as e:
            err_msg = 'remote or local PIVX-cli running?'
            if str(e.args[0]) != "Request-sent":
                printException(getCallerName(), getFunctionName(), err_msg,
                               e.args)
            n = 0
        finally:
            self.lock.release()

        return n

    def getProposals(self):
        proposals = []
        try:
            self.lock.acquire()
            data = self.conn.getbudgetinfo()
        except Exception as e:
            err_msg = "error getting proposals"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            data = []
        finally:
            self.lock.release()

        for p in data:
            new_proposal = Proposal(p.get('Name'), p.get('URL'), p.get('Hash'),
                                    p.get('FeeHash'), p.get('BlockStart'),
                                    p.get('BlockEnd'),
                                    p.get('TotalPaymentCount'),
                                    p.get('RemainingPaymentCount'),
                                    p.get('PaymentAddress'), p.get('Yeas'),
                                    p.get('Nays'), p.get('Abstains'),
                                    float(p.get('TotalPayment')),
                                    float(p.get('MonthlyPayment')))
            proposals.append(new_proposal)

        return proposals

    def getProposalsProjection(self):
        proposals = []
        try:
            self.lock.acquire()
            data = self.conn.getbudgetprojection()
        except Exception as e:
            err_msg = "error getting proposals projection"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            data = []
        finally:
            self.lock.release()

        for p in data:
            new_proposal = Proposal(p.get('Name'), p.get('URL'), p.get('Hash'),
                                    p.get('FeeHash'), p.get('BlockStart'),
                                    p.get('BlockEnd'),
                                    p.get('TotalPaymentCount'),
                                    p.get('RemainingPaymentCount'),
                                    p.get('PaymentAddress'), p.get('Yeas'),
                                    p.get('Nays'), p.get('Abstains'),
                                    p.get('TotalPayment'),
                                    p.get('MonthlyPayment'))
            new_proposal = {}
            new_proposal['Name'] = p.get('Name')
            new_proposal['Allotted'] = float(p.get("Alloted"))
            new_proposal['Votes'] = p.get('Yeas') - p.get('Nays')
            new_proposal['Total_Allotted'] = float(p.get('TotalBudgetAlloted'))
            proposals.append(new_proposal)

        return proposals

    def getProtocolVersion(self):
        try:
            self.lock.acquire()
            prot_version = self.conn.getinfo().get('protocolversion')
            res = int(prot_version)
        except Exception as e:
            err_msg = 'error in getProtocolVersion'
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = DEFAULT_PROTOCOL_VERSION
        finally:
            self.lock.release()

        return res

    def getRawTransaction(self, txid):
        try:
            self.lock.acquire()
            res = self.conn.getrawtransaction(txid)
        except Exception as e:
            err_msg = "is Blockchain synced?"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = None
        finally:
            self.lock.release()

        return res

    def getStatus(self):
        status = False
        statusMess = "Unable to connect to a PIVX RPC server.\n"
        statusMess += "Either the local PIVX wallet is not open, or the remote RPC server is not responding."
        n = 0
        try:
            self.lock.acquire()
            n = self.conn.getblockcount()
            if n > 0:
                status = True
                statusMess = "Connected to PIVX RPC client"

        except Exception as e:
            # If loading block index set lastBlock=1
            if str(e.args[0]) == "Loading block index..." or str(
                    e.args[0]) == "Verifying blocks...":
                printDbg(str(e.args[0]))
                statusMess = "PIVX wallet is connected but still synchronizing / verifying blocks"
                n = 1
            elif str(e.args[0]) != "Request-sent" and str(
                    e.args[0]) != "10061":
                err_msg = "Error while contacting RPC server"
                printException(getCallerName(), getFunctionName(), err_msg,
                               e.args)

        finally:
            self.lock.release()

        return status, statusMess, n

    def isBlockchainSynced(self):
        try:
            self.lock.acquire()
            res = self.conn.mnsync('status').get("IsBlockchainSynced")
        except Exception as e:
            if str(e.args[0]) != "Request-sent":
                err_msg = "error in isBlockchainSynced"
                printException(getCallerName(), getFunctionName(), err_msg,
                               e.args)
            res = False
        finally:
            self.lock.release()

        return res

    def mnBudgetRawVote(self, mn_tx_hash, mn_tx_index, proposal_hash, vote,
                        time, vote_sig):
        try:
            self.lock.acquire()
            res = self.conn.mnbudgetrawvote(mn_tx_hash, mn_tx_index,
                                            proposal_hash, vote, time,
                                            vote_sig)
        except Exception as e:
            err_msg = "error in mnBudgetRawVote"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = None
        finally:
            self.lock.release()

        return res

    def decodemasternodebroadcast(self, work):
        try:
            self.lock.acquire()
            res = self.conn.decodemasternodebroadcast(work.strip())
        except Exception as e:
            err_msg = "error in decodemasternodebroadcast"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = ""
        finally:
            self.lock.release()

        return res

    def relaymasternodebroadcast(self, work):
        try:
            self.lock.acquire()
            res = self.conn.relaymasternodebroadcast(work.strip())
        except Exception as e:
            err_msg = "error in relaymasternodebroadcast"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = ""
        finally:
            self.lock.release()

        return res

    def sendRawTransaction(self, tx_hex, use_swiftx):
        try:
            self.lock.acquire()
            tx_id = self.conn.sendrawtransaction(tx_hex, True,
                                                 bool(use_swiftx))
        except Exception as e:
            err_msg = 'error in rpcClient.sendRawTransaction'
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            tx_id = None
        finally:
            self.lock.release()

        return tx_id

    def verifyMessage(self, pivxaddress, signature, message):
        try:
            self.lock.acquire()
            res = self.conn.verifymessage(pivxaddress, signature, message)
        except Exception as e:
            err_msg = "error in verifyMessage"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = False
        finally:
            self.lock.release()

        return res
Esempio n. 3
0
class RpcClient:

    def __init__(self, rpc_protocol, rpc_host, rpc_user, rpc_password):
        # Lock for threads
        self.lock = threading.RLock()

        self.rpc_url = "%s://%s:%s@%s" % (rpc_protocol, rpc_user, rpc_password, rpc_host)

        host, port = rpc_host.split(":")
        if rpc_protocol == "https":
            self.httpConnection = httplib.HTTPSConnection(host, port, timeout=20, context=ssl._create_unverified_context())
        else:
            self.httpConnection = httplib.HTTPConnection(host, port, timeout=20)

        self.conn = AuthServiceProxy(self.rpc_url, timeout=1000, connection=self.httpConnection)




    @process_RPC_exceptions
    def getBlockCount(self):
        n = 0
        with self.lock:
            n = self.conn.getblockcount()

        return n



    @process_RPC_exceptions
    def getBlockHash(self, blockNum):
        h = None
        with self.lock:
            h = self.conn.getblockhash(blockNum)

        return h



    @process_RPC_exceptions
    def getBudgetVotes(self, proposal):
        votes = {}
        with self.lock:
            votes = self.conn.getbudgetvotes(proposal)

        return votes



    @process_RPC_exceptions
    def getFeePerKb(self):
        res = MINIMUM_FEE
        with self.lock:
            # get transaction data from last 200 blocks
            feePerKb = float(self.conn.getfeeinfo(200)['feeperkb'])
            res = (feePerKb if feePerKb > MINIMUM_FEE else MINIMUM_FEE)

        return res



    @process_RPC_exceptions
    def getMNStatus(self, address):
        mnStatus = None
        with self.lock:
            mnStatusList = self.conn.listmasternodes(address)
            if not mnStatusList:
                return None
            mnStatus = mnStatusList[0]
            mnStatus['mnCount'] = self.conn.getmasternodecount()['enabled']

        return mnStatus



    @process_RPC_exceptions
    def getMasternodeCount(self):
        ans = None
        with self.lock:
            ans = self.conn.getmasternodecount()

        return ans



    @process_RPC_exceptions
    def getMasternodes(self):
        printDbg("RPC: Getting masternode list...")
        mnList = {}
        score = []
        masternodes = []
        with self.lock:
            masternodes = self.conn.listmasternodes()

        for mn in masternodes:
            if mn.get('status') == 'ENABLED':
                # compute masternode score
                if mn.get('lastpaid') == 0:
                    mn['score'] = mn.get('activetime')
                else:
                    lastpaid_ago = now() - mn.get('lastpaid')
                    mn['score'] = min(lastpaid_ago, mn.get('activetime'))

            else:
                mn['score'] = 0

            score.append(mn)

        # sort masternodes by decreasing score
        score.sort(key=lambda x: x['score'], reverse=True)

        # save masternode position in the payment queue
        for mn in masternodes:
            mn['queue_pos'] = score.index(mn)

        mnList['masternodes'] = masternodes

        return mnList



    @process_RPC_exceptions
    def getNextSuperBlock(self):
        n = 0
        with self.lock:
            n = self.conn.getnextsuperblock()

        return n



    @process_RPC_exceptions
    def getProposals(self):
        printDbg("RPC: Getting proposals list...")
        proposals = []
        data = []
        with self.lock:
            # get proposals JSON data
            data = self.conn.getbudgetinfo()

        for p in data:
            # create proposal Object
            new_proposal = Proposal(p.get('Name'), p.get('URL'), p.get('Hash'), p.get('FeeHash'), p.get('BlockStart'),
                                    p.get('BlockEnd'), p.get('TotalPaymentCount'), p.get('RemainingPaymentCount'), p.get('PaymentAddress'),
                                    p.get('Yeas'), p.get('Nays'), p.get('Abstains'),
                                    float(p.get('TotalPayment')), float(p.get('MonthlyPayment')))
            # append object to list
            proposals.append(new_proposal)

        # return proposals list
        return proposals



    @process_RPC_exceptions
    def getProposalsProjection(self):
        printDbg("RPC: Getting proposals projection...")
        data = []
        proposals = []
        with self.lock:
            # get budget projection JSON data
            data = self.conn.getbudgetprojection()

        for p in data:
            # create proposal-projection dictionary
            new_proposal = {}
            new_proposal['Name'] = p.get('Name')
            new_proposal['Allotted'] = float(p.get("Allotted"))
            new_proposal['Votes'] = p.get('Yeas') - p.get('Nays')
            new_proposal['Total_Allotted'] = float(p.get('TotalBudgetAllotted'))
            # append dictionary to list
            proposals.append(new_proposal)

        # return proposals list
        return proposals



    @process_RPC_exceptions
    def getProtocolVersion(self):
        res = DEFAULT_PROTOCOL_VERSION
        with self.lock:
            prot_version = self.conn.getinfo().get('protocolversion')
            res = int(prot_version)

        return res



    @process_RPC_exceptions
    def getRawTransaction(self, txid):
        res = None
        with self.lock:
            res = self.conn.getrawtransaction(txid)

        return res



    @process_RPC_exceptions
    def getStatus(self):
        status = False
        statusMess = "Unable to connect to a PIVX RPC server.\n"
        statusMess += "Either the local PIVX wallet is not open, or the remote RPC server is not responding."
        n = 0
        response_time = None
        with self.lock:
            isTestnet = self.conn.getinfo()['testnet']
            n, response_time = timeThis(self.conn.getblockcount)
            if n is None:
                n = 0

        if n > 0:
            status = True
            statusMess = "Connected to PIVX Blockchain"

        return status, statusMess, n, response_time, isTestnet



    @process_RPC_exceptions
    def isBlockchainSynced(self):
        res = False
        response_time = None
        with self.lock:
            status, response_time = timeThis(self.conn.mnsync, 'status')
            if status is not None:
                res = status.get("IsBlockchainSynced")

        return res, response_time



    @process_RPC_exceptions
    def mnBudgetRawVote(self, mn_tx_hash, mn_tx_index, proposal_hash, vote, time, vote_sig):
        res = None
        with self.lock:
            res = self.conn.mnbudgetrawvote(mn_tx_hash, mn_tx_index, proposal_hash, vote, time, vote_sig)

        return res



    @process_RPC_exceptions
    def decodemasternodebroadcast(self, work):
        printDbg("RPC: Decoding masternode broadcast...")
        res = ""
        with self.lock:
            res = self.conn.decodemasternodebroadcast(work.strip())

        return res



    @process_RPC_exceptions
    def relaymasternodebroadcast(self, work):
        printDbg("RPC: Relaying masternode broadcast...")
        res = ""
        with self.lock:
            res = self.conn.relaymasternodebroadcast(work.strip())

        return res



    @process_RPC_exceptions
    def sendRawTransaction(self, tx_hex):
        dbg_mess = "RPC: Sending raw transaction"
        dbg_mess += "..."
        printDbg(dbg_mess)
        tx_id = None
        with self.lock:
            tx_id = self.conn.sendrawtransaction(tx_hex, True)

        return tx_id



    @process_RPC_exceptions
    def verifyMessage(self, pivxaddress, signature, message):
        printDbg("RPC: Verifying message...")
        res = False
        with self.lock:
            res = self.conn.verifymessage(pivxaddress, signature, message)

        return res
Esempio n. 4
0
class RpcClient:
    def __init__(self):
        # Lock for threads
        self.lock = threading.Lock()
        self.rpc_ip, self.rpc_port, self.rpc_user, self.rpc_passwd = readRPCfile(
        )
        rpc_url = "http://%s:%s@%s:%d" % (self.rpc_user, self.rpc_passwd,
                                          self.rpc_ip, self.rpc_port)
        try:
            self.lock.acquire()
            self.conn = AuthServiceProxy(rpc_url, timeout=8)
        except JSONRPCException as e:
            err_msg = 'remote or local PIVX-cli running?'
            printException(getCallerName(), getFunctionName(), err_msg, e)
        except Exception as e:
            err_msg = 'remote or local PIVX-cli running?'
            printException(getCallerName(), getFunctionName(), err_msg, e)
        finally:
            self.lock.release()

    def decodeRawTransaction(self, rawTx):
        try:
            self.lock.acquire()
            res = self.conn.decoderawtransaction(rawTx)
        except Exception as e:
            err_msg = 'error in decodeRawTransaction'
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = None
        finally:
            self.lock.release()

        return res

    def getAddressUtxos(self, addresses):
        try:
            self.lock.acquire()
            res = self.conn.getaddressutxos({'addresses': addresses})
        except Exception as e:
            err_msg = "error in getAddressUtxos"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = None
        finally:
            self.lock.release()

        return res

    def getBlockCount(self):
        try:
            self.lock.acquire()
            n = self.conn.getblockcount()
        except Exception as e:
            err_msg = 'remote or local PIVX-cli running?'
            if str(e.args[0]) != "Request-sent":
                printException(getCallerName(), getFunctionName(), err_msg,
                               e.args)
            n = 0
        finally:
            self.lock.release()

        return n

    def getBlockHash(self, blockNum):
        try:
            self.lock.acquire()
            h = self.conn.getblockhash(blockNum)
        except Exception as e:
            err_msg = 'remote or local PIVX-cli running?'
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            h = None
        finally:
            self.lock.release()

        return h

    def getFeePerKb(self):
        try:
            self.lock.acquire()
            # get transaction data from last 10 blocks
            feePerKb = float(self.conn.getfeeinfo(10)['feeperkb'])
            res = (feePerKb if feePerKb > MINIMUM_FEE else MINIMUM_FEE)
        except Exception as e:
            err_msg = 'error in getFeePerKb'
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = MINIMUM_FEE
        finally:
            self.lock.release()

        return res

    def getMNStatus(self, address):
        try:
            self.lock.acquire()
            mnStatusList = self.conn.listmasternodes(address)
            if not mnStatusList:
                return None
            mnStatus = mnStatusList[0]
            mnStatus['mnCount'] = self.conn.getmasternodecount()['enabled']
        except Exception as e:
            err_msg = "error in getMNStatus"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            mnStatus = None
        finally:
            self.lock.release()

        return mnStatus

    def getMasternodes(self):
        mnList = {}
        mnList['last_update'] = now()
        score = []
        try:
            self.lock.acquire()
            masternodes = self.conn.listmasternodes()
        except Exception as e:
            err_msg = "error in getMasternodes"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            masternodes = []
        finally:
            self.lock.release()

        for mn in masternodes:

            if mn.get('status') == 'ENABLED':
                if mn.get('lastpaid') == 0:
                    mn['score'] = mn.get('activetime')
                else:
                    lastpaid_ago = now() - mn.get('lastpaid')
                    mn['score'] = min(lastpaid_ago, mn.get('activetime'))

            else:
                mn['score'] = 0

            score.append(mn)

        score.sort(key=lambda x: x['score'], reverse=True)

        for mn in masternodes:
            mn['queue_pos'] = score.index(mn)

        mnList['masternodes'] = masternodes

        return mnList

    def getProtocolVersion(self):
        try:
            self.lock.acquire()
            prot_version = self.conn.getinfo().get('protocolversion')
            res = int(prot_version)
        except Exception as e:
            err_msg = 'error in getProtocolVersion'
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = DEFAULT_PROTOCOL_VERSION
        finally:
            self.lock.release()

        return res

    def getRawTransaction(self, txid):
        try:
            self.lock.acquire()
            res = self.conn.getrawtransaction(txid)
        except Exception as e:
            err_msg = "is Blockchain synced?"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = None
        finally:
            self.lock.release()

        return res

    def getStatus(self):
        status = False
        n = 0
        try:
            self.lock.acquire()
            n = self.conn.getblockcount()
            if n > 0:
                status = True

        except Exception as e:
            # If loading block index set lastBlock=1
            if str(e.args[0]) == "Loading block index..." or str(
                    e.args[0]) == "Verifying blocks...":
                printDbg(str(e.args[0]))
                n = 1
            else:
                err_msg = "Error while contacting RPC server"
                printException(getCallerName(), getFunctionName(), err_msg,
                               e.args)

        finally:
            self.lock.release()

        return status, n

    def getStatusMess(self, status=None):
        if status == None:
            status = self.getStatus()
        if status:
            return "Connected to PIVX RPC client"
        else:
            return "Unable to connect to a PIVX RPC server, needed to broadcast messages to the network. Either the local PIVX wallet is not open, or the remote RPC server is not responding."

    def isBlockchainSynced(self):
        try:
            self.lock.acquire()
            res = self.conn.mnsync('status').get("IsBlockchainSynced")
        except Exception as e:
            if str(e.args[0]) != "Request-sent":
                err_msg = "error in isBlockchainSynced"
                printException(getCallerName(), getFunctionName(), err_msg,
                               e.args)
            res = False
        finally:
            self.lock.release()

        return res

    def decodemasternodebroadcast(self, work):
        try:
            self.lock.acquire()
            res = self.conn.decodemasternodebroadcast(work.strip())
        except Exception as e:
            err_msg = "error in decodemasternodebroadcast"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = ""
        finally:
            self.lock.release()

        return res

    def relaymasternodebroadcast(self, work):
        try:
            self.lock.acquire()
            res = self.conn.relaymasternodebroadcast(work.strip())
        except Exception as e:
            err_msg = "error in relaymasternodebroadcast"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = ""
        finally:
            self.lock.release()

        return res

    def sendRawTransaction(self, tx_hex):
        try:
            self.lock.acquire()
            tx_id = self.conn.sendrawtransaction(tx_hex)
        except Exception as e:
            err_msg = 'error in rpcClient.sendRawTransaction'
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            tx_id = None
        finally:
            self.lock.release()

        return tx_id

    def verifyMessage(self, pivxaddress, signature, message):
        try:
            self.lock.acquire()
            res = self.conn.verifymessage(pivxaddress, signature, message)
        except Exception as e:
            err_msg = "error in verifyMessage"
            printException(getCallerName(), getFunctionName(), err_msg, e.args)
            res = False
        finally:
            self.lock.release()

        return res