3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-13 10:45:51 +00:00

In UTXO property scriptBytes changed to class Script.

Property addressType deleted.
FullPrunedBlockChain code re-formatted, changed UTXO class usage.
This commit is contained in:
Loco 2015-04-29 13:16:51 +03:00 committed by Mike Hearn
parent 4e8f1bb153
commit bee1873e1a
4 changed files with 83 additions and 103 deletions

View File

@ -47,7 +47,9 @@ import static com.google.common.base.Preconditions.checkState;
public class FullPrunedBlockChain extends AbstractBlockChain {
private static final Logger log = LoggerFactory.getLogger(FullPrunedBlockChain.class);
/** Keeps a map of block hashes to StoredBlocks. */
/**
* Keeps a map of block hashes to StoredBlocks.
*/
protected final FullPrunedBlockStore blockStore;
// Whether or not to execute scriptPubKeys before accepting a transaction (i.e. check signatures).
@ -70,12 +72,16 @@ public class FullPrunedBlockChain extends AbstractBlockChain {
this(Context.getOrCreate(params), wallet, blockStore);
}
/** Constructs a block chain connected to the given store. */
/**
* Constructs a block chain connected to the given store.
*/
public FullPrunedBlockChain(Context context, FullPrunedBlockStore blockStore) throws BlockStoreException {
this(context, new ArrayList<BlockChainListener>(), blockStore);
}
/** See {@link #FullPrunedBlockChain(Context, Wallet, FullPrunedBlockStore)} */
/**
* See {@link #FullPrunedBlockChain(Context, Wallet, FullPrunedBlockStore)}
*/
public FullPrunedBlockChain(NetworkParameters params, FullPrunedBlockStore blockStore) throws BlockStoreException {
this(Context.getOrCreate(params), blockStore);
}
@ -90,7 +96,9 @@ public class FullPrunedBlockChain extends AbstractBlockChain {
this.chainHead = blockStore.getVerifiedChainHead();
}
/** See {@link #FullPrunedBlockChain(Context, List, FullPrunedBlockStore)} */
/**
* See {@link #FullPrunedBlockChain(Context, List, FullPrunedBlockStore)}
*/
public FullPrunedBlockChain(NetworkParameters params, List<BlockChainListener> listeners,
FullPrunedBlockStore blockStore) throws BlockStoreException {
this(Context.getOrCreate(params), listeners, blockStore);
@ -139,14 +147,18 @@ public class FullPrunedBlockChain extends AbstractBlockChain {
ExecutorService scriptVerificationExecutor = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors(), new ContextPropagatingThreadFactory("Script verification"));
/** A job submitted to the executor which verifies signatures. */
/**
* A job submitted to the executor which verifies signatures.
*/
private static class Verifier implements Callable<VerificationException> {
final Transaction tx;
final List<Script> prevOutScripts;
final Set<VerifyFlag> verifyFlags;
public Verifier(final Transaction tx, final List<Script> prevOutScripts, final Set<VerifyFlag> verifyFlags) {
this.tx = tx; this.prevOutScripts = prevOutScripts; this.verifyFlags = verifyFlags;
this.tx = tx;
this.prevOutScripts = prevOutScripts;
this.verifyFlags = verifyFlags;
}
@Nullable
@ -164,18 +176,20 @@ public class FullPrunedBlockChain extends AbstractBlockChain {
}
}
/** Get the {@link Script} from the script bytes or null if it doesn't parse. */
@Nullable
/**
* Get the {@link Script} from the script bytes or null if it doesn't parse.
*/
private Script getScript(byte[] scriptBytes) {
try {
return new Script(scriptBytes);
} catch (Exception e) {
return null;
return new Script(new byte[0]);
}
}
/**
* Get the address from the {@link Script} if it exists otherwise return empty string "".
*
* @param script The script.
* @return The address.
*/
@ -190,18 +204,6 @@ public class FullPrunedBlockChain extends AbstractBlockChain {
return address;
}
/**
* Get the {@link Script.ScriptType} of this script.
* @param script The script.
* @return The script type.
*/
private Script.ScriptType getScriptType(@Nullable Script script) {
if (script != null) {
return script.getScriptType();
}
return Script.ScriptType.NO_TYPE;
}
@Override
protected TransactionOutputChanges connectTransactions(int height, Block block)
throws VerificationException, BlockStoreException {
@ -266,16 +268,13 @@ public class FullPrunedBlockChain extends AbstractBlockChain {
// TODO: Check we're not spending the genesis transaction here. Satoshis code won't allow it.
valueIn = valueIn.add(prevOut.getValue());
if (verifyFlags.contains(VerifyFlag.P2SH)) {
if (new Script(prevOut.getScriptBytes()).isPayToScriptHash())
if (prevOut.getScript().isPayToScriptHash())
sigOps += Script.getP2SHSigOpCount(in.getScriptBytes());
if (sigOps > Block.MAX_BLOCK_SIGOPS)
throw new VerificationException("Too many P2SH SigOps in block");
}
prevOutScripts.add(new Script(prevOut.getScriptBytes()));
//in.getScriptSig().correctlySpends(tx, index, new Script(params, prevOut.getScriptBytes(), 0, prevOut.getScriptBytes().length));
prevOutScripts.add(prevOut.getScript());
blockStore.removeUnspentTransactionOutput(prevOut);
txOutsSpent.add(prevOut);
}
@ -289,9 +288,8 @@ public class FullPrunedBlockChain extends AbstractBlockChain {
out.getIndex(),
out.getValue(),
height, isCoinBase,
out.getScriptBytes(),
getScriptAddress(script),
getScriptType(script).ordinal());
script,
getScriptAddress(script));
blockStore.addUnspentTransactionOutput(newOut);
txOutsCreated.add(newOut);
}
@ -397,14 +395,13 @@ public class FullPrunedBlockChain extends AbstractBlockChain {
throw new VerificationException("Tried to spend coinbase at depth " + (newBlock.getHeight() - prevOut.getHeight()));
valueIn = valueIn.add(prevOut.getValue());
if (verifyFlags.contains(VerifyFlag.P2SH)) {
Script script = new Script(prevOut.getScriptBytes());
if (script.isPayToScriptHash())
if (prevOut.getScript().isPayToScriptHash())
sigOps += Script.getP2SHSigOpCount(in.getScriptBytes());
if (sigOps > Block.MAX_BLOCK_SIGOPS)
throw new VerificationException("Too many P2SH SigOps in block");
}
prevOutScripts.add(new Script(prevOut.getScriptBytes()));
prevOutScripts.add(prevOut.getScript());
blockStore.removeUnspentTransactionOutput(prevOut);
txOutsSpent.add(prevOut);
@ -419,9 +416,8 @@ public class FullPrunedBlockChain extends AbstractBlockChain {
out.getValue(),
newBlock.getHeight(),
isCoinBase,
out.getScriptBytes(),
getScriptAddress(script),
getScriptType(script).ordinal());
script,
getScriptAddress(script));
blockStore.addUnspentTransactionOutput(newOut);
txOutsCreated.add(newOut);
}

