def OnAddHeader(self, header):

        hHash = header.Hash.ToBytes()

        if hHash not in self._header_index:
            self._header_index.append(hHash)

        while header.Index - 2000 >= self._stored_header_count:
            ms = StreamManager.GetStream()
            w = BinaryWriter(ms)
            headers_to_write = self._header_index[
                self._stored_header_count:self._stored_header_count + 2000]
            w.Write2000256List(headers_to_write)
            out = ms.ToArray()
            StreamManager.ReleaseStream(ms)
            with self._db.write_batch() as wb:
                wb.put(
                    DBPrefix.IX_HeaderHashList +
                    self._stored_header_count.to_bytes(4, 'little'), out)

            self._stored_header_count += 2000

            logger.debug("Trimming stored header index %s" %
                         self._stored_header_count)

        with self._db.write_batch() as wb:
            wb.put(DBPrefix.DATA_Block + hHash, bytes(8) + header.ToArray())
            wb.put(DBPrefix.SYS_CurrentHeader,
                   hHash + header.Index.to_bytes(4, 'little'))
Exemplo n.º 2
0
    def Serialize(self, writer: BinaryWriter):
        """
        Serialize full object.

        Args:
            writer (neo.IO.BinaryWriter):
        """
        super(ValidatorState, self).Serialize(writer)
        self.PublicKey.Serialize(writer)
        writer.WriteBool(self.Registered)
        writer.WriteFixed8(self.Votes)
Exemplo n.º 3
0
    def test_serialize(self):
        data = b'abc'

        stream = BytesIO()
        u1 = UIntBase(3, bytearray(data))
        u1.Serialize(BinaryWriter(stream))
        self.assertEqual(stream.getvalue(), data)

        stream = BytesIO()
        u1 = UIntBase(3, data)
        u1.Serialize(BinaryWriter(stream))
        self.assertEqual(stream.getvalue(), data)
Exemplo n.º 4
0
def get_tx_data(tx):
    ms = MemoryStream()
    w = BinaryWriter(ms)
    tx.Serialize(w)
    ms.flush()
    tx_data = ms.ToArray().decode()
    return tx_data
    def test_network_addrtime(self):

        addr = "55.15.69.104"
        port = 10333
        ts = int(datetime.now().timestamp())
        services = 0

        nawt = NetworkAddressWithTime(addr, port, services, ts)

        ms = StreamManager.GetStream()
        writer = BinaryWriter(ms)

        nawt.Serialize(writer)

        arr = ms.ToArray()
        arhex = binascii.unhexlify(arr)

        StreamManager.ReleaseStream(ms)

        ms = StreamManager.GetStream(arhex)
        reader = BinaryReader(ms)

        nawt2 = NetworkAddressWithTime()
        nawt2.Deserialize(reader)

        StreamManager.ReleaseStream(ms)

#        self.assertEqual(nawt.Address, nawt2.Address)
        self.assertEqual(nawt.Services, nawt2.Services)
        self.assertEqual(nawt.Port, nawt2.Port)
        self.assertEqual(nawt.Timestamp, nawt2.Timestamp)
Exemplo n.º 6
0
    def test_2_serialize_notify_no_payload(self):

        sc = SmartContractEvent(
            SmartContractEvent.RUNTIME_NOTIFY,
            ContractParameter(ContractParameterType.Array, []),
            self.contract_hash, 99, self.event_tx, True, False)

        stream = StreamManager.GetStream()
        writer = BinaryWriter(stream)
        sc.Serialize(writer)

        out = bytes(stream.getvalue())

        self.assertEqual(
            out,
            b'\x1cSmartContract.Runtime.Notify\x11\xc4\xd1\xf4\xfb\xa6\x19\xf2b\x88p\xd3n:\x97s\xe8tp[c\x00\x00\x00\x90\xe4\xf1\xbbb\x8e\xf1\x07\xde\xe9\xf0\xd2\x12\xd1w\xbco\x844\x07=\x1b\xa7\x1f\xa7\x94`\x0b\xb4\x88|K'
        )

        StreamManager.ReleaseStream(stream)
        new_event = SmartContractEvent.FromByteArray(out)

        self.assertEqual(new_event.event_type, sc.event_type)
        self.assertEqual(new_event.contract_hash, sc.contract_hash)
        self.assertEqual(new_event.test_mode, sc.test_mode)
        self.assertEqual(new_event.tx_hash, sc.tx_hash)
        self.assertEqual(new_event.block_number, sc.block_number)
