-
Notifications
You must be signed in to change notification settings - Fork 12
/
blockchain.rs
159 lines (134 loc) · 3.75 KB
/
blockchain.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
use crate::types::Timestamp;
use minicbor::encode::{Error, Write};
use minicbor::{decode, Decode, Decoder, Encode, Encoder};
#[derive(Clone, Debug, PartialEq)]
pub enum SingleBlockQuery {
Hash(Vec<u8>),
Height(u64),
}
impl<C> Encode<C> for SingleBlockQuery {
fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> {
match &self {
SingleBlockQuery::Hash(hash) => {
e.map(1)?.u8(0)?.bytes(hash)?;
}
SingleBlockQuery::Height(height) => {
e.map(1)?.u8(1)?.u64(*height)?;
}
}
Ok(())
}
}
impl<'d, C> Decode<'d, C> for SingleBlockQuery {
fn decode(d: &mut Decoder<'d>, _: &mut C) -> Result<Self, decode::Error> {
let mut indefinite = false;
let key = match d.map()? {
None => {
indefinite = true;
d.u8()
}
Some(1) => d.u8(),
Some(_) => Err(decode::Error::message(
"Invalid length for single block query map.",
)),
}?;
let result = match key {
0 => Ok(SingleBlockQuery::Hash(d.bytes()?.to_vec())),
1 => Ok(SingleBlockQuery::Height(d.u64()?)),
x => Err(decode::Error::unknown_variant(u32::from(x))),
};
if indefinite {
d.skip()?;
}
result
}
}
#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq)]
#[cbor(map)]
pub struct BlockIdentifier {
#[cbor(n(0), with = "minicbor::bytes")]
pub hash: Vec<u8>,
#[n(1)]
pub height: u64,
}
impl BlockIdentifier {
pub fn new(hash: Vec<u8>, height: u64) -> Self {
Self { hash, height }
}
pub fn genesis() -> Self {
Self::new(vec![], 0)
}
}
#[derive(Clone, Decode, Encode)]
#[cbor(map)]
pub struct TransactionIdentifier {
#[cbor(n(0), with = "minicbor::bytes")]
pub hash: Vec<u8>,
}
#[derive(Clone, Decode, Encode)]
#[cbor(map)]
pub struct Transaction {
#[n(0)]
pub id: TransactionIdentifier,
#[cbor(n(1), with = "minicbor::bytes")]
pub content: Option<Vec<u8>>,
}
#[derive(Clone, Decode, Encode)]
#[cbor(map)]
pub struct Block {
#[n(0)]
pub id: BlockIdentifier,
#[n(1)]
pub parent: BlockIdentifier,
#[n(2)]
pub app_hash: Option<Vec<u8>>,
#[n(3)]
pub timestamp: Timestamp,
#[n(4)]
pub txs_count: u64,
#[n(5)]
pub txs: Vec<Transaction>,
}
// TODO: This doesn't look right according to the spec
// single-transaction-query =
// ; A transaction hash.
// { 0 => bstr }
// ; A block + transaction index.
// / { 1 => [ single-block-query, uint ] }
#[derive(Clone, Debug, PartialEq)]
pub enum SingleTransactionQuery {
Hash(Vec<u8>),
}
impl<C> Encode<C> for SingleTransactionQuery {
fn encode<W: Write>(&self, e: &mut Encoder<W>, _: &mut C) -> Result<(), Error<W::Error>> {
match &self {
SingleTransactionQuery::Hash(hash) => {
e.map(1)?.u8(0)?.bytes(hash)?;
}
}
Ok(())
}
}
impl<'d, C> Decode<'d, C> for SingleTransactionQuery {
fn decode(d: &mut Decoder<'d>, _: &mut C) -> Result<Self, decode::Error> {
let mut indefinite = false;
let key = match d.map()? {
None => {
indefinite = true;
d.u8()
}
Some(1) => d.u8(),
Some(_) => Err(decode::Error::message(
"Invalid hash for single transaction query.",
)),
}?;
let result = match key {
0 => Ok(SingleTransactionQuery::Hash(d.bytes()?.to_vec())),
x => Err(decode::Error::unknown_variant(u32::from(x))),
};
if indefinite {
d.skip()?;
}
result
}
}