3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 23:03:04 +00:00

Widen the scoping on a few methods in preparation for moving the Script class.

This commit is contained in:
Mike Hearn 2013-04-23 17:57:16 +02:00
parent 7837a54bc0
commit 775af907ae
4 changed files with 36 additions and 13 deletions

View File

@ -17,6 +17,7 @@
package com.google.bitcoin.core;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.crypto.digests.RIPEMD160Digest;
@ -56,18 +57,33 @@ public class Script {
* An element that is either an opcode or a raw byte array (signature, pubkey, etc).
*/
public static class Chunk {
public boolean isOpCode;
private boolean isOpCode;
public byte[] data;
public int startLocationInProgram;
private int startLocationInProgram;
public Chunk(boolean isOpCode, byte[] data) {
this(isOpCode, data, -1);
}
public Chunk(boolean isOpCode, byte[] data, int startLocationInProgram) {
this.isOpCode = isOpCode;
this.data = data;
this.startLocationInProgram = startLocationInProgram;
}
public boolean equalsOpCode(int opCode) {
return isOpCode && data.length == 1 && (0xFF & data[0]) == opCode;
}
public boolean isOpCode() {
return isOpCode;
}
public int getStartLocationInProgram() {
checkState(startLocationInProgram >= 0);
return startLocationInProgram;
}
public void write(OutputStream stream) throws IOException {
if (isOpCode) {
checkState(data.length == 1);
@ -98,8 +114,10 @@ public class Script {
// must preserve the exact bytes that we read off the wire, along with the parsed form.
protected byte[] program;
// Only for internal use
private Script() {}
/** Creates an empty script that serializes to nothing. */
public Script() {
chunks = Lists.newArrayList();
}
/**
* Construct a Script that copies and wraps the programBytes array. The array is parsed and checked for syntactic
@ -146,6 +164,11 @@ public class Script {
}
}
/** Returns an immutable list of the scripts parsed form. */
public List<Chunk> getChunks() {
return Collections.unmodifiableList(chunks);
}
/**
* <p>To run a script, first we parse it which breaks it up into chunks representing pushes of data or logical
* opcodes. Then we can run the parsed chunks.</p>
@ -1056,7 +1079,7 @@ public class Script {
stack.add(Utils.doubleDigest(stack.pollLast()));
break;
case OP_CODESEPARATOR:
lastCodeSepLocation = chunk.startLocationInProgram + 1;
lastCodeSepLocation = chunk.getStartLocationInProgram() + 1;
break;
case OP_CHECKSIG:
case OP_CHECKSIGVERIFY:

View File

@ -599,11 +599,11 @@ public class Transaction extends ChildMessage implements Serializable {
try {
Script scriptSig = in.getScriptSig();
if (scriptSig.chunks.size() == 2)
if (scriptSig.getChunks().size() == 2)
s.append(scriptSig.getFromAddress(params).toString());
else if (scriptSig.chunks.size() == 1) {
else if (scriptSig.getChunks().size() == 1) {
s.append("[sig:");
s.append(bytesToHexString(scriptSig.chunks.get(0).data));
s.append(bytesToHexString(scriptSig.getChunks().get(0).data));
s.append("]");
} else
s.append("???");
@ -813,7 +813,7 @@ public class Transaction extends ChildMessage implements Serializable {
* This is required for signatures which use a sigHashType which cannot be represented using SigHash and anyoneCanPay
* See transaction c99c49da4c38af669dea436d3e73780dfdb6c1ecf9958baa52960e8baee30e73, which has sigHashType 0
*/
synchronized Sha256Hash hashTransactionForSignature(int inputIndex, byte[] connectedScript,
public synchronized Sha256Hash hashTransactionForSignature(int inputIndex, byte[] connectedScript,
byte sigHashType) throws ScriptException {
// TODO: This whole separate method should be un-necessary if we fix how we deserialize sighash flags.

View File

@ -347,7 +347,7 @@ public class Utils {
* the number in big endian format (with a sign bit).
* @param hasLength can be set to false if the given array is missing the 4 byte length field
*/
static BigInteger decodeMPI(byte[] mpi, boolean hasLength) {
public static BigInteger decodeMPI(byte[] mpi, boolean hasLength) {
byte[] buf;
if (hasLength) {
int length = (int) readUint32BE(mpi, 0);
@ -370,7 +370,7 @@ public class Utils {
* the number in big endian format (with a sign bit).
* @param includeLength indicates whether the 4 byte length field should be included
*/
static byte[] encodeMPI(BigInteger value, boolean includeLength) {
public static byte[] encodeMPI(BigInteger value, boolean includeLength) {
if (value.equals(BigInteger.ZERO)) {
if (!includeLength)
return new byte[] {};

View File

@ -853,8 +853,8 @@ public class WalletTest extends TestWithWallet {
Transaction t2 = wallet.createSend(new ECKey().toAddress(params), value);
assertNotNull(t2);
// TODO: This code is messy, improve the Script class and fixinate!
assertEquals(t2.toString(), 1, t2.getInputs().get(0).getScriptSig().chunks.size());
assertTrue(t2.getInputs().get(0).getScriptSig().chunks.get(0).data.length > 50);
assertEquals(t2.toString(), 1, t2.getInputs().get(0).getScriptSig().getChunks().size());
assertTrue(t2.getInputs().get(0).getScriptSig().getChunks().get(0).data.length > 50);
log.info(t2.toString(chain));
}