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
use crate::codec as cyfs_base;
use crate::*;
use generic_array::typenum::U32;
use generic_array::GenericArray;
use std::convert::TryFrom;
use std::ops::{Deref, DerefMut};
#[derive(Clone, Debug, RawEncode, RawDecode)]
pub struct ERC20 {
pub name: String,
pub symbol: String,
pub decimals: u8,
pub total_supply: u64,
}
#[derive(Clone, Debug, RawEncode, RawDecode)]
pub struct SNContract {
pub service_id: ObjectId,
pub account: UnionAccount,
pub start_time: u64,
pub stop_time: u64,
}
#[derive(Clone, Debug, RawEncode, RawDecode)]
pub enum ServiceAuthType {
Any,
WhiteList,
BlackList,
}
#[derive(Clone, Debug, RawEncode, RawDecode)]
pub struct SNContractBody {
auth_type: ServiceAuthType,
list: Vec<ObjectId>,
}
#[derive(Clone, Debug, RawEncode, RawDecode)]
pub enum ContractTypeCode {
DSG,
Solidity,
}
#[derive(Clone, Debug, RawEncode, RawDecode)]
pub struct ContractDescContent<T> {
pub contract_type: ContractTypeCode,
pub data: T,
}
impl<T> Deref for ContractDescContent<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<T> DerefMut for ContractDescContent<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
#[derive(Clone, Debug, RawEncode, RawDecode)]
pub struct ContractData {
pub data: Vec<u8>,
}
impl Deref for ContractData {
type Target = Vec<u8>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<T> DescContent for ContractDescContent<T> {
fn obj_type() -> u16 {
ObjectTypeCode::Contract.into()
}
type OwnerType = Option<ObjectId>;
type AreaType = SubDescNone;
type AuthorType = Option<ObjectId>;
type PublicKeyType = SubDescNone;
}
#[derive(Clone, Debug)]
pub struct ContractBodyContent<T> {
pub data: T,
}
impl TryFrom<protos::ContractBodyContent> for ContractBodyContent<ContractData> {
type Error = BuckyError;
fn try_from(mut value: protos::ContractBodyContent) -> BuckyResult<Self> {
Ok(Self {
data: ContractData {
data: value.take_data(),
},
})
}
}
impl TryFrom<&ContractBodyContent<ContractData>> for protos::ContractBodyContent {
type Error = BuckyError;
fn try_from(value: &ContractBodyContent<ContractData>) -> BuckyResult<Self> {
let mut ret = protos::ContractBodyContent::new();
ret.set_data(value.data.data.clone());
Ok(ret)
}
}
impl<T> BodyContent for ContractBodyContent<T> {
fn format(&self) -> u8 {
OBJECT_CONTENT_CODEC_FORMAT_PROTOBUF
}
}
type ContractBodyContentContractData = ContractBodyContent<ContractData>;
inner_impl_default_protobuf_raw_codec!(
ContractBodyContentContractData,
protos::ContractBodyContent
);
type ContractType =
NamedObjType<ContractDescContent<ContractData>, ContractBodyContent<ContractData>>;
pub type ContractBuilder =
NamedObjectBuilder<ContractDescContent<ContractData>, ContractBodyContent<ContractData>>;
pub type ContractDesc = NamedObjectDesc<ContractDescContent<ContractData>>;
pub type ContractId = NamedObjectId<ContractType>;
pub type Contract = NamedObjectBase<ContractType>;
impl ContractDesc {
pub fn contract_id(&self) -> ContractId {
ContractId::try_from(self.calculate_id()).unwrap()
}
}
impl ContractId {
pub fn from_hash_256(hash: GenericArray<u8, U32>) -> Self {
let mut id = Self::default();
id.object_id_mut().as_mut_slice()[1..].copy_from_slice(&hash[1..]);
id
}
}
impl NamedObjectBase<ContractType> {
pub fn new(
owner: ObjectId,
author: ObjectId,
desc: ContractDescContent<ContractData>,
body: ContractBodyContent<ContractData>,
) -> ContractBuilder {
ContractBuilder::new(desc, body).owner(owner).author(author)
}
}
#[cfg(test)]
mod test {
use crate::{Contract, RawConvertTo, RawFrom};
use crate::{
ContractBodyContent, ContractData, ContractDescContent, ContractTypeCode, ObjectId,
};
#[test]
fn contract() {
let desc = ContractDescContent {
contract_type: ContractTypeCode::DSG,
data: ContractData { data: Vec::new() },
};
let body = ContractBodyContent {
data: ContractData { data: Vec::new() },
};
let object = Contract::new(ObjectId::default(), ObjectId::default(), desc, body).build();
let buf = object.to_vec().unwrap();
let _obj = Contract::clone_from_slice(&buf).unwrap();
}
}