forked from samuelgoh1525/falcon-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutxo.py
180 lines (149 loc) · 5.36 KB
/
utxo.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import copy
import hashlib
import json
import keys
class UTXO:
'''
Format of transactions <dict>:
- id (hash)
- <list> inputs:
<dict>
- id (-1 for mined coins)
- output_index (-1 for mined coins)
- block_index
- amount
- signature
- <list> outputs
<dict>
- amount
- address (public key)
'''
def __init__(self):
# List of (unspent) transaction outputs
'''
UTXO format:
<list> dict:
- id
- output_index
- block_index
- amount
- address
'''
self.transaction_outputs = []
# Remaining coins to be mined
self.unmined = 24000000
def mine_new_coins(self, amount):
self.unmined -= amount
return self.unmined
def get_user_amount(self, user):
total = 0
for output in self.transaction_outputs:
if output['address'] == user:
total += output['amount']
return total
def add_utxo(self, id_in, output_index_in, block_index_in, amount_in, address_in):
tx = {
'id': id_in,
'output_index': output_index_in,
'block_index': block_index_in,
'amount': amount_in,
'address': address_in,
}
self.transaction_outputs.append(tx)
return tx
def modify_utxo(self, transaction, user_addr, block_index):
for input in transaction['inputs']:
# Remove these from the utxo set
# Except if they are newly mined coins
if (input['id'] != 'mined') and (input['output_index'] != 'mined'):
tx_remove = {
'id': input['id'],
'output_index': input['output_index'],
'block_index': input['block_index'],
'amount': input['amount'],
'address': user_addr,
}
self.transaction_outputs.remove(tx_remove)
index = 0
for output in transaction['outputs']:
# Add these to the utxo set
tx_add = {
'id': transaction['id'],
'output_index': index,
'block_index': block_index,
'amount': output['amount'],
'address': output['address'],
}
self.transaction_outputs.append(tx_add)
index += 1
return self.transaction_outputs
def assign_block_index(self, transaction, block_index):
index = 0
for output in transaction['outputs']:
# Assign block index to the utxo set
tx_to_find = {
'id': transaction['id'],
'output_index': index,
'block_index': None,
'amount': output['amount'],
'address': output['address'],
}
try:
tx_index = self.transaction_outputs.index(tx_to_find)
self.transaction_outputs[tx_index]['block_index'] = block_index
except ValueError:
return False
index += 1
return True
def make_transaction(self, sender, recipients, amounts, sender_key, is_falcon, ind_sym):
# Note: recipients and amounts are <lists>
total_amount = sum(amounts)
sender_balance = 0
tx_in = []
# Outputs of the transaction
tx_out = []
for i in range(len(recipients)):
tx = {
'amount': amounts[i],
'address': recipients[i],
}
tx_out.append(tx)
# Search the UTXO set for coins adding up to at least the total amount
# These will be the inputs for the transaction
# Additional coins will be returned to the user as change
for output in self.transaction_outputs:
if sender_balance >= total_amount:
# Once we have enough coins, stop searching
break
elif output['address'] == sender:
sender_balance += output['amount']
utxo_entry = copy.deepcopy(output)
utxo_entry.pop('address')
# For each input, generate a signature with the sender's private key
signature = keys.sign(utxo_entry, sender_key, ind_sym)
if is_falcon:
utxo_entry['signature'] = signature.hex()
else:
utxo_entry['signature'] = (signature.signature).hex()
tx_in.append(utxo_entry)
# Check if sender has enough coins
# Or if excess coins need to be returned as change
remainder = sender_balance - total_amount
if remainder < 0:
# Not enough coins
return None
elif remainder > 0:
# Create additional transaction to return change to sender
tx = {
'amount': remainder,
'address': sender,
}
tx_out.append(tx)
transaction = {
'inputs': tx_in,
'outputs': tx_out,
}
# Transaction ID is just a unique hash to identify the transaction
transaction_string = json.dumps(transaction, sort_keys=True).encode()
transaction['id'] = hashlib.sha256(transaction_string).hexdigest()
return transaction