dusk_node_data/ledger/
transaction.rs1use dusk_bytes::Serializable as DuskSerializable;
8use dusk_core::signatures::bls::PublicKey as AccountPublicKey;
9use dusk_core::transfer::moonlight::Transaction as MoonlightTransaction;
10use dusk_core::transfer::phoenix::Transaction as PhoenixTransaction;
11use dusk_core::transfer::Transaction as ProtocolTransaction;
12use serde::Serialize;
13use sha3::Digest;
14
15use crate::Serializable;
16
17#[derive(Debug, Clone)]
18pub struct Transaction {
19 pub version: u32,
20 pub r#type: u32,
21 pub inner: ProtocolTransaction,
22 pub(crate) size: Option<usize>,
23}
24
25impl Transaction {
26 pub fn size(&self) -> usize {
27 match self.size {
28 Some(size) => size,
29 None => {
30 let mut buf = vec![];
31 self.write(&mut buf).expect("write to vec should not fail");
32 buf.len()
33 }
34 }
35 }
36}
37
38impl From<ProtocolTransaction> for Transaction {
39 fn from(value: ProtocolTransaction) -> Self {
40 Self {
41 inner: value,
42 r#type: 1,
43 version: 1,
44 size: None,
45 }
46 }
47}
48
49#[derive(Debug, Clone, Serialize)]
52pub struct SpentTransaction {
53 pub inner: Transaction,
55 pub block_height: u64,
57 pub gas_spent: u64,
60 pub err: Option<String>,
63}
64
65impl SpentTransaction {
66 pub fn public(&self) -> Option<&MoonlightTransaction> {
69 match &self.inner.inner {
70 ProtocolTransaction::Moonlight(public_tx) => Some(public_tx),
71 _ => None,
72 }
73 }
74
75 pub fn shielded(&self) -> Option<&PhoenixTransaction> {
78 match &self.inner.inner {
79 ProtocolTransaction::Phoenix(shielded_tx) => Some(shielded_tx),
80 _ => None,
81 }
82 }
83}
84
85impl Transaction {
86 pub fn digest(&self) -> [u8; 32] {
96 sha3::Sha3_256::digest(self.inner.to_var_bytes()).into()
97 }
98
99 pub fn id(&self) -> [u8; 32] {
110 self.inner.hash().to_bytes()
111 }
112
113 pub fn gas_price(&self) -> u64 {
114 self.inner.gas_price()
115 }
116
117 pub fn to_spend_ids(&self) -> Vec<SpendingId> {
118 match &self.inner {
119 ProtocolTransaction::Phoenix(p) => p
120 .nullifiers()
121 .iter()
122 .map(|n| SpendingId::Nullifier(n.to_bytes()))
123 .collect(),
124 ProtocolTransaction::Moonlight(m) => {
125 vec![SpendingId::AccountNonce(*m.sender(), m.nonce())]
126 }
127 }
128 }
129
130 pub fn next_spending_id(&self) -> Option<SpendingId> {
131 match &self.inner {
132 ProtocolTransaction::Phoenix(_) => None,
133 ProtocolTransaction::Moonlight(m) => {
134 Some(SpendingId::AccountNonce(*m.sender(), m.nonce() + 1))
135 }
136 }
137 }
138}
139
140impl PartialEq<Self> for Transaction {
141 fn eq(&self, other: &Self) -> bool {
142 self.r#type == other.r#type
143 && self.version == other.version
144 && self.id() == other.id()
145 }
146}
147
148impl Eq for Transaction {}
149
150impl PartialEq<Self> for SpentTransaction {
151 fn eq(&self, other: &Self) -> bool {
152 self.inner == other.inner && self.gas_spent == other.gas_spent
153 }
154}
155
156impl Eq for SpentTransaction {}
157
158pub enum SpendingId {
159 Nullifier([u8; 32]),
160 AccountNonce(AccountPublicKey, u64),
161}
162
163impl SpendingId {
164 pub fn to_bytes(&self) -> Vec<u8> {
165 match self {
166 SpendingId::Nullifier(n) => n.to_vec(),
167 SpendingId::AccountNonce(account, nonce) => {
168 let mut id = account.to_bytes().to_vec();
169 id.extend_from_slice(&nonce.to_le_bytes());
170 id
171 }
172 }
173 }
174
175 pub fn next(&self) -> Option<SpendingId> {
176 match self {
177 SpendingId::Nullifier(_) => None,
178 SpendingId::AccountNonce(account, nonce) => {
179 Some(SpendingId::AccountNonce(*account, nonce + 1))
180 }
181 }
182 }
183}
184
185#[cfg(any(feature = "faker", test))]
186pub mod faker {
187 use dusk_core::transfer::data::{ContractCall, TransactionData};
188 use dusk_core::transfer::phoenix::{
189 Fee, Note, Payload as PhoenixPayload, PublicKey as PhoenixPublicKey,
190 SecretKey as PhoenixSecretKey, Transaction as PhoenixTransaction,
191 TxSkeleton,
192 };
193 use dusk_core::{BlsScalar, JubJubScalar};
194 use rand::Rng;
195
196 use super::*;
197 use crate::ledger::Dummy;
198
199 impl<T> Dummy<T> for Transaction {
200 fn dummy_with_rng<R: Rng + ?Sized>(_config: &T, _rng: &mut R) -> Self {
201 gen_dummy_tx(1_000_000)
202 }
203 }
204
205 impl<T> Dummy<T> for SpentTransaction {
206 fn dummy_with_rng<R: Rng + ?Sized>(_config: &T, _rng: &mut R) -> Self {
207 let tx = gen_dummy_tx(1_000_000);
208 SpentTransaction {
209 inner: tx,
210 block_height: 0,
211 gas_spent: 3,
212 err: Some("error".to_string()),
213 }
214 }
215 }
216
217 pub fn gen_dummy_tx(gas_price: u64) -> Transaction {
220 let pk = PhoenixPublicKey::from(&PhoenixSecretKey::new(
221 JubJubScalar::from(42u64),
222 JubJubScalar::from(42u64),
223 ));
224 let gas_limit = 1;
225
226 let fee = Fee::deterministic(
227 &JubJubScalar::from(5u64),
228 &pk,
229 gas_limit,
230 gas_price,
231 &[JubJubScalar::from(9u64), JubJubScalar::from(10u64)],
232 );
233
234 let tx_skeleton = TxSkeleton {
235 root: BlsScalar::from(12345u64),
236 nullifiers: vec![
237 BlsScalar::from(1u64),
238 BlsScalar::from(2u64),
239 BlsScalar::from(3u64),
240 ],
241 outputs: [Note::empty(), Note::empty()],
242 max_fee: gas_price * gas_limit,
243 deposit: 0,
244 };
245
246 let contract_call = ContractCall::new([21; 32], "some_method");
247
248 let payload = PhoenixPayload {
249 chain_id: 0xFA,
250 tx_skeleton,
251 fee,
252 data: Some(TransactionData::Call(contract_call)),
253 };
254 let proof = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
255
256 let tx: ProtocolTransaction =
257 PhoenixTransaction::from_payload_and_proof(payload, proof).into();
258
259 tx.into()
260 }
261}