Coverage Summary for Class: Genesis (org.ethereum.core)

Class Class, % Method, % Line, %
Genesis 100% (1/1) 85.7% (6/7) 83.3% (15/18)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) 5  * 6  * This program is free software: you can redistribute it and/or modify 7  * it under the terms of the GNU Lesser General Public License as published by 8  * the Free Software Foundation, either version 3 of the License, or 9  * (at your option) any later version. 10  * 11  * This program is distributed in the hope that it will be useful, 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14  * GNU Lesser General Public License for more details. 15  * 16  * You should have received a copy of the GNU Lesser General Public License 17  * along with this program. If not, see <http://www.gnu.org/licenses/>. 18  */ 19  20 package org.ethereum.core; 21  22 import co.rsk.core.RskAddress; 23 import co.rsk.crypto.Keccak256; 24 import org.ethereum.vm.DataWord; 25  26 import java.util.Arrays; 27 import java.util.Collections; 28 import java.util.Map; 29  30 /** 31  * The genesis block is the first block in the chain and has fixed values according to 32  * the protocol specification. The genesis block is 13 items, and is specified thus: 33  * <p> 34  * ( zerohash_256 , SHA3 RLP () , zerohash_160 , stateRoot, 0, 2^22 , 0, 0, 1000000, 0, 0, 0, SHA3 (42) , (), () ) 35  * <p> 36  * - Where zerohash_256 refers to the parent hash, a 256-bit hash which is all zeroes; 37  * - zerohash_160 refers to the coinbase address, a 160-bit hash which is all zeroes; 38  * - 2^22 refers to the difficulty; 39  * - 0 refers to the timestamp (the Unix epoch); 40  * - the transaction trie root and extradata are both 0, being equivalent to the empty byte array. 41  * - The sequences of both uncles and transactions are empty and represented by (). 42  * - SHA3 (42) refers to the SHA3 hash of a byte array of length one whose first and only byte is of value 42. 43  * - SHA3 RLP () value refers to the hash of the uncle lists in RLP, both empty lists. 44  * <p> 45  * See Yellow Paper: http://www.gavwood.com/Paper.pdf (Appendix I. Genesis Block) 46  */ 47 public class Genesis extends Block { 48  49  private final Map<RskAddress, AccountState> initialAccounts; 50  private final Map<RskAddress, byte[]> initialCodes; 51  private final Map<RskAddress, Map<DataWord, byte[]>> initialStorages; 52  53  private static final byte[] ZERO_HASH_2048 = new byte[256]; 54  protected static final long NUMBER = 0; 55  56  public Genesis(boolean isRskip126Enabled, 57  Map<RskAddress, AccountState> initialAccounts, 58  Map<RskAddress, byte[]> initialCodes, 59  Map<RskAddress, Map<DataWord, byte[]>> initialStorages, 60  BlockHeader header){ 61  super( 62  header, 63  Collections.emptyList(), 64  Collections.emptyList(), 65  isRskip126Enabled, 66  false 67  ); 68  if (!initialAccounts.keySet().containsAll(initialCodes.keySet())) { 69  throw new IllegalArgumentException("Code must have an associated account"); 70  } 71  if (!initialAccounts.keySet().containsAll(initialStorages.keySet())) { 72  throw new IllegalArgumentException("Storage must have an associated account"); 73  } 74  this.initialAccounts = Collections.unmodifiableMap(initialAccounts); 75  this.initialCodes = Collections.unmodifiableMap(initialCodes); 76  this.initialStorages = Collections.unmodifiableMap(initialStorages); 77  setTransactionsList(Collections.emptyList()); 78  } 79  80  public static byte[] getZeroHash(){ 81  return Arrays.copyOf(ZERO_HASH_2048, ZERO_HASH_2048.length); 82  } 83  84  /** 85  * WORKAROUND. 86  * This is overrode because the Genesis' parent hash is an empty byte array, 87  * which isn't a valid Keccak256 hash. 88  * For encoding purposes, the empty byte array is used instead. 89  */ 90  @Override 91  public Keccak256 getParentHash() { 92  return Keccak256.ZERO_HASH; 93  } 94  95  public Map<RskAddress, AccountState> getAccounts() { 96  return initialAccounts; 97  } 98  99  public Map<RskAddress, byte[]> getCodes() { 100  return initialCodes; 101  } 102  103  public Map<RskAddress, Map<DataWord, byte[]>> getStorages() { 104  return initialStorages; 105  } 106  107 }