From 0b8f2b06f405f3f0fe5febceb01a7e65e38f3ae7 Mon Sep 17 00:00:00 2001 From: nicksavers Date: Thu, 30 Oct 2014 21:04:37 +0100 Subject: [PATCH] Revert to 0-, ''- and empty list hashes --- .../java/org/ethereum/core/AccountState.java | 19 +++++-------------- .../java/org/ethereum/core/BlockHeader.java | 11 +++++------ .../main/java/org/ethereum/core/Genesis.java | 11 +++++------ .../java/org/ethereum/crypto/HashUtil.java | 1 + .../main/java/org/ethereum/trie/TrieImpl.java | 3 +-- .../main/java/org/ethereum/util/ByteUtil.java | 4 ---- .../java/org/ethereum/core/BlockTest.java | 4 ++-- .../test/java/org/ethereum/trie/TrieTest.java | 3 ++- 8 files changed, 21 insertions(+), 35 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/core/AccountState.java b/ethereumj-core/src/main/java/org/ethereum/core/AccountState.java index 02f18c33..bd26f336 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/AccountState.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/AccountState.java @@ -1,14 +1,9 @@ package org.ethereum.core; -import static org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY; import static org.ethereum.crypto.HashUtil.EMPTY_DATA_HASH; -import static org.ethereum.util.ByteUtil.EMTPY_SHA3_RLP_ELEMENT_HASH; -import static org.ethereum.util.ByteUtil.EMTPY_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; @@ -35,7 +30,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_DATA_HASH; /* The hash of the EVM code of this contract—this is the code * that gets executed should this address receive a message call; @@ -43,7 +38,7 @@ public class AccountState { * after construction. All such code fragments are contained in * the state database under their corresponding hashes for later * retrieval */ - private byte[] codeHash = EMTPY_SHA3_RLP_ELEMENT_HASH; + private byte[] codeHash = EMPTY_DATA_HASH; public AccountState() { this(BigInteger.ZERO, BigInteger.ZERO); @@ -121,13 +116,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; } } diff --git a/ethereumj-core/src/main/java/org/ethereum/core/BlockHeader.java b/ethereumj-core/src/main/java/org/ethereum/core/BlockHeader.java index dab51011..57725cf0 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/BlockHeader.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/BlockHeader.java @@ -3,14 +3,13 @@ package org.ethereum.core; import java.math.BigInteger; import static org.ethereum.util.ByteUtil.*; +import static org.ethereum.crypto.HashUtil.*; 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 @@ -78,11 +77,11 @@ public class BlockHeader { this.txTrieRoot = ((RLPItem) rlpHeader.get(4)).getRLPData(); if(this.txTrieRoot == null) - this.txTrieRoot = EMTPY_SHA3_RLP_ELEMENT_HASH; + this.txTrieRoot = EMPTY_DATA_HASH; this.recieptTrieRoot = ((RLPItem) rlpHeader.get(5)).getRLPData(); if(this.recieptTrieRoot == null) - this.recieptTrieRoot = EMTPY_SHA3_RLP_ELEMENT_HASH; + this.recieptTrieRoot = EMPTY_DATA_HASH; this.logsBloom = ((RLPItem) rlpHeader.get(6)).getRLPData(); this.difficulty = ((RLPItem) rlpHeader.get(7)).getRLPData(); @@ -290,10 +289,10 @@ public class BlockHeader { byte[] stateRoot = RLP.encodeElement(this.stateRoot); - if (txTrieRoot == null) this.txTrieRoot = ByteUtil.EMTPY_TRIE_HASH; + if (txTrieRoot == null) this.txTrieRoot = EMPTY_DATA_HASH; byte[] txTrieRoot = RLP.encodeElement(this.txTrieRoot); - if (recieptTrieRoot == null) this.recieptTrieRoot = ByteUtil.EMTPY_TRIE_HASH; + if (recieptTrieRoot == null) this.recieptTrieRoot = EMPTY_DATA_HASH; byte[] recieptTrieRoot = RLP.encodeElement(this.recieptTrieRoot); byte[] logsBloom = RLP.encodeElement(this.logsBloom); diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Genesis.java b/ethereumj-core/src/main/java/org/ethereum/core/Genesis.java index d421cf02..6ca12cc2 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Genesis.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Genesis.java @@ -1,11 +1,10 @@ package org.ethereum.core; -import org.ethereum.crypto.HashUtil; -import org.ethereum.crypto.SHA3Helper; +import static org.ethereum.crypto.HashUtil.sha3; +import static org.ethereum.crypto.HashUtil.EMPTY_LIST_HASH; + 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 java.math.BigInteger; @@ -47,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 = ByteUtil.EMTPY_SHA3_RLP_ELEMENT_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(); @@ -57,7 +56,7 @@ public class Genesis extends Block { public static long GAS_USED = 0; public static long TIMESTAMP = 0; public static byte[] EXTRA_DATA = new byte[0]; - public static byte[] NONCE = HashUtil.sha3(new byte[]{42}); + public static byte[] NONCE = sha3(new byte[]{42}); private static Block instance; diff --git a/ethereumj-core/src/main/java/org/ethereum/crypto/HashUtil.java b/ethereumj-core/src/main/java/org/ethereum/crypto/HashUtil.java index 1eff7321..b21bc138 100644 --- a/ethereumj-core/src/main/java/org/ethereum/crypto/HashUtil.java +++ b/ethereumj-core/src/main/java/org/ethereum/crypto/HashUtil.java @@ -17,6 +17,7 @@ public class HashUtil { private static final int MAX_ENTRIES = 100; // Should contain most commonly hashed values private static LRUMap sha3Cache = new LRUMap<>(0, MAX_ENTRIES); public static final byte[] EMPTY_DATA_HASH = Hex.decode("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"); + public static final byte[] EMPTY_LIST_HASH = Hex.decode("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"); private static final MessageDigest sha256digest; diff --git a/ethereumj-core/src/main/java/org/ethereum/trie/TrieImpl.java b/ethereumj-core/src/main/java/org/ethereum/trie/TrieImpl.java index a160ed5c..bf3049b6 100644 --- a/ethereumj-core/src/main/java/org/ethereum/trie/TrieImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/trie/TrieImpl.java @@ -11,7 +11,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; @@ -155,7 +154,7 @@ public class TrieImpl implements Trie { if (root == null || (root instanceof byte[] && ((byte[]) root).length == 0) || (root instanceof String && "".equals((String) root))) { - return ByteUtil.EMTPY_TRIE_HASH; + return HashUtil.EMPTY_DATA_HASH; } else if (root instanceof byte[]) { return (byte[]) this.getRoot(); } else { diff --git a/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java b/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java index 94a6069f..b4cc298b 100644 --- a/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java +++ b/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java @@ -6,15 +6,11 @@ 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 { public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; - public static final byte[] EMTPY_SHA3_RLP_ELEMENT_HASH = SHA3Helper.sha3(""); - public static final byte[] EMTPY_TRIE_HASH = SHA3Helper.sha3(RLP.encodeElement(ByteUtil.EMPTY_BYTE_ARRAY)); /** * Creates a copy of bytes and appends b to the end of it diff --git a/ethereumj-core/src/test/java/org/ethereum/core/BlockTest.java b/ethereumj-core/src/test/java/org/ethereum/core/BlockTest.java index 6a1e94be..3d2d1d2a 100644 --- a/ethereumj-core/src/test/java/org/ethereum/core/BlockTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/core/BlockTest.java @@ -30,8 +30,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 = "f8abf8a7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a0477fd4d3a3bae60913bbb0cbffbc1aae5c57a60f6351f9ed702d97378f851fc680830200008080830f4240808080a004994f67dc55b09e814ab7ffc8df3686b4afb2bb53e60eae97ef043fe03fb829c0c0"; + private String PoC7_GENESIS_HEX_HASH = "c4bcce917758c95695b8a368f920d166b4f9b3572c54ff40385ef84ea6850099"; String block_2 = "f8b5f8b1a0cf4b25b08b39350304fe12a16e4216c01a426f8f3dbf0d392b5b45" + "8ffb6a399da01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a1" diff --git a/ethereumj-core/src/test/java/org/ethereum/trie/TrieTest.java b/ethereumj-core/src/test/java/org/ethereum/trie/TrieTest.java index 293dfbf0..1e64d57f 100644 --- a/ethereumj-core/src/test/java/org/ethereum/trie/TrieTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/trie/TrieTest.java @@ -15,6 +15,7 @@ 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.json.simple.JSONArray; @@ -29,7 +30,7 @@ import org.spongycastle.util.encoders.Hex; public class TrieTest { private static String LONG_STRING = "1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ"; - private static String ROOT_HASH_EMPTY = ""; + private static String ROOT_HASH_EMPTY = Hex.toHexString(HashUtil.EMPTY_DATA_HASH); private static String c = "c"; private static String ca = "ca";