Skip to content

Commit

Permalink
execution-core: Hide payload from public api
Browse files Browse the repository at this point in the history
  • Loading branch information
moCello committed Aug 6, 2024
1 parent f8ba62b commit d6a187f
Show file tree
Hide file tree
Showing 3 changed files with 507 additions and 245 deletions.
59 changes: 19 additions & 40 deletions execution-core/src/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use dusk_bytes::{DeserializableSlice, Error as BytesError};
use rkyv::{Archive, Deserialize, Serialize};

use crate::{
signatures::bls::{
PublicKey as AccountPublicKey, Signature as AccountSignature,
},
BlsScalar, ContractId,
signatures::bls::PublicKey as AccountPublicKey, BlsScalar, ContractId,
};

pub mod contract_exec;
Expand All @@ -29,12 +26,9 @@ pub mod withdraw;
pub const TRANSFER_CONTRACT: ContractId = crate::reserved(0x1);

use contract_exec::{ContractCall, ContractDeploy};
use moonlight::{
Payload as MoonlightPayload, Transaction as MoonlightTransaction,
};
use moonlight::Transaction as MoonlightTransaction;
use phoenix::{
Note, Payload as PhoenixPayload, Sender, StealthAddress,
Transaction as PhoenixTransaction,
Note, Sender, StealthAddress, Transaction as PhoenixTransaction,
};

/// The transaction used by the transfer contract.
Expand All @@ -49,37 +43,22 @@ pub enum Transaction {
}

