Coverage Summary for Class: GetUncleCoinbaseAddress (co.rsk.pcc.blockheader)
Class |
Class, %
|
Method, %
|
Line, %
|
GetUncleCoinbaseAddress |
0%
(0/1)
|
0%
(0/3)
|
0%
(0/17)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2019 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.pcc.blockheader;
21
22 import co.rsk.pcc.ExecutionEnvironment;
23 import co.rsk.pcc.exception.NativeContractIllegalArgumentException;
24 import org.ethereum.core.Block;
25 import org.ethereum.core.BlockHeader;
26 import org.ethereum.core.CallTransaction;
27 import org.ethereum.util.ByteUtil;
28
29 import java.math.BigInteger;
30 import java.util.List;
31
32 /**
33 * This implements the "getUncleCoinbaseAddress" method
34 * that belongs to the BlockHeaderContract native contract.
35 *
36 * @author Diego Masini
37 */
38 public class GetUncleCoinbaseAddress extends BlockHeaderContractMethod {
39 private final CallTransaction.Function function = CallTransaction.Function.fromSignature(
40 "getUncleCoinbaseAddress",
41 new String[]{"int256", "int256"},
42 new String[]{"bytes"}
43 );
44
45 public GetUncleCoinbaseAddress(ExecutionEnvironment executionEnvironment, BlockAccessor blockAccessor) {
46 super(executionEnvironment, blockAccessor);
47 }
48
49 @Override
50 public CallTransaction.Function getFunction() {
51 return function;
52 }
53
54 @Override
55 protected Object internalExecute(Block block, Object[] arguments) throws NativeContractIllegalArgumentException {
56 List<BlockHeader> uncles = block.getUncleList();
57 if (uncles == null) {
58 return ByteUtil.EMPTY_BYTE_ARRAY;
59 }
60
61 short uncleIndex;
62 try {
63 uncleIndex = ((BigInteger) arguments[1]).shortValueExact();
64 } catch (ArithmeticException e) {
65 return ByteUtil.EMPTY_BYTE_ARRAY;
66 }
67
68 if (uncleIndex < 0) {
69 throw new NativeContractIllegalArgumentException(String.format(
70 "Invalid uncle index '%d' (should be a non-negative value)",
71 uncleIndex
72 ));
73 }
74
75 if (uncleIndex >= uncles.size()) {
76 return ByteUtil.EMPTY_BYTE_ARRAY;
77 }
78
79 BlockHeader uncle = uncles.get(uncleIndex);
80 return uncle.getCoinbase().getBytes();
81 }
82 }
83