mirror of
https://github.com/status-im/ethereumj-personal.git
synced 2025-02-16 13:46:46 +00:00
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
35
ethereumj-core/src/main/java/org/ethereum/core/Genesis.java
Normal file
35
ethereumj-core/src/main/java/org/ethereum/core/Genesis.java
Normal file
@ -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) {
|
if(inputAsBytes.length == 1) {
|
||||||
return inputAsBytes;
|
return inputAsBytes;
|
||||||
} else {
|
} else {
|
||||||
byte[] firstByte = encodeLength(inputAsBytes.length, OFFSET_SHORT_ITEM);
|
byte[] firstByte = encodeLength(inputAsBytes.length, OFFSET_SHORT_ITEM);
|
||||||
return concatenate(firstByte, inputAsBytes);
|
return concatenate(firstByte, inputAsBytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -744,7 +744,11 @@ public class RLP {
|
|||||||
byte firstByte = (byte) (length + offset);
|
byte firstByte = (byte) (length + offset);
|
||||||
return new byte[] { firstByte };
|
return new byte[] { firstByte };
|
||||||
} else if (length < MAX_ITEM_LENGTH) {
|
} 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 );
|
byte firstByte = (byte) (binaryLength.length + offset + SIZE_THRESHOLD - 1 );
|
||||||
return concatenate(new byte[] { firstByte }, binaryLength);
|
return concatenate(new byte[] { firstByte }, binaryLength);
|
||||||
} else {
|
} else {
|
||||||
@ -777,7 +781,10 @@ public class RLP {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] encodeBigInteger(BigInteger srcBigInteger) {
|
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) {
|
public static byte[] encodeElement(byte[] srcData) {
|
||||||
@ -864,6 +871,9 @@ public class RLP {
|
|||||||
} else if (input instanceof String) {
|
} else if (input instanceof String) {
|
||||||
String inputString = (String) input;
|
String inputString = (String) input;
|
||||||
return inputString.getBytes();
|
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) {
|
} else if(input instanceof Integer) {
|
||||||
Integer inputInt = (Integer) input;
|
Integer inputInt = (Integer) input;
|
||||||
return (inputInt == 0) ? new byte[0] : BigInteger.valueOf(inputInt.longValue()).toByteArray();
|
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.spongycastle.util.encoders.Hex;
|
||||||
import org.ethereum.core.Block;
|
import org.ethereum.core.Block;
|
||||||
|
import org.ethereum.core.Genesis;
|
||||||
import org.ethereum.crypto.HashUtil;
|
import org.ethereum.crypto.HashUtil;
|
||||||
|
import org.ethereum.util.ByteUtil;
|
||||||
import org.ethereum.util.RLP;
|
import org.ethereum.util.RLP;
|
||||||
import org.ethereum.util.RLPList;
|
import org.ethereum.util.RLPList;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -11,6 +13,7 @@ import static org.junit.Assert.*;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class BlockTest {
|
public class BlockTest {
|
||||||
|
|
||||||
@ -120,17 +123,12 @@ public class BlockTest {
|
|||||||
public void testGenesisFromRLP(){
|
public void testGenesisFromRLP(){
|
||||||
// from RLP encoding
|
// from RLP encoding
|
||||||
byte[] genesisBytes = Hex.decode(CPP_PoC5_GENESIS_HEX_RLP_ENCODED);
|
byte[] genesisBytes = Hex.decode(CPP_PoC5_GENESIS_HEX_RLP_ENCODED);
|
||||||
Block genesis = new Block(genesisBytes);
|
Block genesis = new Block(RLP.decode2(genesisBytes));
|
||||||
assertEquals(CPP_PoC5_GENESIS_HEX_HASH, Hex.toHexString(genesis.hash()));
|
assertEquals(CPP_PoC5_GENESIS_HEX_HASH, Hex.toHexString(genesis.getHash()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGenesisFromNew() {
|
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
|
/* From: https://ethereum.etherpad.mozilla.org/11
|
||||||
Genesis block is:
|
Genesis block is:
|
||||||
(
|
(
|
||||||
@ -149,32 +147,11 @@ public class BlockTest {
|
|||||||
B32(sha3(B(42)))
|
B32(sha3(B(42)))
|
||||||
)
|
)
|
||||||
*/
|
*/
|
||||||
|
Block genesis = new Genesis();
|
||||||
byte[] parentHash = new byte[32]; System.out.println(Hex.toHexString(parentHash));
|
assertEquals(CPP_PoC5_GENESIS_HEX_RLP_ENCODED, Hex.toHexString(genesis.getEncoded()));
|
||||||
byte[] unclesHash = HashUtil.sha3(new byte[0]); System.out.println(Hex.toHexString(unclesHash));
|
assertEquals(CPP_PoC5_GENESIS_HEX_HASH, Hex.toHexString(genesis.getHash()));
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 */
|
@Test /* create BlockData from part of real RLP BLOCKS message */
|
||||||
public void test3(){
|
public void test3(){
|
||||||
|
|
||||||
|
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user