Exemplo n.º 7
0
    def test_4_serialize_full_transfer_notify_payload(self):

        payload = ContractParameter(ContractParameterType.Array, [
            ContractParameter(ContractParameterType.String, b'transfer'),
            ContractParameter(ContractParameterType.ByteArray, self.addr_to),
            ContractParameter(ContractParameterType.ByteArray, self.addr_from),
            ContractParameter(ContractParameterType.Integer, 123000)
        ])

        sc = NotifyEvent(SmartContractEvent.RUNTIME_NOTIFY, payload,
                         self.contract_hash, 91349, self.event_tx, True, False)

        stream = StreamManager.GetStream()
        writer = BinaryWriter(stream)
        sc.Serialize(writer)

        out = bytes(stream.getvalue())

        StreamManager.ReleaseStream(stream)
        new_event = SmartContractEvent.FromByteArray(out)

        self.assertEqual(new_event.event_type, sc.event_type)
        self.assertEqual(new_event.contract_hash, sc.contract_hash)
        self.assertEqual(new_event.test_mode, sc.test_mode)
        self.assertEqual(new_event.tx_hash, sc.tx_hash)
        self.assertEqual(new_event.block_number, sc.block_number)

        self.assertEqual(new_event.notify_type, b'transfer')
        self.assertEqual(new_event.AddressTo,
                         'ALb8FEhEmtSqv97fuNVuoLmcmrSKckffRf')
        self.assertEqual(new_event.AddressFrom,
                         'AKZmSGPD7ytJBbxpRPmobYGLNxdWH3Jiqs')
        self.assertEqual(new_event.Amount, 123000)
        self.assertEqual(new_event.is_standard_notify, True)
    def test_6_serialize_full_approve_payload(self):

        sc = NotifyEvent(
            SmartContractEvent.RUNTIME_NOTIFY,
            [b'approve', self.addr_to, self.addr_from, b'x\xe0\x01'],
            self.contract_hash, 91349, self.event_tx, True, False)

        stream = StreamManager.GetStream()
        writer = BinaryWriter(stream)
        sc.Serialize(writer)

        out = bytes(stream.getvalue())

        StreamManager.ReleaseStream(stream)
        new_event = SmartContractEvent.FromByteArray(out)

        self.assertEqual(new_event.event_type, sc.event_type)
        self.assertEqual(new_event.contract_hash, sc.contract_hash)
        self.assertEqual(new_event.test_mode, sc.test_mode)
        self.assertEqual(new_event.tx_hash, sc.tx_hash)
        self.assertEqual(new_event.block_number, sc.block_number)

        self.assertEqual(new_event.notify_type, b'approve')
        self.assertEqual(new_event.AddressFrom,
                         'AKZmSGPD7ytJBbxpRPmobYGLNxdWH3Jiqs')
        self.assertEqual(new_event.AddressTo,
                         'ALb8FEhEmtSqv97fuNVuoLmcmrSKckffRf')
        self.assertEqual(new_event.Amount, 123000)
        self.assertEqual(new_event.is_standard_notify, True)
        self.assertEqual(new_event.ShouldPersist, True)
