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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
use alloc::{string::ToString, vec::Vec};
use miden_verifier::ExecutionProof;
use super::{InputNote, ToInputNoteCommitments};
use crate::{
accounts::delta::AccountUpdateDetails,
notes::NoteHeader,
transaction::{
AccountId, Digest, InputNotes, Nullifier, OutputNote, OutputNotes, TransactionId,
},
utils::serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
ProvenTransactionError,
};
// PROVEN TRANSACTION
// ================================================================================================
/// Result of executing and proving a transaction. Contains all the data required to verify that a
/// transaction was executed correctly.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProvenTransaction {
/// A unique identifier for the transaction, see [TransactionId] for additional details.
id: TransactionId,
/// Account update data.
account_update: TxAccountUpdate,
/// Commited details of all notes consumed by the transaction.
input_notes: InputNotes<InputNoteCommitment>,
/// Notes created by the transaction. For private notes, this will contain only note headers,
/// while for public notes this will also contain full note details.
output_notes: OutputNotes,
/// The block hash of the last known block at the time the transaction was executed.
block_ref: Digest,
/// A STARK proof that attests to the correct execution of the transaction.
proof: ExecutionProof,
}
impl ProvenTransaction {
/// Returns unique identifier of this transaction.
pub fn id(&self) -> TransactionId {
self.id
}
/// Returns ID of the account against which this transaction was executed.
pub fn account_id(&self) -> AccountId {
self.account_update.account_id()
}
/// Returns the account update details.
pub fn account_update(&self) -> &TxAccountUpdate {
&self.account_update
}
/// Returns a reference to the notes consumed by the transaction.
pub fn input_notes(&self) -> &InputNotes<InputNoteCommitment> {
&self.input_notes
}
/// Returns a reference to the notes produced by the transaction.
pub fn output_notes(&self) -> &OutputNotes {
&self.output_notes
}
/// Returns the proof of the transaction.
pub fn proof(&self) -> &ExecutionProof {
&self.proof
}
/// Returns the block reference the transaction was executed against.
pub fn block_ref(&self) -> Digest {
self.block_ref
}
/// Returns an iterator of the headers of unauthenticated input notes in this transaction.
pub fn get_unauthenticated_notes(&self) -> impl Iterator<Item = &NoteHeader> {
self.input_notes.iter().filter_map(|note| note.header())
}
/// Returns an iterator over the nullifiers of all input notes in this transaction.
///
/// This includes both authenticated and unauthenticated notes.
pub fn get_nullifiers(&self) -> impl Iterator<Item = Nullifier> + '_ {
self.input_notes.iter().map(InputNoteCommitment::nullifier)
}
// HELPER METHODS
// --------------------------------------------------------------------------------------------
fn validate(self) -> Result<Self, ProvenTransactionError> {
if self.account_id().is_on_chain() {
let is_new_account = self.account_update.init_state_hash() == Digest::default();
match self.account_update.details() {
AccountUpdateDetails::Private => {
return Err(ProvenTransactionError::OnChainAccountMissingDetails(
self.account_id(),
))
},
AccountUpdateDetails::New(ref account) => {
if !is_new_account {
return Err(
ProvenTransactionError::ExistingOnChainAccountRequiresDeltaDetails(
self.account_id(),
),
);
}
if account.id() != self.account_id() {
return Err(ProvenTransactionError::AccountIdMismatch(
self.account_id(),
account.id(),
));
}
if account.hash() != self.account_update.final_state_hash() {
return Err(ProvenTransactionError::AccountFinalHashMismatch(
self.account_update.final_state_hash(),
account.hash(),
));
}
},
AccountUpdateDetails::Delta(_) => {
if is_new_account {
return Err(ProvenTransactionError::NewOnChainAccountRequiresFullDetails(
self.account_id(),
));
}
},
}
} else if !self.account_update.is_private() {
return Err(ProvenTransactionError::OffChainAccountWithDetails(self.account_id()));
}
Ok(self)
}
}
impl Serializable for ProvenTransaction {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.account_update.write_into(target);
self.input_notes.write_into(target);
self.output_notes.write_into(target);
self.block_ref.write_into(target);
self.proof.write_into(target);
}
}
impl Deserializable for ProvenTransaction {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let account_update = TxAccountUpdate::read_from(source)?;
let input_notes = <InputNotes<InputNoteCommitment>>::read_from(source)?;
let output_notes = OutputNotes::read_from(source)?;
let block_ref = Digest::read_from(source)?;
let proof = ExecutionProof::read_from(source)?;
let id = TransactionId::new(
account_update.init_state_hash(),
account_update.final_state_hash(),
input_notes.commitment(),
output_notes.commitment(),
);
let proven_transaction = Self {
id,
account_update,
input_notes,
output_notes,
block_ref,
proof,
};
proven_transaction
.validate()
.map_err(|err| DeserializationError::InvalidValue(err.to_string()))
}
}
// PROVEN TRANSACTION BUILDER
// ================================================================================================
/// Builder for a proven transaction.
#[derive(Clone, Debug)]
pub struct ProvenTransactionBuilder {
/// ID of the account that the transaction was executed against.
account_id: AccountId,
/// The hash of the account before the transaction was executed.
initial_account_hash: Digest,
/// The hash of the account after the transaction was executed.
final_account_hash: Digest,
/// State changes to the account due to the transaction.
account_update_details: AccountUpdateDetails,
/// List of [InputNoteCommitment]s of all consumed notes by the transaction.
input_notes: Vec<InputNoteCommitment>,
/// List of [OutputNote]s of all notes created by the transaction.
output_notes: Vec<OutputNote>,
/// Block [Digest] of the transaction's reference block.
block_ref: Digest,
/// A STARK proof that attests to the correct execution of the transaction.
proof: ExecutionProof,
}
impl ProvenTransactionBuilder {
// CONSTRUCTOR
// --------------------------------------------------------------------------------------------
/// Returns a [ProvenTransactionBuilder] used to build a [ProvenTransaction].
pub fn new(
account_id: AccountId,
initial_account_hash: Digest,
final_account_hash: Digest,
block_ref: Digest,
proof: ExecutionProof,
) -> Self {
Self {
account_id,
initial_account_hash,
final_account_hash,
account_update_details: AccountUpdateDetails::Private,
input_notes: Vec::new(),
output_notes: Vec::new(),
block_ref,
proof,
}
}
// PUBLIC ACCESSORS
// --------------------------------------------------------------------------------------------
/// Sets the account's update details.
pub fn account_update_details(mut self, details: AccountUpdateDetails) -> Self {
self.account_update_details = details;
self
}
/// Add notes consumed by the transaction.
pub fn add_input_notes<I, T>(mut self, notes: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<InputNoteCommitment>,
{
self.input_notes.extend(notes.into_iter().map(|note| note.into()));
self
}
/// Add notes produced by the transaction.
pub fn add_output_notes<T>(mut self, notes: T) -> Self
where
T: IntoIterator<Item = OutputNote>,
{
self.output_notes.extend(notes);
self
}
/// Builds the [ProvenTransaction].
///
/// # Errors
///
/// An error will be returned if an on-chain account is used without provided on-chain detail.
/// Or if the account details, i.e. account id and final hash, don't match the transaction.
pub fn build(self) -> Result<ProvenTransaction, ProvenTransactionError> {
let input_notes =
InputNotes::new(self.input_notes).map_err(ProvenTransactionError::InputNotesError)?;
let output_notes = OutputNotes::new(self.output_notes)
.map_err(ProvenTransactionError::OutputNotesError)?;
let id = TransactionId::new(
self.initial_account_hash,
self.final_account_hash,
input_notes.commitment(),
output_notes.commitment(),
);
let account_update = TxAccountUpdate::new(
self.account_id,
self.initial_account_hash,
self.final_account_hash,
self.account_update_details,
);
let proven_transaction = ProvenTransaction {
id,
account_update,
input_notes,
output_notes,
block_ref: self.block_ref,
proof: self.proof,
};
proven_transaction.validate()
}
}
// TRANSACTION ACCOUNT UPDATE
// ================================================================================================
/// Describes the changes made to the account state resulting from a transaction execution.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TxAccountUpdate {
/// ID of the account updated by a transaction.
account_id: AccountId,
/// The hash of the account before a transaction was executed.
///
/// Set to `Digest::default()` for new accounts.
init_state_hash: Digest,
/// The hash of the account state after a transaction was executed.
final_state_hash: Digest,
/// A set of changes which can be applied the account's state prior to the transaction to
/// get the account state after the transaction. For private accounts this is set to
/// [AccountUpdateDetails::Private].
details: AccountUpdateDetails,
}
impl TxAccountUpdate {
/// Returns a new [TxAccountUpdate] instantiated from the specified components.
pub const fn new(
account_id: AccountId,
init_state_hash: Digest,
final_state_hash: Digest,
details: AccountUpdateDetails,
) -> Self {
Self {
account_id,
init_state_hash,
final_state_hash,
details,
}
}
/// Returns the ID of the updated account.
pub fn account_id(&self) -> AccountId {
self.account_id
}
/// Returns the hash of the account's initial state.
pub fn init_state_hash(&self) -> Digest {
self.init_state_hash
}
/// Returns the hash of the account's after a transaction was executed.
pub fn final_state_hash(&self) -> Digest {
self.final_state_hash
}
/// Returns the description of the updates for public accounts.
///
/// These descriptions can be used to build the new account state from the previous account
/// state.
pub fn details(&self) -> &AccountUpdateDetails {
&self.details
}
/// Returns `true` if the account update details are for a private account.
pub fn is_private(&self) -> bool {
self.details.is_private()
}
}
impl Serializable for TxAccountUpdate {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.account_id.write_into(target);
self.init_state_hash.write_into(target);
self.final_state_hash.write_into(target);
self.details.write_into(target);
}
}
impl Deserializable for TxAccountUpdate {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
Ok(Self {
account_id: AccountId::read_from(source)?,
init_state_hash: Digest::read_from(source)?,
final_state_hash: Digest::read_from(source)?,
details: AccountUpdateDetails::read_from(source)?,
})
}
}
// INPUT NOTE COMMITMENT
// ================================================================================================
/// The commitment to an input note.
///
/// For notes authenticated by the transaction kernel, the commitment consists only of the note's
/// nullifier. For notes whose authentication is delayed to batch/block kernels, the commitment
/// also includes full note header (i.e., note ID and metadata).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InputNoteCommitment {
nullifier: Nullifier,
header: Option<NoteHeader>,
}
impl InputNoteCommitment {
/// Returns the nullifier of the input note committed to by this commitment.
pub fn nullifier(&self) -> Nullifier {
self.nullifier
}
/// Returns the header of the input committed to by this commitment.
///
/// Note headers are present only for notes whose presence in the change has not yet been
/// authenticated.
pub fn header(&self) -> Option<&NoteHeader> {
self.header.as_ref()
}
/// Returns true if this commitment is for a note whose presence in the chain has been
/// authenticated.
///
/// Authenticated notes are represented solely by their nullifiers and are missing the note
/// header.
pub fn is_authenticated(&self) -> bool {
self.header.is_none()
}
}
impl From<InputNote> for InputNoteCommitment {
fn from(note: InputNote) -> Self {
Self::from(¬e)
}
}
impl From<&InputNote> for InputNoteCommitment {
fn from(note: &InputNote) -> Self {
match note {
InputNote::Authenticated { note, .. } => Self {
nullifier: note.nullifier(),
header: None,
},
InputNote::Unauthenticated { note } => Self {
nullifier: note.nullifier(),
header: Some(*note.header()),
},
}
}
}
impl From<Nullifier> for InputNoteCommitment {
fn from(nullifier: Nullifier) -> Self {
Self { nullifier, header: None }
}
}
impl ToInputNoteCommitments for InputNoteCommitment {
fn nullifier(&self) -> Nullifier {
self.nullifier
}
fn note_hash(&self) -> Option<Digest> {
self.header.map(|header| header.hash())
}
}
// SERIALIZATION
// ------------------------------------------------------------------------------------------------
impl Serializable for InputNoteCommitment {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.nullifier.write_into(target);
self.header.write_into(target);
}
}
impl Deserializable for InputNoteCommitment {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let nullifier = Nullifier::read_from(source)?;
let header = <Option<NoteHeader>>::read_from(source)?;
Ok(Self { nullifier, header })
}
}
// TESTS
// ================================================================================================
#[cfg(test)]
mod tests {
use super::ProvenTransaction;
fn check_if_sync<T: Sync>() {}
fn check_if_send<T: Send>() {}
/// [ProvenTransaction] being Sync is part of its public API and changing it is backwards
/// incompatible.
#[test]
fn proven_transaction_is_sync() {
check_if_sync::<ProvenTransaction>();
}
/// [ProvenTransaction] being Send is part of its public API and changing it is backwards
/// incompatible.
#[test]
fn proven_transaction_is_send() {
check_if_send::<ProvenTransaction>();
}
}