Пример #1
0
    def getNewBlock(self, address, previousHash, bits, extraNonce):
        previousIndexBlock = self.getIndexBlockByHash(previousHash)
        block = Block()

        gasLimit = Config.getIntValue("BLOCK_REWARDS_GAS_LIMIT")
        gasPrice = Config.getIntValue("BLOCK_REWARDS_GAS_PRICE")
        transaction = Transaction(gasLimit, gasPrice)

        height = previousIndexBlock.height + 1
        coinbaseData = [
            DataType.asString(height), 
            DataType.asString(bits), 
            DataType.asString(extraNonce)
        ]
        transaction.addCoinbaseInput(coinbaseData)
        block.transactions.append(transaction) 
        txFees = 0
        totalTxGasUsed = 0
        unconfirmedTransactions = MemoryPool.getMemoryPool()
        for txId in unconfirmedTransactions:
            unconfirmedTransaction = unconfirmedTransactions[txId]
            block.transactions.append(unconfirmedTransaction)  
            txFees += unconfirmedTransaction.calculateTxFee()
            totalTxGasUsed += unconfirmedTransaction.calculateTxGasUsed()
        blockRewards = Config.getDecimalValue("BLOCK_REWARDS")
        blockRewards = Units.toUnits(blockRewards)
        coinbaseValue = blockRewards + txFees
        script = Script.verifySignature()

        transaction.addOutput(address, script, coinbaseValue)
        transaction.hash()

        #Include coinbase tx gas used
        totalTxGasUsed += transaction.calculateTxGasUsed()

        block.merkleRoot = MerkleTree.getMerkleRoot(block.transactions, False)
        block.witnessRoot = MerkleTree.getMerkleRoot(block.transactions, True)

        blockGasLimit = previousIndexBlock.gasLimit + (previousIndexBlock.gasLimit * (1 / 1024))
        blockGasLimit = math.ceil(blockGasLimit)

        block.gasLimit = blockGasLimit
        block.gasUsed = totalTxGasUsed
        block.nonce = 0
        block.bits = bits
        block.previousHash = previousHash
        return block
Пример #2
0
def discoverInternetGatewayDevices():
    payload, addr = Network.sendDataByUDP(M_SEARCH_HOST, M_SEARCH_PORT,
                                          M_SEARCH_QUERY)
    payload = DataType.asString(payload)
    for data in payload.split(M_SEARCH_ESCAPE):
        if data.startswith('LOCATION'):
            location = data.split(" ")
            INTERNET_GATEWAY_DEVICE_LOCATIONS[addr] = location[1]