Exemplo n.º 9
0
 def ToByteArray(self):
     stream = StreamManager.GetStream()
     writer = BinaryWriter(stream)
     self.Serialize(writer)
     out = stream.getvalue()
     StreamManager.ReleaseStream(stream)
     return out
    def test_1_serialize_runtime_log(self):

        sc = SmartContractEvent(SmartContractEvent.RUNTIME_LOG, [],
                                self.contract_hash, 99999, self.event_tx, True,
                                False)

        stream = StreamManager.GetStream()
        writer = BinaryWriter(stream)
        sc.Serialize(writer)

        out = bytes(stream.getvalue())

        self.assertEqual(
            out,
            b'\x19SmartContract.Runtime.Log\x11\xc4\xd1\xf4\xfb\xa6\x19\xf2b\x88p\xd3n:\x97s\xe8tp[\x9f\x86\x01\x00\x90\xe4\xf1\xbbb\x8e\xf1\x07\xde\xe9\xf0\xd2\x12\xd1w\xbco\x844\x07=\x1b\xa7\x1f\xa7\x94`\x0b\xb4\x88|K'
        )

        StreamManager.ReleaseStream(stream)
        new_event = SmartContractEvent.FromByteArray(out)

        self.assertEqual(new_event.event_type, sc.event_type)
        self.assertEqual(new_event.contract_hash, sc.contract_hash)
        self.assertEqual(new_event.test_mode, sc.test_mode)
        self.assertEqual(new_event.tx_hash, sc.tx_hash)
        self.assertEqual(new_event.block_number, sc.block_number)
Exemplo n.º 11
0
    def test_tx_deserialize(self):
        ms = MemoryStream(self.tx_raw_hex)

        reader = BinaryReader(ms)

        tx = Transaction.DeserializeFrom(reader)

        self.assertEqual(type(tx), MinerTransaction)

        self.assertEqual(tx.Hash.ToBytes(), self.tx_id)

        self.assertEqual(tx.Nonce, self.tx_nonce)

        self.assertEqual(tx.inputs, [])
        self.assertEqual(tx.outputs, [])
        self.assertEqual(tx.scripts, [])

        ms = MemoryStream()
        writer = BinaryWriter(ms)

        tx.Serialize(writer)
        out = ms.ToArray()

        self.assertEqual(out, self.tx_raw)

        json = tx.ToJson()
        self.assertEqual(json['nonce'], self.tx_nonce)
    def test_3_serialize_single_transfer_notify_payload(self):

        sc = NotifyEvent(SmartContractEvent.RUNTIME_NOTIFY, [b'transfer'],
                         self.contract_hash, 99, self.event_tx, True, False)

        stream = StreamManager.GetStream()
        writer = BinaryWriter(stream)
        sc.Serialize(writer)

        out = bytes(stream.getvalue())

        StreamManager.ReleaseStream(stream)
        new_event = SmartContractEvent.FromByteArray(out)

        self.assertEqual(new_event.event_type, sc.event_type)
        self.assertEqual(new_event.contract_hash, sc.contract_hash)
        self.assertEqual(new_event.test_mode, sc.test_mode)
        self.assertEqual(new_event.tx_hash, sc.tx_hash)
        self.assertEqual(new_event.block_number, sc.block_number)

        self.assertEqual(new_event.notify_type, b'transfer')
        self.assertEqual(new_event.AddressFrom, None)
        self.assertEqual(new_event.AddressTo, None)
        self.assertEqual(new_event.Amount, 0)
        self.assertEqual(new_event.is_standard_notify, False)
        self.assertEqual(new_event.ShouldPersist, False)
    def Serialize(self, writer: BinaryWriter):
        """
        Serialize full object.

        Args:
            writer (neo.IO.BinaryWriter):
        """
        byt = None
        if self.Type == StateType.Account:
            byt = b'\x40'
        elif self.Type == StateType.Validator:
            byt = b'\x48'
        writer.WriteByte(byt)
        writer.WriteVarBytes(self.Key)
        writer.WriteVarString(self.Field)
        writer.WriteVarBytes(self.Value)
    def test_5_serialize_full_refund_payload(self):

        sc = NotifyEvent(SmartContractEvent.RUNTIME_NOTIFY,
                         [b'refund', self.addr_to,
                          BigInteger(123000)], self.contract_hash, 91349,
                         self.event_tx, True, False)

        stream = StreamManager.GetStream()
        writer = BinaryWriter(stream)
        sc.Serialize(writer)

        out = bytes(stream.getvalue())

        StreamManager.ReleaseStream(stream)
        new_event = SmartContractEvent.FromByteArray(out)

        self.assertEqual(new_event.event_type, sc.event_type)
        self.assertEqual(new_event.contract_hash, sc.contract_hash)
        self.assertEqual(new_event.test_mode, sc.test_mode)
        self.assertEqual(new_event.tx_hash, sc.tx_hash)
        self.assertEqual(new_event.block_number, sc.block_number)

        self.assertEqual(new_event.notify_type, b'refund')
        self.assertEqual(new_event.AddressTo,
                         'AKZmSGPD7ytJBbxpRPmobYGLNxdWH3Jiqs')
        self.assertEqual(new_event.addr_from, sc.contract_hash)
        self.assertEqual(new_event.Amount, 123000)
        self.assertEqual(new_event.is_standard_notify, True)
    def test_2_serialize_single_notify_payload(self):

        sc = NotifyEvent(SmartContractEvent.RUNTIME_NOTIFY, [b'hello'],
                         self.contract_hash, 99, self.event_tx, True, False)

        stream = StreamManager.GetStream()
        writer = BinaryWriter(stream)
        sc.Serialize(writer)

        out = bytes(stream.getvalue())

        self.assertEqual(
            out,
            b'\x1cSmartContract.Runtime.Notify\x11\xc4\xd1\xf4\xfb\xa6\x19\xf2b\x88p\xd3n:\x97s\xe8tp[c\x00\x00\x00\x90\xe4\xf1\xbbb\x8e\xf1\x07\xde\xe9\xf0\xd2\x12\xd1w\xbco\x844\x07=\x1b\xa7\x1f\xa7\x94`\x0b\xb4\x88|K\x05hello'
        )

        StreamManager.ReleaseStream(stream)
        new_event = SmartContractEvent.FromByteArray(out)

        self.assertEqual(new_event.event_type, sc.event_type)
        self.assertEqual(new_event.contract_hash, sc.contract_hash)
        self.assertEqual(new_event.test_mode, sc.test_mode)
        self.assertEqual(new_event.tx_hash, sc.tx_hash)
        self.assertEqual(new_event.block_number, sc.block_number)

        self.assertEqual(new_event.notify_type, b'hello')
        self.assertEqual(new_event.AddressFrom, None)
        self.assertEqual(new_event.AddressTo, None)
        self.assertEqual(new_event.Amount, 0)
        self.assertEqual(new_event.is_standard_notify, False)
