Skip to content

Commit ce2d935

Browse files
committed
Add ibc_data state
1 parent 09801d8 commit ce2d935

File tree

7 files changed

+152
-7
lines changed

7 files changed

+152
-7
lines changed

state/src/cache/global_cache.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use super::lru_cache::LruCache;
1818
use super::{ShardCache, TopCache};
19-
use crate::{Account, ActionData, Metadata, RegularAccount, Shard, ShardText};
19+
use crate::{Account, ActionData, IBCData, Metadata, RegularAccount, Shard, ShardText};
2020
use ctypes::ShardId;
2121
use std::collections::{HashMap, HashSet};
2222

@@ -26,18 +26,27 @@ pub struct GlobalCache {
2626
metadata: LruCache<Metadata>,
2727
shard: LruCache<Shard>,
2828
action_data: LruCache<ActionData>,
29+
ibc_data: LruCache<IBCData>,
2930

3031
shard_text: LruCache<ShardText>,
3132
}
3233

3334
impl GlobalCache {
34-
pub fn new(account: usize, regular_account: usize, shard: usize, action_data: usize, shard_text: usize) -> Self {
35+
pub fn new(
36+
account: usize,
37+
regular_account: usize,
38+
shard: usize,
39+
action_data: usize,
40+
ibc_data: usize,
41+
shard_text: usize,
42+
) -> Self {
3543
Self {
3644
account: LruCache::new(account),
3745
regular_account: LruCache::new(regular_account),
3846
metadata: LruCache::new(1),
3947
shard: LruCache::new(shard),
4048
action_data: LruCache::new(action_data),
49+
ibc_data: LruCache::new(ibc_data),
4150

4251
shard_text: LruCache::new(shard_text),
4352
}
@@ -50,6 +59,7 @@ impl GlobalCache {
5059
self.metadata.iter().map(|(addr, item)| (*addr, item.clone())),
5160
self.shard.iter().map(|(addr, item)| (*addr, item.clone())),
5261
self.action_data.iter().map(|(addr, item)| (*addr, item.clone())),
62+
self.ibc_data.iter().map(|(addr, item)| (*addr, item.clone())),
5363
)
5464
}
5565

@@ -103,6 +113,12 @@ impl GlobalCache {
103113
None => self.action_data.remove(&addr),
104114
};
105115
}
116+
for (addr, item) in top_cache.cached_ibc_data().into_iter() {
117+
match item {
118+
Some(item) => self.ibc_data.insert(addr, item),
119+
None => self.ibc_data.remove(&addr),
120+
};
121+
}
106122

107123
let mut cached_shard_texts: Vec<_> =
108124
shard_caches.iter().flat_map(|(_, shard_cache)| shard_cache.cached_shard_text().into_iter()).collect();
@@ -121,6 +137,7 @@ impl GlobalCache {
121137
self.metadata.clear();
122138
self.shard.clear();
123139
self.action_data.clear();
140+
self.ibc_data.clear();
124141
self.shard_text.clear();
125142
}
126143
}
@@ -132,8 +149,9 @@ impl Default for GlobalCache {
132149
const N_REGULAR_ACCOUNT: usize = 100;
133150
const N_SHARD: usize = 100;
134151
const N_ACTION_DATA: usize = 10;
152+
const N_IBC_DATA: usize = 100;
135153
const N_SHARD_TEXT: usize = 1000;
136-
Self::new(N_ACCOUNT, N_REGULAR_ACCOUNT, N_SHARD, N_ACTION_DATA, N_SHARD_TEXT)
154+
Self::new(N_ACCOUNT, N_REGULAR_ACCOUNT, N_SHARD, N_ACTION_DATA, N_IBC_DATA, N_SHARD_TEXT)
137155
}
138156
}
139157

@@ -145,6 +163,7 @@ impl Clone for GlobalCache {
145163
metadata: self.metadata.clone(),
146164
shard: self.shard.clone(),
147165
action_data: self.action_data.clone(),
166+
ibc_data: self.ibc_data.clone(),
148167

149168
shard_text: self.shard_text.clone(),
150169
}

