Coverage Summary for Class: Sibling (co.rsk.remasc)
Class |
Class, %
|
Method, %
|
Line, %
|
Sibling |
100%
(1/1)
|
20%
(2/10)
|
34.2%
(13/38)
|
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 org.ethereum.core.BlockHeader;
24 import org.ethereum.util.RLP;
25 import org.ethereum.util.RLPElement;
26 import org.ethereum.util.RLPList;
27 import org.bouncycastle.util.BigIntegers;
28
29 import java.math.BigInteger;
30 import java.util.ArrayList;
31
32 /**
33 * Siblings are part of the remasc contract state
34 * Sibling information is added to contract state as blocks are processed and removed when no longer needed.
35 * @author Oscar Guindzberg
36 */
37 public class Sibling {
38
39 // Hash of the sibling block
40 private final byte[] hash;
41 // Coinbase address of the sibling block
42 private final RskAddress coinbase;
43 // Fees paid by the sibling block
44 private final Coin paidFees;
45 // Coinbase address of the block that included the sibling block as uncle
46 private final RskAddress includedBlockCoinbase;
47 // Height of the block that included the sibling block as uncle
48 private final long includedHeight;
49 // Number of uncles
50 private final int uncleCount;
51
52
53 public Sibling(BlockHeader blockHeader, RskAddress includedBlockCoinbase, long includedHeight){
54 this(blockHeader.getHash().getBytes(),
55 blockHeader.getCoinbase(),
56 includedBlockCoinbase,
57 blockHeader.getPaidFees(),
58 includedHeight,
59 blockHeader.getUncleCount());
60 }
61
62 private Sibling(byte[] hash, RskAddress coinbase, RskAddress includedBlockCoinbase, Coin paidFees, long includedHeight, int uncleCount) {
63 this.hash = hash;
64 this.coinbase = coinbase;
65 this.paidFees = paidFees;
66 this.includedBlockCoinbase = includedBlockCoinbase;
67 this.includedHeight = includedHeight;
68 this.uncleCount = uncleCount;
69 }
70
71 public byte[] getHash() {
72 return hash;
73 }
74
75 public RskAddress getCoinbase() {
76 return coinbase;
77 }
78
79 public Coin getPaidFees() {
80 return paidFees;
81 }
82
83 public RskAddress getIncludedBlockCoinbase() {
84 return includedBlockCoinbase;
85 }
86
87 public long getIncludedHeight() {
88 return includedHeight;
89 }
90
91 public int getUncleCount() { return uncleCount; }
92
93 public byte[] getEncoded() {
94 byte[] rlpHash = RLP.encodeElement(this.hash);
95 byte[] rlpCoinbase = RLP.encodeRskAddress(this.coinbase);
96 byte[] rlpIncludedBlockCoinbase = RLP.encodeRskAddress(this.includedBlockCoinbase);
97
98 byte[] rlpPaidFees = RLP.encodeCoin(this.paidFees);
99 byte[] rlpIncludedHeight = RLP.encodeBigInteger(BigInteger.valueOf(this.includedHeight));
100 byte[] rlpUncleCount = RLP.encodeBigInteger(BigInteger.valueOf((this.uncleCount)));
101
102 return RLP.encodeList(rlpHash, rlpCoinbase, rlpIncludedBlockCoinbase, rlpPaidFees, rlpIncludedHeight, rlpUncleCount);
103 }
104
105 public static Sibling create(byte[] data) {
106 ArrayList<RLPElement> params = RLP.decode2(data);
107 RLPList sibling = (RLPList) params.get(0);
108
109 byte[] hash = sibling.get(0).getRLPData();
110 RskAddress coinbase = RLP.parseRskAddress(sibling.get(1).getRLPData());
111 RskAddress includedBlockCoinbase = RLP.parseRskAddress(sibling.get(2).getRLPData());
112
113 Coin paidFees = RLP.parseCoin(sibling.get(3).getRLPData());
114 byte[] bytesIncludedHeight = sibling.get(4).getRLPData();
115
116 RLPElement uncleCountElement = sibling.get(5);
117 byte[] bytesUncleCount = uncleCountElement != null? uncleCountElement.getRLPData():null;
118
119 long includedHeight = bytesIncludedHeight == null ? 0 : BigIntegers.fromUnsignedByteArray(bytesIncludedHeight).longValue();
120 int uncleCount = bytesUncleCount == null ? 0 : BigIntegers.fromUnsignedByteArray(bytesUncleCount).intValue();
121
122 return new Sibling(hash, coinbase, includedBlockCoinbase, paidFees, includedHeight, uncleCount);
123 }
124 }