sv1_api 3.0.0

API for bridging SV1 miners to SV2 pools
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use crate::error::{self, Error};
use binary_sv2::{B032, U256};
use bitcoin_hashes::hex::{FromHex, ToHex};
use byteorder::{BigEndian, ByteOrder, LittleEndian, WriteBytesExt};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt;
use std::{convert::TryFrom, mem::size_of, ops::BitAnd};

/// Helper type that allows simple serialization and deserialization of byte vectors
/// that are represented as hex strings in JSON.
/// Extranonce must be less than or equal to 32 bytes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Extranonce<'a>(pub B032<'a>);

impl fmt::Display for Extranonce<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0.inner_as_ref().to_hex())
    }
}

impl Extranonce<'_> {
    pub fn len(&self) -> usize {
        self.0.inner_as_ref().len()
    }
    pub fn is_empty(&self) -> bool {
        self.0.inner_as_ref().is_empty()
    }
}

impl<'a> TryFrom<Vec<u8>> for Extranonce<'a> {
    type Error = Error<'a>;
    fn try_from(value: Vec<u8>) -> Result<Self, Error<'a>> {
        Ok(Extranonce(B032::try_from(value)?))
    }
}

impl<'a> From<Extranonce<'a>> for Vec<u8> {
    fn from(v: Extranonce<'a>) -> Self {
        v.0.to_vec()
    }
}

impl<'a> From<Extranonce<'a>> for Value {
    fn from(eb: Extranonce<'a>) -> Self {
        Into::<String>::into(eb).into()
    }
}

/// fix for error on odd-length hex sequences
/// FIXME: find a nicer solution
fn hex_decode(s: &str) -> Result<Vec<u8>, Error<'static>> {
    if s.len() % 2 != 0 {
        Vec::<u8>::from_hex(&format!("0{s}")).map_err(Error::HexError)
    } else {
        Vec::<u8>::from_hex(s).map_err(Error::HexError)
    }
}

impl<'a> TryFrom<&str> for Extranonce<'a> {
    type Error = error::Error<'a>;

    fn try_from(value: &str) -> Result<Self, Error<'a>> {
        Ok(Extranonce(B032::try_from(hex_decode(value)?)?))
    }
}

impl<'a> From<Extranonce<'a>> for String {
    fn from(bytes: Extranonce<'a>) -> String {
        bytes.0.inner_as_ref().to_hex()
    }
}

impl BitAnd<u32> for HexU32Be {
    type Output = u32;

    fn bitand(self, rhs: u32) -> Self::Output {
        self.0 & rhs
    }
}

impl<'a> From<B032<'a>> for Extranonce<'a> {
    fn from(b: B032<'a>) -> Self {
        Extranonce(b)
    }
}

/// Big-endian alternative of the HexU32
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HexU32Be(pub u32);

impl fmt::Display for HexU32Be {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:08x}", self.0)
    }
}

impl HexU32Be {
    pub fn check_mask(&self, mask: &HexU32Be) -> bool {
        ((!self.0) & mask.0) == 0
    }
}

impl From<HexU32Be> for Value {
    fn from(eu: HexU32Be) -> Self {
        Into::<String>::into(eu).into()
    }
}

impl TryFrom<&str> for HexU32Be {
    type Error = Error<'static>;

    fn try_from(value: &str) -> Result<Self, Error<'static>> {
        let expected_len = 8;
        if value.len() > expected_len {
            return Err(Error::InvalidHexLen(value.to_string()));
        }

        let delta_len = expected_len - value.len();
        let mut prefix = "".to_string();
        for _ in 0..delta_len {
            prefix.push('0');
        }
        prefix.push_str(value);
        let parsed_bytes: [u8; 4] = FromHex::from_hex(&prefix)?;
        Ok(HexU32Be(u32::from_be_bytes(parsed_bytes)))
    }
}

/// Helper Serializer
impl From<HexU32Be> for String {
    fn from(v: HexU32Be) -> Self {
        v.0.to_be_bytes().to_hex()
    }
}

impl From<u32> for HexU32Be {
    fn from(a: u32) -> Self {
        HexU32Be(a)
    }
}

//Example of serialization for testing purpose
impl Serialize for HexU32Be {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let serialized_string = self.0.to_be_bytes().to_hex();
        serializer.serialize_str(&serialized_string)
    }
}

