def confirm_vault_txs(self): # Add confirmed vault transactions to mempool # FIXME: what if its not a wholesome transction ... # if it contains other normal or fast txs? which might # inject dependency into middle blocks? # Lets assume that vaulttx has only one output (vault) and fees # Else create two transactions ... in the beginning # txs = self.get_confirmed_vault_txs() # do not modify anything ... this stuff will be used later # split the fees # check if its not already withdrawn or confirmed # get unconfirmed vault transactions txhashes = self.vaultdb.getconfirmedvaulttxs() # TODO: check to confirm that these transactions are not overwritten # confirm vault transactions for txhash in txhashes: tx = self.gettx(txhash) for txin in tx.vin: if txin.scriptSig and (txin.scriptSig[0] == chr(OP_VAULT_WITHDRAW)): newtx = core.CTransaction() newtx.copy(tx) # TODO: assuming only one input newtx.vin[0].scriptSig = chr(OP_VAULT_CONFIRM) + \ txin.scriptSig[1:] newtx.calc_sha256() if not self.mempool.add(newtx): self.logger.error( "tx is already in mempool{0}".format(tx)) return
def create_new_contract(btcamount, address): """Creates a new bitcoin transaction contract for btcamount of satoshis, sending them to address. This transaction will have zero inputs and a single output and is thus invalid: people that want to pledge bitcoins to this contract will make inputs. Once we get enough inputs then the transaction can be sent to the bitcoin network, sending the bitcoins to the specified address.""" txout = core.CTxOut() txout.nValue = btcamount #This creats a standard send-to-address transaction #We need to remove the first byte, which is the version keyhash = btcaddress.base58checksum_decode(address)[1:] txout.scriptPubKey = chr(script.OP_DUP) + \ chr(script.OP_HASH160) + \ chr(len(keyhash)) + \ keyhash + \ chr(script.OP_EQUALVERIFY) + \ chr(script.OP_CHECKSIG) tx = core.CTransaction() tx.vout = [txout] print "Contract tx:" print "Amount: ", btcamount / 100000000 print "To: ", address print tx.serialize().encode("hex")
def removeconfirmedvaulttxs(self, txs): if not txs: return # remove confirmed transactions from VaultDB self.logger.debug('Removing confirmed transactions from VaultDB') connection = sqlite.connect(self.vaultfile) cursor = connection.cursor() cmd_confirmed = "DELETE FROM vaults WHERE txhash = (?)" cmd_override = "DELETE FROM vaults WHERE fromaddress = (?)" for tx in txs: if tx.is_coinbase(): continue for txin in tx.vin: if txin.scriptSig[0] == chr(script.OP_VAULT_CONFIRM): newtx = core.CTransaction() newtx.copy(tx) # TODO: assuming only one input newtx.vin[0].scriptSig = chr(script.OP_VAULT_WITHDRAW) + \ txin.scriptSig[1:] newtx.calc_sha256() values = (str(newtx.sha256), ) cursor.execute(cmd_confirmed, values) elif txin.scriptSig[0] == chr(script.OP_VAULT_OVERRIDE): fromaddress = str( utils.scriptSig_to_vault_address(txin.scriptSig)) values = (fromaddress, ) cursor.execute(cmd_override, values) connection.commit() connection.close() self.logger.debug('Removed confirmed transactions from VaultDB')
def listpendingtxhashes(self): txhashes = [] end_height = self.getheight() for height in range(end_height + 1): data = self.db.Get('height:' + str(height)) heightidx = HeightIdx() heightidx.deserialize(data) blkhash = heightidx.blocks[0] block = self.getblock(blkhash) for tx in block.vtx: # if its a coinbase transaction, skip if tx.is_coinbase(): continue if not utils.is_vault_tx(tx): continue txin = tx.vin[0] """ # if not a input from vault, skip if ord(txin.scriptSig[0]) not in [OP_VAULT_WITHDRAW, OP_VAULT_CONFIRM, OP_VAULT_OVERRIDE]: continue """ # add a transaction is received through withdraw if txin.scriptSig[0] == chr(OP_VAULT_WITHDRAW): tx.calc_sha256() self.logger.debug("Vault withdraw initiated: %d %064x" \ % (height, tx.sha256)) txhashes.append(tx.sha256) # remove if a transaction is spent through vault confirm or override elif txin.scriptSig[0] == chr(OP_VAULT_CONFIRM): newtx = core.CTransaction() newtx.copy(tx) newtx.vin[0].scriptSig = chr(OP_VAULT_WITHDRAW) + \ txin.scriptSig[1:] newtx.calc_sha256() txhashes.remove(newtx.sha256) elif txin.scriptSig[0] == chr(OP_VAULT_OVERRIDE): self.logger.debug("Vault confirmed %064x" % txin.prevout.hash) #TODO(obulpathi): optimize for txhash in txhashes: tmp_tx = self.gettx(txhash) if tmp_tx.vin[0].prevout.hash == txin.prevout.hash: tmp_tx.calc_sha256() txhashes.remove(tmp_tx.sha256) return txhashes
def decodetx(tx): rawtx = StringIO.StringIO(tx.decode("hex")) tx = core.CTransaction() tx.deserialize(rawtx) return tx