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
use crate::instructions_data::dvl_deserializable_instruction::DvlDeserializableInstruction;

pub const INSTRUCTION_PAYOFF_SIZE: usize = 12;
pub const INSTRUCTION_PAYOFF_VERSION: u8 = 2;

#[repr(C)]
pub struct InstructionPayoff {
    pub cmd: u8,
    pub version: u8,
    pub reserved: [u8; 2],
    pub worker_id: u32,
    pub pool_id: u32,
}

impl<'a> DvlDeserializableInstruction<'a> for InstructionPayoff {
    #[inline(always)]
    fn expected_size() -> usize { INSTRUCTION_PAYOFF_SIZE }
    #[inline(always)]
    fn expected_version() -> u8 { INSTRUCTION_PAYOFF_VERSION }
}

#[cfg(not(feature = "on-chain"))]
#[cfg(test)]
mod tests {
    use std::mem;
    use crate::instructions_data::constructors::payoff::PayoffParams;
    use crate::instructions_data::dvl_instruction_data::DvlInstruction;
    use crate::instructions_data::payoff::{INSTRUCTION_PAYOFF_SIZE, InstructionPayoff};

    pub const INSTR_PAYOFF_CMD_OFFSET: usize = 0;
    pub const INSTR_PAYOFF_VERSION_OFFSET: usize = 1;
    pub const INSTR_PAYOFF_RESERVED_OFFSET: usize = 2;
    pub const INSTR_PAYOFF_WORKER_ID_OFFSET: usize = 4;
    pub const INSTR_PAYOFF_POOL_ID_OFFSET: usize = 8;

    #[test]
    fn test_instruction_data_offsets() {
        let payoff_params = PayoffParams {
            worker_id: 1,
            pool_id: 1,
        };

        let data = DvlInstruction::new::<InstructionPayoff>(payoff_params).unwrap();

        let base_ptr = &*data as *const _ as usize;

        assert_eq!(&data.cmd as *const _ as usize - base_ptr, INSTR_PAYOFF_CMD_OFFSET);
        assert_eq!(&data.version as *const _ as usize - base_ptr, INSTR_PAYOFF_VERSION_OFFSET);
        assert_eq!(&data.reserved as *const _ as usize - base_ptr, INSTR_PAYOFF_RESERVED_OFFSET);
        assert_eq!(&data.worker_id as *const _ as usize - base_ptr, INSTR_PAYOFF_WORKER_ID_OFFSET);
        assert_eq!(&data.pool_id as *const _ as usize - base_ptr, INSTR_PAYOFF_POOL_ID_OFFSET);

        assert_eq!(mem::size_of::<InstructionPayoff>(), INSTRUCTION_PAYOFF_SIZE);
    }
}