forked from zack-bitcoin/basiccoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransactions.py
More file actions
70 lines (61 loc) · 2.44 KB
/
transactions.py
File metadata and controls
70 lines (61 loc) · 2.44 KB
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
import blockchain, custom, copy, tools
#This file explains how we tell if a transaction is valid or not, it explains
#how we update the system when new transactions are added to the blockchain.
def addr(tx): return tools.make_address(tx['pubkeys'], len(tx['signatures']))
def spend_verify(tx, txs, DB):
def sigs_match(sigs, pubs, msg):
for sig in sigs:
for pub in pubs:
try:
if tools.verify(msg, sig, pub):
sigs.remove(sig)
pubs.remove(pub)
except:
pass
return len(sigs)==0
tx_copy=copy.deepcopy(tx)
tx_copy_2=copy.deepcopy(tx)
tx_copy.pop('signatures')
if len(tx['pubkeys'])==0: return False
if len(tx['signatures'])>len(tx['pubkeys']): return False
msg=tools.det_hash(tx_copy)
if not sigs_match(copy.deepcopy(tx['signatures']), copy.deepcopy(tx['pubkeys']), msg): return False
if tx['amount']<custom.fee: return False
address=addr(tx_copy_2)
total_cost=0
for Tx in filter(lambda t: address==addr(t), [tx]+txs):
if Tx['type']=='spend':
total_cost+=Tx['amount']
if Tx['type']=='mint':
total_cost-=custom.block_reward
return int(blockchain.db_get(address, DB)['amount'])>=total_cost
def mint_verify(tx, txs, DB):
return 0==len(filter(lambda t: t['type']=='mint', txs))
tx_check={'spend':spend_verify, 'mint':mint_verify}####
#------------------------------------------------------
def adjust(key, pubkey, amount, DB):
acc=blockchain.db_get(pubkey, DB)
acc[key]+=amount
blockchain.db_put(pubkey, acc, DB)
def mint(tx, DB):
address=addr(tx)
adjust('amount', address, custom.block_reward, DB)
adjust('count', address, 1, DB)
def spend(tx, DB):
address=addr(tx)
adjust('amount', address, -tx['amount'], DB)
adjust('amount', tx['to'], tx['amount']-custom.fee, DB)
adjust('count', address, 1, DB)
add_block={'mint':mint, 'spend':spend}####
#-----------------------------------------
def unmint(tx, DB):
address=addr(tx)
adjust('amount', address, -custom.block_reward, DB)
adjust('count', address, -1, DB)
def unspend(tx, DB):
address=addr(tx)
adjust('amount', address, tx['amount'], DB)
adjust('amount', tx['to'], custom.fee-tx['amount'], DB)
adjust('count', address, -1, DB)
delete_block={'mint':unmint, 'spend':unspend}####
#------------------------------------------------