Ejemplo n.º 1
0
    def HandleGetHeadersMessageReceived(self, payload):

        if not self.leader.ServiceEnabled:
            return

        inventory = IOHelper.AsSerializableWithType(payload, 'neo.Network.Payloads.GetBlocksPayload.GetBlocksPayload')

        if not inventory:
            return

        blockchain = BC.Default()

        hash = inventory.HashStart[0]

        if hash is None or hash == inventory.HashStop:
            self.Log("getheaders: Hash {} not found or hashstop reached".format(inventory.HashStart))
            return

        headers = []
        header_count = 0

        while hash != inventory.HashStop and header_count < 2000:
            hash = blockchain.GetNextBlockHash(hash)
            if not hash:
                break
            headers.append(blockchain.GetHeader(hash))
            header_count += 1

        if header_count > 0:
            self.SendSerializedMessage(Message('headers', HeadersPayload(headers=headers)))
Ejemplo n.º 2
0
    def test_block_deserialize(self):

        block = Helper.AsSerializableWithType(self.rawblock_hex,
                                              'neo.Core.Block.Block')

        self.assertEqual(self.rb_prev, block.PrevHash.ToBytes())
        self.assertEqual(self.rb_merlke, block.MerkleRoot.ToBytes())
        self.assertEqual(self.rb_ts, block.Timestamp)
        self.assertEqual(self.rb_h, block.Index)
        self.assertEqual(self.rb_nonce, block.ConsensusData)
        self.assertEqual(self.rconsenusdata, block.ConsensusData)
        #        self.assertEqual(self.rb_hash, block.HashToString())
        tx = block.Transactions[0]

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

        self.assertEqual(len(tx.inputs), 0)
        self.assertEqual(len(tx.outputs), 0)
        self.assertEqual(len(tx.Attributes), 0)

        self.assertEqual(type(tx.scripts), list)

        rawdata = block.RawData()
        compair_data = self.rawblock[:len(rawdata)]
        self.assertEqual(rawdata, compair_data)
        out = block.Hash.ToBytes()
        self.assertEqual(out, self.rb_hash)

        root = MerkleTree.ComputeRoot([tx.Hash for tx in block.Transactions])
        self.assertEqual(root, block.MerkleRoot)
Ejemplo n.º 3
0
    def test_testnet797966(self):

        path = '%s/fixtures/797966.txt' % os.getcwd()

        with open(path, 'rb') as f:
            blockraw = f.read().strip()

            hex = binascii.unhexlify(blockraw)

            block = Helper.AsSerializableWithType(hex, 'neo.Core.Block.Block')

            trimmed = block.Trim()

            trimmed_unhex = binascii.unhexlify(trimmed)

            blockfrom_trimmed = Block.FromTrimmedData(trimmed_unhex)

            self.assertEqual(blockfrom_trimmed.Version, block.Version)
            self.assertEqual(blockfrom_trimmed.Index, block.Index)
            self.assertEqual(blockfrom_trimmed.PrevHash, block.PrevHash)
            self.assertEqual(blockfrom_trimmed.MerkleRoot, block.MerkleRoot)
            self.assertEqual(blockfrom_trimmed.Timestamp, block.Timestamp)
            self.assertEqual(blockfrom_trimmed.Index, block.Index)
            self.assertEqual(blockfrom_trimmed.ConsensusData,
                             block.ConsensusData)
            self.assertEqual(blockfrom_trimmed.NextConsensus,
                             block.NextConsensus)
            self.assertEqual(len(block.Transactions),
                             len(blockfrom_trimmed.Transactions))
Ejemplo n.º 4
0
    def test_add_header(self):

        hexdata = binascii.unhexlify(self.block_one_raw)

        block_one = Helper.AsSerializableWithType(hexdata,
                                                  'neo.Core.Block.Block')
        header = block_one.Header()
        hHash = header.HashToByteString()

        self.assertEqual(
            self._blockchain.CurrentHeaderHash(),
            b'b3181718ef6167105b70920e4a8fbbd0a0a56aacf460d70e10ba6fa1668f1fef'
        )
        self.assertEqual(self._blockchain.HeaderHeight(), 0)

        #add it to leveldb
        self._blockchain.AddHeader(header)
        self.assertEqual(self._blockchain.HeaderHeight(), 1)

        #now retrieve it
        block_one_again = self._blockchain.GetHeader(hHash)

        self.assertEqual(type(block_one_again), Header)
        self.assertEqual(block_one_again.HashToByteString(),
                         self.block_one_hash)