Пример #3
0
def handleCommand(command, parameters):
    id = 1
    commands = []
    if command == 'CreateAtomicSwapAccount':
        commands.append(JSONRPC.createMethodObject(command, parameters, id))
    elif command == 'CreateMultiSigAccount':
        commands.append(JSONRPC.createMethodObject(command, parameters, id))
    elif command == 'GetAccounts':
        commands.append(JSONRPC.createMethodObject(command, [], id))
    elif command == 'GetNewAccount':
        commands.append(JSONRPC.createMethodObject(command, [], id))
    elif command == 'DeleteAccounts':
        commands.append(JSONRPC.createMethodObject(command, parameters, id))
    elif command == 'SendTransaction':
        fromAddress = parameters[0]
        toAddress = parameters[1]
        value = DataType.asFloat(parameters[2])
        gasLimit = DataType.asInt(parameters[3])
        gasPrice = DataType.asFloat(parameters[4])
        params = {
            'fromAddress': fromAddress,
            'toAddress': toAddress,
            'value': value,
            'gasLimit': gasLimit,
            'gasPrice': gasPrice
        }
        commands.append(JSONRPC.createMethodObject(command, params, id))
    elif command == 'SendTransactionToMultiSig':
        fromAddress = parameters[0]
        threshold = DataType.asInt(parameters[1])
        toMultiSigAddress = parameters[2]
        value = DataType.asFloat(parameters[3])
        gasLimit = DataType.asInt(parameters[4])
        gasPrice = DataType.asFloat(parameters[5])
        params = {
            'fromAddress': fromAddress,
            'threshold': threshold,
            'toAddress': toMultiSigAddress,
            'value': value,
            'gasLimit': gasLimit,
            'gasPrice': gasPrice
        }
        commands.append(JSONRPC.createMethodObject(command, params, id))
    elif command == 'SendTransactionFromMultiSig':
        fromAddress = parameters[0]
        publicKeys = []
        signatures = []
        idx = 0
        for parameter in parameters[1:-5]:
            if idx % 2 == 0:
                publicKeys.append(parameter)
            else:
                signatures.append(parameter)
            idx += 1
        threshold = DataType.asInt(parameters[-5])
        toAddress = parameters[-4]
        value = DataType.asFloat(parameters[-3])
        gasLimit = DataType.asInt(parameters[-2])
        gasPrice = DataType.asFloat(parameters[-1])
        params = {
            'fromAddress': fromAddress,
            'publicKeys': publicKeys,
            'signatures': signatures,
            'threshold': threshold,
            'toAddress': toAddress,
            'value': value,
            'gasLimit': gasLimit,
            'gasPrice': gasPrice
        }
        commands.append(JSONRPC.createMethodObject(command, params, id))
    elif command == 'SignMultiSigOutput':
        multiSigAddress = parameters[0]
        threshold = DataType.asInt(parameters[1])
        toAddress = parameters[2]
        value = DataType.asFloat(parameters[3])
        gasLimit = DataType.asInt(parameters[4])
        gasPrice = DataType.asFloat(parameters[5])
        params = {
            'fromAddress': multiSigAddress,
            'threshold': threshold,
            'toAddress': toAddress,
            'value': value,
            'gasLimit': gasLimit,
            'gasPrice': gasPrice
        }
        commands.append(JSONRPC.createMethodObject(command, params, id))
    elif command == 'GetMiningWorker':
        address = parameters[0]
        enabled = DataType.asBool(parameters[1])
        params = {'address': address, 'enabled': enabled}
        commands.append(JSONRPC.createMethodObject(command, params, id))
    elif command == 'CreateAtomicSwapTx':
        fromAddress = parameters[0]
        threshold = DataType.asInt(parameters[1])
        toAddress = parameters[2]
        value = DataType.asFloat(parameters[3])
        gasLimit = DataType.asInt(parameters[4])
        gasPrice = DataType.asFloat(parameters[5])
        params = {
            'fromAddress': fromAddress,
            'threshold': threshold,
            'toAddress': toAddress,
            'value': value,
            'gasLimit': gasLimit,
            'gasPrice': gasPrice
        }
        commands.append(JSONRPC.createMethodObject(command, params, id))
    elif command == 'SendAtomicSwapTx':
        fromAddress = parameters[0]
        toAddress = parameters[1]
        value = DataType.asFloat(parameters[2])
        gasLimit = DataType.asInt(parameters[3])
        gasPrice = DataType.asFloat(parameters[4])
        params = {
            'fromAddress': fromAddress,
            'toAddress': toAddress,
            'value': value,
            'gasLimit': gasLimit,
            'gasPrice': gasPrice
        }
        commands.append(JSONRPC.createMethodObject(command, params, id))
    elif command == 'SignAtomicSwapTx':
        commands.append(JSONRPC.createMethodObject(command, parameters, id))
    elif command == 'WatchContracts':
        commands.append(JSONRPC.createMethodObject(command, parameters, id))
    elif command == 'DeleteContracts':
        commands.append(JSONRPC.createMethodObject(command, parameters, id))
    elif command == 'GetContracts':
        commands.append(JSONRPC.createMethodObject(command, [], id))
    elif command == 'GetScriptLogs':
        fromAddress = parameters[0]
        scriptAddress = parameters[1]
        value = DataType.asFloat(parameters[2])
        gasLimit = DataType.asInt(parameters[3])
        gasPrice = DataType.asFloat(parameters[4])
        params = {
            'fromAddress': fromAddress,
            'script': scriptAddress,
            'value': value,
            'gasLimit': gasLimit,
            'gasPrice': gasPrice
        }
        commands.append(JSONRPC.createMethodObject(command, params, id))
    elif command == 'DeployScript' or command == 'DeployLocalScript':
        fromAddress = parameters[0]
        with open(parameters[1], 'r') as scriptFile:
            script = scriptFile.read()
        _parameters = parameters[2:-3]
        value = DataType.asFloat(parameters[-3])
        gasLimit = DataType.asInt(parameters[-2])
        gasPrice = DataType.asFloat(parameters[-1])
        params = {
            'fromAddress': fromAddress,
            'script': script,
            'parameters': _parameters,
            'value': value,
            'gasLimit': gasLimit,
            'gasPrice': gasPrice
        }
        commands.append(JSONRPC.createMethodObject(command, params, id))
    elif command == 'CallTxScript' or command == 'CallLocalScript':
        fromAddress = parameters[0]
        scriptAddress = parameters[1]
        _parameters = parameters[2:-3]
        if _parameters and len(_parameters) > 0:
            _parameters[0] = DataType.serialize(_parameters[0])
            hashFunction = keccak.new(data=_parameters[0], digest_bits=256)
            methodHash = hashFunction.hexdigest()
            _parameters[0] = methodHash[0:8]
        value = DataType.asFloat(parameters[-3])
        gasLimit = DataType.asInt(parameters[-2])
        gasPrice = DataType.asFloat(parameters[-1])
        params = {
            'fromAddress': fromAddress,
            'script': scriptAddress,
            'parameters': _parameters,
            'value': value,
            'gasLimit': gasLimit,
            'gasPrice': gasPrice
        }
        commands.append(JSONRPC.createMethodObject(command, params, id))
    elif command == 'WatchSideChain':
        commands.append(JSONRPC.createMethodObject(command, parameters, id))
    elif command == 'DeleteSideChains':
        commands.append(JSONRPC.createMethodObject(command, parameters, id))
    elif command == 'GetSideChains':
        commands.append(JSONRPC.createMethodObject(command, [], id))
    elif command == 'DeploySideChain':
        fromAddress = parameters[0]
        parameters = parameters[1]
        value = DataType.asFloat(parameters[2])
        gasLimit = DataType.asInt(parameters[3])
        gasPrice = DataType.asFloat(parameters[4])
        params = {
            'fromAddress': fromAddress,
            'parameters': parameters,
            'value': value,
            'gasLimit': gasLimit,
            'gasPrice': gasPrice
        }
        commands.append(JSONRPC.createMethodObject(command, params, id))
    elif command == 'GetPeers':
        commands.append(JSONRPC.createMethodObject(command, [], id))
    elif command == 'AddPeers':
        commands.append(JSONRPC.createMethodObject(command, parameters, id))
    elif command == 'DeletePeers':
        commands.append(JSONRPC.createMethodObject(command, parameters, id))
    else:
        raise ValueError('Unsupported command: ' + command)

    url = '%s:%s' % (Http.getNodeHostname(), Http.getHttpPort())
    for cmd in commands:
        cmd = json.dumps(cmd)
        response = Http.send(url, cmd, doBasicAuth=True)
        if response == None or len(response) == 0:
            print('Empty response from ', url, 'request', cmd)
            continue
        response = json.loads(response)
        value = '\n'
        if 'result' in response:
            result = response['result']
            if isinstance(result, list):
                for item in result:
                    if isinstance(item, dict):
                        for v in item.values():
                            value += ('%s\t' % v)
                        value += '\n'
                    else:
                        value += item
                        value += '\n'
            else:
                value += DataType.asString(result)
                value += '\n'
        elif 'error' in response:
            error = response['error']

            code = error['code']
            message = error['message']
            data = error['data']

            value += '%s %s %s' % (code, message, data)
            value += '\n'
        print(value)