Skip to content

Commit

Permalink
UTXO: Migrate constructor that takes a stream to a static constructor…
Browse files Browse the repository at this point in the history
… fromStream().
  • Loading branch information
Andreas Schildbach authored and ripcurlx committed Aug 23, 2021
1 parent 83ad23b commit 354b914
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public TransactionOutputChanges(InputStream in) throws IOException {
int numOutsCreated = (int) Utils.readUint32FromStream(in);
txOutsCreated = new LinkedList<>();
for (int i = 0; i < numOutsCreated; i++)
txOutsCreated.add(new UTXO(in));
txOutsCreated.add(UTXO.fromStream(in));

int numOutsSpent = (int) Utils.readUint32FromStream(in);
txOutsSpent = new LinkedList<>();
for (int i = 0; i < numOutsSpent; i++)
txOutsSpent.add(new UTXO(in));
txOutsSpent.add(UTXO.fromStream(in));
}

public void serializeToStream(OutputStream bos) throws IOException {
Expand Down
20 changes: 9 additions & 11 deletions core/src/main/java/org/bitcoinj/core/UTXO.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ public UTXO(Sha256Hash hash,
this.address = address;
}

public UTXO(InputStream in) throws IOException {
deserializeFromStream(in);
}

/** The value which this Transaction output holds. */
public Coin getValue() {
return value;
Expand Down Expand Up @@ -153,32 +149,34 @@ public void serializeToStream(OutputStream bos) throws IOException {
bos.write(new byte[] { (byte)(coinbase ? 1 : 0) });
}

public void deserializeFromStream(InputStream in) throws IOException {
public static UTXO fromStream(InputStream in) throws IOException {
byte[] valueBytes = new byte[8];
if (in.read(valueBytes, 0, 8) != 8)
throw new EOFException();
value = Coin.valueOf(Utils.readInt64(valueBytes, 0));
Coin value = Coin.valueOf(Utils.readInt64(valueBytes, 0));

int scriptBytesLength = (int) Utils.readUint32FromStream(in);
byte[] scriptBytes = new byte[scriptBytesLength];
if (in.read(scriptBytes) != scriptBytesLength)
throw new EOFException();
script = new Script(scriptBytes);
Script script = new Script(scriptBytes);

byte[] hashBytes = new byte[32];
if (in.read(hashBytes) != 32)
throw new EOFException();
hash = Sha256Hash.wrap(hashBytes);
Sha256Hash hash = Sha256Hash.wrap(hashBytes);

byte[] indexBytes = new byte[4];
if (in.read(indexBytes) != 4)
throw new EOFException();
index = Utils.readUint32(indexBytes, 0);
long index = Utils.readUint32(indexBytes, 0);

height = (int) Utils.readUint32FromStream(in);
int height = (int) Utils.readUint32FromStream(in);

byte[] coinbaseByte = new byte[1];
in.read(coinbaseByte);
coinbase = coinbaseByte[0] == 1;
boolean coinbase = coinbaseByte[0] == 1;

return new UTXO(hash, index, value, height, coinbase, script);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ public UTXO getTransactionOutput(Sha256Hash hash, long index) throws BlockStoreE
return null;
}
ByteArrayInputStream bis = new ByteArrayInputStream(inbytes);
UTXO txout = new UTXO(bis);
UTXO txout = UTXO.fromStream(bis);

if (instrument)
endMethod("getTransactionOutput");
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/org/bitcoinj/core/UTXOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void testSerialization() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
utxo.serializeToStream(os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
UTXO utxoCopy = new UTXO(is);
UTXO utxoCopy = UTXO.fromStream(is);
assertEquals(utxo, utxoCopy);
assertEquals(utxo.getValue(), utxoCopy.getValue());
assertEquals(utxo.getHeight(), utxoCopy.getHeight());
Expand Down

0 comments on commit 354b914

Please sign in to comment.