referrerpolicy=no-referrer-when-downgrade

pallet_alliance/
types.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use alloc::vec::Vec;
19use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
20use frame_support::{traits::ConstU32, BoundedVec};
21use scale_info::TypeInfo;
22use sp_runtime::RuntimeDebug;
23
24/// A Multihash instance that only supports the basic functionality and no hashing.
25#[derive(
26	Clone,
27	PartialEq,
28	Eq,
29	PartialOrd,
30	Ord,
31	RuntimeDebug,
32	Encode,
33	Decode,
34	DecodeWithMemTracking,
35	TypeInfo,
36	MaxEncodedLen,
37)]
38pub struct Multihash {
39	/// The code of the Multihash.
40	pub code: u64,
41	/// The digest.
42	pub digest: BoundedVec<u8, ConstU32<68>>, // 4 byte dig size + 64 bytes hash digest
43}
44
45impl Multihash {
46	/// Returns the size of the digest.
47	pub fn size(&self) -> usize {
48		self.digest.len()
49	}
50}
51
52/// The version of the CID.
53#[derive(
54	Clone,
55	Copy,
56	PartialEq,
57	Eq,
58	PartialOrd,
59	Ord,
60	RuntimeDebug,
61	Encode,
62	Decode,
63	DecodeWithMemTracking,
64	TypeInfo,
65	MaxEncodedLen,
66)]
67pub enum Version {
68	/// CID version 0.
69	V0,
70	/// CID version 1.
71	V1,
72}
73
74/// Representation of a CID.
75///
76/// The generic is about the allocated size of the multihash.
77#[derive(
78	Clone,
79	PartialEq,
80	Eq,
81	PartialOrd,
82	Ord,
83	RuntimeDebug,
84	Encode,
85	Decode,
86	DecodeWithMemTracking,
87	TypeInfo,
88	MaxEncodedLen,
89)]
90pub struct Cid {
91	/// The version of CID.
92	pub version: Version,
93	/// The codec of CID.
94	pub codec: u64,
95	/// The multihash of CID.
96	pub hash: Multihash,
97}
98
99impl Cid {
100	/// Creates a new CIDv0.
101	pub fn new_v0(sha2_256_digest: impl Into<Vec<u8>>) -> Self {
102		/// DAG-PB multicodec code
103		const DAG_PB: u64 = 0x70;
104		/// The SHA_256 multicodec code
105		const SHA2_256: u64 = 0x12;
106
107		let digest = sha2_256_digest.into();
108		assert_eq!(digest.len(), 32);
109
110		Self {
111			version: Version::V0,
112			codec: DAG_PB,
113			hash: Multihash { code: SHA2_256, digest: digest.try_into().expect("msg") },
114		}
115	}
116}
117
118/// Witness data for the `disband` call.
119#[derive(
120	Copy,
121	Clone,
122	Encode,
123	Decode,
124	DecodeWithMemTracking,
125	Eq,
126	PartialEq,
127	RuntimeDebug,
128	MaxEncodedLen,
129	TypeInfo,
130	Default,
131)]
132pub struct DisbandWitness {
133	/// Total number of fellow members in the current Alliance.
134	#[codec(compact)]
135	pub(super) fellow_members: u32,
136	/// Total number of ally members in the current Alliance.
137	#[codec(compact)]
138	pub(super) ally_members: u32,
139}
140
141#[cfg(test)]
142impl DisbandWitness {
143	// Creates new DisbandWitness.
144	pub(super) fn new(fellow_members: u32, ally_members: u32) -> Self {
145		Self { fellow_members, ally_members }
146	}
147}
148
149impl DisbandWitness {
150	pub(super) fn is_zero(self) -> bool {
151		self == Self::default()
152	}
153}