Exemplo n.º 16
0
 def get_tx_data(self):
     ms = MemoryStream()
     w = BinaryWriter(ms)
     self.SerializeUnsigned(w)
     ms.flush()
     tx_data = ms.ToArray().decode()
     return tx_data
Exemplo n.º 17
0
    def Serialize(self, writer: BinaryWriter):
        """
        Serialize object.

        Args:
            writer (neo.IO.BinaryWriter):
        """
        writer.WriteSerializableArray(self.Headers)
Exemplo n.º 18
0
    def Trim(self):
        """
        Returns a byte array that contains only the block header and transaction hash.

        Returns:
            bytes:
        """
        ms = StreamManager.GetStream()
        writer = BinaryWriter(ms)
        self.SerializeUnsigned(writer)
        writer.WriteByte(1)
        self.Script.Serialize(writer)

        writer.WriteHashes([tx.Hash.ToBytes() for tx in self.Transactions])
        retVal = ms.ToArray()
        StreamManager.ReleaseStream(ms)
        return retVal
Exemplo n.º 19
0
def deploy(from_addr, bytecode, contract_params, return_type, details):
    response = requests.post('http://127.0.0.1:20332',
                             json={
                                 'jsonrpc':
                                 '2.0',
                                 'id':
                                 1,
                                 'method':
                                 'mw_construct_deploy_tx',
                                 'params': [{
                                     'from_addr': from_addr,
                                     'bin': bytecode,
                                     'contract_params': contract_params,
                                     'return_type': return_type,
                                     'details': details,
                                 }]
                             }).json()
    if 'error' in response:
        print(response['error']['message'])
        return
    context = response['result']['context']
    binary_tx = response['result']['tx']
    contract_hash = response['result']['hash']

    tx = ContractTransaction.DeserializeFromBufer(
        binascii.unhexlify(binary_tx))

    scripts = requests.post('http://127.0.0.1:5000/neo_sign/',
                            json={
                                'binary_tx': binary_tx
                            }).json()
    tx.scripts = [
        Witness(
            x['invocation'].encode(),
            x['verification'].encode(),
        ) for x in scripts
    ]

    ms = StreamManager.GetStream()
    writer = BinaryWriter(ms)
    tx.Serialize(writer)
    ms.flush()
    signed_tx = ms.ToArray()

    response = requests.post('http://127.0.0.1:20332',
                             json={
                                 'jsonrpc': '2.0',
                                 'id': 1,
                                 'method': 'sendrawtransaction',
                                 'params': [
                                     signed_tx.decode(),
                                 ]
                             }).json()

    print('contract hash:', contract_hash)
    print(response)
