forked from CodeChain-io/codechain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraits.rs
192 lines (153 loc) · 7.6 KB
/
traits.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2018-2019 Kodebox, Inc.
// This file is part of CodeChain.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use ckey::{public_to_address, Address, Public, Signature};
use cmerkle::Result as TrieResult;
use ctypes::transaction::ShardTransaction;
use ctypes::{BlockNumber, CommonParams, ShardId};
use cvm::ChainTimeInfo;
use primitives::{Bytes, H160, H256};
use crate::{
Account, ActionData, AssetScheme, CacheableItem, Metadata, OwnedAsset, RegularAccount, Shard, StateDB, StateResult,
Text,
};
pub trait TopStateView {
/// Check caches for required data
/// First searches for account in the local, then the shared cache.
/// Populates local cache if nothing found.
fn account(&self, a: &Address) -> TrieResult<Option<Account>>;
/// Get the seq of account `a`.
fn seq(&self, a: &Address) -> TrieResult<u64> {
Ok(self.account(a)?.map_or(0, |account| account.seq()))
}
/// Get the balance of account `a`.
fn balance(&self, a: &Address) -> TrieResult<u64> {
Ok(self.account(a)?.map_or(0, |account| account.balance()))
}
fn account_exists(&self, a: &Address) -> TrieResult<bool> {
// Bloom filter does not contain empty accounts, so it is important here to
// check if account exists in the database directly before EIP-161 is in effect.
Ok(self.account(a)?.is_some())
}
fn account_exists_and_not_null(&self, a: &Address) -> TrieResult<bool> {
Ok(self.account(a)?.map(|a| !a.is_null()).unwrap_or(false))
}
fn account_exists_and_has_seq(&self, a: &Address) -> TrieResult<bool> {
Ok(self.account(a)?.map(|a| a.seq() != 0).unwrap_or(false))
}
fn regular_account_by_address(&self, a: &Address) -> TrieResult<Option<RegularAccount>>;
fn regular_account(&self, p: &Public) -> TrieResult<Option<RegularAccount>> {
self.regular_account_by_address(&public_to_address(p))
}
/// Get the regular key of account `a`.
fn regular_key(&self, a: &Address) -> TrieResult<Option<Public>> {
Ok(self.account(a)?.and_then(|account| account.regular_key()))
}
fn regular_key_owner(&self, address: &Address) -> TrieResult<Option<Address>> {
Ok(self
.regular_account_by_address(&address)?
.map(|regular_account| public_to_address(regular_account.owner_public())))
}
fn regular_account_exists_and_not_null(&self, p: &Public) -> TrieResult<bool> {
Ok(self.regular_account(p)?.map_or(false, |a| !a.is_null()))
}
fn regular_account_exists_and_not_null_by_address(&self, a: &Address) -> TrieResult<bool> {
Ok(self.regular_account_by_address(a)?.map_or(false, |a| !a.is_null()))
}
fn metadata(&self) -> TrieResult<Option<Metadata>>;
fn number_of_shards(&self) -> TrieResult<ShardId> {
Ok(*self.metadata()?.expect("Metadata must exist").number_of_shards())
}
fn shard_id_by_hash(&self, tx_hash: &H256) -> TrieResult<Option<ShardId>> {
Ok(self.metadata()?.and_then(|metadata| metadata.shard_id_by_hash(tx_hash)))
}
fn shard(&self, shard_id: ShardId) -> TrieResult<Option<Shard>>;
fn shard_state<'db>(&'db self, shard_id: ShardId) -> TrieResult<Option<Box<ShardStateView + 'db>>>;
fn shard_root(&self, shard_id: ShardId) -> TrieResult<Option<H256>> {
Ok(self.shard(shard_id)?.map(|shard| *shard.root()))
}
fn shard_owners(&self, shard_id: ShardId) -> TrieResult<Option<Vec<Address>>> {
Ok(self.shard(shard_id)?.map(|shard| shard.owners().to_vec()))
}
fn shard_users(&self, shard_id: ShardId) -> TrieResult<Option<Vec<Address>>> {
Ok(self.shard(shard_id)?.map(|shard| shard.users().to_vec()))
}
/// Get the asset scheme.
fn asset_scheme(&self, shard_id: ShardId, asset_type: H160) -> TrieResult<Option<AssetScheme>> {
match self.shard_state(shard_id)? {
None => Ok(None),
Some(state) => state.asset_scheme(asset_type),
}
}
/// Get the asset.
fn asset(&self, shard_id: ShardId, tracker: H256, index: usize) -> TrieResult<Option<OwnedAsset>> {
match self.shard_state(shard_id)? {
None => Ok(None),
Some(state) => state.asset(tracker, index),
}
}
fn text(&self, key: &H256) -> TrieResult<Option<Text>>;
fn action_data(&self, key: &H256) -> TrieResult<Option<ActionData>>;
}
pub trait ShardStateView {
/// Get the asset scheme.
fn asset_scheme(&self, asset_type: H160) -> TrieResult<Option<AssetScheme>>;
/// Get the asset.
fn asset(&self, tracker: H256, index: usize) -> TrieResult<Option<OwnedAsset>>;
}
pub trait ShardState {
fn apply<C: ChainTimeInfo>(
&mut self,
transaction: &ShardTransaction,
sender: &Address,
shard_owners: &[Address],
approvers: &[Address],
client: &C,
parent_block_number: BlockNumber,
parent_block_timestamp: u64,
) -> StateResult<()>;
}
pub trait TopState {
/// Remove an existing account.
fn kill_account(&mut self, account: &Address);
fn kill_regular_account(&mut self, account: &Public);
/// Add `incr` to the balance of account `a`.
fn add_balance(&mut self, a: &Address, incr: u64) -> TrieResult<()>;
/// Subtract `decr` from the balance of account `a`.
fn sub_balance(&mut self, a: &Address, decr: u64) -> StateResult<()>;
/// Subtracts `by` from the balance of `from` and adds it to that of `to`.
fn transfer_balance(&mut self, from: &Address, to: &Address, by: u64) -> StateResult<()>;
/// Increment the seq of account `a` by 1.
fn inc_seq(&mut self, a: &Address) -> TrieResult<()>;
/// Set the regular key of account `owner_public`
fn set_regular_key(&mut self, owner_public: &Public, key: &Public) -> StateResult<()>;
fn create_shard(&mut self, fee_payer: &Address, tx_hash: H256, users: Vec<Address>) -> StateResult<()>;
fn change_shard_owners(&mut self, shard_id: ShardId, owners: &[Address], sender: &Address) -> StateResult<()>;
fn change_shard_users(&mut self, shard_id: ShardId, users: &[Address], sender: &Address) -> StateResult<()>;
fn set_shard_root(&mut self, shard_id: ShardId, new_root: H256) -> StateResult<()>;
fn set_shard_owners(&mut self, shard_id: ShardId, new_owners: Vec<Address>) -> StateResult<()>;
fn set_shard_users(&mut self, shard_id: ShardId, new_users: Vec<Address>) -> StateResult<()>;
fn store_text(&mut self, key: &H256, text: Text, sig: &Signature) -> StateResult<()>;
fn remove_text(&mut self, key: &H256, sig: &Signature) -> StateResult<()>;
fn increase_term_id(&mut self, last_term_finished_block_num: u64) -> StateResult<()>;
fn update_action_data(&mut self, key: &H256, data: Bytes) -> StateResult<()>;
fn remove_action_data(&mut self, key: &H256);
fn update_params(&mut self, metadata_seq: u64, params: CommonParams) -> StateResult<()>;
}
pub trait StateWithCache {
/// Commits our cached account changes into the trie.
fn commit(&mut self) -> StateResult<H256>;
fn commit_and_into_db(self) -> StateResult<(StateDB, H256)>;
}