def mine(): """ Main miner method. """ # Found last block and unchecked transactions. last_block = BlockChainDB().last() if len(last_block) == 0: last_block = coinbase().to_dict() untxdb = UnTransactionDB() # Miner reward rw = reward() untxs = untxdb.find_all() untxs.append(rw.to_dict()) # untxs_dict = [untx.to_dict() for untx in untxs] untx_hashes = untxdb.all_hashes() # Clear the untransaction database. untxdb.clear() # Miner reward is the first transaction. untx_hashes.insert(0, rw.hash) cb = Block(last_block['index'] + 1, int(time.time()), untx_hashes, last_block['hash']) nouce = cb.pow() cb.make(nouce) # Save block and transactions to database. BlockChainDB().insert(cb.to_dict()) TransactionDB().insert(untxs) # Broadcast to other nodes Block.spread(cb.to_dict()) Transaction.blocked_spread(untxs) return cb
def transfer(cls, from_addr, to_addr, amount): if not isinstance(amount, int): amount = int(amount) unspents = Vout.get_unspent(from_addr) ready_utxo, change = select_outputs_greedy(unspents, amount) print('ready_utxo', ready_utxo[0].to_dict()) vin = ready_utxo vout = [] vout.append(Vout(to_addr, amount)) vout.append(Vout(from_addr, change)) tx = cls(vin, vout) tx_dict = tx.to_dict() UnTransactionDB().insert(tx_dict) return tx_dict
def getUntransactions(): utxdb= UnTransactionDB() utxs= utxdb.read() utxs= [ fromDict(utx) for utx in utxs] return utxs
def createTransaction(sender, recipientID, value): tx= Transaction(sender, recipientID, value) tx_dict = tx.to_dict() UnTransactionDB().insert(tx_dict) return tx
def createTransaction(organization, recipientID, recCode, date, period): tx = Transaction(organization, recipientID, recCode, date, period) tx_dict = tx.to_dict() UnTransactionDB().insert(tx_dict) return tx
def get_all_untransactions(): UnTransactionDB().all_hashes()
def new_untransaction(self,untx): cprint(__name__,untx) UnTransactionDB().insert(untx) cprint('INFO',"Receive new unchecked transaction.") return True
def new_block(self,block): cprint(__name__, block) BlockChainDB().insert(block) UnTransactionDB().clear() cprint('INFO',"Receive new block.") return True