Merge branch 'master' of https://github.com/romanman/ethereumj
Conflicts: src/main/java/org/ethereum/core/Block.java
This commit is contained in:
commit
1e8697ae8b
|
@ -0,0 +1,35 @@
|
|||
package org.ethereum.core;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import org.ethereum.crypto.HashUtil;
|
||||
import org.ethereum.util.RLP;
|
||||
import org.spongycastle.util.encoders.Hex;
|
||||
|
||||
public class Genesis extends Block {
|
||||
|
||||
private static byte[] zeroHash256 = new byte[32];
|
||||
private static byte[] zeroHash160 = new byte[20];
|
||||
private static byte[] sha3EmptyList = HashUtil.sha3(RLP.encodeList());
|
||||
|
||||
private static byte[] parentHash = zeroHash256;
|
||||
private static byte[] unclesHash = sha3EmptyList;
|
||||
private static byte[] coinbase = zeroHash160;
|
||||
private static byte[] stateRoot = // TODO: Get stateRoot from actual state
|
||||
Hex.decode("2f4399b08efe68945c1cf90ffe85bbe3ce978959da753f9e649f034015b8817d");
|
||||
private static byte[] txTrieRoot = zeroHash256;
|
||||
private static byte[] difficulty = BigInteger.valueOf((long) Math.pow(2, 22)).toByteArray();
|
||||
private static long number = 0;
|
||||
private static long minGasPrice = 0;
|
||||
private static long gasLimit = 1000000;
|
||||
private static long gasUsed = 0;
|
||||
private static long timestamp = 0;
|
||||
private static byte[] extraData = new byte[0];
|
||||
private static byte[] nonce = HashUtil.sha3(new byte[]{42});
|
||||
|
||||
public Genesis() {
|
||||
super(parentHash, unclesHash, coinbase, stateRoot,
|
||||
txTrieRoot, difficulty, number, minGasPrice, gasLimit, gasUsed,
|
||||
timestamp, extraData, nonce, null, null);
|
||||
}
|
||||
}
|
|
@ -732,7 +732,7 @@ public class RLP {
|
|||
if(inputAsBytes.length == 1) {
|
||||
return inputAsBytes;
|
||||
} else {
|
||||
byte[] firstByte = encodeLength(inputAsBytes.length, OFFSET_SHORT_ITEM);
|
||||
byte[] firstByte = encodeLength(inputAsBytes.length, OFFSET_SHORT_ITEM);
|
||||
return concatenate(firstByte, inputAsBytes);
|
||||
}
|
||||
}
|
||||
|
@ -744,7 +744,11 @@ public class RLP {
|
|||
byte firstByte = (byte) (length + offset);
|
||||
return new byte[] { firstByte };
|
||||
} else if (length < MAX_ITEM_LENGTH) {
|
||||
byte[] binaryLength = BigInteger.valueOf(length).toByteArray();
|
||||
byte[] binaryLength;
|
||||
if(length > 0xFF)
|
||||
binaryLength = BigInteger.valueOf(length).toByteArray();
|
||||
else
|
||||
binaryLength = new byte[] { (byte) length };
|
||||
byte firstByte = (byte) (binaryLength.length + offset + SIZE_THRESHOLD - 1 );
|
||||
return concatenate(new byte[] { firstByte }, binaryLength);
|
||||
} else {
|
||||
|
@ -777,7 +781,10 @@ public class RLP {
|
|||
}
|
||||
|
||||
public static byte[] encodeBigInteger(BigInteger srcBigInteger) {
|
||||
return encodeElement(srcBigInteger.toByteArray());
|
||||
if(srcBigInteger == BigInteger.ZERO)
|
||||
return encodeByte((byte)0);
|
||||
else
|
||||
return encodeElement(srcBigInteger.toByteArray());
|
||||
}
|
||||
|
||||
public static byte[] encodeElement(byte[] srcData) {
|
||||
|
@ -864,6 +871,9 @@ public class RLP {
|
|||
} else if (input instanceof String) {
|
||||
String inputString = (String) input;
|
||||
return inputString.getBytes();
|
||||
} else if(input instanceof Long) {
|
||||
Long inputLong = (Long) input;
|
||||
return (inputLong == 0) ? new byte[0] : BigInteger.valueOf(inputLong).toByteArray();
|
||||
} else if(input instanceof Integer) {
|
||||
Integer inputInt = (Integer) input;
|
||||
return (inputInt == 0) ? new byte[0] : BigInteger.valueOf(inputInt.longValue()).toByteArray();
|
||||
|
|
|
@ -2,7 +2,9 @@ package org.ethereum.block;
|
|||
|
||||
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;
|
||||
|
@ -11,6 +13,7 @@ import static org.junit.Assert.*;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class BlockTest {
|
||||
|
||||
|
@ -120,17 +123,12 @@ public class BlockTest {
|
|||
public void testGenesisFromRLP(){
|
||||
// from RLP encoding
|
||||
byte[] genesisBytes = Hex.decode(CPP_PoC5_GENESIS_HEX_RLP_ENCODED);
|
||||
Block genesis = new Block(genesisBytes);
|
||||
assertEquals(CPP_PoC5_GENESIS_HEX_HASH, Hex.toHexString(genesis.hash()));
|
||||
Block genesis = new Block(RLP.decode2(genesisBytes));
|
||||
assertEquals(CPP_PoC5_GENESIS_HEX_HASH, Hex.toHexString(genesis.getHash()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenesisFromNew() {
|
||||
|
||||
System.out.println(CPP_PoC5_GENESIS_HEX_RLP_ENCODED);
|
||||
Object genesisItems = RLP.decode(Hex.decode(CPP_PoC5_GENESIS_HEX_RLP_ENCODED), 0).getDecoded();
|
||||
// TODO: verify genesis items with expected values
|
||||
|
||||
/* From: https://ethereum.etherpad.mozilla.org/11
|
||||
Genesis block is:
|
||||
(
|
||||
|
@ -149,32 +147,11 @@ public class BlockTest {
|
|||
B32(sha3(B(42)))
|
||||
)
|
||||
*/
|
||||
|
||||
byte[] parentHash = new byte[32]; System.out.println(Hex.toHexString(parentHash));
|
||||
byte[] unclesHash = HashUtil.sha3(new byte[0]); System.out.println(Hex.toHexString(unclesHash));
|
||||
byte[] coinbase = new byte[20]; System.out.println(Hex.toHexString(coinbase));
|
||||
byte[] stateRoot = Hex.decode(CPP_PoC5_GENESIS_STATE_ROOT_HEX_HASH); System.out.println(Hex.toHexString(stateRoot));
|
||||
byte[] txTrieRoot = new byte[32]; System.out.println(Hex.toHexString(txTrieRoot));
|
||||
byte[] difficulty = doubleToByteArray(Math.pow(2, 22)); System.out.println(Hex.toHexString(difficulty));
|
||||
long timestamp = 0; System.out.println(Long.toHexString(timestamp));
|
||||
long number = 0; System.out.println(Long.toHexString(number));
|
||||
long minGasPrice = 0; System.out.println(Long.toHexString(minGasPrice));
|
||||
long gasLimit = 1000000; System.out.println(Long.toHexString(gasLimit));
|
||||
long gasUsed = 0; System.out.println(Long.toHexString(gasUsed));
|
||||
byte[] extraData = new byte[0]; System.out.println(Hex.toHexString(extraData));
|
||||
byte[] nonce = HashUtil.sha3(new byte[]{42}); System.out.println(Hex.toHexString(nonce));
|
||||
Block block = new Block(parentHash, unclesHash, coinbase, stateRoot, txTrieRoot, difficulty, number, minGasPrice, gasLimit, gasUsed, timestamp, extraData, nonce, null, null);
|
||||
assertEquals(CPP_PoC5_GENESIS_HEX_RLP_ENCODED, Hex.toHexString(block.getEncoded()));
|
||||
assertEquals(CPP_PoC5_GENESIS_HEX_HASH, block.hash());
|
||||
Block genesis = new Genesis();
|
||||
assertEquals(CPP_PoC5_GENESIS_HEX_RLP_ENCODED, Hex.toHexString(genesis.getEncoded()));
|
||||
assertEquals(CPP_PoC5_GENESIS_HEX_HASH, Hex.toHexString(genesis.getHash()));
|
||||
}
|
||||
|
||||
private byte[] doubleToByteArray(double d) {
|
||||
byte[] output = new byte[8];
|
||||
long lng = Double.doubleToLongBits(d);
|
||||
for(int i = 0; i < 8; i++) output[i] = (byte)((lng >> ((7 - i) * 8)) & 0xff);
|
||||
return output;
|
||||
}
|
||||
|
||||
@Test /* create BlockData from part of real RLP BLOCKS message */
|
||||
public void test3(){
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue