Coverage Summary for Class: Constants (org.ethereum.config)
Class |
Class, %
|
Method, %
|
Line, %
|
Constants |
100%
(1/1)
|
48.5%
(16/33)
|
54%
(34/63)
|
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.config;
21
22 import co.rsk.bitcoinj.core.BtcECKey;
23 import co.rsk.config.*;
24 import co.rsk.core.BlockDifficulty;
25 import org.bouncycastle.util.encoders.Hex;
26 import org.ethereum.config.blockchain.upgrades.ActivationConfig;
27 import org.ethereum.config.blockchain.upgrades.ConsensusRule;
28
29 import java.math.BigDecimal;
30 import java.math.BigInteger;
31 import java.util.Arrays;
32 import java.util.List;
33
34 /**
35 * Describes different constants specific for a blockchain
36 */
37 public class Constants {
38 public static final byte MAINNET_CHAIN_ID = (byte) 30;
39 public static final byte TESTNET_CHAIN_ID = (byte) 31;
40 public static final byte DEVNET_CHAIN_ID = (byte) 32;
41 public static final byte REGTEST_CHAIN_ID = (byte) 33;
42
43 private static final byte[] FALLBACKMINING_PUBKEY_0 = Hex.decode("041e2b148c024770e19c4f31db2233cac791583df95b4d14a5e9fd4b38dc8254b3048f937f169446b19d2eca40db1dd93fab34c0cd8a310afd6e6211f9a89e4bca");
44 private static final byte[] FALLBACKMINING_PUBKEY_1 = Hex.decode("04b55031870df5de88bdb84f65bd1c6f8331c633e759caa5ac7cad3fa4f8a36791e995804bba1558ddcf330a67ff5bfa253fa1d8789735f97a97e849686527976e");
45 private static final BigInteger SECP256K1N = new BigInteger("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16);
46 private static final BigInteger TRANSACTION_GAS_CAP = BigDecimal.valueOf(Math.pow(2, 60)).toBigInteger();
47 private static final BigInteger RSKIP156_DIF_BOUND_DIVISOR = BigInteger.valueOf(400);
48
49 private final byte chainId;
50 private final boolean seedCowAccounts;
51 private final int durationLimit;
52 private final BlockDifficulty minimumDifficulty;
53 private final BlockDifficulty fallbackMiningDifficulty;
54 private final BigInteger difficultyBoundDivisor;
55 private final int newBlockMaxSecondsInTheFuture;
56 public final BridgeConstants bridgeConstants;
57
58 public Constants(
59 byte chainId,
60 boolean seedCowAccounts,
61 int durationLimit,
62 BlockDifficulty minimumDifficulty,
63 BlockDifficulty fallbackMiningDifficulty,
64 BigInteger difficultyBoundDivisor,
65 int newBlockMaxSecondsInTheFuture,
66 BridgeConstants bridgeConstants) {
67 this.chainId = chainId;
68 this.seedCowAccounts = seedCowAccounts;
69 this.durationLimit = durationLimit;
70 this.minimumDifficulty = minimumDifficulty;
71 this.fallbackMiningDifficulty = fallbackMiningDifficulty;
72 this.difficultyBoundDivisor = difficultyBoundDivisor;
73 this.newBlockMaxSecondsInTheFuture = newBlockMaxSecondsInTheFuture;
74 this.bridgeConstants = bridgeConstants;
75 }
76
77 public boolean seedCowAccounts() {
78 return seedCowAccounts;
79 }
80
81 // Average Time between blocks
82 public int getDurationLimit() {
83 return durationLimit;
84 }
85
86 public BlockDifficulty getMinimumDifficulty() {
87 return minimumDifficulty;
88 }
89
90 public BlockDifficulty getFallbackMiningDifficulty() {
91 return fallbackMiningDifficulty;
92 }
93
94 public BigInteger getDifficultyBoundDivisor(ActivationConfig.ForBlock activationConfig) {
95 // divisor used since inception until the RSKIP156
96 if (activationConfig.isActive(ConsensusRule.RSKIP156)
97 && getChainId() != Constants.REGTEST_CHAIN_ID) {
98 // Unless we are in regtest, this RSKIP increments the difficulty divisor from 50 to 400
99 return RSKIP156_DIF_BOUND_DIVISOR;
100 }
101 return difficultyBoundDivisor;
102 }
103
104 /**
105 * EIP155: https://github.com/ethereum/EIPs/issues/155
106 */
107 public byte getChainId() {
108 return chainId;
109 }
110
111 public int getNewBlockMaxSecondsInTheFuture() {
112 return newBlockMaxSecondsInTheFuture;
113 }
114
115 public BridgeConstants getBridgeConstants() {
116 return bridgeConstants;
117 }
118
119 public BigInteger getInitialNonce() {
120 return BigInteger.ZERO;
121 }
122
123 public byte[] getFallbackMiningPubKey0() {
124 return Arrays.copyOf(FALLBACKMINING_PUBKEY_0, FALLBACKMINING_PUBKEY_0.length);
125 }
126
127 public byte[] getFallbackMiningPubKey1() {
128 return Arrays.copyOf(FALLBACKMINING_PUBKEY_1, FALLBACKMINING_PUBKEY_1.length);
129 }
130
131 public int getMaximumExtraDataSize() {
132 return 32;
133 }
134
135 public int getMinGasLimit() {
136 return 3000000;
137 }
138
139 public int getGasLimitBoundDivisor() {
140 return 1024;
141 }
142
143 public int getExpDifficultyPeriod() {
144 return 100000;
145 }
146
147 public int getUncleGenerationLimit() {
148 return 7;
149 }
150
151 public int getUncleListLimit() {
152 return 10;
153 }
154
155 public int getBestNumberDiffLimit() {
156 return 100;
157 }
158
159 public BigInteger getMinimumPayableGas() {
160 return BigInteger.valueOf(200000);
161 }
162
163 public BigInteger getFederatorMinimumPayableGas() {
164 return BigInteger.valueOf(50000);
165 }
166
167 public static BigInteger getSECP256K1N() {
168 return SECP256K1N;
169 }
170
171 public static BigInteger getTransactionGasCap() {
172 return TRANSACTION_GAS_CAP;
173 }
174
175 public static int getMaxContractSize() {
176 return 0x6000;
177 }
178
179 public static int getMaxAddressByteLength() {
180 return 20;
181 }
182
183 public static long getMaxTimestampsDiffInSecs() {
184 return 300;
185 }
186
187 public static int getMaxBitcoinMergedMiningMerkleProofLength() {
188 return 960;
189 }
190
191 public static Constants mainnet() {
192 return new Constants(
193 MAINNET_CHAIN_ID,
194 false,
195 14,
196 new BlockDifficulty(BigInteger.valueOf((long) 14E15 / 2)),
197 new BlockDifficulty(BigInteger.valueOf((long) 14E15)),
198 BigInteger.valueOf(50),
199 60,
200 BridgeMainNetConstants.getInstance()
201 );
202 }
203
204 public static Constants devnetWithFederation(List<BtcECKey> federationPublicKeys) {
205 return new Constants(
206 DEVNET_CHAIN_ID,
207 false,
208 14,
209 new BlockDifficulty(BigInteger.valueOf(131072)),
210 new BlockDifficulty(BigInteger.valueOf((long) 14E15)),
211 BigInteger.valueOf(50),
212 540,
213 new BridgeDevNetConstants(federationPublicKeys)
214 );
215 }
216
217 public static Constants testnet() {
218 return new Constants(
219 TESTNET_CHAIN_ID,
220 false,
221 14,
222 new BlockDifficulty(BigInteger.valueOf(131072)),
223 new BlockDifficulty(BigInteger.valueOf((long) 14E15)),
224 BigInteger.valueOf(50),
225 540,
226 BridgeTestNetConstants.getInstance()
227 );
228 }
229
230 public static Constants regtest() {
231 return new Constants(
232 REGTEST_CHAIN_ID,
233 true,
234 10,
235 new BlockDifficulty(BigInteger.ONE),
236 BlockDifficulty.ZERO,
237 BigInteger.valueOf(2048),
238 0,
239 BridgeRegTestConstants.getInstance()
240 );
241 }
242
243 public static Constants regtestWithFederation(List<BtcECKey> genesisFederationPublicKeys) {
244 return new Constants(
245 REGTEST_CHAIN_ID,
246 true,
247 10,
248 new BlockDifficulty(BigInteger.ONE),
249 BlockDifficulty.ZERO,
250 BigInteger.valueOf(2048),
251 0,
252 new BridgeRegTestConstants(genesisFederationPublicKeys)
253 );
254 }
255 }