Exemplo n.º 20
0
    def test_token_serialize(self):

        wallet = self.GetWallet1(recreate=True)
        token = self.get_token(wallet)

        stream = StreamManager.GetStream()
        writer = BinaryWriter(stream)
        token.Serialize(writer)

        self.assertEqual(b'0f4e45582054656d706c617465205634044e58543408',
                         stream.ToArray())
Exemplo n.º 21
0
def send(addr_from, addr_to, asset, amount):
    response = requests.post('http://127.0.0.1:20332',
                             json={
                                 'jsonrpc': '2.0',
                                 'id': 1,
                                 'method': 'mw_construct_send_tx',
                                 'params': {
                                     'from': addr_from,
                                     'to': addr_to,
                                     'asset': asset,
                                     'amount': amount,
                                 }
                             }).json()
    print(response)
    context = response['result']['context']
    binary_tx = response['result']['tx']

    tx = ContractTransaction.DeserializeFromBufer(
        binascii.unhexlify(binary_tx))
    scripts = requests.post('http://127.0.0.1:5000/neo_sign/',
                            json={
                                'binary_tx': binary_tx,
                                'address': addr_from
                            }).json()
    print('scripts', scripts)
    tx.scripts = [
        Witness(
            x['invocation'].encode(),
            x['verification'].encode(),
        ) for x in scripts
    ]

    print(scripts)
    ms = StreamManager.GetStream()
    writer = BinaryWriter(ms)
    tx.Serialize(writer)
    ms.flush()
    signed_tx = ms.ToArray()

    print(tx.ToJson())

    #    print('does not send: return') ; return

    response = requests.post('http://127.0.0.1:20332',
                             json={
                                 'jsonrpc': '2.0',
                                 'id': 1,
                                 'method': 'sendrawtransaction',
                                 'params': [
                                     signed_tx.decode(),
                                 ]
                             }).json()
    print(response)
Exemplo n.º 22
0
    def GetHashData(self):
        """
        Get the hashable data.

        Returns:
            bytes:
        """
        ms = MemoryStream()
        w = BinaryWriter(ms)
        self.SerializeUnsigned(w)
        ms.flush()
        return ms.ToArray()
Exemplo n.º 23
0
    def test_token_serialize(self):

        wallet = self.GetWallet1(recreate=True)

        ImportToken(wallet, self.token_hash_str)

        token = self.get_token(wallet)

        stream = StreamManager.GetStream()
        writer = BinaryWriter(stream)
        token.Serialize(writer)

        self.assertEqual(b'0d4e455035205374616e64617264044e45503508',
                         stream.ToArray())
Exemplo n.º 24
0
    def ToByteArray(self):
        """
        Serialize self and get the byte stream.

        Returns:
            bytes: serialized object.
        """
        ms = StreamManager.GetStream()
        writer = BinaryWriter(ms)
        self.Serialize(writer)

        retval = ms.ToArray()
        StreamManager.ReleaseStream(ms)

        return retval
