Example #1
0
    def GenesisBlock():
        """
        Create the GenesisBlock.

        Returns:
            BLock:
        """
        prev_hash = UInt256(data=bytearray(32))
        timestamp = int(
            datetime(2016, 7, 15, 15, 8, 21, tzinfo=pytz.utc).timestamp())
        index = 0
        consensus_data = 2083236893  # 向比特币致敬 ( Pay Tribute To Bitcoin )
        next_consensus = Blockchain.GetConsensusAddress(
            Blockchain.StandbyValidators())
        script = Witness(bytearray(0), bytearray(PUSHT))

        mt = MinerTransaction()
        mt.Nonce = 2083236893

        output = TransactionOutput(
            Blockchain.SystemShare().Hash,
            Blockchain.SystemShare().Amount,
            Crypto.ToScriptHash(
                Contract.CreateMultiSigRedeemScript(
                    int(len(Blockchain.StandbyValidators()) / 2) + 1,
                    Blockchain.StandbyValidators())))

        it = IssueTransaction([], [output], [], [script])

        return Block(
            prev_hash, timestamp, index, consensus_data, next_consensus,
            script,
            [mt, Blockchain.SystemShare(),
             Blockchain.SystemCoin(), it], True)
Example #2
0
    def GenesisBlock():
        """
        Create the GenesisBlock.

        Returns:
            BLock:
        """
        prev_hash = UInt256(data=bytearray(32))
        timestamp = int(datetime(2016, 7, 15, 15, 8, 21, tzinfo=pytz.utc).timestamp())
        index = 0
        consensus_data = 2083236893  # 向比特币致敬 ( Pay Tribute To Bitcoin )
        next_consensus = Blockchain.GetConsensusAddress(Blockchain.StandbyValidators())
        script = Witness(bytearray(0), bytearray(PUSHT))

        mt = MinerTransaction()
        mt.Nonce = 2083236893

        output = TransactionOutput(
            Blockchain.SystemShare().Hash,
            Blockchain.SystemShare().Amount,
            Crypto.ToScriptHash(Contract.CreateMultiSigRedeemScript(int(len(Blockchain.StandbyValidators()) / 2) + 1,
                                                                    Blockchain.StandbyValidators()))
        )

        it = IssueTransaction([], [output], [], [script])

        return Block(prev_hash, timestamp, index, consensus_data,
                     next_consensus, script,
                     [mt, Blockchain.SystemShare(), Blockchain.SystemCoin(), it],
                     True)
Example #3
0
    def test_issue_tx(self):

        miner_tx = MinerTransaction()
        miner_tx.Nonce = 2083236893

        share_tx = GetSystemShare()
        coin_tx = GetSystemCoin()

        script = Contract.CreateMultiSigRedeemScript(int(len(Blockchain.StandbyValidators()) / 2) + 1, Blockchain.StandbyValidators())

        if Settings.MAGIC == 1953787457:
            self.assertEqual(script, self.contractraw)
            out = Crypto.ToScriptHash(script)

            output = TransactionOutput(
                share_tx.Hash,
                Blockchain.SystemShare().Amount,
                out
            )

            script = Witness( bytearray(0), bytearray(PUSHT))

            issue_tx = IssueTransaction([],[output],[], [script])
            self.assertEqual(issue_tx.GetHashData(), self.issuetx_rraw)
            self.assertEqual(issue_tx.Hash.ToBytes(), self.gen_issue_tx_id)
Example #4
0
    def test_issue_tx(self):

        miner_tx = MinerTransaction()
        miner_tx.Nonce = 2083236893

        share_tx = GetSystemShare()
        coin_tx = GetSystemCoin()

        script = Contract.CreateMultiSigRedeemScript(int(len(Blockchain.StandbyValidators()) / 2) + 1, Blockchain.StandbyValidators())

        if settings.MAGIC == 1953787457:
            self.assertEqual(script, self.contractraw)
            out = Crypto.ToScriptHash(script)

            output = TransactionOutput(
                share_tx.Hash,
                Blockchain.SystemShare().Amount,
                out
            )

            script = Witness(bytearray(0), bytearray(PUSHT))

            issue_tx = IssueTransaction([], [output], [], [script])
            self.assertEqual(issue_tx.GetHashData(), self.issuetx_rraw)
            self.assertEqual(issue_tx.Hash.ToBytes(), self.gen_issue_tx_id)
Example #5
0
    def test_relay(self):
        leader = NodeLeader.Instance()

        def mock_call_later(delay, method, *args):
            method(*args)

        def mock_connect_tcp(host, port, factory, timeout=120):
            node = NeoNode()
            node.endpoint = Endpoint(host, port)
            leader.AddConnectedPeer(node)
            return node

        def mock_send_msg(node, message):
            return True

        with patch('twisted.internet.reactor.connectTCP', mock_connect_tcp):
            with patch('twisted.internet.reactor.callLater', mock_call_later):
                with patch('neo.Network.NeoNode.NeoNode.SendSerializedMessage',
                           mock_send_msg):
                    leader.Start()

                    miner = MinerTransaction()

                    res = leader.Relay(miner)
                    self.assertFalse(res)

                    tx = self._generate_tx()

                    res = leader.Relay(tx)
                    self.assertEqual(res, True)

                    self.assertTrue(tx.Hash.ToBytes() in leader.MemPool.keys())
                    res2 = leader.Relay(tx)
                    self.assertFalse(res2)
