-
Notifications
You must be signed in to change notification settings - Fork 2
/
transaction.py
58 lines (49 loc) · 2.12 KB
/
transaction.py
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
import logging
# A Transaction in the context of a specific mempool and blocktemplate state.
# Ancestors, Parents, and children will be updated as the blocktemplate is being built whenever anything gets picked from the Cluster.
class Transaction():
def __init__(self, txid, fee, weight, parents=None, children=None, ancestors=None, descendants=None):
self.txid = txid
self.fee = int(fee)
self.feerate = None
self.weight = int(weight)
self.hash = None
if parents is None:
parents = []
self.parents = set([] + parents)
if ancestors is None:
ancestors = []
self.ancestors = set([] + ancestors)
self.permanent_parents = []
if children is None:
children = []
self.children = set([] + children)
if descendants is None:
descendants = []
self.descendants = set([] + descendants)
def createExportDict(self):
txRep = { 'fee': self.fee, 'weight': self.weight, 'spentby': list(self.children), 'depends': list(self.parents) }
return txRep
def get_feerate(self):
if not self.feerate:
self.feerate = self.fee / self.weight
return self.feerate
def getLocalClusterTxids(self):
return list(set([self.txid] + list(self.children) + list(self.parents)))
def __str__(self):
return "{txid: " + self.txid + ", children: " + str(self.children) + ", parents: " + str(self.parents) + ", fee: " + str(self.fee) + ", weight: " + str(self.weight) + "}"
def __repr__(self):
return "Transaction(%s)" % (self.txid)
def __eq__(self, other):
if isinstance(other, Transaction):
return self.txid == other.txid
return NotImplemented
def __lt__(self, other):
# Sort highest feerate first, use highest weight as tiebreaker
if self.get_feerate() == other.get_feerate():
return self.weight > other.weight
return self.get_feerate() > other.get_feerate()
def __hash__(self):
if self.hash is None:
self.hash = hash(self.__repr__())
return self.hash