Skip to content

Commit

Permalink
Merge pull request #1921 from dusk-network/execution-core-cleanup
Browse files Browse the repository at this point in the history
Small `execution-core` cleanup
  • Loading branch information
Eduardo Leegwater Simões authored Jul 3, 2024
2 parents af979b0 + 766309b commit e7237e4
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 119 deletions.
8 changes: 4 additions & 4 deletions contracts/stake/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ pub fn create_transaction<const I: usize>(
.expect("The input notes should be the correct ammount"),
tx_output_notes,
payload_hash,
tx_payload.tx_skeleton().root,
tx_payload.tx_skeleton().deposit,
tx_payload.tx_skeleton().max_fee,
tx_payload.tx_skeleton.root,
tx_payload.tx_skeleton.deposit,
tx_payload.tx_skeleton.max_fee,
sender_pk,
(sig_a, sig_b),
[transfer_sender_blinder, change_sender_blinder],
Expand All @@ -360,5 +360,5 @@ pub fn create_transaction<const I: usize>(
.expect("creating a proof should succeed");

// build the transaction from the payload and proof
Transaction::new(tx_payload, proof.to_bytes().into())
Transaction::new(tx_payload, proof.to_bytes())
}
10 changes: 5 additions & 5 deletions contracts/transfer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl TransferState {
&mut self,
tx: Transaction,
) -> Result<Vec<u8>, ContractError> {
let tx_skeleton = tx.payload().tx_skeleton();
let tx_skeleton = &tx.payload().tx_skeleton;

// panic if the root is invalid
if !self.root_exists(&tx_skeleton.root) {
Expand All @@ -190,8 +190,8 @@ impl TransferState {
.extend_notes(block_height, tx_skeleton.outputs.clone());

// if present, place the contract deposit on the state
if tx.payload().tx_skeleton().deposit > 0 {
let contract = match tx.payload().contract_call() {
if tx.payload().tx_skeleton.deposit > 0 {
let contract = match &tx.payload().contract_call {
Some(call) => ContractId::from_bytes(call.contract),
None => {
panic!("There needs to be a contract call when depositing funds");
Expand All @@ -202,7 +202,7 @@ impl TransferState {

// perform contract call if present
let mut result = Ok(Vec::new());
if let Some(call) = tx.payload().contract_call() {
if let Some(call) = &tx.payload().contract_call {
result = rusk_abi::call_raw(
ContractId::from_bytes(call.contract),
&call.fn_name,
Expand Down Expand Up @@ -368,7 +368,7 @@ fn verify_tx_proof(tx: &Transaction) -> bool {
.to_vec();

// verify the proof
rusk_abi::verify_proof(vd, tx.proof().clone(), pis)
rusk_abi::verify_proof(vd, tx.proof().to_vec(), pis)
}

#[cfg(test)]
Expand Down
10 changes: 5 additions & 5 deletions contracts/transfer/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub fn execute(session: &mut Session, tx: Transaction) -> Result<u64, Error> {
TRANSFER_CONTRACT,
"spend_and_execute",
&tx,
tx.payload().fee().gas_limit,
tx.payload().fee.gas_limit,
)?;

// Ensure all gas is consumed if there's an error in the contract call
Expand Down Expand Up @@ -359,9 +359,9 @@ pub fn create_transaction<const I: usize>(
.expect("The input notes should be the correct ammount"),
tx_output_notes,
payload_hash,
tx_payload.tx_skeleton().root,
tx_payload.tx_skeleton().deposit,
tx_payload.tx_skeleton().max_fee,
tx_payload.tx_skeleton.root,
tx_payload.tx_skeleton.deposit,
tx_payload.tx_skeleton.max_fee,
sender_pk,
(sig_a, sig_b),
[transfer_sender_blinder, change_sender_blinder],
Expand All @@ -374,5 +374,5 @@ pub fn create_transaction<const I: usize>(
.expect("creating a proof should succeed");

// build the transaction from the payload and proof
Transaction::new(tx_payload, proof.to_bytes().into())
Transaction::new(tx_payload, proof.to_bytes())
}
2 changes: 1 addition & 1 deletion contracts/transfer/tests/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ fn deposit_and_withdraw() {
transfer_value
+ tx.payload().tx_skeleton.deposit
+ tx.payload().tx_skeleton.max_fee
+ tx.payload().tx_skeleton().outputs[1]
+ tx.payload().tx_skeleton.outputs[1]
.value(Some(&ViewKey::from(&sender_sk)))
.unwrap()
);
Expand Down
25 changes: 8 additions & 17 deletions execution-core/src/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,14 @@ impl ContractCall {
#[derive(Debug, Clone, Copy, Archive, Serialize, Deserialize)]
#[archive_attr(derive(CheckBytes))]
pub struct Fee {
/// The gas limit set for the fee
/// Gas limit set for a phoenix transaction
pub gas_limit: u64,
/// the gas price set for the fee
/// Gas price set for a phoenix transaction
pub gas_price: u64,
pub(crate) stealth_address: StealthAddress,
pub(crate) sender: Sender,
/// Address to send the remainder note
pub stealth_address: StealthAddress,
/// Sender to use for the remainder
pub sender: Sender,
}

impl PartialEq for Fee {
Expand All @@ -199,6 +201,7 @@ impl Fee {
gas_price: u64,
) -> Self {
let r = JubJubScalar::random(&mut *rng);

let sender_blinder = [
JubJubScalar::random(&mut *rng),
JubJubScalar::random(&mut *rng),
Expand Down Expand Up @@ -228,18 +231,6 @@ impl Fee {
}
}

/// Return the [`StealthAddress`] to which return the unspend fee to.
#[must_use]
pub fn stealth_address(&self) -> &StealthAddress {
&self.stealth_address
}

/// Return the [`Sender`] of the unspend fee note.
#[must_use]
pub fn sender(&self) -> &Sender {
&self.sender
}

/// Calculate the max-fee.
#[must_use]
pub fn max_fee(&self) -> u64 {
Expand All @@ -249,7 +240,7 @@ impl Fee {
/// Return a hash represented by `H(gas_limit, gas_price, H([note_pk]))`
#[must_use]
pub fn hash(&self) -> BlsScalar {
let npk = self.stealth_address().note_pk().as_ref().to_hash_inputs();
let npk = self.stealth_address.note_pk().as_ref().to_hash_inputs();

let hash_inputs = [
BlsScalar::from(self.gas_limit),
Expand Down
60 changes: 11 additions & 49 deletions execution-core/src/transfer/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,6 @@ impl PartialEq for Payload {
impl Eq for Payload {}

impl Payload {
/// Create a new `Payload`.
#[must_use]
pub fn new(
tx_skeleton: TxSkeleton,
fee: Fee,
contract_call: Option<ContractCall>,
) -> Self {
Self {
tx_skeleton,
fee,
contract_call,
}
}

/// Return the tx-skeleton.
#[must_use]
pub fn tx_skeleton(&self) -> &TxSkeleton {
&self.tx_skeleton
}

/// Return the fee.
#[must_use]
pub fn fee(&self) -> &Fee {
&self.fee
}

/// Return the contract-call data.
#[must_use]
pub fn contract_call(&self) -> &Option<ContractCall> {
&self.contract_call
}

/// Serialize the `Payload` into a variable length byte buffer.
#[must_use]
pub fn to_var_bytes(&self) -> Vec<u8> {
Expand Down Expand Up @@ -174,30 +142,24 @@ impl PartialEq for Transaction {
impl Eq for Transaction {}

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

/// Return the `Payload` of the `Transaction`.
/// The payload of the transaction.
#[must_use]
pub fn payload(&self) -> &Payload {
&self.payload
}

/// Return the tx-payload hash.
/// The `payload_hash` is signed in the proof of the `Transaction`. This
/// means that if the value of the `payload_hash` changes *after* the
/// proof was created, the `Transaction` will not be executable.
#[must_use]
pub fn payload_hash(&self) -> BlsScalar {
self.payload().hash()
}

/// Return the serialized zk-proof of the `Transaction`.
/// The proof of the transaction.
#[must_use]
pub fn proof(&self) -> &Vec<u8> {
pub fn proof(&self) -> &[u8] {
&self.proof
}

Expand Down Expand Up @@ -272,7 +234,7 @@ impl Transaction {
/// [`Sender::Encryption`].
#[must_use]
pub fn public_inputs(&self) -> Vec<BlsScalar> {
let tx_skeleton = self.payload().tx_skeleton();
let tx_skeleton = &self.payload.tx_skeleton;

// retrieve the number of input and output notes
let input_len = tx_skeleton.nullifiers.len();
Expand All @@ -293,7 +255,7 @@ impl Transaction {
+ 2 * 4 * output_len;
// build the public input vector
let mut pis = Vec::<BlsScalar>::with_capacity(size);
pis.push(self.payload_hash());
pis.push(self.payload.hash());
pis.push(tx_skeleton.root);
pis.extend(tx_skeleton.nullifiers().iter());
tx_skeleton.outputs().iter().for_each(|note| {
Expand Down
6 changes: 5 additions & 1 deletion execution-core/tests/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ fn transaction_from_to_bytes() -> Result<(), Error> {
};

// build the payload
let payload = Payload::new(tx_skeleton, fee, Some(call));
let payload = Payload {
tx_skeleton,
fee,
contract_call: Some(call),
};

// set a random proof
let proof = [42; 42].to_vec();
Expand Down
12 changes: 8 additions & 4 deletions node-data/src/ledger/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ impl Transaction {
}

pub fn gas_price(&self) -> u64 {
self.inner.payload().fee().gas_price
self.inner.payload().fee.gas_price
}
pub fn to_nullifiers(&self) -> Vec<[u8; 32]> {
self.inner
.payload()
.tx_skeleton()
.nullifiers()
.tx_skeleton
.nullifiers
.iter()
.map(|n| n.to_bytes())
.collect()
Expand Down Expand Up @@ -150,7 +150,11 @@ pub mod faker {
let contract_call =
ContractCall::new([21; 32], "some_method", &()).unwrap();

let payload = Payload::new(tx_skeleton, fee, Some(contract_call));
let payload = Payload {
tx_skeleton,
fee,
contract_call: Some(contract_call),
};
let proof = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

PhoenixTransaction::new(payload, proof).into()
Expand Down
4 changes: 2 additions & 2 deletions node/src/database/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ impl<'db, DB: DBAccess> Mempool for DBTransaction<'db, DB> {

// Add Secondary indexes //
// Nullifiers
for n in tx.inner.payload().tx_skeleton().nullifiers().iter() {
for n in tx.inner.payload().tx_skeleton.nullifiers.iter() {
let key = n.to_bytes();
self.put_cf(self.nullifiers_cf, key, hash)?;
}
Expand Down Expand Up @@ -626,7 +626,7 @@ impl<'db, DB: DBAccess> Mempool for DBTransaction<'db, DB> {

// Delete Secondary indexes
// Delete Nullifiers
for n in tx.inner.payload().tx_skeleton().nullifiers().iter() {
for n in tx.inner.payload().tx_skeleton.nullifiers.iter() {
let key = n.to_bytes();
self.inner.delete_cf(self.nullifiers_cf, key)?;
}
Expand Down
8 changes: 4 additions & 4 deletions node/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,17 @@ impl MempoolSrv {
let nullifiers: Vec<_> = tx
.inner
.payload()
.tx_skeleton()
.nullifiers()
.tx_skeleton
.nullifiers
.iter()
.map(|nullifier| nullifier.to_bytes())
.collect();

// ensure nullifiers do not exist in the mempool
for m_tx_id in view.get_txs_by_nullifiers(&nullifiers) {
if let Some(m_tx) = view.get_tx(m_tx_id)? {
if m_tx.inner.payload().fee().gas_price
< tx.inner.payload().fee().gas_price
if m_tx.inner.payload().fee.gas_price
< tx.inner.payload().fee.gas_price
{
view.delete_tx(m_tx_id)?;
} else {
Expand Down
4 changes: 2 additions & 2 deletions rusk-prover/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub struct UnprovenTransaction {
impl UnprovenTransaction {
/// Consumes self and a proof to generate a transaction.
pub fn gen_transaction(self, proof: Proof) -> Transaction {
Transaction::new(self.payload, proof.to_bytes().to_vec())
Transaction::new(self.payload, proof.to_bytes())
}

/// Serialize the transaction to a variable length byte buffer.
Expand Down Expand Up @@ -329,7 +329,7 @@ impl UnprovenTransaction {

/// Returns the payload-hash of the transaction.
pub fn payload_hash(&self) -> BlsScalar {
self.payload().hash()
self.payload.hash()
}
}

Expand Down
2 changes: 1 addition & 1 deletion rusk/src/lib/chain/rusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Rusk {
}
}
let tx_id = hex::encode(unspent_tx.id());
if unspent_tx.inner.payload().fee().gas_limit > block_gas_left {
if unspent_tx.inner.payload().fee.gas_limit > block_gas_left {
info!("Skipping {tx_id} due gas_limit greater than left: {block_gas_left}");
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion rusk/src/lib/chain/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl VMExecution for Rusk {
info!("Received preverify request");
let tx = &tx.inner;
let existing_nullifiers = self
.existing_nullifiers(&tx.payload().tx_skeleton().nullifiers)
.existing_nullifiers(&tx.payload().tx_skeleton.nullifiers)
.map_err(|e| anyhow::anyhow!("Cannot check nullifiers: {e}"))?;

if !existing_nullifiers.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion rusk/src/lib/http/chain/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Query {
t.0.inner
.inner
.payload()
.contract_call()
.contract_call
.as_ref()
.map(|c| c.contract)
.unwrap_or(
Expand Down
Loading

0 comments on commit e7237e4

Please sign in to comment.