Example #6
0
    def DeserializeFrom(reader):
        ttype = reader.ReadByte()
        tx = None

        from neo.Core.TX.RegisterTransaction import RegisterTransaction
        from neo.Core.TX.IssueTransaction import IssueTransaction
        from neo.Core.TX.ClaimTransaction import ClaimTransaction
        from neo.Core.TX.MinerTransaction import MinerTransaction

        if ttype == int.from_bytes(TransactionType.RegisterTransaction,
                                   'little'):
            tx = RegisterTransaction()
        elif ttype == int.from_bytes(TransactionType.MinerTransaction,
                                     'little'):
            tx = MinerTransaction()
        elif ttype == int.from_bytes(TransactionType.IssueTransaction,
                                     'little'):
            tx = IssueTransaction()
        elif ttype == int.from_bytes(TransactionType.ClaimTransaction,
                                     'little'):
            tx = ClaimTransaction()

        tx.DeserializeUnsignedWithoutType(reader)

        try:
            witness = Witness()
            witness.Deserialize(reader)
            tx.scripts = [witness]
        except Exception as e:
            pass

        tx.OnDeserialized()

        return tx
Example #7
0
    def DeserializeFrom(reader):
        """
        Deserialize full object.

        Args:
            reader (neo.IO.BinaryReader):

        Returns:
            Transaction:
        """
        ttype = reader.ReadByte()
        tx = None

        from neo.Core.TX.RegisterTransaction import RegisterTransaction
        from neo.Core.TX.IssueTransaction import IssueTransaction
        from neo.Core.TX.ClaimTransaction import ClaimTransaction
        from neo.Core.TX.MinerTransaction import MinerTransaction
        from neo.Core.TX.PublishTransaction import PublishTransaction
        from neo.Core.TX.InvocationTransaction import InvocationTransaction
        from neo.Core.TX.EnrollmentTransaction import EnrollmentTransaction
        from neo.Core.TX.StateTransaction import StateTransaction

        if ttype == int.from_bytes(TransactionType.RegisterTransaction, 'little'):
            tx = RegisterTransaction()
        elif ttype == int.from_bytes(TransactionType.MinerTransaction, 'little'):
            tx = MinerTransaction()
        elif ttype == int.from_bytes(TransactionType.IssueTransaction, 'little'):
            tx = IssueTransaction()
        elif ttype == int.from_bytes(TransactionType.ClaimTransaction, 'little'):
            tx = ClaimTransaction()
        elif ttype == int.from_bytes(TransactionType.PublishTransaction, 'little'):
            tx = PublishTransaction()
        elif ttype == int.from_bytes(TransactionType.InvocationTransaction, 'little'):
            tx = InvocationTransaction()
        elif ttype == int.from_bytes(TransactionType.EnrollmentTransaction, 'little'):
            tx = EnrollmentTransaction()
        elif ttype == int.from_bytes(TransactionType.StateTransaction, 'little'):
            tx = StateTransaction()
        else:
            tx = Transaction()
            tx.Type = ttype

        tx.DeserializeUnsignedWithoutType(reader)

        tx.scripts = []
        byt = reader.ReadVarInt()

        if byt > 0:
            for i in range(0, byt):
                witness = Witness()
                witness.Deserialize(reader)

                tx.scripts.append(witness)

        tx.OnDeserialized()

        return tx
Example #8
0
    def test_inventory_received(self):

        leader = NodeLeader.Instance()

        miner = MinerTransaction()
        miner.Nonce = 1234
        res = leader.InventoryReceived(miner)

        self.assertFalse(res)

        block = Blockchain.Default().GenesisBlock()

        res2 = leader.InventoryReceived(block)

        self.assertFalse(res2)

        tx = self._generate_tx()

        res = leader.InventoryReceived(tx)

        self.assertIsNone(res)
Example #9
0
    def test_inventory_received(self):

        leader = NodeLeader.Instance()

        miner = MinerTransaction()
        miner.Nonce = 1234
        res = leader.InventoryReceived(miner)

        self.assertFalse(res)

        block = Blockchain.Default().GenesisBlock()

        res2 = leader.InventoryReceived(block)

        self.assertFalse(res2)

        tx = self._generate_tx()

        res = leader.InventoryReceived(tx)

        self.assertIsNone(res)
 def test_miner_tx(self):
     miner_tx = MinerTransaction()
     miner_tx.Nonce = 2083236893
     self.assertEqual(miner_tx.Hash.ToBytes(), self.gen_miner_tx_id)
Example #11
0
 def test_miner_tx(self):
     miner_tx = MinerTransaction()
     miner_tx.Nonce = 2083236893
     self.assertEqual(miner_tx.Hash.ToBytes(), self.gen_miner_tx_id)
Example #12
0
 def test_check_miner_tx_size(self):
     """ see original test here
         https://github.com/neo-project/neo/blob/5ce0e4c3192cdcce700105030ed03197961e0466/neo.UnitTests/UT_MinerTransaction.cs#L39-L48
     """
     m_tx = MinerTransaction()
     self.assertEqual(m_tx.Size(), 10)