コード例 #1
0
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
コード例 #2
0
 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
コード例 #3
0
def getUntransactions():
    utxdb= UnTransactionDB()
    utxs= utxdb.read()
    utxs= [ fromDict(utx) for utx in utxs]
    return utxs
コード例 #4
0
def createTransaction(sender, recipientID, value):
    tx= Transaction(sender, recipientID, value)
    tx_dict = tx.to_dict()
    UnTransactionDB().insert(tx_dict)
    return tx
コード例 #5
0
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
コード例 #6
0
def get_all_untransactions():
    UnTransactionDB().all_hashes()
コード例 #7
0
 def new_untransaction(self,untx):
     cprint(__name__,untx)
     UnTransactionDB().insert(untx)
     cprint('INFO',"Receive new unchecked transaction.")
     return True
コード例 #8
0
 def new_block(self,block):
     cprint(__name__, block)
     BlockChainDB().insert(block)
     UnTransactionDB().clear()
     cprint('INFO',"Receive new block.")
     return True