Ejemplo n.º 5
0
    def test_invocation_assetcreate_block(self):

        hexdata = binascii.unhexlify(self.asset_create_block)

        block = Helper.AsSerializableWithType(hexdata, 'neo.Core.Block.Block')

        self.assertEqual(block.Index, self.asset_create_index)

        snapshot = Blockchain.Default()._db.createSnapshot()
        snapshot.PersistingBlock = block
        snapshot.UnspentCoins.Add(b'aa2051096e7be45ed991279a1a4c2678eb886690829a2729f4caa82192ff7f34', UnspentCoinState.FromTXOutputsConfirmed([0]))

        result = False
        with BlockchainFixtureTestCase.MPPersist():
            result = Blockchain.Default().Persist(block, snapshot)

        self.assertTrue(result)

        # now the asset that was created should be there
        newasset = snapshot.Assets.TryGet(self.asset_create_id)

        self.assertIsNotNone(newasset)

        self.assertEqual(newasset.AssetType, 1)
        self.assertEqual(newasset.Precision, 8)
        self.assertEqual(Crypto.ToAddress(newasset.Admin), self.asset_admin)
        self.assertEqual(Crypto.ToAddress(newasset.Issuer), self.asset_admin)
        self.assertIsInstance(newasset.AssetId, UInt256)
        self.assertEqual(newasset.AssetId.ToBytes(), self.asset_create_id)
Ejemplo n.º 6
0
    def HandleGetBlocksMessageReceived(self, payload):
        """
        Process a GetBlocksPayload payload.

        Args:
            payload (neo.Network.Payloads.GetBlocksPayload):
        """
        if not self.leader.ServiceEnabled:
            return

        inventory = IOHelper.AsSerializableWithType(
            payload, 'neo.Network.Payloads.GetBlocksPayload.GetBlocksPayload')
        if not inventory:
            return

        blockchain = BC.Default()
        hash = inventory.HashStart[0]
        if not blockchain.GetHeader(hash):
            return

        hashes = []
        hcount = 0
        while hash != inventory.HashStop and hcount < 500:
            hash = blockchain.GetNextBlockHash(hash)
            if hash is None:
                break
            hashes.append(hash)
            hcount += 1
        if hcount > 0:
            self.SendSerializedMessage(
                Message('inv',
                        InvPayload(type=InventoryType.Block, hashes=hashes)))
Ejemplo n.º 7
0
    def HandleBlockReceived(self, inventory):
        """
        Process a Block inventory payload.

        Args:
            inventory (neo.Network.Inventory):
        """
        block = IOHelper.AsSerializableWithType(inventory,
                                                'neo.Core.Block.Block')

        blockhash = block.Hash.ToBytes()

        if blockhash in BC.Default().BlockRequests:
            BC.Default().BlockRequests.remove(blockhash)
        if blockhash in self.myblockrequests:
            self.myblockrequests.remove(blockhash)

            error = self.leader.InventoryReceived(block)
            if error == "FALSE_BLOCK":
                rating = self.getRating()
                if (rating <= 80):
                    rating += 20
                elif rating < 100:
                    rating = 100

                self.changeRating(rating)

        if len(self.myblockrequests) < self.leader.NREQMAX:
            self.DoAskForMoreBlocks()
