Coverage Summary for Class: BlockChainLoader (org.ethereum.core.genesis)
Class |
Class, %
|
Method, %
|
Line, %
|
BlockChainLoader |
100%
(1/1)
|
100%
(3/3)
|
84%
(21/25)
|
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.genesis;
21
22 import co.rsk.core.BlockDifficulty;
23 import co.rsk.core.bc.BlockChainImpl;
24 import co.rsk.core.bc.BlockExecutor;
25 import co.rsk.db.StateRootHandler;
26 import co.rsk.validators.BlockValidator;
27 import org.ethereum.core.Block;
28 import org.ethereum.core.Genesis;
29 import org.ethereum.core.TransactionPool;
30 import org.ethereum.db.BlockStore;
31 import org.ethereum.db.ReceiptStore;
32 import org.ethereum.listener.EthereumListener;
33 import org.ethereum.util.ByteUtil;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import java.util.ArrayList;
38
39 /**
40 * Created by mario on 13/01/17.
41 */
42 public class BlockChainLoader {
43
44 private static final Logger logger = LoggerFactory.getLogger("general");
45
46 private final BlockStore blockStore;
47 private final ReceiptStore receiptStore;
48 private final TransactionPool transactionPool;
49 private final EthereumListener listener;
50 private final BlockValidator blockValidator;
51 private final BlockExecutor blockExecutor;
52 private final Genesis genesis;
53 private final StateRootHandler stateRootHandler;
54
55 public BlockChainLoader(
56 BlockStore blockStore,
57 ReceiptStore receiptStore,
58 TransactionPool transactionPool,
59 EthereumListener listener,
60 BlockValidator blockValidator,
61 BlockExecutor blockExecutor,
62 Genesis genesis,
63 StateRootHandler stateRootHandler) {
64 this.blockStore = blockStore;
65 this.receiptStore = receiptStore;
66 this.transactionPool = transactionPool;
67 this.listener = listener;
68 this.blockValidator = blockValidator;
69 this.blockExecutor = blockExecutor;
70 this.genesis = genesis;
71 this.stateRootHandler = stateRootHandler;
72 }
73
74 public BlockChainImpl loadBlockchain() {
75 BlockDifficulty totalDifficulty;
76 Block bestBlock = blockStore.getBestBlock();
77 if (bestBlock == null) {
78 logger.info("DB is empty - adding Genesis");
79
80 bestBlock = genesis;
81 totalDifficulty = genesis.getCumulativeDifficulty();
82
83 listener.onBlock(genesis, new ArrayList<>());
84
85 logger.info("Genesis block loaded");
86 } else {
87 totalDifficulty = blockStore.getTotalDifficultyForHash(bestBlock.getHash().getBytes());
88
89 logger.info("*** Loaded up to block [{}] totalDifficulty [{}] with stateRoot [{}]",
90 bestBlock.getNumber(),
91 totalDifficulty,
92 ByteUtil.toHexString(bestBlock.getStateRoot()));
93 }
94
95 BlockChainImpl blockchain = new BlockChainImpl(
96 blockStore,
97 receiptStore,
98 transactionPool,
99 listener,
100 blockValidator,
101 blockExecutor,
102 stateRootHandler
103 );
104 blockchain.setStatus(bestBlock, totalDifficulty);
105
106 return blockchain;
107 }
108 }