Coverage Summary for Class: Repository (org.ethereum.core)
Class |
Class, %
|
Method, %
|
Line, %
|
Repository |
100%
(1/1)
|
100%
(2/2)
|
60%
(6/10)
|
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 co.rsk.core.RskAddress;
24 import co.rsk.db.RepositorySnapshot;
25 import co.rsk.trie.Trie;
26 import org.ethereum.vm.DataWord;
27
28 import java.math.BigInteger;
29
30 public interface Repository extends RepositorySnapshot {
31 Trie getTrie();
32
33 /**
34 * Create a new account in the database
35 *
36 * @param addr of the contract
37 * @return newly created account state
38 *
39 * This method creates an account, but is DOES NOT create a contract.
40 * To create a contract, internally the account node is extended with a root node
41 * for storage. To avoid creating the root node for storage each time a storage cell
42 * is added, we pre-create the storage node when we know the account will become a
43 * contract. This is done in setupContract().
44 * Note that we can't use the length or existence of the code node for this,
45 * because a contract's code can be empty!
46 *
47 */
48 AccountState createAccount(RskAddress addr);
49
50 /**
51 * Create a new account in the database, and optionally carry over any existing balance
52 *
53 * @param addr of the contract
54 * @param carryOverBalance if true, then carry over any existing balance
55 * @return newly created account state
56 *
57 * @see #createAccount(RskAddress)
58 */
59 default AccountState createAccount(RskAddress addr, boolean carryOverBalance) {
60 AccountState newAccount;
61 if (carryOverBalance) { // carry over existing balance
62 Coin oldBalance = getBalance(addr);
63 newAccount = createAccount(addr);
64 addBalance(addr, oldBalance);
65 } else {
66 newAccount = createAccount(addr);
67 }
68
69 return newAccount;
70 }
71
72 void setupContract(RskAddress addr);
73
74 /**
75 * Deletes the account. This is recursive: all storage keys are deleted
76 *
77 * @param addr of the account
78 */
79 void delete(RskAddress addr);
80
81 /**
82 * Hibernates the account
83 *
84 * @param addr of the account
85 */
86 void hibernate(RskAddress addr);
87
88 /**
89 * Increase the account nonce of the given account by one
90 *
91 * @param addr of the account
92 * @return new value of the nonce
93 */
94 BigInteger increaseNonce(RskAddress addr);
95
96 void setNonce(RskAddress addr,BigInteger nonce);
97
98 /**
99 * Store code associated with an account
100 *
101 * @param addr for the account
102 * @param code that will be associated with this account
103 */
104 void saveCode(RskAddress addr, byte[] code);
105
106 /**
107 * Put a value in storage of an account at a given key
108 *
109 * @param addr of the account
110 * @param key of the data to store
111 * @param value is the data to store
112 */
113 void addStorageRow(RskAddress addr, DataWord key, DataWord value);
114
115 void addStorageBytes(RskAddress addr, DataWord key, byte[] value);
116
117 /**
118 * Add value to the balance of an account
119 *
120 * @param addr of the account
121 * @param value to be added
122 * @return new balance of the account
123 */
124 Coin addBalance(RskAddress addr, Coin value);
125
126 /**
127 * Store all the temporary changes made
128 * to the repository in the actual database
129 */
130 void commit();
131
132 /**
133 * Undo all the changes made so far
134 * to a snapshot of the repository
135 */
136 void rollback();
137
138 void save();
139
140 void updateAccountState(RskAddress addr, AccountState accountState);
141
142 default void transfer(RskAddress fromAddr, RskAddress toAddr, Coin value) {
143 addBalance(fromAddr, value.negate());
144 addBalance(toAddr, value);
145 }
146 }