Ejemplo n.º 8
0
    def HandlePeerInfoReceived(self, payload):

        addrs = IOHelper.AsSerializableWithType(
            payload, 'neo.Network.Payloads.AddrPayload.AddrPayload')

        for nawt in addrs.NetworkAddressesWithTime:
            self.leader.RemoteNodePeerReceived(nawt.Address, nawt.Port)
    def HandlePeerInfoReceived(self, payload):
        """Process response of `self.RequestPeerInfo`."""
        addrs = IOHelper.AsSerializableWithType(
            payload, 'neo.Network.Payloads.AddrPayload.AddrPayload')

        for index, nawt in enumerate(addrs.NetworkAddressesWithTime):
            self.leader.RemoteNodePeerReceived(nawt.Address, nawt.Port, index)
    def test_invocation_assetcreate_block(self):

        hexdata = binascii.unhexlify(self.asset_create_block)

        block = Helper.AsSerializableWithType(hexdata, 'neo.Core.Block.Block')

        self.assertEqual(block.Index, self.asset_create_index)

        result = Blockchain.Default().Persist(block)

        self.assertTrue(result)

        # now the asset that was created should be there
        assets = DBCollection(Blockchain.Default()._db, DBPrefix.ST_Asset, AssetState)

        newasset = assets.TryGet(self.asset_create_id)

        self.assertIsNotNone(newasset)

        self.assertEqual(newasset.AssetType, 1)
        self.assertEqual(newasset.Precision, 8)
        self.assertEqual(Crypto.ToAddress(newasset.Admin), self.asset_admin)
        self.assertEqual(Crypto.ToAddress(newasset.Issuer), self.asset_admin)
        self.assertIsInstance(newasset.AssetId, UInt256)
        self.assertEqual(newasset.AssetId.ToBytes(), self.asset_create_id)
Ejemplo n.º 11
0
    def HandleGetDataMessageReceived(self, payload):
        """
        Process a InvPayload payload.

        Args:
            payload (neo.Network.Inventory):
        """
        inventory = IOHelper.AsSerializableWithType(payload, 'neo.Network.Payloads.InvPayload.InvPayload')

        for hash in inventory.Hashes:
            hash = hash.encode('utf-8')

            item = None
            # try to get the inventory to send from relay cache

            if hash in self.leader.RelayCache.keys():
                item = self.leader.RelayCache[hash]

            if item:
                if inventory.Type == int.from_bytes(InventoryType.TX, 'little'):
                    message = Message(command='tx', payload=item, print_payload=True)
                    self.SendSerializedMessage(message)

                elif inventory.Type == int.from_bytes(InventoryType.Block, 'little'):
                    logger.info("handle block!")

                elif inventory.Type == int.from_bytes(InventoryType.Consensus, 'little'):
                    logger.info("handle consensus")
 def HandleVersion(self, payload):
     """Process the response of `self.RequestVersion`."""
     self.Version = IOHelper.AsSerializableWithType(
         payload, "neo.Network.Payloads.VersionPayload.VersionPayload")
     self.nodeid = self.Version.Nonce
     #        self.Log("Remote version %s " % vars(self.Version))
     self.SendVersion()
    def test_contract_create_block(self):

        hexdata = binascii.unhexlify(self.contract_create_block)

        block = Helper.AsSerializableWithType(hexdata, 'neo.Core.Block.Block')

        self.assertEqual(block.Index, self.contract_block_index)

        result = Blockchain.Default().Persist(block)

        self.assertTrue(result)

        contracts = DBCollection(Blockchain.Default()._db, DBPrefix.ST_Contract, ContractState)

        contract_added = contracts.TryGet(self.contract_hash)

        self.assertIsNotNone(contract_added)

        self.assertEqual(contract_added.HasStorage, False)
        self.assertEqual(contract_added.Name, b'test')
        self.assertEqual(contract_added.Email, b'test')

        self.assertEqual(len(Blockchain.Default().SearchContracts("test NEX Template V3")), 1)
        self.assertEqual(len(Blockchain.Default().SearchContracts("TEST nex TEMPLATE v3")), 1)
        self.assertEqual(len(Blockchain.Default().SearchContracts("TEST!")), 0)

        code = contract_added.Code

        self.assertIsNotNone(code)

        self.assertEqual(code.ReturnType, 2)

        self.assertEqual(code.ScriptHash().ToBytes(), self.contract_hash)
        self.assertEqual(code.Script.hex().encode('utf-8'), self.contract_block_script)
