Transaction data/init refactoring

-- One field is enough for new spec
This commit is contained in:
romanman 2014-05-26 21:25:17 +03:00
parent 28086ce09e
commit 3f6c813ce1
1 changed files with 22 additions and 51 deletions

View File

@ -12,7 +12,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.spongycastle.util.BigIntegers; import org.spongycastle.util.BigIntegers;
import java.math.BigInteger;
import java.security.SignatureException; import java.security.SignatureException;
import java.util.Arrays; import java.util.Arrays;
@ -33,7 +32,7 @@ public class Transaction {
public static final byte[] ZERO_ADDRESS = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; public static final byte[] ZERO_ADDRESS = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
/* creation contract tx /* creation contract tx
* [ nonce, endowment, 0, gasPrice, gasDeposit (for init), body, init, signature(v, r, s) ] * [ nonce, endowment, 0000000000000000, gasPrice, gasDeposit data, signature(v, r, s) ]
* or simple send tx * or simple send tx
* [ nonce, value, receiveAddress, gasPrice, gasDeposit, data, signature(v, r, s) ] * [ nonce, value, receiveAddress, gasPrice, gasDeposit, data, signature(v, r, s) ]
*/ */
@ -63,10 +62,8 @@ public class Transaction {
/* An unlimited size byte array specifying /* An unlimited size byte array specifying
* input [data] of the message call */ * input [data] of the message call */
private byte[] data;
/* Initialisation code for a new contract */ /* Initialisation code for a new contract */
private byte[] init; private byte[] data;
/* the elliptic curve signature /* the elliptic curve signature
* (including public key recovery bits) */ * (including public key recovery bits) */
@ -90,11 +87,10 @@ public class Transaction {
this.gasLimit = gasLimit; this.gasLimit = gasLimit;
this.receiveAddress = receiveAddress; this.receiveAddress = receiveAddress;
this.value = value; this.value = value;
this.data = data;
if(receiveAddress == null || receiveAddress.length == 0) { if(receiveAddress == null || receiveAddress.length == 0) {
this.receiveAddress = ZERO_ADDRESS; this.receiveAddress = ZERO_ADDRESS;
this.init = data;
} else {
this.data = data;
} }
parsed = true; parsed = true;
} }
@ -111,30 +107,17 @@ public class Transaction {
this.value = ((RLPItem) transaction.get(4)).getRLPData(); this.value = ((RLPItem) transaction.get(4)).getRLPData();
if (isContract()){ // Simple transaction this.data = ((RLPItem) transaction.get(5)).getRLPData();
// only parse signature in case tx is signed
if(((RLPItem) transaction.get(6)).getRLPData() != null) {
byte v = ((RLPItem) transaction.get(6)).getRLPData()[0];
byte[] r = ((RLPItem) transaction.get(7)).getRLPData();
byte[] s = ((RLPItem) transaction.get(8)).getRLPData();
this.signature = ECDSASignature.fromComponents(r, s, v);
} else {
logger.debug("RLP encoded tx is not signed!");
}
this.init = ((RLPItem) transaction.get(5)).getRLPData();
// only parse signature in case tx is signed
if(((RLPItem) transaction.get(6)).getRLPData() != null) {
byte v = ((RLPItem) transaction.get(6)).getRLPData()[0];
byte[] r = ((RLPItem) transaction.get(7)).getRLPData();
byte[] s = ((RLPItem) transaction.get(8)).getRLPData();
this.signature = ECDSASignature.fromComponents(r, s, v);
} else {
logger.debug("RLP encoded tx is not signed!");
}
} else { // Contract creation transaction
this.init = ((RLPItem) transaction.get(5)).getRLPData();
// only parse signature in case tx is signed
if(((RLPItem) transaction.get(6)).getRLPData() != null) {
byte v = ((RLPItem) transaction.get(6)).getRLPData()[0];
byte[] r = ((RLPItem) transaction.get(7)).getRLPData();
byte[] s = ((RLPItem) transaction.get(8)).getRLPData();
this.signature = ECDSASignature.fromComponents(r, s, v);
} else {
logger.debug("RLP encoded tx is not signed!");
}
}
this.parsed = true; this.parsed = true;
this.hash = this.getHash(); this.hash = this.getHash();
} }
@ -182,7 +165,7 @@ public class Transaction {
public byte[] getInit() { public byte[] getInit() {
if (!parsed) rlpParse(); if (!parsed) rlpParse();
return init; return data;
} }
public ECDSASignature getSignature() { public ECDSASignature getSignature() {
@ -241,7 +224,6 @@ public class Transaction {
", receiveAddress=" + ByteUtil.toHexString(receiveAddress) + ", receiveAddress=" + ByteUtil.toHexString(receiveAddress) +
", value=" + ByteUtil.toHexString(value) + ", value=" + ByteUtil.toHexString(value) +
", data=" + ByteUtil.toHexString(data) + ", data=" + ByteUtil.toHexString(data) +
", init=" + ByteUtil.toHexString(init) +
", signatureV=" + signature.v + ", signatureV=" + signature.v +
", signatureR=" + ByteUtil.toHexString(BigIntegers.asUnsignedByteArray(signature.r)) + ", signatureR=" + ByteUtil.toHexString(BigIntegers.asUnsignedByteArray(signature.r)) +
", signatureS=" + ByteUtil.toHexString(BigIntegers.asUnsignedByteArray(signature.s)) + ", signatureS=" + ByteUtil.toHexString(BigIntegers.asUnsignedByteArray(signature.s)) +
@ -262,16 +244,10 @@ public class Transaction {
byte[] gasLimit = RLP.encodeElement(this.gasLimit); byte[] gasLimit = RLP.encodeElement(this.gasLimit);
byte[] receiveAddress = RLP.encodeElement(this.receiveAddress); byte[] receiveAddress = RLP.encodeElement(this.receiveAddress);
byte[] value = RLP.encodeElement(this.value); byte[] value = RLP.encodeElement(this.value);
byte[] data = RLP.encodeElement(this.data); byte[] init = RLP.encodeElement(this.data);
if(isContract()) {
byte[] init = RLP.encodeElement(this.init);
this.rlpRaw = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress, value, this.rlpRaw = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress, value,
init); init);
} else {
this.rlpRaw = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress, value,
data);
}
return rlpRaw; return rlpRaw;
} }
@ -284,7 +260,7 @@ public class Transaction {
byte[] gasLimit = RLP.encodeElement(this.gasLimit); byte[] gasLimit = RLP.encodeElement(this.gasLimit);
byte[] receiveAddress = RLP.encodeElement(this.receiveAddress); byte[] receiveAddress = RLP.encodeElement(this.receiveAddress);
byte[] value = RLP.encodeElement(this.value); byte[] value = RLP.encodeElement(this.value);
byte[] data = RLP.encodeElement(this.data); byte[] init = RLP.encodeElement(this.data);
byte[] v, r, s; byte[] v, r, s;
@ -298,14 +274,9 @@ public class Transaction {
s = RLP.encodeElement(new byte[0]); s = RLP.encodeElement(new byte[0]);
} }
if(isContract()) {
byte[] init = RLP.encodeElement(this.init);
this.rlpEncoded = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress, value, this.rlpEncoded = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress, value,
init, v, r, s); init, v, r, s);
} else {
this.rlpEncoded = RLP.encodeList(nonce, gasPrice, gasLimit, receiveAddress, value,
data, v, r, s);
}
return rlpEncoded; return rlpEncoded;
} }