Exemplo n.º 1
0
def encodeItem(item, itemLen, prefix):
    if itemLen <= SHORT_PAYLOAD:
        return DataType.intToBytes(prefix + itemLen) + item
    else:
        itemLen = DataType.intToBytes(itemLen)
        return DataType.intToBytes(prefix + len(itemLen) +
                                   SHORT_PAYLOAD) + itemLen + item
Exemplo n.º 2
0
 def writeResponse(self, response):
     response = DataType.serialize(response)
     responseLen = len(response)
     responseLen = DataType.intToBytes(
         responseLen, Config.getIntValue("SOCKET_HEADER_BUFFER_SIZE"))
     self.request.sendall(responseLen)
     self.request.sendall(response)
Exemplo n.º 3
0
def decode(buffer):
    decodedBuffer = b''
    if len(buffer) > 0:
        prefix = buffer[0]
        if prefix >= BYTE_PREFIX_START and prefix <= BYTE_PREFIX_END:
            decodedBuffer += DataType.intToBytes(prefix)
        elif prefix >= STRING_PREFIX_START and prefix <= STRING_PREFIX_END:
            offset = 1
            decodedBufferLen = None
            if prefix > STRING_55_LEN_MAX_RANGE:
                prefixLen = prefix - STRING_55_LEN_MAX_RANGE
                bufferLen = buffer[offset:offset + prefixLen]
                decodedBufferLen = DataType.bytesToInt(bufferLen)
                offset += prefixLen
            else:
                decodedBufferLen = prefix - STRING_PREFIX_START
            decodedBuffer += buffer[offset:offset + decodedBufferLen]
        elif prefix >= LIST_PREFIX_START and prefix <= LIST_PREFIX_END:
            decodedBuffer = []
            offset = 1
            decodedBufferLen = None
            if prefix > LIST_55_LEN_MAX_RANGE:
                prefixLen = prefix - LIST_55_LEN_MAX_RANGE
                bufferLen = buffer[offset:offset + prefixLen]
                decodedBufferLen = DataType.bytesToInt(bufferLen)
                offset += prefixLen
            else:
                decodedBufferLen = prefix - LIST_PREFIX_START
            buffer = buffer[offset:offset + decodedBufferLen]
            while len(buffer) > 0:
                tempBufferDecoded = decode(buffer)
                decodedBuffer.append(tempBufferDecoded)
                offset = 1
                prefix = buffer[0]
                if prefix >= BYTE_PREFIX_START and prefix <= BYTE_PREFIX_END:
                    decodedBufferLen = 0
                elif prefix >= STRING_PREFIX_START and prefix <= STRING_PREFIX_END:
                    if prefix > STRING_55_LEN_MAX_RANGE:
                        prefixLen = prefix - STRING_55_LEN_MAX_RANGE
                        bufferLen = buffer[offset:offset + prefixLen]
                        decodedBufferLen = DataType.bytesToInt(bufferLen)
                        offset += prefixLen
                    else:
                        decodedBufferLen = prefix - STRING_PREFIX_START
                elif prefix >= LIST_PREFIX_START and prefix <= LIST_PREFIX_END:
                    if prefix > LIST_55_LEN_MAX_RANGE:
                        prefixLen = prefix - LIST_55_LEN_MAX_RANGE
                        bufferLen = buffer[offset:offset + prefixLen]
                        decodedBufferLen = DataType.bytesToInt(bufferLen)
                        offset += prefixLen
                    else:
                        decodedBufferLen = prefix - LIST_PREFIX_START
                buffer = buffer[offset + decodedBufferLen:]
    return decodedBuffer
Exemplo n.º 4
0
def encode(item):
    item = DataType.serialize(item)
    encodedItem = b''
    if len(item) > 0:
        if isinstance(item, bytes):
            itemLen = len(item)
            if itemLen == 1 and item[0] <= BYTE_PREFIX_END:
                encodedItem += item
            else:
                encodedItem += encodeItem(item, itemLen, STRING_PREFIX_START)
        elif isinstance(item, list):
            for i in item:
                encodedItem += encode(i)
            itemLen = len(encodedItem)
            encodedItem = encodeItem(encodedItem, itemLen, LIST_PREFIX_START)
    else:
        if isinstance(item, bytes):
            encodedItem += DataType.intToBytes(STRING_PREFIX_START)
        elif isinstance(item, list):
            encodedItem += DataType.intToBytes(LIST_PREFIX_START)
    return encodedItem
Exemplo n.º 5
0
    def call(self):
        gas = self.popAsInt()
        address = DataType.intToBytes(self.pop())
        value = self.popAsInt()
        argsOffset = self.popAsInt()
        argsLength = self.popAsInt()
        retOffset = self.popAsInt()
        retLength = self.popAsInt()

        self.transaction.addInternalOutput(address, Script.verifySignature(), value)

        _address = self.output.address
        _script = self.output.script
        _value = self.output.value - value
        self.transaction.addInternalOutput(_address, _script, _value)
        
        #self.memory[retOffset:retOffset + retLength] = self.memory[argsOffset:argsOffset + argsLength]
        self.stack.append(1)
Exemplo n.º 6
0
def _sendData(host, data, hasPayload):
    try:
        if ':' in host:
            host = host.split(':')
            host[1] = DataType.asInt(host[1])
            host = tuple(host)
        else:
            host = (host, getSocketPort())
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
            sock.connect(host)
            sock.settimeout(Config.getIntValue("CONNECTION_TIMEOUT"))
            data = DataType.serialize(data)
            dataLen = len(data)
            dataLen = DataType.intToBytes(
                dataLen, Config.getIntValue("SOCKET_HEADER_BUFFER_SIZE"))
            sock.sendall(dataLen)
            sock.sendall(data)
            if hasPayload:
                payload = getSocketPayload(sock)
                return RLP.decode(payload)
    except IOError:
        Log.error('Unable to send data to host: %s data: %s' % (host, data))
    return None