-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransaction.py
72 lines (55 loc) · 2.27 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from enum import Enum
import hashlib
class Tx:
"""A blockchain transaction.
"""
def __init__(self, curr_tick, miner_id, tx_id, pointers):
"""
Arguments:
curr_tick {int} -- Current simulation tick.
miner_id {int} -- Id of originating miner.
tx_id {int} -- Transaction id (may be duplicate for reissued transaction).
"""
self.origin = miner_id
self.id = tx_id
self.birthday = curr_tick
self.pointers = pointers # Backpointer(s) are an inherent part of the tx, each miner takes it or leaves it as a whole.
self.history = [] # Event history.
self.addEvent(curr_tick, miner_id, State.CREATED)
self.stats = {} # Filled in after simulation for data.
str_to_hash = ''.join(self.pointers)+str(self.id)+str(self.birthday)+str(self.origin) # Don't include mutable properties like history.
self.hash = hashlib.md5(str_to_hash).hexdigest()
def addEvent(self, time_stamp, miner_id, state):
"""Add event to transaction history.
Arguments:
time_stamp {int} -- Tick that event happened on.
miner_id {int} -- Id of miner with new state for this transaction.
state {State} -- New state that the miner entered into for this transaction.
"""
self.history.append(Event(time_stamp, miner_id, state))
def __str__(self):
"""
Returns:
str -- String representation of transaction.
"""
return "%d %d %d %s" % (self.id, self.origin, self.birthday, self.pointers)
class Event:
"""Event marking a change in state of a miner for a given transaction.
"""
def __init__(self, time_stamp, miner_id, state):
"""Event in transaction history.
Arguments:
time_stamp {int} -- Tick that event happened on.
miner_id {int} -- Id of miner with new state for this transaction.
state {State} -- New state that the miner entered into for this transaction.
"""
self.time_stamp = time_stamp
self.miner_id = miner_id
self.state = state
class State(Enum):
"""Enumeration of transaction states.
"""
CREATED = 0
PRE_CONSENSUS = 1
CONSENSUS = 2
DISCONSENSED = 3