Use original uncleHash for genesis

This commit is contained in:
nicksavers 2014-11-04 00:17:20 +01:00
parent db1388a279
commit 555cbb8bbe
9 changed files with 40 additions and 57 deletions

View File

@ -1,13 +1,10 @@
package org.ethereum.core;
import static org.ethereum.crypto.HashUtil.EMTPY_TRIE_HASH;
import static org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY;
import static org.ethereum.crypto.HashUtil.EMPTY_DATA_HASH;
import static org.ethereum.crypto.HashUtil.EMPTY_TRIE_HASH;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPList;
import org.spongycastle.util.Arrays;
import org.spongycastle.util.encoders.Hex;
import java.math.BigInteger;
@ -34,7 +31,7 @@ public class AccountState {
* I define a convenient equivalence TRIE (σ[a] s ) σ[a] s .
* It shall be understood that σ[a] s is not a physical member
* of the account and does not contribute to its later serialisation */
private byte[] stateRoot = EMTPY_TRIE_HASH;
private byte[] stateRoot = EMPTY_TRIE_HASH;
/* The hash of the EVM code of this contractthis is the code
* that gets executed should this address receive a message call;
@ -57,10 +54,10 @@ public class AccountState {
this.rlpEncoded = rlpData;
RLPList items = (RLPList) RLP.decode2(rlpEncoded).get(0);
this.nonce = new BigInteger(1, ((items.get(0).getRLPData()) == null ? new byte[]{0} :
items.get(0).getRLPData()));
this.balance = new BigInteger(1, ((items.get(1).getRLPData()) == null ? new byte[]{0} :
items.get(1).getRLPData()));
this.nonce = items.get(0).getRLPData() == null ? BigInteger.ZERO
: new BigInteger(1, items.get(0).getRLPData());
this.balance = items.get(1).getRLPData() == null ? BigInteger.ZERO
: new BigInteger(1, items.get(1).getRLPData());
this.stateRoot = items.get(2).getRLPData();
this.codeHash = items.get(3).getRLPData();
}
@ -120,13 +117,9 @@ public class AccountState {
public String toString() {
String ret = "Nonce: " + this.getNonce().toString() + "\n" +
"Balance: " + Denomination.toFriendlyString(getBalance()) + "\n";
if(this.getStateRoot()!= null && !Arrays.areEqual(this.getStateRoot(), EMPTY_BYTE_ARRAY))
ret += "State Root: " + Hex.toHexString(this.getStateRoot()) + "\n";
if(this.getCodeHash() != null && !Arrays.areEqual(this.getCodeHash(), EMPTY_DATA_HASH))
ret += "Code Hash: " + Hex.toHexString(this.getCodeHash());
"Balance: " + Denomination.toFriendlyString(getBalance()) + "\n" +
"State Root: " + Hex.toHexString(this.getStateRoot()) + "\n" +
"Code Hash: " + Hex.toHexString(this.getCodeHash());
return ret;
}
}

View File

@ -2,17 +2,14 @@ package org.ethereum.core;
import java.math.BigInteger;
import static org.ethereum.crypto.HashUtil.EMPTY_DATA_HASH;
import static org.ethereum.crypto.HashUtil.EMTPY_TRIE_HASH;
import static org.ethereum.crypto.HashUtil.EMPTY_TRIE_HASH;
import static org.ethereum.util.ByteUtil.*;
import org.ethereum.crypto.HashUtil;
import org.ethereum.crypto.SHA3Helper;
import org.ethereum.manager.WorldManager;
import org.ethereum.util.*;
import org.spongycastle.util.Arrays;
import org.spongycastle.util.BigIntegers;
import org.spongycastle.util.encoders.Hex;
/**
* Block header is a value object containing
@ -80,11 +77,11 @@ public class BlockHeader {
this.txTrieRoot = ((RLPItem) rlpHeader.get(4)).getRLPData();
if(this.txTrieRoot == null)
this.txTrieRoot = EMTPY_TRIE_HASH;
this.txTrieRoot = EMPTY_TRIE_HASH;
this.recieptTrieRoot = ((RLPItem) rlpHeader.get(5)).getRLPData();
if(this.recieptTrieRoot == null)
this.recieptTrieRoot = EMPTY_DATA_HASH;
this.recieptTrieRoot = EMPTY_TRIE_HASH;
this.logsBloom = ((RLPItem) rlpHeader.get(6)).getRLPData();
this.difficulty = ((RLPItem) rlpHeader.get(7)).getRLPData();
@ -292,10 +289,10 @@ public class BlockHeader {
byte[] stateRoot = RLP.encodeElement(this.stateRoot);
if (txTrieRoot == null) this.txTrieRoot = EMTPY_TRIE_HASH;
if (txTrieRoot == null) this.txTrieRoot = EMPTY_TRIE_HASH;
byte[] txTrieRoot = RLP.encodeElement(this.txTrieRoot);
if (recieptTrieRoot == null) this.recieptTrieRoot = EMTPY_TRIE_HASH;
if (recieptTrieRoot == null) this.recieptTrieRoot = EMPTY_TRIE_HASH;
byte[] recieptTrieRoot = RLP.encodeElement(this.recieptTrieRoot);
byte[] logsBloom = RLP.encodeElement(this.logsBloom);

View File

@ -1,17 +1,13 @@
package org.ethereum.core;
import org.ethereum.crypto.HashUtil;
import org.ethereum.crypto.SHA3Helper;
import org.ethereum.trie.Trie;
import org.ethereum.trie.TrieImpl;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.spongycastle.util.encoders.Hex;
import static org.ethereum.crypto.HashUtil.EMPTY_LIST_HASH;
import static org.ethereum.crypto.HashUtil.sha3;
import java.math.BigInteger;
import static org.ethereum.crypto.HashUtil.*;
import static org.ethereum.crypto.HashUtil.EMPTY_DATA_HASH;
import org.ethereum.trie.Trie;
import org.ethereum.trie.TrieImpl;
import org.spongycastle.util.encoders.Hex;
/**
* The genesis block is the first block in the chain and has fixed values according to
@ -50,7 +46,7 @@ public class Genesis extends Block {
private static byte[] zeroHash512 = new byte[64];
public static byte[] PARENT_HASH = zeroHash256;
public static byte[] UNCLES_HASH = EMPTY_DATA_HASH;
public static byte[] UNCLES_HASH = EMPTY_LIST_HASH;
public static byte[] COINBASE = zeroHash160;
public static byte[] LOG_BLOOM = zeroHash512;
public static byte[] DIFFICULTY = BigInteger.valueOf(2).pow(17).toByteArray();
@ -73,8 +69,7 @@ public class Genesis extends Block {
// The proof-of-concept series include a development pre-mine, making the state root hash
// some value stateRoot. The latest documentation should be consulted for the value of the state root.
for (String address : premine) {
AccountState acctState = new AccountState();
acctState.addToBalance(PREMINE_AMOUNT);
AccountState acctState = new AccountState(BigInteger.ZERO, PREMINE_AMOUNT);
state.update(Hex.decode(address), acctState.getEncoded());
}

View File

@ -6,8 +6,9 @@ import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import static org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.ethereum.util.Utils;
import org.spongycastle.crypto.Digest;
@ -19,8 +20,9 @@ public class HashUtil {
private static final int MAX_ENTRIES = 100; // Should contain most commonly hashed values
private static LRUMap<ByteArrayWrapper, byte[]> sha3Cache = new LRUMap<>(0, MAX_ENTRIES);
public static final byte[] EMPTY_DATA_HASH = SHA3Helper.sha3("");
public static final byte[] EMTPY_TRIE_HASH = SHA3Helper.sha3(RLP.encodeElement(ByteUtil.EMPTY_BYTE_ARRAY));
public static final byte[] EMPTY_DATA_HASH = sha3(EMPTY_BYTE_ARRAY);
public static final byte[] EMPTY_LIST_HASH = sha3(RLP.encodeList());
public static final byte[] EMPTY_TRIE_HASH = sha3(RLP.encodeElement(EMPTY_BYTE_ARRAY));
private static final MessageDigest sha256digest;

View File

@ -1,7 +1,7 @@
package org.ethereum.trie;
import static java.util.Arrays.copyOfRange;
import static org.ethereum.crypto.HashUtil.EMTPY_TRIE_HASH;
import static org.ethereum.crypto.HashUtil.EMPTY_TRIE_HASH;
import static org.ethereum.util.ByteUtil.matchingNibbleLength;
import static org.ethereum.util.CompactEncoder.binToNibbles;
import static org.ethereum.util.CompactEncoder.packNibbles;
@ -12,7 +12,6 @@ import java.util.*;
import org.ethereum.crypto.HashUtil;
import org.ethereum.db.ByteArrayWrapper;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.Value;
import org.iq80.leveldb.DB;
import org.slf4j.Logger;
@ -156,7 +155,7 @@ public class TrieImpl implements Trie {
if (root == null
|| (root instanceof byte[] && ((byte[]) root).length == 0)
|| (root instanceof String && "".equals((String) root))) {
return EMTPY_TRIE_HASH;
return EMPTY_TRIE_HASH;
} else if (root instanceof byte[]) {
return (byte[]) this.getRoot();
} else {

View File

@ -6,8 +6,6 @@ import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.ethereum.crypto.SHA3Helper;
import org.ethereum.trie.TrieImpl;
import org.spongycastle.util.encoders.Hex;
public class ByteUtil {

View File

@ -13,7 +13,7 @@ public class AccountStateTest {
public void testGetEncoded() {
String expected = "f85e809"
+ "a0100000000000000000000000000000000000000000000000000"
+ "a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
+ "a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
+ "a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470";
AccountState acct = new AccountState(BigInteger.ZERO, BigInteger.valueOf(2).pow(200));
assertEquals(expected, Hex.toHexString(acct.getEncoded()));

View File

@ -15,8 +15,7 @@ import static org.junit.Assert.*;
public class BlockTest {
private static final Logger logger = LoggerFactory.getLogger("test");
private static final Logger logger = LoggerFactory.getLogger("test");
@BeforeClass
public static void setUp() {
@ -30,8 +29,8 @@ public class BlockTest {
}
// https://ethereum.etherpad.mozilla.org/12
private String PoC7_GENESIS_HEX_RLP_ENCODED = "f9012ff9012aa00000000000000000000000000000000000000000000000000000000000000000a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470940000000000000000000000000000000000000000a0c67c70f5d7d3049337d1dcc0503a249881120019a8e7322774dbfe57b463718ca056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b84000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000830200008080830f4240808080a004994f67dc55b09e814ab7ffc8df3686b4afb2bb53e60eae97ef043fe03fb829c0c0";
private String PoC7_GENESIS_HEX_HASH = "19039f385f269fae48d497fba579d7079c761cec5643ec1096e98bdda4ffcf00";
private String PoC7_GENESIS_HEX_RLP_ENCODED = "f9012ff9012aa00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a0c67c70f5d7d3049337d1dcc0503a249881120019a8e7322774dbfe57b463718ca056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b84000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000830200008080830f4240808080a004994f67dc55b09e814ab7ffc8df3686b4afb2bb53e60eae97ef043fe03fb829c0c0";
private String PoC7_GENESIS_HEX_HASH = "955f36d073ccb026b78ab3424c15cf966a7563aa270413859f78702b9e8e22cb";
String block_2 = "f8b5f8b1a0cf4b25b08b39350304fe12a16e4216c01a426f8f3dbf0d392b5b45"
+ "8ffb6a399da01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a1"
@ -52,12 +51,14 @@ public class BlockTest {
+ "7dedb91a07b72f34074e76a00cd22d78d556175604407dc6109797f5c8d990d05f1b352e"
+ "10c71b3dd74bc70f8201f4c0";
String block_32 = "f8b5f8b1a00a312c2b0a8f125c60a3976b6e508e740e095eb59943988d9bbfb8"
String block_32 = "f8f8f8f4a00a312c2b0a8f125c60a3976b6e508e740e095eb59943988d9bbfb8"
+ "aa43922e31a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4"
+ "934794e559de5527492bcb42ec68d07df0742a98ec3f1ea050188ab86bdf164ac90eb283"
+ "5a04a8930aae5393c3a2ef1166fb95028f9456b880833ee248208609184e72a000830eca"
+ "0080845387fd2080a00000000000000000000000000000000000000000000000001f52eb"
+ "b192c4ea97c0c0";
+ "5a04a8930aae5393c3a2ef1166fb95028f9456b88080b840000000000000000000000000"
+ "000000000000000000000000000000000000000000000000000000000000000000000000"
+ "00000000000000000000000000000000833ee248208609184e72a000830eca0080845387"
+ "fd2080a00000000000000000000000000000000000000000000000001f52ebb192c4ea97"
+ "c0c0";
@Test /* got from go guy */
public void testGenesisFromRLP() {

View File

@ -1,6 +1,6 @@
package org.ethereum.trie;
import static org.ethereum.crypto.HashUtil.EMTPY_TRIE_HASH;
import static org.ethereum.crypto.HashUtil.EMPTY_TRIE_HASH;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
@ -16,10 +16,8 @@ import java.nio.file.Files;
import java.util.*;
import org.ethereum.core.AccountState;
import org.ethereum.crypto.HashUtil;
import org.ethereum.db.DatabaseImpl;
import org.ethereum.db.MockDB;
import org.ethereum.util.ByteUtil;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
@ -32,7 +30,7 @@ import org.spongycastle.util.encoders.Hex;
public class TrieTest {
private static String LONG_STRING = "1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ";
private static String ROOT_HASH_EMPTY = Hex.toHexString(EMTPY_TRIE_HASH);
private static String ROOT_HASH_EMPTY = Hex.toHexString(EMPTY_TRIE_HASH);
private static String c = "c";
private static String ca = "ca";