//Example of deserialization for testing purpose
impl<'de> Deserialize<'de> for HexU32Be {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let hex_string: String = Deserialize::deserialize(deserializer)?;

        match u32::from_str_radix(&hex_string, 16) {
            Ok(value) => Ok(HexU32Be(value)),
            Err(_) => Err(serde::de::Error::custom("Invalid hex value")),
        }
    }
}

/// PrevHash in Stratum V1 has brain-damaged serialization as it swaps bytes of every u32 word
/// into big endian. Therefore, we need a special type for it
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PrevHash<'a>(pub U256<'a>);

impl fmt::Display for PrevHash<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Reuse the Stratum V1 serialization logic
        let s = String::from(self.clone());
        f.write_str(&s)
    }
}

impl<'a> From<PrevHash<'a>> for Vec<u8> {
    fn from(p_hash: PrevHash<'a>) -> Self {
        p_hash.0.to_vec()
    }
}

impl<'a> TryFrom<&str> for PrevHash<'a> {
    type Error = Error<'a>;

    fn try_from(value: &str) -> Result<Self, Error<'a>> {
        // Reorder PrevHash will be stored via this cursor
        // let mut prev_hash_cursor = std::io::Cursor::new([0_u8; 32]);
        let mut prev_hash_arr = [0_u8; 32];

        // Decode the plain byte array and sanity check
        let prev_hash_stratum_order = hex_decode(value)?;

        match prev_hash_stratum_order.len() {
            32 => {
                // Swap every u32 from big endian to little endian byte order
                for (chunk, mut arr_chunks) in prev_hash_stratum_order
                    .chunks(size_of::<u32>())
                    .zip(prev_hash_arr.chunks_mut(size_of::<u32>()))
                {
                    let prev_hash_word = BigEndian::read_u32(chunk);
                    arr_chunks
                        .write_u32::<LittleEndian>(prev_hash_word)
                        .expect("Internal error: Could not write buffer");
                }
                Ok(PrevHash(prev_hash_arr.into()))
            }
            _ => Err(error::Error::BadBytesConvert(
                binary_sv2::Error::InvalidU256(prev_hash_stratum_order.len()),
            )),
        }
    }
}

impl From<PrevHash<'_>> for Value {
    fn from(ph: PrevHash) -> Self {
        Into::<String>::into(ph).into()
    }
}

/// Helper Serializer that peforms the reverse process of converting the prev hash into stratum V1
/// ordering
impl From<PrevHash<'_>> for String {
    fn from(v: PrevHash) -> Self {
        let mut prev_hash_stratum_cursor = std::io::Cursor::new(Vec::new());
        // swap every u32 from little endian to big endian
        for chunk in v.0.inner_as_ref().chunks(size_of::<u32>()) {
            let prev_hash_word = LittleEndian::read_u32(chunk);
            prev_hash_stratum_cursor
                .write_u32::<BigEndian>(prev_hash_word)
                .expect("Internal error: Could not write buffer");
        }
        prev_hash_stratum_cursor.into_inner().to_hex()
    }
}

// / Referencing the internal part of hex bytes
impl AsRef<[u8]> for PrevHash<'_> {
    fn as_ref(&self) -> &[u8] {
        self.0.inner_as_ref()
    }
}

/// Referencing the internal part of hex bytes
impl<'a> AsRef<U256<'a>> for PrevHash<'a> {
    fn as_ref(&self) -> &U256<'a> {
        &self.0
    }
}

/// Referencing the internal part of hex bytes
impl AsRef<[u8]> for Extranonce<'_> {
    fn as_ref(&self) -> &[u8] {
        self.0.inner_as_ref()
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MerkleNode<'a>(pub U256<'a>);

impl fmt::Display for MerkleNode<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0.inner_as_ref().to_hex())
    }
}

impl MerkleNode<'_> {
    pub fn is_empty(&self) -> bool {
        self.0.inner_as_ref().is_empty()
    }
}

impl<'a> TryFrom<Vec<u8>> for MerkleNode<'a> {
    type Error = Error<'a>;

    fn try_from(value: Vec<u8>) -> Result<Self, Error<'a>> {
        let merkle = U256::try_from(value).map_err(error::Error::from)?;
        Ok(MerkleNode(merkle))
    }
}

impl<'a> From<MerkleNode<'a>> for Vec<u8> {
    fn from(v: MerkleNode<'a>) -> Self {
        v.0.to_vec()
    }
}

