public TransactionOutputChanges(InputStream in) throws IOException { int numOutsCreated = (in.read() & 0xFF) | ((in.read() & 0xFF) << 8) | ((in.read() & 0xFF) << 16) | ((in.read() & 0xFF) << 24); txOutsCreated = new LinkedList<>(); for (int i = 0; i < numOutsCreated; i++) txOutsCreated.add(new UTXO(in)); int numOutsSpent = (in.read() & 0xFF) | ((in.read() & 0xFF) << 8) | ((in.read() & 0xFF) << 16) | ((in.read() & 0xFF) << 24); txOutsSpent = new LinkedList<>(); for (int i = 0; i < numOutsSpent; i++) txOutsSpent.add(new UTXO(in)); }
/** * Construct a free standing Transaction Output. * @param params The network parameters. * @param output The stored output (free standing). */ public FreeStandingTransactionOutput(NetworkParameters params, UTXO output, int chainHeight) { super(params, null, output.getValue(), output.getScript().getProgram()); this.output = output; this.chainHeight = chainHeight; }
@Override public void addUnspentTransactionOutput(UTXO out) throws BlockStoreException { maybeConnect(); PreparedStatement s = null; try { s = conn.get().prepareStatement(getInsertOpenoutputsSQL()); s.setBytes(1, out.getHash().getBytes()); // index is actually an unsigned int s.setInt(2, (int) out.getIndex()); s.setInt(3, out.getHeight()); s.setLong(4, out.getValue().value); s.setBytes(5, out.getScript().getProgram()); s.setString(6, out.getAddress()); s.setInt(7, out.getScript().getScriptType().ordinal()); s.setBoolean(8, out.isCoinbase()); s.executeUpdate(); s.close(); } catch (SQLException e) { if (!(e.getSQLState().equals(getDuplicateKeyErrorCode()))) throw new BlockStoreException(e); } finally { if (s != null) { try { s.close(); } catch (SQLException e) { throw new BlockStoreException(e); } } } }
beginMethod("removeUnspentTransactionOutput"); byte[] key = getTxKey(KeyType.OPENOUT_ALL, out.getHash(), (int) out.getIndex()); byte[] hashBytes = null; try { String address = out.getAddress(); if (address == null || address.equals("")) { Script sc = out.getScript(); a = sc.getToAddress(params); hashBytes = a.getHash160(); } else { a = Address.fromBase58(params, out.getAddress()); hashBytes = a.getHash160(); bb.put(out.getHash().getBytes()); bb.putInt((int) out.getIndex()); batchDelete(bb.array());
@Test public void testJavaSerialization() throws Exception { ECKey key = new ECKey(); UTXO utxo = new UTXO(Sha256Hash.of(new byte[]{1,2,3}), 1, Coin.COIN, 10, true, ScriptBuilder.createOutputScript(key)); ByteArrayOutputStream os = new ByteArrayOutputStream(); new ObjectOutputStream(os).writeObject(utxo); UTXO utxoCopy = (UTXO) new ObjectInputStream( new ByteArrayInputStream(os.toByteArray())).readObject(); assertEquals(utxo, utxoCopy); assertEquals(utxo.getValue(), utxoCopy.getValue()); assertEquals(utxo.getHeight(), utxoCopy.getHeight()); assertEquals(utxo.isCoinbase(), utxoCopy.isCoinbase()); assertEquals(utxo.getScript(), utxoCopy.getScript()); } }
bloom.add(out.getHash()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { out.serializeToStream(bos); } catch (IOException e) { throw new BlockStoreException("problem serialising utxo", e); byte[] key = getTxKey(KeyType.OPENOUT_ALL, out.getHash(), (int) out.getIndex()); batchPut(key, bos.toByteArray()); if (out.getAddress() == null || out.getAddress().equals("")) { if (instrument) endMethod("addUnspentTransactionOutput"); } else { try { a = Address.fromBase58(params, out.getAddress()); } catch (AddressFormatException e) { if (instrument) bb.put((byte) KeyType.ADDRESS_HASHINDEX.ordinal()); bb.put(a.getHash160()); bb.put(out.getHash().getBytes()); bb.putInt((int) out.getIndex()); byte[] value = new byte[0]; batchPut(bb.array(), value);
int chainHeight = utxoProvider.getChainHeadHeight(); for (UTXO output : getStoredOutputsFromUTXOProvider()) { boolean coinbase = output.isCoinbase(); int depth = chainHeight - output.getHeight() + 1; // the current depth of the output (1 = same as head).
assertEquals("Wrong Number of Outputs", 1, outputs.size()); UTXO output = outputs.get(0); assertEquals("The address is not equal", address.toString(), output.getAddress()); assertEquals("The amount is not equal", totalAmount, output.getValue());
Wallet w = new Wallet(new Context(params)); Address a = w.currentReceiveAddress(); final UTXO utxo = new UTXO(Sha256Hash.of(new byte[] { 1, 2, 3 }), 1, Coin.COIN, 0, false, ScriptBuilder.createOutputScript(a)); w.setUTXOProvider(new UTXOProvider() { assertEquals(utxo.getHash(), txInToDisconnect.getOutpoint().connectedOutput.getParentTransactionHash());
@Override public Sha256Hash getParentTransactionHash() { return output.getHash(); } }
@Override public List<UTXO> getOpenTransactionOutputs(List<Address> addresses) throws UTXOProviderException { // This is *NOT* optimal: We go through all the outputs and select the ones we are looking for. // If someone uses this store for production then they have a lot more to worry about than an inefficient impl :) List<UTXO> foundOutputs = new ArrayList<>(); List<UTXO> outputsList = transactionOutputMap.values(); for (UTXO output : outputsList) { for (Address address : addresses) { if (output.getAddress().equals(address.toString())) { foundOutputs.add(output); } } } return foundOutputs; } }
/** * Get the depth withing the chain of the parent tx, depth is 1 if it the output height is the height of * the latest block. * @return The depth. */ @Override public int getParentTransactionDepthInBlocks() { return chainHeight - output.getHeight() + 1; }
@Override public int getIndex() { return (int) output.getIndex(); }
private void writeObject(ObjectOutputStream o) throws IOException { serializeToStream(o); }
@Override public void addUnspentTransactionOutput(UTXO out) throws BlockStoreException { maybeConnect(); PreparedStatement s = null; try { s = conn.get().prepareStatement(getInsertOpenoutputsSQL()); s.setBytes(1, out.getHash().getBytes()); // index is actually an unsigned int s.setInt(2, (int) out.getIndex()); s.setInt(3, out.getHeight()); s.setLong(4, out.getValue().value); s.setBytes(5, out.getScript().getProgram()); s.setString(6, out.getAddress()); s.setInt(7, out.getScript().getScriptType().ordinal()); s.setBoolean(8, out.isCoinbase()); s.executeUpdate(); s.close(); } catch (SQLException e) { if (!(e.getSQLState().equals(getDuplicateKeyErrorCode()))) throw new BlockStoreException(e); } finally { if (s != null) { try { s.close(); } catch (SQLException e) { throw new BlockStoreException(e); } } } }
beginMethod("removeUnspentTransactionOutput"); byte[] key = getTxKey(KeyType.OPENOUT_ALL, out.getHash(), (int) out.getIndex()); byte[] hashBytes = null; try { String address = out.getAddress(); if (address == null || address.equals("")) { Script sc = out.getScript(); a = sc.getToAddress(params); hashBytes = a.getHash160(); } else { a = Address.fromBase58(params, out.getAddress()); hashBytes = a.getHash160(); bb.put(out.getHash().getBytes()); bb.putInt((int) out.getIndex()); batchDelete(bb.array());
if (prevOut.isCoinbase()) { if (height - prevOut.getHeight() < params.getSpendableCoinbaseDepth()) { throw new VerificationException("Tried to spend coinbase at depth " + (height - prevOut.getHeight())); valueIn = valueIn.add(prevOut.getValue()); if (verifyFlags.contains(VerifyFlag.P2SH)) { if (prevOut.getScript().isPayToScriptHash()) sigOps += Script.getP2SHSigOpCount(in.getScriptBytes()); if (sigOps > Block.MAX_BLOCK_SIGOPS) prevOutScripts.add(prevOut.getScript()); blockStore.removeUnspentTransactionOutput(prevOut); txOutsSpent.add(prevOut); UTXO newOut = new UTXO(hash, out.getIndex(), out.getValue(),