eosio_chain/
transaction.rs

1use crate::serializer::{
2    Packer,
3    Encoder,
4    Decoder,
5};
6
7use crate::structs::{
8    TimePointSec,
9};
10
11use crate::varint::{
12    VarUint32,
13};
14
15use crate::action::{
16    Action,
17};
18
19use crate::{
20    vec::Vec,
21};
22
23#[cfg_attr(feature = "std", derive(crate::eosio_scale_info::TypeInfo))]
24#[derive(Clone, Eq, PartialEq, Default)]
25pub struct TransactionExtension {
26    ty:     u16,
27    data:   Vec<u8>,
28}
29
30impl Packer for TransactionExtension {
31    fn size(&self) -> usize {
32        let mut _size: usize = 0;
33        _size += self.ty.size();
34        _size += self.data.size();
35        return _size;
36    }
37    fn pack(&self) -> Vec<u8> {
38        let mut enc = Encoder::new(self.size());
39        enc.pack::<u16>(&self.ty);
40        enc.pack::<Vec<u8>>(&self.data);
41        return enc.get_bytes();
42    }
43    fn unpack(&mut self, data: &[u8]) -> usize {
44        let mut dec = Decoder::new(data);
45        dec.unpack::<u16>(&mut self.ty);
46        dec.unpack::<Vec<u8>>(&mut self.data);
47        return dec.get_pos();
48    }
49}
50
51#[cfg_attr(feature = "std", derive(crate::eosio_scale_info::TypeInfo))]
52#[derive(Clone, Eq, PartialEq, Default)]
53pub struct Transaction {
54    expiration:             TimePointSec,
55    ref_block_num:          u16,
56    ref_block_prefix:       u32,
57    //[VLQ or Base-128 encoding](https://en.wikipedia.org/wiki/Variable-length_quantity)
58    //unsigned_int vaint (eosio.cdt/libraries/eosiolib/core/eosio/varint.hpp)
59    max_net_usage_words:    VarUint32, /// number of 8 byte words this transaction can serialize into after compressions
60    max_cpu_usage_ms:       u8, /// number of CPU usage units to bill transaction for
61    delay_sec:              VarUint32, /// number of seconds to delay transaction, default: 0
62    context_free_actions:   Vec<Action>,
63    actions:                Vec<Action>,
64    extention:              Vec<TransactionExtension>,
65}
66
67impl Transaction {
68    pub fn expiration(&self) -> TimePointSec {
69        return self.expiration;
70    }
71
72    pub fn ref_block_num(&self) -> u32 {
73        return self.ref_block_num as u32;
74    }
75
76    pub fn ref_block_prefix(&self) -> u32 {
77        return self.ref_block_prefix;
78    }
79
80    pub fn max_net_usage_words(&self) -> u32 {
81        return self.max_net_usage_words.value();
82    }
83
84    pub fn max_cpu_usage_ms(&self) -> u32 {
85        return self.max_cpu_usage_ms as u32;
86    }
87
88    pub fn delay_sec(&self) -> u32 {
89        return self.delay_sec.value();
90    }
91
92    pub fn actions(&self) -> Vec<Action> {
93        return self.actions.clone();
94    }
95
96    pub fn context_free_actions(&self) -> Vec<Action> {
97        return self.context_free_actions.clone();
98    }
99
100    pub fn extention(&self) -> Vec<TransactionExtension> {
101        return self.extention.clone();
102    }
103
104}
105
106impl Packer for Transaction {
107    fn size(&self) -> usize {
108        let mut _size: usize = 0;
109        _size += self.expiration.size();
110        _size += self.ref_block_num.size();
111        _size += self.ref_block_prefix.size();
112        _size += self.max_net_usage_words.size();
113        _size += self.max_cpu_usage_ms.size();
114        _size += self.delay_sec.size();
115        _size += self.context_free_actions.size();
116        _size += self.actions.size();
117        _size += self.extention.size();
118        return _size;
119    }
120
121    fn pack(&self) -> Vec<u8> {
122        let mut enc = Encoder::new(self.size());
123        enc.pack::<TimePointSec>(&self.expiration);
124        enc.pack::<u16>(&self.ref_block_num);
125        enc.pack::<u32>(&self.ref_block_prefix);
126        enc.pack::<VarUint32>(&self.max_net_usage_words);
127        enc.pack::<u8>(&self.max_cpu_usage_ms);
128        enc.pack::<VarUint32>(&self.delay_sec);
129        enc.pack::<Vec<Action>>(&self.context_free_actions);
130        enc.pack::<Vec<Action>>(&self.actions);
131        enc.pack::<Vec<TransactionExtension>>(&self.extention);
132        return enc.get_bytes();
133    }
134
135    fn unpack(&mut self, data: &[u8]) -> usize {
136        let mut dec = Decoder::new(data);
137        dec.unpack::<TimePointSec>(&mut self.expiration);
138        dec.unpack::<u16>(&mut self.ref_block_num);
139        dec.unpack::<u32>(&mut self.ref_block_prefix);
140        dec.unpack::<VarUint32>(&mut self.max_net_usage_words);
141        dec.unpack::<u8>(&mut self.max_cpu_usage_ms);
142        dec.unpack::<VarUint32>(&mut self.delay_sec);
143        dec.unpack::<Vec<Action>>(&mut self.context_free_actions);
144        dec.unpack::<Vec<Action>>(&mut self.actions);
145        dec.unpack::<Vec<TransactionExtension>>(&mut self.extention);
146        return dec.get_pos();
147    }
148}
149
150// bool
151// check_transaction_authorization( const transaction&                 trx,
152//                                  const std::set<permission_level>&  provided_permissions ,
153//                                  const std::set<public_key>&        provided_keys = std::set<public_key>()
154//                                )