Merge back block encoding now using rlpData as byte[] and bugfix for encodeElement of null

This commit is contained in:
nicksavers 2014-05-15 16:13:19 +02:00
parent 1e8697ae8b
commit 0c9e304b90
17 changed files with 140 additions and 112 deletions

View File

@ -1,6 +1,7 @@
package org.ethereum.core;
import org.ethereum.crypto.HashUtil;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPElement;
import org.ethereum.util.RLPItem;
import org.ethereum.util.RLPList;
@ -24,8 +25,7 @@ public class Block {
/* A scalar value equal to the current limit of gas expenditure per block */
private static int GAS_LIMIT = (int) Math.pow(10, 6);
private RLPList rawData;
private byte[] encodedBlock;
private byte[] rlpEncoded;
private boolean parsed = false;
private byte[] hash;
@ -72,19 +72,15 @@ public class Block {
private List<Transaction> transactionsList = new ArrayList<Transaction>();
private List<Block> uncleList = new ArrayList<Block>();
public Block(RLPList rawData) {
this.rawData = rawData;
public Block(byte[] rawData) {
this.rlpEncoded = rawData;
this.parsed = false;
}
public Block(byte[] encodedBlock) {
this.encodedBlock = encodedBlock;
}
public Block(byte[] parentHash, byte[] unclesHash, byte[] coinbase,
byte[] stateRoot, byte[] txTrieRoot, byte[] difficulty,
long timestamp, long number, long minGasPrice, long gasLimit,
long gasUsed, byte[] extraData, byte[] nonce,
long number, long minGasPrice, long gasLimit, long gasUsed,
long timestamp, byte[] extraData, byte[] nonce,
List<Transaction> transactionsList, List<Block> uncleList) {
this.parentHash = parentHash;
this.unclesHash = unclesHash;
@ -92,11 +88,11 @@ public class Block {
this.stateRoot = stateRoot;
this.txTrieRoot = txTrieRoot;
this.difficulty = difficulty;
this.timestamp = timestamp;
this.number = number;
this.minGasPrice = minGasPrice;
this.gasLimit = gasLimit;
this.gasUsed = gasUsed;
this.timestamp = timestamp;
this.extraData = extraData;
this.nonce = nonce;
this.transactionsList = transactionsList;
@ -105,47 +101,49 @@ public class Block {
}
// [parent_hash, uncles_hash, coinbase, state_root, tx_trie_root,
// difficulty, timestamp, number, minGasPrice, gasLimit, gasUsed,
// difficulty, number, minGasPrice, gasLimit, gasUsed, timestamp,
// extradata, nonce]
private void parseRLP() {
RLPList params = (RLPList) RLP.decode2(rlpEncoded);
RLPList params = (RLPList) rawData.get(0);
this.hash = HashUtil.sha3(rlpEncoded);
this.hash = HashUtil.sha3(rawData.getRLPData());
RLPList block = (RLPList) params.get(0);
RLPList header = (RLPList) block.get(0);
this.parentHash = ((RLPItem) header.get(0)).getRLPData();
this.unclesHash = ((RLPItem) header.get(1)).getRLPData();
this.coinbase = ((RLPItem) header.get(2)).getRLPData();
this.stateRoot = ((RLPItem) header.get(3)).getRLPData();
this.txTrieRoot = ((RLPItem) header.get(4)).getRLPData();
this.difficulty = ((RLPItem) header.get(5)).getRLPData();
byte[] tsBytes = ((RLPItem) header.get(6)).getRLPData();
byte[] nrBytes = ((RLPItem) header.get(7)).getRLPData();
byte[] gpBytes = ((RLPItem) header.get(8)).getRLPData();
byte[] glBytes = ((RLPItem) header.get(9)).getRLPData();
byte[] guBytes = ((RLPItem) header.get(10)).getRLPData();
this.parentHash = ((RLPItem) params.get(0)).getData();
this.unclesHash = ((RLPItem) params.get(1)).getData();
this.coinbase = ((RLPItem) params.get(2)).getData();
this.stateRoot = ((RLPItem) params.get(3)).getData();
this.txTrieRoot = ((RLPItem) params.get(4)).getData();
this.difficulty = ((RLPItem) params.get(5)).getData();
byte[] tsBytes = ((RLPItem) params.get(6)).getData();
byte[] nrBytes = ((RLPItem) params.get(7)).getData();
byte[] gpBytes = ((RLPItem) params.get(8)).getData();
byte[] glBytes = ((RLPItem) params.get(9)).getData();
byte[] guBytes = ((RLPItem) params.get(10)).getData();
this.timestamp = (new BigInteger(tsBytes)).longValue();
this.number = (new BigInteger(nrBytes)).longValue();
this.timestamp = tsBytes == null ? 0 : (new BigInteger(tsBytes)).longValue();
this.number = nrBytes == null ? 0 : (new BigInteger(nrBytes)).longValue();
this.minGasPrice = gpBytes == null ? 0 : (new BigInteger(gpBytes)).longValue();
this.gasLimit = glBytes == null ? 0 : (new BigInteger(glBytes)).longValue();
this.gasUsed = guBytes == null ? 0 : (new BigInteger(guBytes)).longValue();
this.extraData = ((RLPItem) params.get(11)).getData();
this.nonce = ((RLPItem) params.get(12)).getData();
this.extraData = ((RLPItem) header.get(11)).getRLPData();
this.nonce = ((RLPItem) header.get(12)).getRLPData();
// parse transactions
RLPList transactions = (RLPList) rawData.get(1);
RLPList transactions = (RLPList) block.get(1);
for (RLPElement rlpTx : transactions){
Transaction tx = new Transaction((RLPList)rlpTx);
this.transactionsList.add(tx);
}
// parse uncles
RLPList uncleBlocks = (RLPList) rawData.get(2);
RLPList uncleBlocks = (RLPList) block.get(2);
for (RLPElement rawUncle : uncleBlocks){
Block blockData = new Block((RLPList)rawUncle);
Block blockData = new Block(rawUncle.getRLPData());
this.uncleList.add(blockData);
}
this.parsed = true;
@ -153,7 +151,7 @@ public class Block {
public byte[] getHash(){
if (!parsed) parseRLP();
return hash;
return HashUtil.sha3(this.getEncoded());
}
public Block getParent() {
@ -224,16 +222,22 @@ public class Block {
public List<Transaction> getTransactionsList() {
if (!parsed) parseRLP();
if (transactionsList == null) {
this.transactionsList = new ArrayList<Transaction>();
}
return transactionsList;
}
public List<Block> getUncleList() {
if (!parsed) parseRLP();
if (uncleList == null) {
this.uncleList = new ArrayList<Block>();
}
return uncleList;
}
// [parent_hash, uncles_hash, coinbase, state_root, tx_trie_root,
// difficulty, timestamp, number, minGasPrice, gasLimit, gasUsed,
// difficulty, number, minGasPrice, gasLimit, gasUsed, timestamp,
// extradata, nonce]
@Override
public String toString() {
@ -247,11 +251,11 @@ public class Block {
", stateHash=" + Utils.toHexString(stateRoot) +
", txTrieHash=" + Utils.toHexString(txTrieRoot) +
", difficulty=" + Utils.toHexString(difficulty) +
", timestamp=" + timestamp +
", number=" + number +
", minGasPrice=" + minGasPrice +
", gasLimit=" + gasLimit +
", gasUsed=" + gasUsed +
", timestamp=" + timestamp +
", extraData=" + Utils.toHexString(extraData) +
", nonce=" + Utils.toHexString(nonce) +
']';
@ -284,11 +288,39 @@ public class Block {
}
public byte[] getEncoded() {
if (this.rawData.getRLPData() == null) parseRLP();
return this.rawData.getRLPData();
}
if(rlpEncoded == null) {
// TODO: Alternative clean way to encode, using RLP.encode() after it's optimized
// Object[] header = new Object[] { parentHash, unclesHash, coinbase,
// stateRoot, txTrieRoot, difficulty, number, minGasPrice,
// gasLimit, gasUsed, timestamp, extraData, nonce };
// Object[] transactions = this.getTransactionsList().toArray();
// Object[] uncles = this.getUncleList().toArray();
// return RLP.encode(new Object[] { header, transactions, uncles });
byte[] parentHash = RLP.encodeElement(this.parentHash);
byte[] unclesHash = RLP.encodeElement(this.unclesHash);
byte[] coinbase = RLP.encodeElement(this.coinbase);
byte[] stateRoot = RLP.encodeElement(this.stateRoot);
byte[] txTrieRoot = RLP.encodeElement(this.txTrieRoot);
byte[] difficulty = RLP.encodeElement(this.difficulty);
byte[] number = RLP.encodeBigInteger(BigInteger.valueOf(this.number));
byte[] minGasPrice = RLP.encodeBigInteger(BigInteger.valueOf(this.minGasPrice));
byte[] gasLimit = RLP.encodeBigInteger(BigInteger.valueOf(this.gasLimit));
byte[] gasUsed = RLP.encodeBigInteger(BigInteger.valueOf(this.gasUsed));
byte[] timestamp = RLP.encodeBigInteger(BigInteger.valueOf(this.timestamp));
byte[] extraData = RLP.encodeElement(this.extraData);
byte[] nonce = RLP.encodeElement(this.nonce);
public byte[] hash() {
return HashUtil.sha3(this.getEncoded());
byte[] header = RLP.encodeList(parentHash, unclesHash, coinbase,
stateRoot, txTrieRoot, difficulty, number,
minGasPrice, gasLimit, gasUsed, timestamp, extraData, nonce);
byte[] transactions = RLP.encodeList();
byte[] uncles = RLP.encodeList();
this.rlpEncoded = RLP.encodeList(header, transactions, uncles);
}
return rlpEncoded;
}
}
}

View File

@ -74,23 +74,23 @@ public class Transaction {
RLPList params = (RLPList) rawData.get(0);
this.hash = HashUtil.sha3(rawData.getRLPData());
this.nonce = ((RLPItem) params.get(0)).getData();
this.value = ((RLPItem) params.get(1)).getData();
this.receiveAddress = ((RLPItem) params.get(2)).getData();
this.gasPrice = ((RLPItem) params.get(3)).getData();
this.gasLimit = ((RLPItem) params.get(4)).getData();
this.data = ((RLPItem) params.get(5)).getData();
this.nonce = ((RLPItem) params.get(0)).getRLPData();
this.value = ((RLPItem) params.get(1)).getRLPData();
this.receiveAddress = ((RLPItem) params.get(2)).getRLPData();
this.gasPrice = ((RLPItem) params.get(3)).getRLPData();
this.gasLimit = ((RLPItem) params.get(4)).getRLPData();
this.data = ((RLPItem) params.get(5)).getRLPData();
if (params.size() == 9){ // Simple transaction
byte v = ((RLPItem) params.get(6)).getData()[0];
byte[] r = ((RLPItem) params.get(7)).getData();
byte[] s = ((RLPItem) params.get(8)).getData();
byte v = ((RLPItem) params.get(6)).getRLPData()[0];
byte[] r = ((RLPItem) params.get(7)).getRLPData();
byte[] s = ((RLPItem) params.get(8)).getRLPData();
this.signature = ECDSASignature.fromComponents(r, s, v);
} else if (params.size() == 10){ // Contract creation transaction
this.init = ((RLPItem) params.get(6)).getData();
byte v = ((RLPItem) params.get(7)).getData()[0];
byte[] r = ((RLPItem) params.get(8)).getData();
byte[] s = ((RLPItem) params.get(9)).getData();
this.init = ((RLPItem) params.get(6)).getRLPData();
byte v = ((RLPItem) params.get(7)).getRLPData()[0];
byte[] r = ((RLPItem) params.get(8)).getRLPData();
byte[] s = ((RLPItem) params.get(9)).getRLPData();
this.signature = ECDSASignature.fromComponents(r, s, v);
} else throw new RuntimeException("Wrong tx data element list size");
this.parsed = true;

View File

@ -356,7 +356,7 @@ public class ECKey implements Serializable {
* Signs the given hash and returns the R and S components as BigIntegers
* and put them in ECDSASignature
*
* @param data to sign
* @param rlpData to sign
* @return ECDSASignature signature that contains the R and S components
*/
public ECDSASignature doSign(byte[] input) {

View File

@ -76,7 +76,7 @@ public class MainData {
if (blockChainDB.isEmpty())
return StaticMessages.GENESSIS_HASH;
else
return blockChainDB.get(blockChainDB.size() - 1).hash();
return blockChainDB.get(blockChainDB.size() - 1).getHash();
}

View File

@ -28,13 +28,13 @@ public class BlocksMessage extends Message {
RLPList paramsList = (RLPList) rawData.get(0);
if (Command.fromInt(((RLPItem) (paramsList).get(0)).getData()[0]) != BLOCKS) {
if (Command.fromInt(((RLPItem) (paramsList).get(0)).getRLPData()[0]) != BLOCKS) {
throw new Error("BlocksMessage: parsing for mal data");
}
for (int i = 1; i < paramsList.size(); ++i) {
RLPList rlpData = ((RLPList) paramsList.get(i));
Block blockData = new Block(rlpData);
Block blockData = new Block(rlpData.getRLPData());
this.blockDataList.add(blockData);
}
parsed = true;

View File

@ -25,11 +25,11 @@ public class DisconnectMessage extends Message {
RLPList paramsList = (RLPList) rawData.get(0);
if (Command.fromInt(((RLPItem)(paramsList).get(0)).getData()[0]) != DISCONNECT){
if (Command.fromInt(((RLPItem)(paramsList).get(0)).getRLPData()[0]) != DISCONNECT){
throw new Error("Disconnect: parsing for mal data");
}
byte[] reasonB = ((RLPItem)paramsList.get(1)).getData();
byte[] reasonB = ((RLPItem)paramsList.get(1)).getRLPData();
if (reasonB == null){
this.reason = DISCONNECT_REQUESTED;
} else {

View File

@ -49,17 +49,17 @@ public class GetChainMessage extends Message {
RLPList paramsList = (RLPList) rawData.get(0);
if (Command.fromInt(((RLPItem)(paramsList).get(0)).getData()[0]) != GET_CHAIN){
if (Command.fromInt(((RLPItem)(paramsList).get(0)).getRLPData()[0]) != GET_CHAIN){
throw new Error("GetChain: parsing for mal data");
}
int size = paramsList.size();
for (int i = 1; i < size - 1; ++i){
blockHashList.add(((RLPItem) paramsList.get(i)).getData());
blockHashList.add(((RLPItem) paramsList.get(i)).getRLPData());
}
// the last element is the num of requested blocks
byte[] blockNumB = ((RLPItem)paramsList.get(size - 1)).getData();
byte[] blockNumB = ((RLPItem)paramsList.get(size - 1)).getRLPData();
this.blockNum = new BigInteger(blockNumB);
this.parsed = true;

View File

@ -44,21 +44,21 @@ public class HelloMessage extends Message {
// the message does no distinguish between the 0 and null so here I check command code for null
// todo: find out if it can be 00
if (((RLPItem)(paramsList).get(0)).getData() != null){
if (((RLPItem)(paramsList).get(0)).getRLPData() != null){
throw new Error("HelloMessage: parsing for mal data");
}
this.protocolVersion = ((RLPItem) paramsList.get(1)).getData()[0];
this.protocolVersion = ((RLPItem) paramsList.get(1)).getRLPData()[0];
byte[] networkIdBytes = ((RLPItem) paramsList.get(2)).getData();
byte[] networkIdBytes = ((RLPItem) paramsList.get(2)).getRLPData();
this.networkId = networkIdBytes == null ? 0 : networkIdBytes[0] ;
this.clientId = new String(((RLPItem) paramsList.get(3)).getData());
this.capabilities = ((RLPItem) paramsList.get(4)).getData()[0];
this.clientId = new String(((RLPItem) paramsList.get(3)).getRLPData());
this.capabilities = ((RLPItem) paramsList.get(4)).getRLPData()[0];
ByteBuffer bb = ByteBuffer.wrap(((RLPItem) paramsList.get(5)).getData());
ByteBuffer bb = ByteBuffer.wrap(((RLPItem) paramsList.get(5)).getRLPData());
this.peerPort = bb.getShort();
this.peerId = ((RLPItem) paramsList.get(6)).getData();
this.peerId = ((RLPItem) paramsList.get(6)).getRLPData();
this.parsed = true;
// todo: what to do when mal data ?
}

View File

@ -25,10 +25,10 @@ public class NotInChainMessage extends Message {
public void parseRLP() {
RLPList paramsList = (RLPList) rawData.get(0);
if (Command.fromInt(((RLPItem)(paramsList).get(0)).getData()[0] & 0xFF) != NOT_IN_CHAIN){
if (Command.fromInt(((RLPItem)(paramsList).get(0)).getRLPData()[0] & 0xFF) != NOT_IN_CHAIN){
throw new Error("NotInChain Message: parsing for mal data");
}
hash = ((RLPItem)paramsList.get(1)).getData();
hash = ((RLPItem)paramsList.get(1)).getRLPData();
}
@Override

View File

@ -33,15 +33,15 @@ public class PeersMessage extends Message {
RLPList paramsList = (RLPList) rawData.get(0);
if (Command.fromInt(((RLPItem)(paramsList).get(0)).getData()[0] & 0xFF) != PEERS){
if (Command.fromInt(((RLPItem)(paramsList).get(0)).getRLPData()[0] & 0xFF) != PEERS){
throw new Error("PeersMessage: parsing for mal data");
}
for (int i = 1; i < paramsList.size(); ++i){
RLPList peerParams = (RLPList)paramsList.get(i);
byte[] ip = ((RLPItem) peerParams.get(0)).getData();
byte[] shortData = ((RLPItem) peerParams.get(1)).getData();
byte[] ip = ((RLPItem) peerParams.get(0)).getRLPData();
byte[] shortData = ((RLPItem) peerParams.get(1)).getRLPData();
short peerPort = 0;
if (shortData.length == 1)
peerPort = shortData[0];
@ -49,7 +49,7 @@ public class PeersMessage extends Message {
ByteBuffer bb = ByteBuffer.wrap(shortData, 0, shortData.length);
peerPort = bb.getShort();
}
byte[] peerId = ((RLPItem) peerParams.get(2)).getData();
byte[] peerId = ((RLPItem) peerParams.get(2)).getRLPData();
PeerData peer = new PeerData(ip, peerPort, peerId);
peers.add(peer);
}

View File

@ -30,7 +30,7 @@ public class TransactionsMessage extends Message {
public void parseRLP() {
RLPList paramsList = (RLPList) rawData.get(0);
if (Command.fromInt(((RLPItem)(paramsList).get(0)).getData()[0] & 0xFF) != TRANSACTIONS) {
if (Command.fromInt(((RLPItem)(paramsList).get(0)).getRLPData()[0] & 0xFF) != TRANSACTIONS) {
throw new Error("TransactionMessage: parsing for mal data");
}

View File

@ -789,7 +789,9 @@ public class RLP {
public static byte[] encodeElement(byte[] srcData) {
if (srcData.length <= 0x37) {
if (srcData == null){
return new byte[] { 0x00 };
} else if (srcData.length <= 0x37) {
// length = 8X
byte length = (byte) (OFFSET_SHORT_ITEM + srcData.length);
byte[] data = Arrays.copyOf(srcData, srcData.length + 1);

View File

@ -1,8 +1,11 @@
package org.ethereum.util;
import java.io.Serializable;
/**
* Wrapper class for decoded elements from an RLP encoded byte array.
*/
public interface RLPElement {
public interface RLPElement extends Serializable {
public byte[] getRLPData();
}

View File

@ -1,24 +1,22 @@
package org.ethereum.util;
import java.io.Serializable;
/**
* www.ethereumJ.com
* User: Roman Mandeleil
* Created on: 21/04/14 16:26
*/
public class RLPItem implements RLPElement, Serializable {
public class RLPItem implements RLPElement {
byte[] data;
public RLPItem(byte[] data) {
this.data = data;
byte[] rlpData;
public RLPItem(byte[] rlpData) {
this.rlpData = rlpData;
}
public byte[] getData() {
if (data.length == 0)
public byte[] getRLPData() {
if (rlpData.length == 0)
return null;
return data;
return rlpData;
}
}

View File

@ -11,7 +11,7 @@ public class RLPList extends ArrayList<RLPElement> implements RLPElement {
byte[] rlpData;
public void setRLPData(byte[] rlpData){
public void setRLPData(byte[] rlpData) {
this.rlpData = rlpData;
}
@ -32,7 +32,7 @@ public class RLPList extends ArrayList<RLPElement> implements RLPElement {
}
System.out.print("]");
} else {
String hex = Utils.toHexString(((RLPItem) element).getData());
String hex = Utils.toHexString(((RLPItem) element).getRLPData());
System.out.print(hex + ", ");
}
}

View File

@ -4,16 +4,13 @@ import org.spongycastle.util.encoders.Hex;
import org.ethereum.core.Block;
import org.ethereum.core.Genesis;
import org.ethereum.crypto.HashUtil;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPList;
import org.junit.Test;
import static org.junit.Assert.*;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
public class BlockTest {
@ -123,7 +120,7 @@ public class BlockTest {
public void testGenesisFromRLP(){
// from RLP encoding
byte[] genesisBytes = Hex.decode(CPP_PoC5_GENESIS_HEX_RLP_ENCODED);
Block genesis = new Block(RLP.decode2(genesisBytes));
Block genesis = new Block(genesisBytes);
assertEquals(CPP_PoC5_GENESIS_HEX_HASH, Hex.toHexString(genesis.getHash()));
}
@ -158,11 +155,8 @@ public class BlockTest {
String blocksMsg = "F8C8F8C4A07B2536237CBF114A043B0F9B27C76F84AC160EA5B87B53E42C7E76148964D450A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347943854AAF203BA5F8D49B1EC221329C7AEBCF050D3A07A3BE0EE10ECE4B03097BF74AABAC628AA0FAE617377D30AB1B97376EE31F41AA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347833FBFE884533F1CE880A0000000000000000000000000000000000000000000000000F3DEEA84969B6E95C0C0";
byte[] payload = Hex.decode(blocksMsg);
RLPList rlpList = RLP.decode2(payload);
Block blockData = new Block(rlpList);
RLPList.recursivePrint(rlpList);
Block blockData = new Block(payload);
System.out.println(blockData.toString());
}
@Test /* create BlockData from part of real RLP BLOCKS message POC-5 */
@ -171,15 +165,9 @@ public class BlockTest {
String blocksMsg = "F8D1A0085F6A51A63D1FBA43D6E5FE166A47BED64A8B93A99012537D50F3279D4CEA52A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D4934794D8758B101609A9F2A881A017BA86CBE6B7F0581DA068472689EA736CFC6B18FCAE9BA7454BADF9C65333A0317DFEFAE1D4AFFF6F90A000000000000000000000000000000000000000000000000000000000000000008401EDF1A18222778609184E72A0008080845373B0B180A0000000000000000000000000000000000000000000000000D1C0D8BC6D744943C0C0";
byte[] payload = Hex.decode(blocksMsg);
RLPList rlpList = RLP.decode2(payload);
Block blockData = new Block(rlpList);
Block blockData = new Block(payload);
System.out.println(blockData.toString());
RLPList.recursivePrint(rlpList);
}
}
/*

View File

@ -1,8 +1,6 @@
package org.ethereum.util;
import org.spongycastle.util.encoders.Hex;
import org.ethereum.core.Block;
import org.ethereum.core.Genesis;
import org.ethereum.crypto.HashUtil;
import org.ethereum.util.Utils;
import org.junit.Test;
@ -234,6 +232,13 @@ public class RLPTest {
byte[] actuals = RLP.encodeList();
assertArrayEquals(new byte[] { (byte) 0xc0 }, actuals);
}
@Test /** encode list */
public void testEncodeElementNull(){
byte[] actuals = RLP.encodeElement(null);
assertArrayEquals(new byte[] { (byte) 0x00 }, actuals);
}
@Test /** found bug encode list affects element value,
hhh... not really at the end but keep the test */