Coverage Summary for Class: ReversibleTransactionExecutor (co.rsk.core)
Class |
Method, %
|
Line, %
|
ReversibleTransactionExecutor |
100%
(3/3)
|
100%
(14/14)
|
ReversibleTransactionExecutor$MockitoMock$21663227 |
ReversibleTransactionExecutor$MockitoMock$21663227$auxiliary$1aOVpBcF |
ReversibleTransactionExecutor$MockitoMock$21663227$auxiliary$DaxnZugk |
ReversibleTransactionExecutor$MockitoMock$21663227$auxiliary$F7jfb1A6 |
ReversibleTransactionExecutor$MockitoMock$21663227$auxiliary$Um85deVE |
ReversibleTransactionExecutor$UnsignedTransaction |
66.7%
(2/3)
|
80%
(4/5)
|
Total |
83.3%
(5/6)
|
94.7%
(18/19)
|
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 co.rsk.core;
21
22 import co.rsk.db.RepositoryLocator;
23 import co.rsk.db.RepositorySnapshot;
24 import org.ethereum.core.Block;
25 import org.ethereum.core.Repository;
26 import org.ethereum.core.Transaction;
27 import org.ethereum.core.TransactionExecutor;
28 import org.ethereum.vm.program.ProgramResult;
29
30 /**
31 * Encapsulates the logic to execute a transaction in an
32 * isolated environment (e.g. no persistent state changes).
33 */
34 public class ReversibleTransactionExecutor {
35 private final RepositoryLocator repositoryLocator;
36 private final TransactionExecutorFactory transactionExecutorFactory;
37
38 public ReversibleTransactionExecutor(
39 RepositoryLocator repositoryLocator,
40 TransactionExecutorFactory transactionExecutorFactory) {
41 this.repositoryLocator = repositoryLocator;
42 this.transactionExecutorFactory = transactionExecutorFactory;
43 }
44
45 public ProgramResult executeTransaction(
46 Block executionBlock,
47 RskAddress coinbase,
48 byte[] gasPrice,
49 byte[] gasLimit,
50 byte[] toAddress,
51 byte[] value,
52 byte[] data,
53 RskAddress fromAddress) {
54 return executeTransaction_workaround(
55 repositoryLocator.snapshotAt(executionBlock.getHeader()),
56 executionBlock,
57 coinbase,
58 gasPrice,
59 gasLimit,
60 toAddress,
61 value,
62 data,
63 fromAddress
64 );
65 }
66
67 @Deprecated
68 public ProgramResult executeTransaction_workaround(
69 RepositorySnapshot snapshot,
70 Block executionBlock,
71 RskAddress coinbase,
72 byte[] gasPrice,
73 byte[] gasLimit,
74 byte[] toAddress,
75 byte[] value,
76 byte[] data,
77 RskAddress fromAddress) {
78 Repository track = snapshot.startTracking();
79
80 byte[] nonce = track.getNonce(fromAddress).toByteArray();
81 UnsignedTransaction tx = new UnsignedTransaction(
82 nonce,
83 gasPrice,
84 gasLimit,
85 toAddress,
86 value,
87 data,
88 fromAddress
89 );
90
91 TransactionExecutor executor = transactionExecutorFactory
92 .newInstance(tx, 0, coinbase, track, executionBlock, 0)
93 .setLocalCall(true);
94
95 executor.executeTransaction();
96
97 return executor.getResult();
98 }
99
100 private static class UnsignedTransaction extends Transaction {
101
102 private UnsignedTransaction(
103 byte[] nonce,
104 byte[] gasPrice,
105 byte[] gasLimit,
106 byte[] receiveAddress,
107 byte[] value,
108 byte[] data,
109 RskAddress fromAddress) {
110 super(nonce, gasPrice, gasLimit, receiveAddress, value, data);
111 this.sender = fromAddress;
112 }
113
114 @Override
115 public boolean acceptTransactionSignature(byte chainId) {
116 // We only allow executing unsigned transactions
117 // in the context of a reversible transaction execution.
118 return true;
119 }
120 }
121 }