The combination of a transaction hash and an index n into its vout
| 85 | |
| 86 | |
| 87 | class COutPoint(ImmutableSerializable): |
| 88 | """The combination of a transaction hash and an index n into its vout""" |
| 89 | __slots__ = ['hash', 'n'] |
| 90 | |
| 91 | def __init__(self, hash=b'\x00'*32, n=0xffffffff): |
| 92 | if not len(hash) == 32: |
| 93 | raise ValueError('COutPoint: hash must be exactly 32 bytes; got %d bytes' % len(hash)) |
| 94 | object.__setattr__(self, 'hash', hash) |
| 95 | if not (0 <= n <= 0xffffffff): |
| 96 | raise ValueError('COutPoint: n must be in range 0x0 to 0xffffffff; got %x' % n) |
| 97 | object.__setattr__(self, 'n', n) |
| 98 | |
| 99 | @classmethod |
| 100 | def stream_deserialize(cls, f): |
| 101 | hash = ser_read(f,32) |
| 102 | n = struct.unpack(b"<I", ser_read(f,4))[0] |
| 103 | return cls(hash, n) |
| 104 | |
| 105 | def stream_serialize(self, f): |
| 106 | assert len(self.hash) == 32 |
| 107 | f.write(self.hash) |
| 108 | f.write(struct.pack(b"<I", self.n)) |
| 109 | |
| 110 | def is_null(self): |
| 111 | return ((self.hash == b'\x00'*32) and (self.n == 0xffffffff)) |
| 112 | |
| 113 | def __repr__(self): |
| 114 | if self.is_null(): |
| 115 | return 'COutPoint()' |
| 116 | else: |
| 117 | return 'COutPoint(lx(%r), %i)' % (b2lx(self.hash), self.n) |
| 118 | |
| 119 | def __str__(self): |
| 120 | return '%s:%i' % (b2lx(self.hash), self.n) |
| 121 | |
| 122 | @classmethod |
| 123 | def from_outpoint(cls, outpoint): |
| 124 | """Create an immutable copy of an existing OutPoint |
| 125 | |
| 126 | If outpoint is already immutable (outpoint.__class__ is COutPoint) it is |
| 127 | returned directly. |
| 128 | """ |
| 129 | if outpoint.__class__ is COutPoint: |
| 130 | return outpoint |
| 131 | |
| 132 | else: |
| 133 | return cls(outpoint.hash, outpoint.n) |
| 134 | |
| 135 | @__make_mutable |
| 136 | class CMutableOutPoint(COutPoint): |
no outgoing calls