コード例 #1
0
 def test_sequence(self):
     sequence_0 = Sequence()
     self.assertEqual(sequence_0, MAX_SEQUENCE)
     self.assertTrue(sequence_0.is_max())
     self.assertFalse(sequence_0.is_relative())
     self.assertIsNone(sequence_0.relative_blocks())
     self.assertIsNone(sequence_0.relative_time())
     self.assertEqual(Sequence.parse(BytesIO(sequence_0.serialize())),
                      sequence_0)
     time_amount = 512 * 1000
     sequence_1 = Sequence.from_relative_time(time_amount)
     self.assertFalse(sequence_1.is_comparable(sequence_0))
     self.assertIsNone(sequence_1.relative_blocks())
     self.assertEqual(sequence_1.relative_time(), time_amount)
     self.assertEqual(Sequence.parse(BytesIO(sequence_1.serialize())),
                      sequence_1)
     blocks_amount = 144
     sequence_2 = Sequence.from_relative_blocks(blocks_amount)
     self.assertIsNone(sequence_2.relative_time())
     self.assertEqual(sequence_2.relative_blocks(), blocks_amount)
     self.assertFalse(sequence_1.is_comparable(sequence_2))
     sequence_3 = Sequence.from_relative_time(512 * 100)
     self.assertTrue(sequence_3 < sequence_1)
     with self.assertRaises(ValueError):
         sequence_2 < sequence_0
     with self.assertRaises(ValueError):
         sequence_2 < sequence_1
     with self.assertRaises(ValueError):
         Sequence(-1)
     with self.assertRaises(ValueError):
         Sequence(1 << 32)
コード例 #2
0
 def parse(cls, s):
     """Takes a byte stream and parses the tx_input at the start
     return a TxIn object
     """
     # s.read(n) will return n bytes
     # prev_tx is 32 bytes, little endian
     prev_tx = s.read(32)[::-1]
     # prev_index is 4 bytes, little endian, interpret as int
     prev_index = little_endian_to_int(s.read(4))
     # script_sig is a variable field (length followed by the data)
     # you can use Script.parse to get the actual script
     script_sig = Script.parse(s)
     # sequence is 4 bytes, little-endian, interpret as int
     sequence = Sequence.parse(s)
     # return an instance of the class (cls(...))
     return cls(prev_tx, prev_index, script_sig, sequence)