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
use byteorder::{ByteOrder, LittleEndian};
use ckb_types::{
    packed::{Byte32, Header},
    prelude::*,
};
use serde::{Deserialize, Serialize};
use std::any::Any;
use std::fmt;
use std::sync::Arc;

mod dummy;
mod eaglesong;
mod eaglesong_blake2b;

pub use crate::dummy::DummyPowEngine;
pub use crate::eaglesong::EaglesongPowEngine;
pub use crate::eaglesong_blake2b::EaglesongBlake2bPowEngine;

#[derive(Clone, Serialize, Deserialize, Eq, PartialEq, Hash, Debug)]
#[serde(tag = "func", content = "params")]
pub enum Pow {
    Dummy,
    Eaglesong,
    EaglesongBlake2b,
}

impl fmt::Display for Pow {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Pow::Dummy => write!(f, "Dummy"),
            Pow::Eaglesong => write!(f, "Eaglesong"),
            Pow::EaglesongBlake2b => write!(f, "EaglesongBlake2b"),
        }
    }
}

impl Pow {
    pub fn engine(&self) -> Arc<dyn PowEngine> {
        match *self {
            Pow::Dummy => Arc::new(DummyPowEngine),
            Pow::Eaglesong => Arc::new(EaglesongPowEngine),
            Pow::EaglesongBlake2b => Arc::new(EaglesongBlake2bPowEngine),
        }
    }

    pub fn is_dummy(&self) -> bool {
        *self == Pow::Dummy
    }
}

pub fn pow_message(pow_hash: &Byte32, nonce: u128) -> [u8; 48] {
    let mut message = [0; 48];
    message[0..32].copy_from_slice(pow_hash.as_slice());
    LittleEndian::write_u128(&mut message[32..48], nonce);
    message
}

pub trait PowEngine: Send + Sync + AsAny {
    fn verify(&self, header: &Header) -> bool;
}

pub trait AsAny {
    fn as_any(&self) -> &dyn Any;
}

impl<T: Any> AsAny for T {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use ckb_hash::blake2b_256;
    #[test]
    fn test_pow_message() {
        let zero_hash = blake2b_256(&[]).pack();
        let nonce = u128::max_value();
        let message = pow_message(&zero_hash, nonce);
        assert_eq!(
            message.to_vec(),
            [
                68, 244, 198, 151, 68, 213, 248, 197, 93, 100, 32, 98, 148, 157, 202, 228, 155,
                196, 231, 239, 67, 211, 136, 197, 161, 47, 66, 181, 99, 61, 22, 62, 255, 255, 255,
                255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
            ]
            .to_vec()
        );
    }
}