impl<'a> From<MerkleNode<'a>> for Value {
    fn from(eb: MerkleNode<'a>) -> Self {
        Into::<String>::into(eb).into()
    }
}

/// Referencing the internal part of hex bytes
impl AsRef<[u8]> for MerkleNode<'_> {
    fn as_ref(&self) -> &[u8] {
        self.0.inner_as_ref()
    }
}

impl<'a> TryFrom<&str> for MerkleNode<'a> {
    type Error = Error<'a>;

    fn try_from(value: &str) -> Result<Self, Error<'a>> {
        let merkle = U256::try_from(hex_decode(value)?).map_err(error::Error::from)?;
        Ok(MerkleNode(merkle))
    }
}

impl<'a> From<MerkleNode<'a>> for String {
    fn from(bytes: MerkleNode<'a>) -> String {
        bytes.0.inner_as_ref().to_hex()
    }
}

/// Helper type that allows simple serialization and deserialization of byte vectors
/// that are represented as hex strings in JSON.
/// HexBytes must be less than or equal to 32 bytes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HexBytes(Vec<u8>);

impl fmt::Display for HexBytes {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0.to_hex())
    }
}

impl HexBytes {
    pub fn len(&self) -> usize {
        self.0.len()
    }
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl From<Vec<u8>> for HexBytes {
    fn from(value: Vec<u8>) -> Self {
        HexBytes(value)
    }
}

impl From<HexBytes> for Vec<u8> {
    fn from(v: HexBytes) -> Self {
        v.0
    }
}

impl From<HexBytes> for Value {
    fn from(eb: HexBytes) -> Self {
        Into::<String>::into(eb).into()
    }
}

/// Referencing the internal part of hex bytes
impl AsRef<Vec<u8>> for HexBytes {
    fn as_ref(&self) -> &Vec<u8> {
        &self.0
    }
}

impl TryFrom<&str> for HexBytes {
    type Error = Error<'static>;

    fn try_from(value: &str) -> Result<Self, Error<'static>> {
        Ok(HexBytes(hex_decode(value)?))
    }
}

impl From<HexBytes> for String {
    fn from(bytes: HexBytes) -> String {
        bytes.0.to_hex()
    }
}

//Example of serialization for testing purpose
impl Serialize for HexBytes {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let bytes = self.0.as_ref();
        let serialized_string = String::from_utf8_lossy(bytes);
        serializer.serialize_str(&serialized_string)
    }
}

//Example of deserialization for testing purpose
impl<'de> Deserialize<'de> for HexBytes {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let hex_string: String = Deserialize::deserialize(deserializer)?;
        let bytes = hex_string.as_bytes().to_vec();
        Ok(HexBytes(bytes))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[quickcheck_macros::quickcheck]
    fn test_prev_hash(mut bytes: Vec<u8>) -> bool {
        bytes.resize(32, 0);
        let be_hex = bytes.to_hex();
        let me = PrevHash::try_from(be_hex.clone().as_str()).unwrap();
        let back_to_hex = String::from(me.clone());
        let back_to_hex_value = Value::from(me.clone());
        let value_to_string = back_to_hex_value.as_str().unwrap();

        let chunk_size: usize = size_of::<u32>();
        let me_chunks = me.clone().0.to_vec();
        let me_chunks = me_chunks.chunks(chunk_size);
        for (be_chunk, le_chunk) in bytes.clone().chunks(chunk_size).zip(me_chunks) {
            let le_chunk = [le_chunk[0], le_chunk[1], le_chunk[2], le_chunk[3]];
            let be_chunk = [be_chunk[0], be_chunk[1], be_chunk[2], be_chunk[3]];
            let le_u32 = u32::from_le_bytes(le_chunk);
            let be_u32 = u32::from_be_bytes(be_chunk);

            if le_u32 != be_u32 {
                return false;
            }
        }

        be_hex == back_to_hex && be_hex == value_to_string
    }

    #[test]
    fn merkle_node_try_from_vec_invalid_len_returns_err() {
        assert!(MerkleNode::try_from(vec![0_u8; 31]).is_err());
    }

    #[test]
    fn merkle_node_try_from_str_invalid_len_returns_err() {
        let invalid_hex = "00".repeat(31);
        assert!(MerkleNode::try_from(invalid_hex.as_str()).is_err());
    }
}