|
| 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 ckey::Address; |
| 18 | +use ctypes::{ShardId, WorldId}; |
| 19 | +use primitives::H256; |
| 20 | +use rlp::{Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp}; |
| 21 | + |
| 22 | +use super::cache::CacheableItem; |
| 23 | + |
| 24 | +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] |
| 25 | +pub struct World { |
| 26 | + world_owners: Vec<Address>, |
| 27 | + nonce: u64, |
| 28 | +} |
| 29 | + |
| 30 | +impl World { |
| 31 | + pub fn new(world_owners: Vec<Address>) -> Self { |
| 32 | + Self { |
| 33 | + world_owners, |
| 34 | + nonce: 0, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + pub fn new_with_nonce(world_owners: Vec<Address>, nonce: u64) -> Self { |
| 39 | + Self { |
| 40 | + world_owners, |
| 41 | + nonce, |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + pub fn world_owners(&self) -> &[Address] { |
| 46 | + &self.world_owners |
| 47 | + } |
| 48 | + |
| 49 | + pub fn nonce(&self) -> &u64 { |
| 50 | + &self.nonce |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl CacheableItem for World { |
| 55 | + type Address = WorldAddress; |
| 56 | + |
| 57 | + fn is_null(&self) -> bool { |
| 58 | + self.world_owners.is_empty() && self.nonce == 0 |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +const PREFIX: u8 = super::WORLD_PREFIX; |
| 63 | + |
| 64 | +impl Encodable for World { |
| 65 | + fn rlp_append(&self, s: &mut RlpStream) { |
| 66 | + s.begin_list(3).append(&PREFIX).append_list(self.world_owners()).append(self.nonce()); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl Decodable for World { |
| 71 | + fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> { |
| 72 | + let prefix = rlp.val_at::<u8>(0)?; |
| 73 | + if PREFIX != prefix { |
| 74 | + cdebug!(STATE, "{} is not an expected prefix for world", prefix); |
| 75 | + return Err(DecoderError::Custom("Unexpected prefix")) |
| 76 | + } |
| 77 | + Ok(Self { |
| 78 | + world_owners: rlp.list_at(1)?, |
| 79 | + nonce: rlp.val_at(2)?, |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] |
| 85 | +pub struct WorldAddress(H256); |
| 86 | + |
| 87 | +impl_address!(WORLD, WorldAddress, PREFIX); |
| 88 | + |
| 89 | +impl WorldAddress { |
| 90 | + pub fn new(shard_id: ShardId, world_id: WorldId) -> Self { |
| 91 | + Self::from_transaction_hash_with_shard_and_world_id(H256::from_slice(b"world"), 0, shard_id, world_id) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +#[cfg(test)] |
| 96 | +mod tests { |
| 97 | + use super::*; |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn address() { |
| 101 | + let shard_id = 0xBeef; |
| 102 | + let world_id = 0xCafe; |
| 103 | + let address = WorldAddress::new(shard_id, world_id); |
| 104 | + assert_eq!(address[0..2], [PREFIX, 0]); |
| 105 | + assert_eq!(address[2..4], [0xBE, 0xEF]); // shard id |
| 106 | + assert_eq!(address[4..6], [0xCA, 0xFE]); // world id |
| 107 | + } |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn parse_fail_return_none() { |
| 111 | + let hash = { |
| 112 | + let mut hash; |
| 113 | + loop { |
| 114 | + hash = H256::random(); |
| 115 | + if hash[0] == PREFIX { |
| 116 | + continue |
| 117 | + } |
| 118 | + for i in 1..6 { |
| 119 | + if hash[i] == 0 { |
| 120 | + continue |
| 121 | + } |
| 122 | + } |
| 123 | + break |
| 124 | + } |
| 125 | + hash |
| 126 | + }; |
| 127 | + let address = WorldAddress::from_hash(hash); |
| 128 | + assert_eq!(None, address); |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn parse_return_some() { |
| 133 | + let hash = { |
| 134 | + let mut hash = H256::random(); |
| 135 | + hash[0..6].clone_from_slice(&[PREFIX, 0, 0, 0, 0, 0]); |
| 136 | + hash |
| 137 | + }; |
| 138 | + let address = WorldAddress::from_hash(hash.clone()); |
| 139 | + assert_eq!(Some(WorldAddress(hash)), address); |
| 140 | + } |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn shard_id() { |
| 144 | + let shard_id = 0xCAA; |
| 145 | + let world_id = 0xBEE; |
| 146 | + let world_address = WorldAddress::new(shard_id, world_id); |
| 147 | + assert_eq!(shard_id, world_address.shard_id()); |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn world_id() { |
| 152 | + let world_id = 0xCAA; |
| 153 | + let shard_id = 0xBEE; |
| 154 | + let world_address = WorldAddress::new(shard_id, world_id); |
| 155 | + assert_eq!(world_id, world_address.world_id()); |
| 156 | + } |
| 157 | + |
| 158 | + #[test] |
| 159 | + fn shard_id_from_hash() { |
| 160 | + let hash = { |
| 161 | + let mut hash = H256::random(); |
| 162 | + hash[0] = PREFIX; |
| 163 | + hash[1] = 0; |
| 164 | + hash |
| 165 | + }; |
| 166 | + assert_eq!(::std::mem::size_of::<u16>(), ::std::mem::size_of::<ShardId>()); |
| 167 | + let shard_id = ((hash[2] as ShardId) << 8) + (hash[3] as ShardId); |
| 168 | + let world_address = WorldAddress::from_hash(hash).unwrap(); |
| 169 | + assert_eq!(shard_id, world_address.shard_id()); |
| 170 | + } |
| 171 | + |
| 172 | + #[test] |
| 173 | + fn world_id_from_hash() { |
| 174 | + let hash = { |
| 175 | + let mut hash = H256::random(); |
| 176 | + hash[0] = PREFIX; |
| 177 | + hash[1] = 0; |
| 178 | + hash |
| 179 | + }; |
| 180 | + assert_eq!(::std::mem::size_of::<u16>(), ::std::mem::size_of::<WorldId>()); |
| 181 | + let world_id = ((hash[4] as WorldId) << 8) + (hash[5] as WorldId); |
| 182 | + let world_address = WorldAddress::from_hash(hash).unwrap(); |
| 183 | + assert_eq!(world_id, world_address.world_id()); |
| 184 | + } |
| 185 | +} |
0 commit comments