The blockchain module¶
-
class
blockchain.
BlockChain
(blocks=None, txindex=None, reward=10, difficulty=3)¶ - A Block chain is:
- blocks: a list of blocks
- txindex: a dict of transactions (key = hash)
- reward: number of coins allocated per verified block
- difficulty: the current difficulty
-
add
(bl, ignoredifficulty=False)¶ - add block bl to the blockchain and perform some basic checks:
- does the difficulty match the current difficulty? (if ignoredifficulty, this is not checked for, use this only when loading blocks from a file)
- is nonce OK?
- is hash of previous block correct?
- is coinbase transaction well formed and does it create the right amount?
- does check_tx work for every transaction?
returns success,msg (if success is True then the block was added)
-
any_spent_txin
(tx)¶ True if any of the inputs of a tx has been spent
-
check_tx
(tx, stx)¶ - Performs different checks to transaction tx and its signature stx
- is the signature of a transaction ok?
- is there any double spending in the transactions?
- is there enough funds for the transactions?
- Returns True, message if success
- False,message if the transaction has a problem
-
check_tx_funds
(tx)¶ check if transaction input and output funds match
-
check_txin
(tx)¶ - check that:
- all inputs reference same public key
- the public keys in all inputs match the public keys in the output in the previous transaction
-
count_funds
(pubkey)¶ return all coins that have not been spent by pubkey
-
find_tx
(h)¶ find a transaction by hash h
-
find_tx_block
(h)¶ find the block where transaction with hash h appears
-
funds
(txin)¶ find funds in txin
-
is_spent
(txin)¶ True if txin is already spent in any previous transactions
-
spent_txout
(tx)¶ a list of same size of the list of ouputs which has the hash of the transaction if spent otherwise empty string
-
tx_reindex
()¶ Rebuilds transaction index from list of blocks
-
wallet
(pubkey)¶ return all coins that have not been spent by a public key, as a dictionary with keys transaction hash, output number and value being the amount of coins
-
where_spent
(txin)¶ return transaction where txin shows as spent
-
blockchain.
alltxins
(txlist)¶ returns a dictionary of all
coins.TxIn
in the list txlist ofcoins.Transaction
. The dictionary key is the tuple (pubkey.N,pubkey.e,hash_prev,output_num) and the value is the number of times a particular txin appears as input in txlist.
-
blockchain.
tx_check_sig
(tx, sig)¶ True if transaction has been signed correctly
-
blockchain.
tx_sig
(tx, privkey)¶ Sign a transaction
Note: all the input transactions are assumed to be from the same public key, so we just sign using one private key which is assumed to be that of the only owner of all inputs In bitcoin transactions, there is a “script” describing which keys to use to verify that the transaction is legit