Skip to content

Commit 1e3e466

Browse files
Seulgi Kimsgkim126
authored andcommitted
Declare Metadata
1 parent 4d0447a commit 1e3e466

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

core/src/state/metadata.rs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright 2018 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 ctypes::H256;
18+
use rlp::{Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp};
19+
20+
use super::CacheableItem;
21+
22+
#[derive(Clone, Debug)]
23+
pub struct Metadata {
24+
number_of_shards: u32,
25+
}
26+
27+
impl Metadata {
28+
pub fn new(number_of_shards: u32) -> Self {
29+
Self {
30+
number_of_shards,
31+
}
32+
}
33+
34+
pub fn number_of_shards(&self) -> &u32 {
35+
&self.number_of_shards
36+
}
37+
}
38+
39+
impl CacheableItem for Metadata {
40+
type Address = MetadataAddress;
41+
42+
fn is_null(&self) -> bool {
43+
self.number_of_shards == 0
44+
}
45+
}
46+
47+
const PREFIX: u8 = 'M' as u8;
48+
49+
impl Encodable for Metadata {
50+
fn rlp_append(&self, s: &mut RlpStream) {
51+
s.begin_list(2).append(&PREFIX).append(&self.number_of_shards);
52+
}
53+
}
54+
55+
impl Decodable for Metadata {
56+
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
57+
if rlp.item_count()? != 2 {
58+
return Err(DecoderError::RlpInvalidLength)
59+
}
60+
let prefix = rlp.val_at::<u8>(0)?;
61+
if PREFIX != prefix {
62+
cdebug!(STATE, "{} is not an expected prefix for asset", prefix);
63+
return Err(DecoderError::Custom("Unexpected prefix"))
64+
}
65+
Ok(Self {
66+
number_of_shards: rlp.val_at(1)?,
67+
})
68+
}
69+
}
70+
71+
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
72+
pub struct MetadataAddress(H256);
73+
74+
impl_address!(MetadataAddress, PREFIX);
75+
76+
impl MetadataAddress {
77+
pub fn new() -> Self {
78+
Self::from_transaction_hash(H256::from_slice(b"metadata address"), 0)
79+
}
80+
}
81+
82+
#[cfg(test)]
83+
mod tests {
84+
use super::*;
85+
86+
#[test]
87+
fn parse_fail_return_none() {
88+
let hash = {
89+
let mut hash;
90+
loop {
91+
hash = H256::random();
92+
if hash[0] == PREFIX {
93+
continue
94+
}
95+
for i in 1..8 {
96+
if hash[i] == 0 {
97+
continue
98+
}
99+
}
100+
break
101+
}
102+
hash
103+
};
104+
let address = MetadataAddress::from_hash(hash);
105+
assert!(address.is_none());
106+
}
107+
108+
#[test]
109+
fn parse_return_some() {
110+
let hash = {
111+
let mut hash = H256::random();
112+
hash[0..8].clone_from_slice(&[PREFIX, 0, 0, 0, 0, 0, 0, 0]);
113+
hash
114+
};
115+
let address = MetadataAddress::from_hash(hash.clone());
116+
assert_eq!(Some(MetadataAddress(hash)), address);
117+
}
118+
}

core/src/state/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ mod asset_scheme;
3939
mod backend;
4040
mod cache;
4141
mod info;
42+
mod metadata;
4243
mod shard;
4344
mod shard_level;
4445
mod shard_metadata;
@@ -53,6 +54,7 @@ pub use self::asset_scheme::{AssetScheme, AssetSchemeAddress};
5354
pub use self::backend::{Backend, Basic as BasicBackend, ShardBackend, TopBackend};
5455
pub use self::cache::CacheableItem;
5556
pub use self::info::{ShardStateInfo, TopStateInfo};
57+
pub use self::metadata::{Metadata, MetadataAddress};
5658
pub use self::shard::{Shard, ShardAddress};
5759
pub use self::shard_metadata::{ShardMetadata, ShardMetadataAddress};
5860
pub use self::shard_state::{ShardState, TransactionOutcome};

0 commit comments

Comments
 (0)