Ejemplo n.º 14
0
    def HandleGetBlocksMessageReceived(self, payload):
        """
        Process a GetBlocksPayload payload.

        Args:
            payload (neo.Network.Payloads.GetBlocksPayload):
        """
        if not self.leader.ServiceEnabled:
            return

        inventory = IOHelper.AsSerializableWithType(
            payload, 'neo.Network.Payloads.GetBlocksPayload.GetBlocksPayload')

        if not BC.Default().GetHeader(inventory.HashStart):
            self.Log("Hash %s not found %s " % inventory.HashStart)
            return

        hashes = []
        hcount = 0
        hash = inventory.HashStart
        while hash != inventory.HashStop and hcount < 500:
            hash = BC.Default().GetNextBlockHash(hash)
            if hash is None:
                break
            hashes.append(hash)
            hcount += 1
        if hcount > 0:
            self.Log("sending inv hashes! %s " % hashes)
            self.SendSerializedMessage(
                Message('inv',
                        InvPayload(type=InventoryType.Block, hashes=hashes)))
Ejemplo n.º 15
0
    def HandleBlockReceived(self, inventory):
        """
        Process a Block inventory payload.

        Args:
            inventory (neo.Network.Inventory):
        """
        block = IOHelper.AsSerializableWithType(inventory, 'neo.Core.Block.Block')
        if not block:
            return

        blockhash = block.Hash.ToBytes()
        try:
            if blockhash in BC.Default().BlockRequests:
                BC.Default().BlockRequests.remove(blockhash)
        except KeyError:
            pass
        try:
            if blockhash in self.myblockrequests:
                # logger.debug(f"{self.prefix} received block: {block.Index}")
                self.heart_beat(HEARTBEAT_BLOCKS)
                self.myblockrequests.remove(blockhash)
        except KeyError:
            pass
        self.leader.InventoryReceived(block)
Ejemplo n.º 16
0
    def HandleInvMessage(self, payload):
        """
        Process a block header inventory payload.

        Args:
            inventory (neo.Network.Payloads.InvPayload):
        """

        if self.sync_mode != MODE_MAINTAIN:
            return

        inventory = IOHelper.AsSerializableWithType(
            payload, 'neo.Network.Payloads.InvPayload.InvPayload')

        if inventory.Type == InventoryType.BlockInt:

            ok_hashes = []
            for hash in inventory.Hashes:
                hash = hash.encode('utf-8')
                if hash not in self.myblockrequests and hash not in BC.Default(
                ).BlockRequests:
                    ok_hashes.append(hash)
                    BC.Default().BlockRequests.add(hash)
                    self.myblockrequests.add(hash)
            if len(ok_hashes):
                #                logger.info("OK HASHES, get data %s " % ok_hashes)
                message = Message("getdata",
                                  InvPayload(InventoryType.Block, ok_hashes))
                self.SendSerializedMessage(message)

        elif inventory.Type == InventoryType.TXInt:
            pass
        elif inventory.Type == InventoryType.ConsensusInt:
            pass
Ejemplo n.º 17
0
    def test_block_publish_tx(self):

        hexdata = binascii.unhexlify(self.pb_raw)

        block = Helper.AsSerializableWithType(hexdata, 'neo.Core.Block.Block')

        self.assertEqual(block.Hash.ToBytes(), self.pb_hash)
Ejemplo n.º 18
0
    def test_1050514(self):

        path = '%s/fixtures/1050514.txt' % os.getcwd()

        with open(path, 'rb') as f:
            #
            blockraw = f.read().strip()

            hex = binascii.unhexlify(blockraw)

            block = Helper.AsSerializableWithType(hex, 'neo.Core.Block.Block')

            self.assertEqual(self.big_tx_hash, block.Hash.ToBytes())

        json = block.ToJson()

        self.assertEqual(json['index'], 1050514)
        self.assertEqual(json['hash'],
                         '0x%s' % self.big_tx_hash.decode('utf-8'))
        self.assertEqual(json['nonce'], 'a62f207d4f00af81')
        self.assertEqual(json['nextconsensus'],
                         'APyEx5f4Zm4oCHwFWiSTaph1fPBxZacYVR')
        self.assertEqual(len(json['tx']), 65)

        for tx in json['tx']:
            if tx['txid'] == '1dc3543a5b54fcfce3fefba6c772f3a59740b2b1784690b3c66e2f7052f002bb':
                bigclaim = tx
                self.assertEqual(len(bigclaim['claims']), 1756)
                vout = bigclaim['vout'][0]
                self.assertEqual(vout['ScriptHash'],
                                 'AFnRHRQceaUgFQxPTttAQzAZvsjGSNtHCH')
                self.assertEqual(vout['Value'], 5786800133972)