impl Transaction {
/// Create a new phoenix transaction.
#[must_use]
pub fn phoenix(payload: PhoenixPayload, proof: impl Into<Vec<u8>>) -> Self {
Self::Phoenix(PhoenixTransaction::new(payload, proof))
}

/// Create a new moonlight transaction.
#[must_use]
pub fn moonlight(
payload: MoonlightPayload,
signature: AccountSignature,
) -> Self {
Self::Moonlight(MoonlightTransaction::new(payload, signature))
}

/// Return the sender of the account for Moonlight transactions.
#[must_use]
pub fn from(&self) -> Option<&AccountPublicKey> {
pub fn from_account(&self) -> Option<&AccountPublicKey> {
match self {
Self::Phoenix(_) => None,
Self::Moonlight(tx) => Some(&tx.payload.from),
Self::Moonlight(tx) => Some(tx.from_account()),
}
}

/// Return the receiver of the transaction for Moonlight transactions, if it
/// exists.
#[must_use]
pub fn to(&self) -> Option<&AccountPublicKey> {
pub fn to_account(&self) -> Option<&AccountPublicKey> {
match self {
Self::Phoenix(_) => None,
Self::Moonlight(tx) => tx.payload.to.as_ref(),
Self::Moonlight(tx) => tx.to_account(),
}
}

Expand All @@ -88,7 +67,7 @@ impl Transaction {
pub fn value(&self) -> Option<u64> {
match self {
Self::Phoenix(_) => None,
Self::Moonlight(tx) => Some(tx.payload.value),
Self::Moonlight(tx) => Some(tx.value()),
}
}

Expand All @@ -97,7 +76,7 @@ impl Transaction {
#[must_use]
pub fn nullifiers(&self) -> &[BlsScalar] {
match self {
Self::Phoenix(tx) => &tx.payload.tx_skeleton.nullifiers,
Self::Phoenix(tx) => tx.nullifiers(),
Self::Moonlight(_) => &[],
}
}
Expand All @@ -106,7 +85,7 @@ impl Transaction {
#[must_use]
pub fn root(&self) -> Option<&BlsScalar> {
match self {
Self::Phoenix(tx) => Some(&tx.payload.tx_skeleton.root),
Self::Phoenix(tx) => Some(&tx.root()),
Self::Moonlight(_) => None,
}
}
Expand All @@ -115,7 +94,7 @@ impl Transaction {
#[must_use]
pub fn outputs(&self) -> &[Note] {
match self {
Self::Phoenix(tx) => &tx.payload.tx_skeleton.outputs,
Self::Phoenix(tx) => &tx.outputs()[..],
Self::Moonlight(_) => &[],
}
}
Expand All @@ -124,7 +103,7 @@ impl Transaction {
#[must_use]
pub fn stealth_address(&self) -> Option<&StealthAddress> {
match self {
Self::Phoenix(tx) => Some(&tx.payload.fee.stealth_address),
Self::Phoenix(tx) => Some(tx.stealth_address()),
Self::Moonlight(_) => None,
}
}
Expand All @@ -133,7 +112,7 @@ impl Transaction {
#[must_use]
pub fn sender(&self) -> Option<&Sender> {
match self {
Self::Phoenix(tx) => Some(&tx.payload.fee.sender),
Self::Phoenix(tx) => Some(tx.sender()),
Self::Moonlight(_) => None,
}
}
Expand All @@ -142,26 +121,26 @@ impl Transaction {
#[must_use]
pub fn deposit(&self) -> u64 {
match self {
Self::Phoenix(tx) => tx.payload.tx_skeleton.deposit,
Self::Moonlight(tx) => tx.payload.deposit,
Self::Phoenix(tx) => tx.deposit(),
Self::Moonlight(tx) => tx.deposit(),
}
}

/// Returns the gas limit of the transaction.
#[must_use]
pub fn gas_limit(&self) -> u64 {
match self {
Self::Phoenix(tx) => tx.payload.fee.gas_limit,
Self::Moonlight(tx) => tx.payload.gas_limit,
Self::Phoenix(tx) => tx.gas_limit(),
Self::Moonlight(tx) => tx.gas_limit(),
}
}

/// Returns the gas price of the transaction.
#[must_use]
pub fn gas_price(&self) -> u64 {
match self {
Self::Phoenix(tx) => tx.payload.fee.gas_price,
Self::Moonlight(tx) => tx.payload.gas_price,
Self::Phoenix(tx) => tx.gas_price(),
Self::Moonlight(tx) => tx.gas_price(),
}
}

Expand Down
101 changes: 64 additions & 37 deletions execution-core/src/transfer/moonlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use dusk_bytes::{DeserializableSlice, Error as BytesError, Serializable};
use rkyv::{Archive, Deserialize, Serialize};

use crate::{
signatures::bls::{
PublicKey as AccountPublicKey, Signature as AccountSignature,
},
signatures::bls::{PublicKey as BlsPublicKey, Signature as BlsSignature},
transfer::contract_exec::{
ContractBytecode, ContractCall, ContractDeploy, ContractExec,
},
Expand All @@ -37,27 +35,63 @@ pub struct AccountData {
#[derive(Debug, Clone, PartialEq, Eq, Archive, Serialize, Deserialize)]
#[archive_attr(derive(CheckBytes))]
pub struct Transaction {
pub(crate) payload: Payload,
pub(crate) signature: AccountSignature,
payload: Payload,
signature: BlsSignature,
}

impl Transaction {
/// Create a new transaction.
#[must_use]
pub fn new(payload: Payload, signature: AccountSignature) -> Self {
pub fn new(payload: Payload, signature: BlsSignature) -> Self {
Self { payload, signature }
}

/// The payload of the transaction.
/// The proof of the transaction.
#[must_use]
pub fn payload(&self) -> &Payload {
&self.payload
pub fn signature(&self) -> &BlsSignature {
&self.signature
}

/// The proof of the transaction.
/// Return the sender of the transaction.
#[must_use]
pub fn signature(&self) -> &AccountSignature {
&self.signature
pub fn from_account(&self) -> &BlsPublicKey {
&self.payload.from
}

/// Return the receiver of the transaction, if it exists.
#[must_use]
pub fn to_account(&self) -> Option<&BlsPublicKey> {
self.payload.to.as_ref()
}

/// Return the value transferred in the transaction.
#[must_use]
pub fn value(&self) -> u64 {
self.payload.value
}

/// Returns the deposit of the transaction.
#[must_use]
pub fn deposit(&self) -> u64 {
self.payload.deposit
}

/// Returns the gas limit of the transaction.
#[must_use]
pub fn gas_limit(&self) -> u64 {
self.payload.gas_limit
}

/// Returns the gas price of the transaction.
#[must_use]
pub fn gas_price(&self) -> u64 {
self.payload.gas_price
}

/// Returns the nonce of the transaction.
#[must_use]
pub fn nonce(&self) -> u64 {
self.payload.nonce
}

/// Return the contract call data, if there is any.
Expand Down Expand Up @@ -93,27 +127,20 @@ impl Transaction {
pub fn strip_off_bytecode(&self) -> Option<Self> {
let deploy = self.deploy()?;

Some(Self::new(
Payload {
from: self.payload.from,
to: self.payload.to,
value: self.payload.value,
deposit: self.payload.deposit,
gas_limit: self.payload.gas_limit,
gas_price: self.payload.gas_price,
nonce: self.payload.nonce,
exec: Some(ContractExec::Deploy(ContractDeploy {
owner: deploy.owner.clone(),
constructor_args: deploy.constructor_args.clone(),
bytecode: ContractBytecode {
hash: deploy.bytecode.hash,
bytes: Vec::new(),
},
nonce: deploy.nonce,
})),
let stripped_deploy = ContractExec::Deploy(ContractDeploy {
owner: deploy.owner.clone(),
constructor_args: deploy.constructor_args.clone(),
bytecode: ContractBytecode {
hash: deploy.bytecode.hash,
bytes: Vec::new(),
},
*self.signature(),
))
nonce: deploy.nonce,
});

let mut stripped_transaction = self.clone();
stripped_transaction.payload.exec = Some(stripped_deploy);

Some(stripped_transaction)
}

/// Serialize a transaction into a byte buffer.
Expand Down Expand Up @@ -148,7 +175,7 @@ impl Transaction {
let payload = Payload::from_slice(payload_buf)?;
buf = new_buf;

let signature = AccountSignature::from_bytes(
let signature = BlsSignature::from_bytes(
buf.try_into().map_err(|_| BytesError::InvalidData)?,
)
.map_err(|_| BytesError::InvalidData)?;
Expand Down Expand Up @@ -186,9 +213,9 @@ impl Transaction {
#[archive_attr(derive(CheckBytes))]
pub struct Payload {
/// Key of the sender of this transaction.
pub from: AccountPublicKey,
pub from: BlsPublicKey,
/// Key of the receiver of the funds.
pub to: Option<AccountPublicKey>,
pub to: Option<BlsPublicKey>,
/// Value to be transferred.
pub value: u64,
/// Deposit for a contract.
Expand Down Expand Up @@ -255,12 +282,12 @@ impl Payload {
pub fn from_slice(buf: &[u8]) -> Result<Self, BytesError> {
let mut buf = buf;

let from = AccountPublicKey::from_reader(&mut buf)?;
let from = BlsPublicKey::from_reader(&mut buf)?;

// deserialize recipient
let to = match u8::from_reader(&mut buf)? {
0 => None,
1 => Some(AccountPublicKey::from_reader(&mut buf)?),
1 => Some(BlsPublicKey::from_reader(&mut buf)?),
_ => {
return Err(BytesError::InvalidData);
}
Expand Down
Loading

0 comments on commit d6a187f

Please sign in to comment.