state/src/cache/top_cache.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use super::WriteBack;
1818
use crate::{
19-
Account, ActionData, Metadata, MetadataAddress, RegularAccount, RegularAccountAddress, Shard, ShardAddress,
19+
Account, ActionData, IBCData, Metadata, MetadataAddress, RegularAccount, RegularAccountAddress, Shard, ShardAddress,
2020
};
2121
use ckey::Address;
2222
use merkle_trie::{Result as TrieResult, Trie, TrieMut};
@@ -29,6 +29,7 @@ pub struct TopCache {
2929
metadata: WriteBack<Metadata>,
3030
shard: WriteBack<Shard>,
3131
action_data: WriteBack<ActionData>,
32+
ibc_data: WriteBack<IBCData>,
3233
}
3334

3435
impl TopCache {
@@ -38,13 +39,15 @@ impl TopCache {
3839
metadata: impl Iterator<Item = (MetadataAddress, Metadata)>,
3940
shards: impl Iterator<Item = (ShardAddress, Shard)>,
4041
action_data: impl Iterator<Item = (H256, ActionData)>,
42+
ibc_data: impl Iterator<Item = (H256, IBCData)>,
4143
) -> Self {
4244
Self {
4345
account: WriteBack::new_with_iter(accounts),
4446
regular_account: WriteBack::new_with_iter(regular_accounts),
4547
metadata: WriteBack::new_with_iter(metadata),
4648
shard: WriteBack::new_with_iter(shards),
4749
action_data: WriteBack::new_with_iter(action_data),
50+
ibc_data: WriteBack::new_with_iter(ibc_data),
4851
}
4952
}
5053

@@ -54,6 +57,7 @@ impl TopCache {
5457
self.metadata.checkpoint();
5558
self.shard.checkpoint();
5659
self.action_data.checkpoint();
60+
self.ibc_data.checkpoint();
5761
}
5862

5963
pub fn discard_checkpoint(&mut self) {
@@ -62,6 +66,7 @@ impl TopCache {
6266
self.metadata.discard_checkpoint();
6367
self.shard.discard_checkpoint();
6468
self.action_data.discard_checkpoint();
69+
self.ibc_data.discard_checkpoint();
6570
}
6671

6772
pub fn revert_to_checkpoint(&mut self) {
@@ -70,6 +75,7 @@ impl TopCache {
7075
self.metadata.revert_to_checkpoint();
7176
self.shard.revert_to_checkpoint();
7277
self.action_data.revert_to_checkpoint();
78+
self.ibc_data.revert_to_checkpoint();
7379
}
7480

7581
pub fn commit<'db>(&mut self, trie: &mut (dyn TrieMut + 'db)) -> TrieResult<()> {
@@ -78,6 +84,7 @@ impl TopCache {
7884
self.metadata.commit(trie)?;
7985
self.shard.commit(trie)?;
8086
self.action_data.commit(trie)?;
87+
self.ibc_data.commit(trie)?;
8188
Ok(())
8289
}
8390

@@ -142,6 +149,18 @@ impl TopCache {
142149
self.action_data.remove(address)
143150
}
144151

152+
pub fn ibc_data(&self, a: &H256, db: &dyn Trie) -> TrieResult<Option<IBCData>> {
153+
self.ibc_data.get(a, db)
154+
}
155+
156+
pub fn ibc_data_mut(&self, a: &H256, db: &dyn Trie) -> TrieResult<RefMut<'_, IBCData>> {
157+
self.ibc_data.get_mut(a, db)
158+
}
159+
160+
pub fn remove_ibc_data(&self, address: &H256) {
161+
self.ibc_data.remove(address)
162+
}
163+
145164
pub fn cached_accounts(&self) -> Vec<(Address, Option<Account>)> {
146165
let mut items = self.account.items();
147166
items.sort_unstable_by(|lhs, rhs| lhs.0.cmp(&rhs.0));
@@ -171,6 +190,12 @@ impl TopCache {
171190
items.sort_unstable_by(|lhs, rhs| lhs.0.cmp(&rhs.0));
172191
items.into_iter().map(|(_, addr, item)| (addr, item)).collect()
173192
}
193+
194+
pub fn cached_ibc_data(&self) -> Vec<(H256, Option<IBCData>)> {
195+
let mut items = self.ibc_data.items();
196+
items.sort_unstable_by(|lhs, rhs| lhs.0.cmp(&rhs.0));
197+
items.into_iter().map(|(_, addr, item)| (addr, item)).collect()
198+
}
174199
}
175200

176201
impl Clone for TopCache {
@@ -181,6 +206,7 @@ impl Clone for TopCache {
181206
metadata: self.metadata.clone(),
182207
shard: self.shard.clone(),
183208
action_data: self.action_data.clone(),
209+
ibc_data: self.ibc_data.clone(),
184210
}
185211
}
186212
}

