tape_api/
instruction.rs

1use steel::*;
2use core::marker::PhantomData;
3use crate::consts::*;
4
5#[repr(u8)]
6#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
7pub enum InstructionType {
8    Unknown = 0,
9    Initialize,
10    Advance,
11
12    // Tape instructions
13    Create,
14    Write,
15    Finalize,
16
17    // Miner instructions
18    Register,
19    Close,
20    Mine,
21    Claim,
22}
23
24instruction!(InstructionType, Initialize);
25instruction!(InstructionType, Advance);
26
27instruction!(InstructionType, Create);
28instruction!(InstructionType, Write);
29instruction!(InstructionType, Finalize);
30
31instruction!(InstructionType, Register);
32instruction!(InstructionType, Close);
33instruction!(InstructionType, Mine);
34instruction!(InstructionType, Claim);
35
36
37#[repr(C)]
38#[derive(Clone, Copy, Debug, Pod, Zeroable)]
39pub struct Initialize {}
40
41#[repr(C)]
42#[derive(Clone, Copy, Debug, Pod, Zeroable)]
43pub struct Advance {}
44
45#[repr(C)]
46#[derive(Clone, Copy, Debug, Pod, Zeroable)]
47pub struct Create {
48    pub name: [u8; MAX_NAME_LEN],
49}
50
51#[repr(C)]
52#[derive(Clone, Copy, Debug, Pod, Zeroable)]
53pub struct Write {
54    pub prev_segment: [u8; 64],
55    _data: PhantomData<[u8]>,
56}
57
58impl Write {
59    pub fn new(
60        prev_segment: [u8; 64],
61    ) -> Self {
62        Self {
63            prev_segment,
64            _data: PhantomData,
65        }
66    }
67
68    pub fn pack(&self, data: &[u8]) -> Vec<u8> {
69        let discriminator = InstructionType::Write as u8;
70        let mut result = Vec::with_capacity(1 + data.len());
71        result.push(discriminator);
72        result.extend_from_slice(&self.prev_segment);
73        result.extend_from_slice(data);
74        result
75    }
76}
77
78pub struct ParsedWrite {
79    pub prev_segment: [u8; 64],
80    pub data: Vec<u8>,
81}
82
83impl ParsedWrite {
84    pub fn try_from_bytes(data: &[u8]) -> Result<Self, ProgramError> {
85        let prev_segment = data[0..64].try_into().unwrap();
86        let data = data[64..].to_vec();
87
88        Ok(Self {
89            prev_segment,
90            data,
91        })
92    }
93}
94
95#[repr(C)]
96#[derive(Clone, Copy, Debug, Pod, Zeroable)]
97pub struct Finalize {
98    pub tail: [u8; 64],
99}
100
101#[repr(C)]
102#[derive(Clone, Copy, Debug, Pod, Zeroable)]
103pub struct Register {
104    pub name: [u8; 32],
105}
106
107#[repr(C)]
108#[derive(Clone, Copy, Debug, Pod, Zeroable)]
109pub struct Close {}
110
111#[repr(C)]
112#[derive(Clone, Copy, Debug, Pod, Zeroable)]
113pub struct Mine {
114    pub digest: [u8; 16],
115    pub nonce: [u8; 8],
116    pub recall_chunk: [u8; CHUNK_SIZE],
117    pub recall_proof: [[u8; 32]; PROOF_LEN],
118}
119
120#[repr(C)]
121#[derive(Clone, Copy, Debug, Pod, Zeroable)]
122pub struct Claim {
123    pub amount: [u8; 8],
124}