View File

@ -1,12 +1,12 @@
/**
* Copyright 2012 Matt Corallo.
*
* <p/>
* 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
*
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p/>
* 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.
@ -16,6 +16,7 @@
package org.bitcoinj.core;
import org.bitcoinj.script.Script;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -36,7 +37,7 @@ public class UTXO implements Serializable {
* this output.
*/
private Coin value;
private byte[] scriptBytes;
private Script script;
/** Hash of the transaction to which we refer. */
private Sha256Hash hash;
@ -48,8 +49,6 @@ public class UTXO implements Serializable {
private boolean coinbase;
/** The address of this output */
private String address;
/** The type of this address */
private int addressType;
/**
* Creates a stored transaction output.
@ -58,22 +57,20 @@ public class UTXO implements Serializable {
* @param value The value available.
* @param height The height this output was created in.
* @param coinbase The coinbase flag.
* @param scriptBytes The script bytes.
*/
public UTXO(Sha256Hash hash,
long index,
Coin value,
int height,
boolean coinbase,
byte[] scriptBytes) {
Script script) {
this.hash = hash;
this.index = index;
this.value = value;
this.height = height;
this.scriptBytes = scriptBytes;
this.script = script;
this.coinbase = coinbase;
this.address = "";
this.addressType = 0;
}
/**
@ -83,21 +80,17 @@ public class UTXO implements Serializable {
* @param value The value available.
* @param height The height this output was created in.
* @param coinbase The coinbase flag.
* @param scriptBytes The script bytes.
* @param address The address.
* @param addressType The address type.
*/
public UTXO(Sha256Hash hash,
long index,
Coin value,
int height,
boolean coinbase,
byte[] scriptBytes,
String address,
int addressType) {
this(hash, index, value, height, coinbase, scriptBytes);
Script script,
String address) {
this(hash, index, value, height, coinbase, script);
this.address = address;
this.addressType = addressType;
}
public UTXO(InputStream in) throws IOException {
@ -110,9 +103,10 @@ public class UTXO implements Serializable {
((in.read() & 0xFF) << 8) |
((in.read() & 0xFF) << 16) |
((in.read() & 0xFF) << 24);
scriptBytes = new byte[scriptBytesLength];
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)
@ -147,11 +141,11 @@ public class UTXO implements Serializable {
}
/**
* The backing script bytes which can be turned into a Script object.
* @return the scriptBytes.
* The Script object which you can use to get address, script bytes or script type.
* @return the script.
*/
public byte[] getScriptBytes() {
return scriptBytes;
public Script getScript() {
return script;
}
/**
@ -194,14 +188,6 @@ public class UTXO implements Serializable {
return address;
}
/**
* The type of the address.
* @return The address type.
*/
public int getAddressType() {
return addressType;
}
@Override
public String toString() {
return String.format("Stored TxOut of %s (%s:%d)", value.toFriendlyString(), hash.toString(), index);
@ -224,6 +210,7 @@ public class UTXO implements Serializable {
public void serializeToStream(OutputStream bos) throws IOException {
Utils.uint64ToByteStreamLE(BigInteger.valueOf(value.value), bos);
byte[] scriptBytes = script.getProgram();
bos.write(0xFF & scriptBytes.length >> 0);
bos.write(0xFF & scriptBytes.length >> 8);
bos.write(0xFF & (scriptBytes.length >> 16));

View File

@ -4006,7 +4006,7 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
* @param output The stored output (free standing).
*/
public FreeStandingTransactionOutput(NetworkParameters params, UTXO output, int chainHeight) {
super(params, null, output.getValue(), output.getScriptBytes());
super(params, null, output.getValue(), output.getScript().getProgram());
this.output = output;
this.chainHeight = chainHeight;
}

View File

@ -19,6 +19,7 @@ package org.bitcoinj.store;
import com.google.common.collect.Lists;
import org.bitcoinj.core.*;
import org.bitcoinj.script.Script;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -929,15 +930,13 @@ public abstract class DatabaseFullPrunedBlockStore implements FullPrunedBlockSto
byte[] scriptBytes = results.getBytes(3);
boolean coinbase = results.getBoolean(4);
String address = results.getString(5);
int addressType = results.getInt(6);
UTXO txout = new UTXO(hash,
index,
value,
height,
coinbase,
scriptBytes,
address,
addressType);
new Script(scriptBytes),
address);
return txout;
} catch (SQLException ex) {
throw new BlockStoreException(ex);
@ -963,9 +962,9 @@ public abstract class DatabaseFullPrunedBlockStore implements FullPrunedBlockSto
s.setInt(2, (int) out.getIndex());
s.setInt(3, out.getHeight());
s.setLong(4, out.getValue().value);
s.setBytes(5, out.getScriptBytes());
s.setBytes(5, out.getScript().getProgram());
s.setString(6, out.getAddress());
s.setInt(7, out.getAddressType());
s.setInt(7, out.getScript().getScriptType().ordinal());
s.setBoolean(8, out.isCoinbase());
s.executeUpdate();
s.close();
@ -1173,15 +1172,13 @@ public abstract class DatabaseFullPrunedBlockStore implements FullPrunedBlockSto
int index = rs.getInt(5);
boolean coinbase = rs.getBoolean(6);
String toAddress = rs.getString(7);
int addressType = rs.getInt(8);
UTXO output = new UTXO(hash,
index,
amount,
height,
coinbase,
scriptBytes,
toAddress,
addressType);
new Script(scriptBytes),
toAddress);
outputs.add(output);
}
}