state/src/impls/top_level.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@
3737
3838
use crate::cache::{ShardCache, TopCache};
3939
use crate::checkpoint::{CheckpointId, StateWithCheckpoint};
40+
use crate::error::Error;
4041
use crate::traits::{ShardState, ShardStateView, StateWithCache, TopState, TopStateView};
4142
use crate::{
42-
Account, ActionData, FindActionHandler, Metadata, MetadataAddress, RegularAccount, RegularAccountAddress, Shard,
43-
ShardAddress, ShardLevelState, StateDB, StateResult,
43+
Account, ActionData, FindActionHandler, IBCData, Metadata, MetadataAddress, RegularAccount, RegularAccountAddress,
44+
Shard, ShardAddress, ShardLevelState, StateDB, StateResult,
4445
};
4546
use ccrypto::BLAKE_NULL_RLP;
4647
use cdb::{AsHashDB, DatabaseError};
@@ -136,6 +137,12 @@ impl TopStateView for TopLevelState {
136137
let trie = TrieFactory::readonly(db.as_hashdb(), &self.root)?;
137138
Ok(self.top_cache.action_data(key, &trie)?.map(Into::into))
138139
}
140+
141+
fn ibc_data(&self, key: &H256) -> Result<Option<IBCData>, TrieError> {
142+
let db = self.db.borrow();
143+
let trie = TrieFactory::readonly(db.as_hashdb(), &self.root)?;
144+
Ok(self.top_cache.ibc_data(key, &trie)?.map(Into::into))
145+
}
139146
}
140147

141148
impl StateWithCache for TopLevelState {
@@ -513,6 +520,12 @@ impl TopLevelState {
513520
self.top_cache.action_data_mut(key, &trie)
514521
}
515522

523+
fn get_ibc_data_mut(&self, key: &H256) -> TrieResult<RefMut<'_, IBCData>> {
524+
let db = self.db.borrow();
525+
let trie = TrieFactory::readonly(db.as_hashdb(), &self.root)?;
526+
self.top_cache.ibc_data_mut(key, &trie)
527+
}
528+
516529
pub fn journal_under(&self, batch: &mut DBTransaction, now: u64) -> Result<u32, DatabaseError> {
517530
self.db.borrow_mut().journal_under(batch, now, self.root)
518531
}
@@ -723,6 +736,16 @@ impl TopState for TopLevelState {
723736
self.top_cache.remove_action_data(key)
724737
}
725738

