forked from nervosnetwork/ckb
/
convert.rs
318 lines (275 loc) · 10.7 KB
/
convert.rs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
use crate::cast;
use crate::protocol_generated::ckb::protocol as ckb_protocol;
use crate::FlatbuffersVectorIterator;
use ckb_core;
use ckb_util::{TryFrom, TryInto};
use failure::Error as FailureError;
use numext_fixed_hash::H256;
use numext_fixed_uint::U256;
impl From<&H256> for ckb_protocol::H256 {
fn from(h256: &H256) -> Self {
let bytes = h256.as_fixed_bytes();
Self::new(
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15],
bytes[16], bytes[17], bytes[18], bytes[19], bytes[20], bytes[21], bytes[22], bytes[23],
bytes[24], bytes[25], bytes[26], bytes[27], bytes[28], bytes[29], bytes[30], bytes[31],
)
}
}
impl TryFrom<&ckb_protocol::H256> for H256 {
type Error = FailureError;
fn try_from(h256: &ckb_protocol::H256) -> Result<Self, Self::Error> {
let ret = H256::from_slice(&[
h256.u0(),
h256.u1(),
h256.u2(),
h256.u3(),
h256.u4(),
h256.u5(),
h256.u6(),
h256.u7(),
h256.u8_(),
h256.u9(),
h256.u10(),
h256.u11(),
h256.u12(),
h256.u13(),
h256.u14(),
h256.u15(),
h256.u16_(),
h256.u17(),
h256.u18(),
h256.u19(),
h256.u20(),
h256.u21(),
h256.u22(),
h256.u23(),
h256.u24(),
h256.u25(),
h256.u26(),
h256.u27(),
h256.u28(),
h256.u29(),
h256.u30(),
h256.u31(),
])?;
Ok(ret)
}
}
impl From<&ckb_core::transaction::ProposalShortId> for ckb_protocol::ProposalShortId {
fn from(short_id: &ckb_core::transaction::ProposalShortId) -> Self {
let bytes = *short_id;
Self::new(
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
bytes[8], bytes[9],
)
}
}
impl TryFrom<&ckb_protocol::ProposalShortId> for ckb_core::transaction::ProposalShortId {
type Error = FailureError;
fn try_from(short_id: &ckb_protocol::ProposalShortId) -> Result<Self, Self::Error> {
let ret = cast!(Self::from_slice(&[
short_id.u0(),
short_id.u1(),
short_id.u2(),
short_id.u3(),
short_id.u4(),
short_id.u5(),
short_id.u6(),
short_id.u7(),
short_id.u8_(),
short_id.u9(),
]))?;
Ok(ret)
}
}
impl<'a> TryFrom<ckb_protocol::Block<'a>> for ckb_core::block::Block {
type Error = FailureError;
fn try_from(block: ckb_protocol::Block<'a>) -> Result<Self, Self::Error> {
let commit_transactions: Result<Vec<ckb_core::transaction::Transaction>, FailureError> =
FlatbuffersVectorIterator::new(cast!(block.commit_transactions())?)
.map(TryInto::try_into)
.collect();
let uncles: Result<Vec<ckb_core::uncle::UncleBlock>, FailureError> =
FlatbuffersVectorIterator::new(cast!(block.uncles())?)
.map(TryInto::try_into)
.collect();
let proposal_transactions: Result<
Vec<ckb_core::transaction::ProposalShortId>,
FailureError,
> = cast!(block.proposal_transactions())?
.iter()
.map(TryInto::try_into)
.collect();
let header = cast!(block.header())?;
Ok(ckb_core::block::BlockBuilder::default()
.header(TryInto::try_into(header)?)
.uncles(uncles?)
.commit_transactions(commit_transactions?)
.proposal_transactions(proposal_transactions?)
.build())
}
}
impl<'a> TryFrom<ckb_protocol::UncleBlock<'a>> for ckb_core::uncle::UncleBlock {
type Error = FailureError;
fn try_from(uncle_block: ckb_protocol::UncleBlock<'a>) -> Result<Self, Self::Error> {
let proposal_transactions: Result<
Vec<ckb_core::transaction::ProposalShortId>,
FailureError,
> = cast!(uncle_block.proposal_transactions())?
.iter()
.map(TryInto::try_into)
.collect();
let header = cast!(uncle_block.header())?;
Ok(ckb_core::uncle::UncleBlock {
header: TryInto::try_into(header)?,
proposal_transactions: proposal_transactions?,
})
}
}
impl<'a> TryFrom<ckb_protocol::Header<'a>> for ckb_core::header::Header {
type Error = FailureError;
fn try_from(header: ckb_protocol::Header<'a>) -> Result<Self, Self::Error> {
let parent_hash = cast!(header.parent_hash())?;
let txs_commit = cast!(header.txs_commit())?;
let txs_proposal = cast!(header.txs_proposal())?;
let witnesses_root = cast!(header.witnesses_root())?;
let uncles_hash = cast!(header.uncles_hash())?;
Ok(ckb_core::header::HeaderBuilder::default()
.version(header.version())
.parent_hash(TryInto::try_into(parent_hash)?)
.timestamp(header.timestamp())
.number(header.number())
.txs_commit(TryInto::try_into(txs_commit)?)
.txs_proposal(TryInto::try_into(txs_proposal)?)
.witnesses_root(TryInto::try_into(witnesses_root)?)
.difficulty(U256::from_little_endian(cast!(header
.difficulty()
.and_then(|d| d.seq()))?)?)
.uncles_hash(TryInto::try_into(uncles_hash)?)
.nonce(header.nonce())
.proof(cast!(header
.proof()
.and_then(|p| p.seq())
.map(|p| p.to_vec()))?)
.uncles_count(header.uncles_count())
.build())
}
}
impl<'a> TryFrom<ckb_protocol::Transaction<'a>> for ckb_core::transaction::Transaction {
type Error = FailureError;
fn try_from(transaction: ckb_protocol::Transaction<'a>) -> Result<Self, Self::Error> {
let deps: Result<Vec<ckb_core::transaction::OutPoint>, FailureError> =
FlatbuffersVectorIterator::new(cast!(transaction.deps())?)
.map(TryInto::try_into)
.collect();
let inputs: Result<Vec<ckb_core::transaction::CellInput>, FailureError> =
FlatbuffersVectorIterator::new(cast!(transaction.inputs())?)
.map(TryInto::try_into)
.collect();
let outputs: Result<Vec<ckb_core::transaction::CellOutput>, FailureError> =
FlatbuffersVectorIterator::new(cast!(transaction.outputs())?)
.map(TryInto::try_into)
.collect();
let witnesses: Result<Vec<ckb_core::transaction::Witness>, FailureError> =
FlatbuffersVectorIterator::new(cast!(transaction.witnesses())?)
.map(TryInto::try_into)
.collect();
Ok(ckb_core::transaction::TransactionBuilder::default()
.version(transaction.version())
.deps(deps?)
.inputs(inputs?)
.outputs(outputs?)
.witnesses(witnesses?)
.build())
}
}
impl<'a> TryFrom<ckb_protocol::ValidTransaction<'a>>
for (ckb_core::transaction::Transaction, ckb_core::Cycle)
{
type Error = FailureError;
fn try_from(vtx: ckb_protocol::ValidTransaction<'a>) -> Result<Self, Self::Error> {
let tx = cast!(vtx.transaction())?;
let cycles = vtx.cycles();
Ok((TryInto::try_into(tx)?, cycles))
}
}
impl<'a> TryFrom<ckb_protocol::Witness<'a>> for ckb_core::transaction::Witness {
type Error = FailureError;
fn try_from(wit: ckb_protocol::Witness<'a>) -> Result<Self, Self::Error> {
let data: Option<Vec<Vec<u8>>> = FlatbuffersVectorIterator::new(cast!(wit.data())?)
.map(|item| item.seq().map(|s| s.to_vec()))
.collect();
Ok(cast!(data)?)
}
}
impl<'a> TryFrom<ckb_protocol::OutPoint<'a>> for ckb_core::transaction::OutPoint {
type Error = FailureError;
fn try_from(out_point: ckb_protocol::OutPoint<'a>) -> Result<Self, Self::Error> {
let hash = cast!(out_point.hash())?;
Ok(ckb_core::transaction::OutPoint {
hash: TryInto::try_into(hash)?,
index: out_point.index(),
})
}
}
impl<'a> TryFrom<ckb_protocol::Script<'a>> for ckb_core::script::Script {
type Error = FailureError;
fn try_from(script: ckb_protocol::Script<'a>) -> Result<Self, Self::Error> {
let args: Option<Vec<Vec<u8>>> = FlatbuffersVectorIterator::new(cast!(script.args())?)
.map(|argument| argument.seq().map(|s| s.to_vec()))
.collect();
let binary_hash = match script.binary_hash() {
Some(binary_hash) => Some(TryInto::try_into(binary_hash)?),
None => None,
};
Ok(ckb_core::script::Script {
args: cast!(args)?,
binary_hash: cast!(binary_hash)?,
})
}
}
impl<'a> TryFrom<ckb_protocol::CellInput<'a>> for ckb_core::transaction::CellInput {
type Error = FailureError;
fn try_from(cell_input: ckb_protocol::CellInput<'a>) -> Result<Self, Self::Error> {
let hash = cast!(cell_input.hash())?;
let args: Option<Vec<Vec<u8>>> = FlatbuffersVectorIterator::new(cast!(cell_input.args())?)
.map(|argument| argument.seq().map(|s| s.to_vec()))
.collect();
Ok(ckb_core::transaction::CellInput {
previous_output: ckb_core::transaction::OutPoint {
hash: TryInto::try_into(hash)?,
index: cell_input.index(),
},
valid_since: cell_input.valid_since(),
args: cast!(args)?,
})
}
}
impl<'a> TryFrom<ckb_protocol::CellOutput<'a>> for ckb_core::transaction::CellOutput {
type Error = FailureError;
fn try_from(cell_output: ckb_protocol::CellOutput<'a>) -> Result<Self, Self::Error> {
let lock = cast!(cell_output.lock())?;
let type_ = match cell_output.type_() {
Some(type_) => Some(TryInto::try_into(type_)?),
None => None,
};
Ok(ckb_core::transaction::CellOutput {
capacity: cell_output.capacity(),
data: cast!(cell_output.data().and_then(|s| s.seq()))?.to_vec(),
lock: TryInto::try_into(lock)?,
type_,
})
}
}
impl<'a> TryFrom<ckb_protocol::IndexTransaction<'a>> for ckb_core::transaction::IndexTransaction {
type Error = FailureError;
fn try_from(it: ckb_protocol::IndexTransaction<'a>) -> Result<Self, Self::Error> {
let transaction = cast!(it.transaction())?;
Ok(ckb_core::transaction::IndexTransaction {
index: it.index() as usize,
transaction: TryInto::try_into(transaction)?,
})
}
}