def add_block(self, transactions, fee=0): # change add "fee" 6.19 last_block = self.get_last_block() prev_hash = last_block.get_header_hash() height = last_block.block_header.height + 1 block_header = BlockHeader('', height, prev_hash) # reward to wallets[0] wallets = Wallets() keys = list(wallets.wallets.keys()) w = wallets[keys[0]] coin_base_tx = self.coin_base_tx(w.address, fee) # change 6.19 transactions.insert(0, coin_base_tx) utxo_set = UTXOSet() # txs = transactions # change 6.21 txs = utxo_set.clear_transactions( transactions) # change clear transactions(add sort) block = Block(block_header, txs) block.mine(self) block.set_header_hash() self.db.create(block.block_header.hash, block.serialize()) last_hash = block.block_header.hash self.set_last_hash(last_hash) utxo_set.update(block)
def add_block(self, transactions=[], fee=0): last_block = None while not last_block: last_block = self.get_last_block() time.sleep(5) prev_hash = last_block.get_header_hash() height = last_block.block_header.height + 1 block_header = BlockHeader('', height, prev_hash) f = open('wallet.dat', 'rb') wallets = pickle.load(f) keys = list(wallets.keys()) w = wallets[keys[0]] coin_base_tx = self.coin_base_tx(w.address, fee) if transactions: transactions.insert(0, coin_base_tx) else: transactions = [coin_base_tx] utxo_set = UTXOSet() txs = utxo_set.clear_transactions(transactions) block = Block(block_header, txs) block.mine(self) blo = None while not blo: blo = self.get_last_block() time.sleep(5) txs1 = blo._transactions # if len(transactions) > 1: # if txs1: # if len(txs1) > 1: # if txs1[1].txid == txs[1].txid: # return block.set_header_hash() self.db.create(block.block_header.hash, block.serialize()) last_hash = block.block_header.hash self.set_last_hash(last_hash) utxo_set.update(block)
def main(): parser = new_parser() args = parser.parse_args() s = ServerProxy("http://localhost:9999") if hasattr(args, 'height'): block_data = s.print_chain(args.height) print(block_data) if hasattr(args, 'address'): balance = s.get_balance(args.address) print("%s balance is %d" % (args.address, balance)) if hasattr(args, 'createwallet'): address = s.create_wallet() print('Wallet address is %s' % address) if hasattr(args, 'start'): start() if hasattr(args, 'printwallet'): wallets = s.print_all_wallet() print('Wallet are:') for wallet in wallets: print("\t%s" % wallet) if hasattr(args, 'genesis_block'): address = s.create_genesis_block() print('Genesis Wallet is: %s' % address) if hasattr(args, 'send_from') \ and hasattr(args, 'send_to') \ and hasattr(args, 'send_amount'): s.send(args.send_from, args.send_to, args.send_amount) # change if hasattr(args, 'start_mining'): # change print("start mining...") # change s.start_find() # change print("after start_find") # 7.10 if hasattr(args, 'print_txpool'): # change txs = s.print_txpool() # change print(type(txs[0])) # dict i = 0 for tx in txs: # change i += 1 print("transaction: ", tx) # change print("") print(i) if hasattr(args, 'sort_txpool'): txs6, no = s.sort_txpool() # for i in range(len(txs6)): # txs6[i] = Transaction.deserialize(txs6[i]) # print("transaction: ", type(txs6[i])) utxo_set = UTXOSet() txs6 = utxo_set.clear_transactions(txs6) print(txs6) if hasattr(args, 'alive'): chain_doc = [] bc1 = BlockChain() last_blo = bc1.get_last_block() last_height = last_blo.block_header.height j = 0 m_total_payoff = -11 u_total_payoff = 11.33 users = {} for i in range(0, last_height + 1): j += 1 blo = bc1.get_block_by_height(i) if blo: txs = blo._transactions for tx in txs: if tx.ip: u_total_payoff -= tx.amount m_total_payoff += tx.amount if tx.ip in users.keys(): users[tx.ip] += (1.33 - tx.amount - 0.05 * j) else: users[tx.ip] = (1.33 - tx.amount - 0.05 * j) print(blo.serialize()) print("") else: print("problems in the docs") break print(j) print("m_total_payoff ", m_total_payoff) print("u_total_payoff ", u_total_payoff) for key in users: print("the user ", key, "'s pay off is ", users[key])