Coverage Summary for Class: OrchidAccountState (co.rsk.trie)
Class |
Class, %
|
Method, %
|
Line, %
|
OrchidAccountState |
0%
(0/1)
|
0%
(0/10)
|
0%
(0/29)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2019 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.trie;
20
21 import co.rsk.core.Coin;
22 import org.ethereum.crypto.HashUtil;
23 import org.ethereum.util.RLP;
24 import org.ethereum.util.RLPList;
25
26 import java.math.BigInteger;
27 import java.util.Arrays;
28
29 import static org.ethereum.crypto.HashUtil.EMPTY_TRIE_HASH;
30 import static org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY;
31
32 /**
33 * This class holds the Orchid account state encoding logic
34 */
35 @SuppressWarnings("squid:S2384") // this class is left for TrieConverter, we don't need to copy the byte[] arguments
36 public class OrchidAccountState {
37 private static final byte[] EMPTY_DATA_HASH = HashUtil.keccak256(EMPTY_BYTE_ARRAY);
38
39 private byte[] rlpEncoded;
40
41 private BigInteger nonce;
42 private Coin balance;
43 private byte[] stateRoot = EMPTY_TRIE_HASH;
44 private byte[] codeHash = EMPTY_DATA_HASH;
45
46 public OrchidAccountState(byte[] rlpData) {
47 RLPList items = (RLPList) RLP.decode2(rlpData).get(0);
48 this.nonce = items.get(0).getRLPData() == null ? BigInteger.ZERO
49 : new BigInteger(1, items.get(0).getRLPData());
50 this.balance = RLP.parseCoin(items.get(1).getRLPData());
51 this.stateRoot = items.get(2).getRLPData();
52 this.codeHash = items.get(3).getRLPData();
53 this.rlpEncoded = rlpData;
54 }
55
56 public OrchidAccountState(BigInteger nonce, Coin balance) {
57 this.nonce = nonce;
58 this.balance = balance;
59 }
60
61 public void setStateRoot(byte[] stateRoot) {
62 rlpEncoded = null;
63 this.stateRoot = stateRoot;
64 }
65
66 public void setCodeHash(byte[] codeHash) {
67 rlpEncoded = null;
68 this.codeHash = codeHash;
69 }
70
71 public byte[] getEncoded() {
72 if (rlpEncoded == null) {
73 byte[] nonce = RLP.encodeBigInteger(this.nonce);
74 byte[] balance = RLP.encodeCoin(this.balance);
75 byte[] stateRoot = RLP.encodeElement(this.stateRoot);
76 byte[] codeHash = RLP.encodeElement(this.codeHash);
77 this.rlpEncoded = RLP.encodeList(nonce, balance, stateRoot, codeHash);
78 }
79
80 return rlpEncoded;
81 }
82
83 public BigInteger getNonce() {
84 return nonce;
85 }
86
87 public Coin getBalance() {
88 return balance;
89 }
90
91 public byte[] getStateRoot() {
92 return Arrays.copyOf(stateRoot, stateRoot.length);
93 }
94
95 public byte[] getCodeHash() {
96 return Arrays.copyOf(codeHash, codeHash.length);
97 }
98 }