mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-11-03 14:07:14 +00:00
Make UTXO serializable
This commit is contained in:
committed by
Andreas Schildbach
parent
cbe57555b2
commit
0566917a8f
@@ -30,7 +30,9 @@ import java.util.Locale;
|
||||
* It avoids having to store the entire parentTransaction just to get the hash and index.
|
||||
* Useful when working with free standing outputs.
|
||||
*/
|
||||
public class UTXO {
|
||||
public class UTXO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 4736241649298988166L;
|
||||
|
||||
private Coin value;
|
||||
private Script script;
|
||||
@@ -86,38 +88,7 @@ public class UTXO {
|
||||
}
|
||||
|
||||
public UTXO(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));
|
||||
|
||||
int scriptBytesLength = ((in.read() & 0xFF)) |
|
||||
((in.read() & 0xFF) << 8) |
|
||||
((in.read() & 0xFF) << 16) |
|
||||
((in.read() & 0xFF) << 24);
|
||||
byte[] scriptBytes = new byte[scriptBytesLength];
|
||||
if (in.read(scriptBytes) != scriptBytesLength)
|
||||
throw new EOFException();
|
||||
script = new Script(scriptBytes);
|
||||
|
||||
byte[] hashBytes = new byte[32];
|
||||
if (in.read(hashBytes) != 32)
|
||||
throw new EOFException();
|
||||
hash = Sha256Hash.wrap(hashBytes);
|
||||
|
||||
byte[] indexBytes = new byte[4];
|
||||
if (in.read(indexBytes) != 4)
|
||||
throw new EOFException();
|
||||
index = Utils.readUint32(indexBytes, 0);
|
||||
|
||||
height = ((in.read() & 0xFF)) |
|
||||
((in.read() & 0xFF) << 8) |
|
||||
((in.read() & 0xFF) << 16) |
|
||||
((in.read() & 0xFF) << 24);
|
||||
|
||||
byte[] coinbaseByte = new byte[1];
|
||||
in.read(coinbaseByte);
|
||||
coinbase = coinbaseByte[0] == 1;
|
||||
deserializeFromStream(in);
|
||||
}
|
||||
|
||||
/** The value which this Transaction output holds. */
|
||||
@@ -193,4 +164,48 @@ public class UTXO {
|
||||
|
||||
bos.write(new byte[] { (byte)(coinbase ? 1 : 0) });
|
||||
}
|
||||
|
||||
public void deserializeFromStream(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));
|
||||
|
||||
int scriptBytesLength = ((in.read() & 0xFF)) |
|
||||
((in.read() & 0xFF) << 8) |
|
||||
((in.read() & 0xFF) << 16) |
|
||||
((in.read() & 0xFF) << 24);
|
||||
byte[] scriptBytes = new byte[scriptBytesLength];
|
||||
if (in.read(scriptBytes) != scriptBytesLength)
|
||||
throw new EOFException();
|
||||
script = new Script(scriptBytes);
|
||||
|
||||
byte[] hashBytes = new byte[32];
|
||||
if (in.read(hashBytes) != 32)
|
||||
throw new EOFException();
|
||||
hash = Sha256Hash.wrap(hashBytes);
|
||||
|
||||
byte[] indexBytes = new byte[4];
|
||||
if (in.read(indexBytes) != 4)
|
||||
throw new EOFException();
|
||||
index = Utils.readUint32(indexBytes, 0);
|
||||
|
||||
height = ((in.read() & 0xFF)) |
|
||||
((in.read() & 0xFF) << 8) |
|
||||
((in.read() & 0xFF) << 16) |
|
||||
((in.read() & 0xFF) << 24);
|
||||
|
||||
byte[] coinbaseByte = new byte[1];
|
||||
in.read(coinbaseByte);
|
||||
coinbase = coinbaseByte[0] == 1;
|
||||
}
|
||||
|
||||
|
||||
private void writeObject(ObjectOutputStream o) throws IOException {
|
||||
serializeToStream(o);
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream o) throws IOException, ClassNotFoundException {
|
||||
deserializeFromStream(o);
|
||||
}
|
||||
}
|
||||
|
||||
46
core/src/test/java/org/bitcoinj/core/UTXOTest.java
Normal file
46
core/src/test/java/org/bitcoinj/core/UTXOTest.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2011 Google Inc.
|
||||
* Copyright 2014 Andreas Schildbach
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.bitcoinj.core;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
import org.bitcoinj.script.ScriptBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UTXOTest {
|
||||
|
||||
@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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user