Exemplo n.º 1
0
    def addBlock(self, blk):
        if self.blocks_db.exists(blk.hash):
            return

        encoded_block = util.encodeMsg(block.encodeBlock(blk))
        self.blocks_db.put(blk.hash, encoded_block)
        self.setTip(blk)
Exemplo n.º 2
0
    def update(self, block):
        with self.utxos_db.snapshot() as s, self.utxo_db.write_batch() as wb:
            for tx in block.transactions:
                # coinbase txs don't have inputs, duh
                if not tx.isCoinbase():
                    # for each input, remove the output that it spends from the UTXO set
                    for txInput in tx.vin:
                        txOutputs = util.decodeMsg(s.get(txInput.txId))
                        updatedOutputs = [txOutput for txOutput in txOutputs if txOutput[b'idx'] != txInput.outIdx]

                        if len(updatedOutputs) == 0:
                            wb.delete(txInput.txId)
                        else:
                            encodedOutputs = util.encodeMsg(updatedOutputs)
                            wb.put(txInput.txId, encodedOutputs)

                # add new outputs to the UTXO set
                encodedNewOutputs = util.encodeMsg(list(tx.outDict.values()), encoder=txout.encodeTXOutput)
                wb.put(tx.id, encodedNewOutputs)
Exemplo n.º 3
0
 def reindex(self):
     # empty the current indexed UTXOs
     self.utxos_db.clear()
     # get the new UTXOs
     UTXO = bx.BlockExplorer().findUTXO()
     with self.utxos_db.write_batch() as wb:
         for txId, txOutputs in UTXO.items():
             # add them to the cache
             txOutputs = list(txOutputs)
             encodedTXOutputs = util.encodeMsg(txOutputs, encoder=txout.encodeTXOutput)
             wb.put(txId, encodedTXOutputs)
Exemplo n.º 4
0
def sendPayload(address, command, payload):
    command = p2p.formatCommand(command)
    payload = command + encodeMsg(payload)
    payload = pack('>I', len(payload)) + payload
    port = 7667
    if ':' in address:
        [address, port] = address.split(':')
        port = int(port)
    sock = p2p.SocketWriter(address, port)
    if sock.isConnected():
        sock.send(payload)
Exemplo n.º 5
0
 def create_wallet(self):
     w = wallet.Wallet()
     address = w.get_address()
     encoded_wallet = util.encodeMsg(wallet.encodeWallet(w))
     self.wallets_db.put(address, encoded_wallet)
     return w