739+
fn update_ibc_data(&mut self, key: &H256, data: Vec<u8>) -> Result<(), Error> {
740+
let mut ibc_data = self.get_ibc_data_mut(key)?;
741+
*ibc_data = data.into();
742+
Ok(())
743+
}
744+
745+
fn remove_ibc_data(&mut self, key: &H256) {
746+
self.top_cache.remove_ibc_data(key)
747+
}
748+
726749
fn update_params(&mut self, metadata_seq: u64, params: CommonParams) -> StateResult<()> {
727750
let mut metadata = self.get_metadata_mut()?;
728751
if metadata.seq() != metadata_seq {

state/src/item/ibc_data.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2020 Kodebox, Inc.
2+
// This file is part of CodeChain.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as
6+
// published by the Free Software Foundation, either version 3 of the
7+
// License, or (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
use crate::CacheableItem;
18+
use primitives::{Bytes, H256};
19+
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream, NULL_RLP};
20+
use std::ops::Deref;
21+
22+
#[derive(Clone, Debug, PartialEq, Eq)]
23+
pub struct IBCData(Bytes);
24+
25+
impl Default for IBCData {
26+
fn default() -> Self {
27+
IBCData(NULL_RLP.to_vec())
28+
}
29+
}
30+
31+
impl Deref for IBCData {
32+
type Target = Bytes;
33+
34+
fn deref(&self) -> &<Self as Deref>::Target {
35+
&self.0
36+
}
37+
}
38+
39+
impl CacheableItem for IBCData {
40+
type Address = H256;
41+
fn is_null(&self) -> bool {
42+
self.is_empty()
43+
}
44+
}
45+
46+
impl Encodable for IBCData {
47+
fn rlp_append(&self, s: &mut RlpStream) {
48+
self.0.rlp_append(s);
49+
}
50+
}
51+
52+
impl Decodable for IBCData {
53+
fn decode(rlp: &Rlp<'_>) -> Result<Self, DecoderError> {
54+
Bytes::decode(rlp).map(IBCData)
55+
}
56+
}
57+
58+
impl From<Bytes> for IBCData {
59+
fn from(f: Vec<u8>) -> Self {
60+
IBCData(f)
61+
}
62+
}
63+
64+
impl From<IBCData> for Bytes {
65+
fn from(f: IBCData) -> Self {
66+
f.0
67+
}
68+
}

state/src/item/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod address;
2020
pub mod account;
2121
pub mod action_data;
2222
pub mod dummy_shard_text;
23+
pub mod ibc_data;
2324
pub mod metadata;
2425
pub mod regular_account;
2526
pub mod shard;

state/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub use crate::impls::{ShardLevelState, TopLevelState};
4747
pub use crate::item::account::Account;
4848
pub use crate::item::action_data::ActionData;
4949
pub use crate::item::dummy_shard_text::{ShardText, ShardTextAddress};
50+
pub use crate::item::ibc_data::IBCData;
5051
pub use crate::item::metadata::{Metadata, MetadataAddress};
5152
pub use crate::item::regular_account::{RegularAccount, RegularAccountAddress};
5253
pub use crate::item::shard::{Shard, ShardAddress};

state/src/traits.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use crate::{Account, ActionData, CacheableItem, Metadata, RegularAccount, Shard, ShardText, StateDB, StateResult};
17+
use crate::{
18+
Account, ActionData, CacheableItem, IBCData, Metadata, RegularAccount, Shard, ShardText, StateDB, StateResult,
19+
};
1820
use ckey::{public_to_address, Address, Public};
1921
use ctypes::transaction::ShardTransaction;
2022
use ctypes::{BlockNumber, CommonParams, ShardId, Tracker, TxHash};
@@ -110,6 +112,8 @@ pub trait TopStateView {
110112
Some(state) => state.text(tracker),
111113
}
112114
}
115+
116+
fn ibc_data(&self, key: &H256) -> TrieResult<Option<IBCData>>;
113117
}
114118

115119
pub trait ShardStateView {
@@ -161,6 +165,9 @@ pub trait TopState {
161165
fn update_action_data(&mut self, key: &H256, data: Bytes) -> StateResult<()>;
162166
fn remove_action_data(&mut self, key: &H256);
163167

168+
fn update_ibc_data(&mut self, key: &H256, data: Bytes) -> StateResult<()>;
169+
fn remove_ibc_data(&mut self, key: &H256);
170+
164171
fn update_params(&mut self, metadata_seq: u64, params: CommonParams) -> StateResult<()>;
165172
fn update_term_params(&mut self) -> StateResult<()>;
166173
}

0 commit comments

Comments
 (0)