Coverage Summary for Class: AccountState (org.ethereum.core)
Class |
Class, %
|
Method, %
|
Line, %
|
AccountState |
100%
(1/1)
|
55.6%
(10/18)
|
56.9%
(33/58)
|
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 org.ethereum.core;
21
22 import co.rsk.core.Coin;
23 import org.bouncycastle.util.BigIntegers;
24 import org.ethereum.util.RLP;
25 import org.ethereum.util.RLPList;
26
27 import java.math.BigInteger;
28
29 public class AccountState {
30
31 private static final int ACC_HIBERNATED_MASK = 1;
32 private byte[] rlpEncoded;
33
34 /* A value equalBytes to the number of transactions sent
35 * from this address, or, in the case of contract accounts,
36 * the number of contract-creations made by this account */
37 private BigInteger nonce;
38
39 /* A scalar value equalBytes to the number of Wei owned by this address */
40 private Coin balance;
41
42
43 /* Account state flags*/
44 private int stateFlags;
45
46 private boolean deleted = false;
47
48 public AccountState() {
49 this(BigInteger.ZERO, Coin.ZERO);
50 }
51
52 public AccountState(BigInteger nonce, Coin balance) {
53 this.nonce = nonce;
54 this.balance = balance;
55 }
56
57 public AccountState(byte[] rlpData) {
58 this.rlpEncoded = rlpData;
59
60 RLPList items = (RLPList) RLP.decode2(rlpEncoded).get(0);
61 this.nonce = items.get(0).getRLPData() == null ? BigInteger.ZERO
62 : new BigInteger(1, items.get(0).getRLPData());
63 this.balance = RLP.parseSignedCoinNonNullZero(items.get(1).getRLPData());
64
65 if (items.size() > 2) {
66 byte[] data = items.get(2).getRLPData();
67
68 this.stateFlags = data == null ? 0 : BigIntegers.fromUnsignedByteArray(data).intValue();
69 }
70 }
71
72 public BigInteger getNonce() {
73 return nonce;
74 }
75
76 public void setNonce(BigInteger nonce) {
77 rlpEncoded = null;
78 this.nonce = nonce;
79 }
80
81
82
83 public void incrementNonce() {
84 rlpEncoded = null;
85 this.nonce = nonce.add(BigInteger.ONE);
86 }
87
88 public Coin getBalance() {
89 return balance;
90 }
91
92 public Coin addToBalance(Coin value) {
93 if (value.equals(Coin.ZERO)) {
94 return this.balance;
95 }
96
97 rlpEncoded = null;
98 this.balance = balance.add(value);
99 return this.balance;
100 }
101
102 public byte[] getEncoded() {
103 if (rlpEncoded == null) {
104 byte[] anonce = RLP.encodeBigInteger(this.nonce);
105 byte[] abalance = RLP.encodeSignedCoinNonNullZero(this.balance);
106 if (stateFlags != 0) {
107 byte[] astateFlags = RLP.encodeInt(this.stateFlags);
108 this.rlpEncoded = RLP.encodeList(anonce, abalance, astateFlags);
109 } else
110 // do not serialize if zero to keep compatibility
111 {
112 this.rlpEncoded = RLP.encodeList(anonce, abalance);
113 }
114 }
115 return rlpEncoded;
116 }
117
118 public void setDeleted(boolean deleted) {
119 this.deleted = deleted;
120 }
121
122 public boolean isDeleted() {
123 return deleted;
124 }
125
126 public AccountState clone() {
127 AccountState accountState = new AccountState(nonce, balance);
128 accountState.setStateFlags(this.stateFlags);
129 return accountState;
130 }
131
132 public String toString() {
133 String ret = " Nonce: " + this.getNonce().toString() + "\n" +
134 " Balance: " + getBalance().asBigInteger() + "\n" +
135 " StateFlags: " + getStateFlags();
136 return ret;
137 }
138
139 /*
140 * Below are methods for hibernating an account that aren't used at the moment (only from tests).
141 * TODO(mc) we should decide whether to finish this feature or delete unused code
142 */
143
144 public int getStateFlags() {
145 return stateFlags;
146 }
147
148 public void setStateFlags(int s) {
149 stateFlags = s;
150 }
151
152 public Boolean isHibernated() {
153 return ((stateFlags & ACC_HIBERNATED_MASK) != 0);
154 }
155
156 public void hibernate() {
157 stateFlags = stateFlags | ACC_HIBERNATED_MASK;
158 rlpEncoded = null;
159 }
160
161 public void wakeUp() {
162 stateFlags = stateFlags & ~ACC_HIBERNATED_MASK;
163 }
164 }