forked from HarukaMa/aleo-prover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.rs
166 lines (151 loc) · 5.76 KB
/
message.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
use std::io::Write;
use anyhow::anyhow;
use byteorder::{LittleEndian, ReadBytesExt};
use bytes::{Buf, BufMut, BytesMut};
use snarkvm::{
dpc::{testnet2::Testnet2, Address, BlockTemplate, PoSWProof},
traits::Network,
utilities::{FromBytes, ToBytes},
};
use tokio_util::codec::{Decoder, Encoder};
#[allow(clippy::large_enum_variant)]
pub enum ProverMessage {
// as in stratum, with an additional protocol version field
Authorize(Address<Testnet2>, String, u16),
AuthorizeResult(bool, Option<String>),
// combine notify and set_difficulty to be consistent
Notify(BlockTemplate<Testnet2>, u64),
// include block height to detect stales faster
Submit(u32, <Testnet2 as Network>::PoSWNonce, PoSWProof<Testnet2>),
// miners might want to know the stale rate, optionally provide a message
SubmitResult(bool, Option<String>),
Canary,
}
#[allow(dead_code)]
static VERSION: u16 = 1;
impl ProverMessage {
#[allow(dead_code)]
pub fn version() -> &'static u16 {
&VERSION
}
pub fn id(&self) -> u8 {
match self {
ProverMessage::Authorize(..) => 0,
ProverMessage::AuthorizeResult(..) => 1,
ProverMessage::Notify(..) => 2,
ProverMessage::Submit(..) => 3,
ProverMessage::SubmitResult(..) => 4,
ProverMessage::Canary => 5,
}
}
pub fn name(&self) -> &'static str {
match self {
ProverMessage::Authorize(..) => "Authorize",
ProverMessage::AuthorizeResult(..) => "AuthorizeResult",
ProverMessage::Notify(..) => "Notify",
ProverMessage::Submit(..) => "Submit",
ProverMessage::SubmitResult(..) => "SubmitResult",
ProverMessage::Canary => "Canary",
}
}
}
impl Encoder<ProverMessage> for ProverMessage {
type Error = anyhow::Error;
fn encode(&mut self, item: ProverMessage, dst: &mut BytesMut) -> Result<(), Self::Error> {
dst.extend_from_slice(&0u32.to_le_bytes());
let mut writer = dst.writer();
writer.write_all(&[item.id()])?;
match item {
ProverMessage::Authorize(addr, password, version) => {
bincode::serialize_into(&mut writer, &addr)?;
bincode::serialize_into(&mut writer, &password)?;
writer.write_all(&version.to_le_bytes())?;
}
ProverMessage::AuthorizeResult(result, message) | ProverMessage::SubmitResult(result, message) => {
writer.write_all(&[match result {
true => 1,
false => 0,
}])?;
if let Some(message) = message {
writer.write_all(&[1])?;
bincode::serialize_into(&mut writer, &message)?;
} else {
writer.write_all(&[0])?;
}
}
ProverMessage::Notify(template, difficulty) => {
template.write_le(&mut writer)?;
writer.write_all(&difficulty.to_le_bytes())?;
}
ProverMessage::Submit(height, nonce, proof) => {
writer.write_all(&height.to_le_bytes())?;
nonce.write_le(&mut writer)?;
proof.write_le(&mut writer)?;
}
ProverMessage::Canary => return Err(anyhow!("Use of unsupported message")),
}
let msg_len = dst.len() - 4;
dst[..4].copy_from_slice(&(msg_len as u32).to_le_bytes());
Ok(())
}
}
impl Decoder for ProverMessage {
type Error = anyhow::Error;
type Item = ProverMessage;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
if src.len() < 4 {
return Ok(None);
}
let length = u32::from_le_bytes(src[..4].try_into().unwrap()) as usize;
if length > 1048576 {
return Err(anyhow!("Message too long"));
}
if src.len() < 4 + length {
return Ok(None);
}
let reader = &mut src.reader();
reader.read_u32::<LittleEndian>()?;
let msg_id = reader.read_u8()?;
let msg = match msg_id {
0 => {
let addr = bincode::deserialize_from(&mut *reader)?;
let password = bincode::deserialize_from(&mut *reader)?;
let version = reader.read_u16::<LittleEndian>()?;
ProverMessage::Authorize(addr, password, version)
}
1 => {
let result = reader.read_u8()? == 1;
let message = if reader.read_u8()? == 1 {
Some(bincode::deserialize_from(reader)?)
} else {
None
};
ProverMessage::AuthorizeResult(result, message)
}
2 => {
let template = BlockTemplate::<Testnet2>::read_le(&mut *reader)?;
let difficulty = reader.read_u64::<LittleEndian>()?;
ProverMessage::Notify(template, difficulty)
}
3 => {
let height = reader.read_u32::<LittleEndian>()?;
let nonce = <Testnet2 as Network>::PoSWNonce::read_le(&mut *reader)?;
let proof = PoSWProof::<Testnet2>::read_le(&mut *reader)?;
ProverMessage::Submit(height, nonce, proof)
}
4 => {
let result = reader.read_u8()? == 1;
let message = if reader.read_u8()? == 1 {
Some(bincode::deserialize_from(reader)?)
} else {
None
};
ProverMessage::SubmitResult(result, message)
}
_ => {
return Err(anyhow!("Unknown message id: {}", msg_id));
}
};
Ok(Some(msg))
}
}