File size: 2,929 Bytes
8f59bb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | import sys
from .StateBase import StateBase
from .CoinState import CoinState
from neocore.IO.BinaryReader import BinaryReader
from neo.IO.MemoryStream import StreamManager
class UnspentCoinState(StateBase):
Items = None
def __init__(self, items=None):
"""
Create an instance.
Args:
items (list, Optional): of neo.Core.TX.Transaction.TransactionOutput items.
"""
if items is None:
self.Items = []
else:
self.Items = items
@staticmethod
def FromTXOutputsConfirmed(outputs):
"""
Get unspent outputs from a list of transaction outputs.
Args:
outputs (list): of neo.Core.TX.Transaction.TransactionOutput items.
Returns:
UnspentCoinState:
"""
uns = UnspentCoinState()
uns.Items = [0] * len(outputs)
for i in range(0, len(outputs)):
uns.Items[i] = int(CoinState.Confirmed)
return uns
def Size(self):
"""
Get the total size in bytes of the object.
Returns:
int: size.
"""
return super(UnspentCoinState, self).Size() + sys.getsizeof(self.Items)
@property
def IsAllSpent(self):
"""
Flag indicating if all balance is spend.
Returns:
bool:
"""
for item in self.Items:
if item == CoinState.Confirmed:
return False
return True
def OrEqValueForItemAt(self, index, value):
length = len(self.Items)
while length < index + 1:
self.Items.append(0)
length = len(self.Items)
self.Items[index] |= value
def Deserialize(self, reader):
"""
Deserialize full object.
Args:
reader (neocore.IO.BinaryReader):
"""
super(UnspentCoinState, self).Deserialize(reader)
blen = reader.ReadVarInt()
self.Items = [0] * blen
for i in range(0, blen):
self.Items[i] = int.from_bytes(reader.ReadByte(do_ord=False), 'little')
@staticmethod
def DeserializeFromDB(buffer):
"""
Deserialize full object.
Args:
buffer (bytes, bytearray, BytesIO): (Optional) data to create the stream from.
Returns:
UnspentCoinState:
"""
m = StreamManager.GetStream(buffer)
reader = BinaryReader(m)
uns = UnspentCoinState()
uns.Deserialize(reader)
StreamManager.ReleaseStream(m)
return uns
def Serialize(self, writer):
"""
Serialize full object.
Args:
writer (neo.IO.BinaryWriter):
"""
super(UnspentCoinState, self).Serialize(writer)
writer.WriteVarInt(len(self.Items))
for item in self.Items:
byt = item.to_bytes(1, 'little')
writer.WriteByte(byt)
|