Ejemplo n.º 19
0
    def test_block_seventyfour(self):

        hexdata = binascii.unhexlify(self.sf_raw)

        block = Helper.AsSerializableWithType(hexdata, 'neo.Core.Block.Block')

        self.assertEqual(block.MerkleRoot.ToBytes(), self.sf_merk)
        self.assertEqual(block.Hash.ToBytes(), self.sf_hash)
Ejemplo n.º 20
0
    def test_a_run_sc(self):

        hexdata = binascii.unhexlify(self.invb)

        block = Helper.AsSerializableWithType(hexdata, 'neo.Core.Block.Block')

        result = self._blockchain.Persist(block)

        self.assertTrue(result)
Ejemplo n.º 21
0
    def HandleBlockHeadersReceived(self, inventory):

        inventory = IOHelper.AsSerializableWithType(
            inventory, 'neo.Network.Payloads.HeadersPayload.HeadersPayload')

        BC.Default().AddHeaders(inventory.Headers)

        if BC.Default().HeaderHeight < self.Version.StartHeight:
            self.AskForMoreHeaders()
Ejemplo n.º 22
0
    def LoadContracts(self):
        ctr = {}

        for ct in Contract.select():
            data = binascii.unhexlify(ct.RawData)
            contract = Helper.AsSerializableWithType(data, 'neo.SmartContract.Contract.Contract')
            ctr[contract.ScriptHash.ToBytes()] = contract

        return ctr
Ejemplo n.º 23
0
    def HandlePeerInfoReceived(self, payload):
        """Process response of `self.RequestPeerInfo`."""
        addrs = IOHelper.AsSerializableWithType(payload, 'neo.Network.Payloads.AddrPayload.AddrPayload')

        if not addrs:
            return

        for nawt in addrs.NetworkAddressesWithTime:
            self.leader.RemoteNodePeerReceived(nawt.Address, nawt.Port, self.prefix)
Ejemplo n.º 24
0
    def test_invocation_txn_size(self):
        """ For more information about the following test read here
            https://github.com/neo-project/neo/issues/652
        """
        raw_tx = b"d1015904802b530b14d5a682e81b8a840cc44b3b360cbd0f1ee6f50efd14235a717ed7ed18a43de47499c3d05b8d4a4bcf3a53c1087472616e7366657267fb1c540417067c270dee32f21023aa8b9b71abcef166fc47646b02d3f92300000000000000000120235a717ed7ed18a43de47499c3d05b8d4a4bcf3a0000014140b9234cad658c4d512bca453908a0df1c2beda49c544ec735bb492b81b4d0974ac8d66046061b3d0ce823e27c71fef1ee6a8f2fa369198ac74acedd045901d7222321030ab39b99d8675cd9bd90aaec37cba964297cc817078d33e508ab11f1d245c068ac"
        tx = IOHelper.DeserializeTX(binascii.unhexlify(raw_tx))

        txjson = tx.ToJson()
        self.assertEqual(227, txjson['size'])
Ejemplo n.º 25
0
    def test_block_non_verbose(self):
        req = self._gen_rpc_req("getblock", params=[2003, 0])
        mock_req = mock_request(json.dumps(req).encode("utf-8"))
        res = json.loads(self.app.home(mock_req))
        self.assertIsNotNone(res['result'])

        # we should be able to instantiate a matching block with the result
        output = binascii.unhexlify(res['result'])
        block = Helper.AsSerializableWithType(output, 'neo.Core.Block.Block')
        self.assertEqual(block.Index, 2003)
        self.assertEqual(len(block.Transactions), 1)
