forked from zack-bitcoin/basiccoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.py
44 lines (38 loc) · 1.6 KB
/
transactions.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
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 spend_verify(tx, txs, DB):
tx_copy=copy.copy(tx)
tx_copy.pop('signature')
msg=tools.det_hash(tx_copy)
if not tools.verify(msg, tx['signature'], tx['id']): return False
if tx['amount']<custom.fee: return False
if int(blockchain.db_get(tx['id'], DB)['amount'])<int(tx['amount']):
return False
return True
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):
adjust('amount', tx['id'], custom.block_reward, DB)
adjust('count', tx['id'], 1, DB)
def spend(tx, DB):
adjust('amount', tx['id'], -tx['amount'], DB)
adjust('amount', tx['to'], tx['amount']-custom.fee, DB)
adjust('count', tx['id'], 1, DB)
add_block={'mint':mint, 'spend':spend}####
#-----------------------------------------
def unmint(tx, DB):
adjust('amount', tx['id'], -custom.block_reward, DB)
adjust('count', tx['id'], -1, DB)
def unspend(tx, DB):
adjust('amount', tx['id'], tx['amount'], DB)
adjust('amount', tx['to'], custom.fee-tx['amount'], DB)
adjust('count', tx['id'], -1, DB)
delete_block={'mint':unmint, 'spend':unspend}####
#------------------------------------------------