Coverage Summary for Class: EvmModuleImpl (co.rsk.rpc.modules.evm)
Class |
Class, %
|
Method, %
|
Line, %
|
EvmModuleImpl |
0%
(0/1)
|
0%
(0/9)
|
0%
(0/30)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2018 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package co.rsk.rpc.modules.evm;
20
21 import co.rsk.core.SnapshotManager;
22 import co.rsk.mine.MinerClient;
23 import co.rsk.mine.MinerClock;
24 import co.rsk.mine.MinerManager;
25 import co.rsk.mine.MinerServer;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import static org.ethereum.rpc.TypeConverter.*;
30 import static org.ethereum.rpc.exception.RskJsonRpcRequestException.*;
31
32 public class EvmModuleImpl implements EvmModule {
33 private static final Logger logger = LoggerFactory.getLogger("web3");
34
35 private final MinerManager minerManager;
36 private final MinerServer minerServer;
37 private final MinerClient minerClient;
38 private final MinerClock minerClock;
39 private final SnapshotManager snapshotManager;
40
41 public EvmModuleImpl(
42 MinerServer minerServer,
43 MinerClient minerClient,
44 MinerClock minerClock,
45 SnapshotManager snapshotManager) {
46 this.minerManager = new MinerManager();
47 this.minerServer = minerServer;
48 this.minerClient = minerClient;
49 this.minerClock = minerClock;
50 this.snapshotManager = snapshotManager;
51 }
52
53 @Override
54 public String evm_snapshot() {
55 int snapshotId = snapshotManager.takeSnapshot();
56 logger.debug("evm_snapshot(): {}", snapshotId);
57 return toQuantityJsonHex(snapshotId);
58 }
59
60 @Override
61 public boolean evm_revert(String snapshotId) {
62 try {
63 int sid = stringHexToBigInteger(snapshotId).intValue();
64 return snapshotManager.revertToSnapshot(sid);
65 } catch (NumberFormatException | StringIndexOutOfBoundsException e) {
66 throw invalidParamError("invalid snapshot id " + snapshotId, e);
67 } finally {
68 logger.debug("evm_revert({})", snapshotId);
69 }
70 }
71
72 @Override
73 public void evm_reset() {
74 snapshotManager.resetSnapshots();
75 logger.debug("evm_reset()");
76 }
77
78 @Override
79 public void evm_mine() {
80 minerManager.mineBlock(minerClient, minerServer);
81 logger.debug("evm_mine()");
82 }
83
84 @Override
85 public void evm_startMining() {
86 minerServer.start();
87 logger.debug("evm_startMining()");
88 }
89
90 @Override
91 public void evm_stopMining() {
92 minerServer.stop();
93 logger.debug("evm_stopMining()");
94 }
95
96 @Override
97 public String evm_increaseTime(String seconds) {
98 try {
99 long nseconds = stringNumberAsBigInt(seconds).longValue();
100 String result = toQuantityJsonHex(minerClock.increaseTime(nseconds));
101 logger.debug("evm_increaseTime({}): {}", nseconds, result);
102 return result;
103 } catch (NumberFormatException | StringIndexOutOfBoundsException e) {
104 throw invalidParamError("invalid number of seconds " + seconds, e);
105 }
106 }
107 }