Coverage Summary for Class: RemascFeesPayer (co.rsk.remasc)
Class |
Class, %
|
Method, %
|
Line, %
|
RemascFeesPayer |
0%
(0/1)
|
0%
(0/5)
|
0%
(0/21)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 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.remasc;
20
21 import co.rsk.core.Coin;
22 import co.rsk.core.RskAddress;
23 import co.rsk.rpc.modules.trace.CallType;
24 import co.rsk.rpc.modules.trace.ProgramSubtrace;
25 import org.ethereum.core.Repository;
26 import org.ethereum.util.RLP;
27 import org.ethereum.vm.DataWord;
28 import org.ethereum.vm.LogInfo;
29 import org.ethereum.vm.program.ProgramResult;
30 import org.ethereum.vm.program.invoke.TransferInvoke;
31
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36
37 /**
38 * Knows how to transfer funds between accounts and how to log that transaction.
39 *
40 * @author martin.medina
41 */
42 class RemascFeesPayer {
43
44 private final Repository repository;
45 private final RskAddress contractAddress;
46 private final List<ProgramSubtrace> subtraces = new ArrayList<>();
47
48 public RemascFeesPayer(Repository repository, RskAddress contractAddress) {
49 this.repository = repository;
50 this.contractAddress = contractAddress;
51 }
52
53 public List<ProgramSubtrace> getSubtraces() { return Collections.unmodifiableList(this.subtraces); }
54
55 public void payMiningFees(byte[] blockHash, Coin value, RskAddress toAddress, List<LogInfo> logs) {
56 this.transferPayment(value, toAddress);
57 this.logPayment(blockHash, value, toAddress, logs);
58 }
59
60 private void transferPayment(Coin value, RskAddress toAddress) {
61 this.repository.addBalance(contractAddress, value.negate());
62 this.repository.addBalance(toAddress, value);
63
64 DataWord from = DataWord.valueOf(contractAddress.getBytes());
65 DataWord to = DataWord.valueOf(toAddress.getBytes());
66 long gas = 0L;
67 DataWord amount = DataWord.valueOf(value.getBytes());
68
69 TransferInvoke invoke = new TransferInvoke(from, to, gas, amount);
70 ProgramResult result = new ProgramResult();
71 ProgramSubtrace subtrace = ProgramSubtrace.newCallSubtrace(CallType.CALL, invoke, result, null, Collections.emptyList());
72
73 this.subtraces.add(subtrace);
74 }
75
76 private void logPayment(byte[] blockHash, Coin value, RskAddress toAddress, List<LogInfo> logs) {
77
78 byte[] loggerContractAddress = this.contractAddress.getBytes();
79 List<DataWord> topics = Arrays.asList(RemascContract.MINING_FEE_TOPIC, DataWord.valueOf(toAddress.getBytes()));
80 byte[] data = RLP.encodeList(RLP.encodeElement(blockHash), RLP.encodeCoin(value));
81
82 logs.add(new LogInfo(loggerContractAddress, topics, data));
83 }
84 }