Exemplo n.º 25
0
    def deploy(self, contract_params='0710', return_type='05'):
        self.compile()
        from_addr = NETWORKS[self.contract.network.name]['address']
        bytecode = self.neo_contract_crowdsale.bytecode
        neo_int = NeoInt(self.contract.network.name)
        print('from address', from_addr)
        details = {
            'name': 'WISH',
            'description': 'NEO smart contract',
            'email': '*****@*****.**',
            'version': '1',
            'author': 'MyWish'
        }
        param_list = {
            'from_addr': from_addr,
            'bin': bytecode,
            'needs_storage': True,
            'needs_dynamic_invoke': False,
            'contract_params': contract_params,
            'return_type': return_type,
            'details': details,
        }
        response = neo_int.mw_construct_deploy_tx(param_list)
        print('construct response', response, flush=True)
        binary_tx = response['tx']
        contract_hash = response['hash']

        tx = ContractTransaction.DeserializeFromBufer(
            binascii.unhexlify(binary_tx))
        tx = sign_neo_transaction(tx, binary_tx, from_addr)
        print('after sign', tx.ToJson()['txid'], flush=True)
        ms = StreamManager.GetStream()
        writer = BinaryWriter(ms)
        tx.Serialize(writer)
        ms.flush()
        signed_tx = ms.ToArray()
        print('full tx:', flush=True)
        print(signed_tx, flush=True)

        result = neo_int.sendrawtransaction(signed_tx.decode())
        print(result, flush=True)
        if not result:
            raise TxFail()
        print('contract hash:', contract_hash)
        print('result of send raw transaction: ', result)
        self.neo_contract_crowdsale.address = contract_hash
        self.neo_contract_crowdsale.tx_hash = tx.ToJson()['txid']
        self.neo_contract_crowdsale.save()
Exemplo n.º 26
0
    def Runtime_Serialize(self, engine):
        stack_item = engine.EvaluationStack.Pop()

        ms = StreamManager.GetStream()
        writer = BinaryWriter(ms)
        try:
            stack_item.Serialize(writer)
        except Exception as e:
            logger.error("Cannot serialize item %s: %s " % (stack_item, e))
            return False

        ms.flush()
        retVal = ByteArray(ms.getvalue())
        StreamManager.ReleaseStream(ms)
        engine.EvaluationStack.PushT(retVal)

        return True
Exemplo n.º 27
0
    def GetHashData(hashable):
        """
        Get the data used for hashing.

        Args:
            hashable (neo.IO.Mixins.SerializableMixin): object extending SerializableMixin

        Returns:
            bytes:
        """
        ms = StreamManager.GetStream()
        writer = BinaryWriter(ms)
        hashable.SerializeUnsigned(writer)
        ms.flush()
        retVal = ms.ToArray()
        StreamManager.ReleaseStream(ms)
        return retVal
Exemplo n.º 28
0
    def test_data_received(self, mock):
        node = NeoNode()
        node.endpoint = Endpoint('hello.com', 1234)
        node.host = node.endpoint.host
        node.port = node.endpoint.port
        payload = VersionPayload(10234, 1234, 'version')
        message = Message('version', payload=payload)
        stream = StreamManager.GetStream()
        writer = BinaryWriter(stream)
        message.Serialize(writer)

        out = stream.getvalue()
        node.dataReceived(out)

        mock.assert_called_once()

        self.assertEqual(node.Version.Nonce, payload.Nonce)
Exemplo n.º 29
0
    def ToJson(self):
        jsn = {}
        jsn['type'] = 'Neo.Core.ContractTransaction'  # Verifiable.GetType().FullName
        ms = MemoryStream()
        w = BinaryWriter(ms)
        self.Verifiable.SerializeUnsigned(w)
        ms.flush()
        jsn['hex'] = ms.ToArray().decode()
        jsn['items'] = {}
        for key, value in self.ContextItems.items():
            if type(key) == str:
                shkey = "0x{}".format(key)
            else:
                shkey = "0x{}".format(key.decode())
            jsn['items'][shkey] = value.ToJson()

        return jsn
Exemplo n.º 30
0
    def ToArray(value):
        """
        Serialize the given `value` to a an array of bytes.

        Args:
            value (neo.IO.Mixins.SerializableMixin): object extending SerializableMixin.

        Returns:
            bytes:
        """
        ms = StreamManager.GetStream()
        writer = BinaryWriter(ms)

        value.Serialize(writer)

        retVal = ms.ToArray()
        StreamManager.ReleaseStream(ms)

        return retVal