Ejemplo n.º 26
0
    def HandleVersion(self, payload):
        """Process the response of `self.RequestVersion`."""
        self.Version = IOHelper.AsSerializableWithType(payload, "neo.Network.Payloads.VersionPayload.VersionPayload")

        if self.incoming_client:
            if self.Version.Nonce == self.nodeid:
                self.Disconnect()
            self.SendVerack()
        else:
            self.nodeid = self.Version.Nonce
            self.SendVersion()
Ejemplo n.º 27
0
    def test_testnet10412011(self):

        block = Helper.AsSerializableWithType(self.block_bytes, 'neo.Core.Block.Block')
        self.assertEqual(len(block.Transactions), 2)
        state_tx = block.Transactions[1]
        self.assertIsInstance(state_tx, StateTransaction)
        self.assertEqual(len(state_tx.Descriptors), 1)
        descriptor = state_tx.Descriptors[0]
        self.assertIsInstance(descriptor, StateDescriptor)
        self.assertEqual(descriptor.Type, StateType.Validator)
        self.assertEqual(descriptor.SystemFee, Fixed8.FromDecimal(1000))
        self.assertEqual(descriptor.Key, binascii.unhexlify(b'03c089d7122b840a4935234e82e26ae5efd0c2acb627239dc9f207311337b6f2c1'))
Ejemplo n.º 28
0
    def test_add_header(self):
        print("add header")
        hexdata = binascii.unhexlify(self.block_one_raw)

        block_one = Helper.AsSerializableWithType(hexdata, 'neo.Core.Block.Block')

        if settings.MAGIC == 1953787457:
            self.assertEqual(self._blockchain.CurrentHeaderHash, b'b3181718ef6167105b70920e4a8fbbd0a0a56aacf460d70e10ba6fa1668f1fef')
        else:
            self.assertEqual(self._blockchain.CurrentHeaderHash, b'd42561e3d30e15be6400b6df2f328e02d2bf6354c41dce433bc57687c82144bf')

        self.assertEqual(self._blockchain.HeaderHeight, 0)
Ejemplo n.º 29
0
    def test_b_invocation(self):

        hexdata = binascii.unhexlify(self.tbblock)

        block = Helper.AsSerializableWithType(hexdata, 'neo.Core.Block.Block')

        json = block.ToJson()

        self.assertIsNotNone(json)

        result = self._blockchain.Persist(block)

        self.assertTrue(result)
Ejemplo n.º 30
0
    def test_add_header(self):

        hexdata = binascii.unhexlify(self.block_one_raw)

        block_one = Helper.AsSerializableWithType(hexdata,
                                                  'neo.Core.Block.Block')
        header = block_one.Header

        if Settings.MAGIC == 1953787457:
            self.assertEqual(
                self._blockchain.CurrentHeaderHash,
                b'b3181718ef6167105b70920e4a8fbbd0a0a56aacf460d70e10ba6fa1668f1fef'
            )
        else:
            self.assertEqual(
                self._blockchain.CurrentHeaderHash,
                b'd42561e3d30e15be6400b6df2f328e02d2bf6354c41dce433bc57687c82144bf'
            )

        self.assertEqual(self._blockchain.HeaderHeight, 0)

        #add it to leveldb


#        self._blockchain.AddHeader( header )
#        self.assertEqual(self._blockchain.HeaderHeight, 1)

#now retrieve it
#        block_one_again = self._blockchain.GetHeader(header.Hash)

#        self.assertEqual(type(block_one_again), Header)
#        self.assertEqual(block_one_again.Hash.ToBytes(), self.block_one_hash)
#check to see if the header hash in correct
#        self.assertEqual(block_one_again.Hash.ToBytes(), self._blockchain.CurrentHeaderHash.ToBytes())

#now try adding the same block again and see if the height changes
#        self._blockchain.AddHeader(header)
#        self.assertEqual(self._blockchain.HeaderHeight, 1)

#current block height should still be zero
#        self.assertEqual(self._blockchain.Height, 0)

#now test adding the second block

#        self._blockchain.Persist(block_one)

#now the block height should be 1
#        self.assertEqual(self._blockchain.Height, 1)

#test contains block functions
#        self.assertTrue( self._blockchain.ContainsBlock(block_one_again.Index))