Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rusk-abi: Clean library #1998

Merged
merged 14 commits into from
Jul 25, 2024
Merged
9 changes: 1 addition & 8 deletions contracts/alice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod wasm {
use super::*;
use state::Alice;

use rusk_abi::{ContractId, PaymentInfo};
use rusk_abi::ContractId;

#[no_mangle]
static SELF_ID: ContractId = ContractId::uninitialized();
Expand All @@ -38,11 +38,4 @@ mod wasm {
unsafe fn deposit(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |arg| STATE.deposit(arg))
}

const PAYMENT_INFO: PaymentInfo = PaymentInfo::Any(None);

#[no_mangle]
fn payment_info(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |_: ()| PAYMENT_INFO)
}
}
9 changes: 0 additions & 9 deletions contracts/bob/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use state::Bob;
mod wasm {
use super::*;

use rusk_abi::PaymentInfo;

#[no_mangle]
static mut STATE: Bob = Bob::new();

Expand All @@ -41,11 +39,4 @@ mod wasm {
unsafe fn value(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |()| STATE.value())
}

const PAYMENT_INFO: PaymentInfo = PaymentInfo::Transparent(None);

#[no_mangle]
fn payment_info(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |_: ()| PAYMENT_INFO)
}
}
11 changes: 2 additions & 9 deletions contracts/host_fn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use execution_core::{
BlsPublicKey, BlsScalar, BlsSignature, PublicKey, SchnorrPublicKey,
SchnorrSignature,
};
use rusk_abi::{ContractId, PaymentInfo, PublicInput};
use rusk_abi::ContractId;

#[no_mangle]
static SELF_ID: ContractId = ContractId::uninitialized();
Expand All @@ -39,7 +39,7 @@ impl HostFnTest {
&self,
verifier_data: Vec<u8>,
proof: Vec<u8>,
public_inputs: Vec<PublicInput>,
public_inputs: Vec<BlsScalar>,
) -> bool {
rusk_abi::verify_proof(verifier_data, proof, public_inputs)
}
Expand Down Expand Up @@ -120,10 +120,3 @@ unsafe fn contract_owner(arg_len: u32) -> u32 {
unsafe fn contract_owner_raw(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |_: ()| STATE.owner_raw())
}

const PAYMENT_INFO: PaymentInfo = PaymentInfo::Transparent(None);

#[no_mangle]
fn payment_info(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |_: ()| PAYMENT_INFO)
}
16 changes: 7 additions & 9 deletions contracts/license/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use dusk_bytes::Serializable;
use poseidon_merkle::{Opening, Tree};

use execution_core::BlsScalar;
use rusk_abi::PublicInput;

use crate::collection::Map;
use crate::error::Error;
Expand Down Expand Up @@ -104,13 +103,12 @@ impl LicenseContractState {
/// creates a session with the corresponding session id.
/// Method intended to be called by the user.
pub fn use_license(&mut self, use_license_arg: UseLicenseArg) {
let mut pi = Vec::new();
for scalar in use_license_arg.public_inputs.iter() {
pi.push(PublicInput::BlsScalar(*scalar));
}
let vd = verifier_data_license_circuit();
Self::assert_proof(vd, use_license_arg.proof.to_bytes().to_vec(), pi)
.expect("Provided proof verification should succeed!");
Self::assert_proof(
verifier_data_license_circuit(),
use_license_arg.proof.to_bytes().to_vec(),
use_license_arg.public_inputs.clone(),
)
.expect("Provided proof verification should succeed!");

// after a successful proof verification we can add a session to a
// shared list of sessions
Expand Down Expand Up @@ -139,7 +137,7 @@ impl LicenseContractState {
fn assert_proof(
verifier_data: &[u8],
proof: Vec<u8>,
public_inputs: Vec<PublicInput>,
public_inputs: Vec<BlsScalar>,
) -> Result<(), Error> {
rusk_abi::verify_proof(verifier_data.to_vec(), proof, public_inputs)
.then_some(())
Expand Down
8 changes: 4 additions & 4 deletions contracts/stake/benches/get_provisioners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use execution_core::{stake::StakeData, BlsPublicKey, BlsSecretKey};
use rand::rngs::StdRng;
use rand::{CryptoRng, RngCore, SeedableRng};
use rusk_abi::{
ContractData, Error, Session, STAKE_CONTRACT, TRANSFER_CONTRACT, VM,
ContractData, PiecrustError, Session, STAKE_CONTRACT, TRANSFER_CONTRACT, VM,
};
use std::sync::mpsc;

Expand All @@ -24,7 +24,7 @@ fn config() -> Criterion {
Criterion::default().sample_size(SAMPLE_SIZE)
}

fn update_root(session: &mut Session) -> Result<(), Error> {
fn update_root(session: &mut Session) -> Result<(), PiecrustError> {
session
.call(TRANSFER_CONTRACT, "update_root", &(), POINT_LIMIT)
.map(|r| r.data)
Expand Down Expand Up @@ -66,7 +66,7 @@ fn instantiate(vm: &VM) -> Session {

fn do_get_provisioners(
session: &mut Session,
) -> Result<impl Iterator<Item = (BlsPublicKey, StakeData)>, Error> {
) -> Result<impl Iterator<Item = (BlsPublicKey, StakeData)>, PiecrustError> {
let (sender, receiver) = mpsc::channel();
session.feeder_call::<_, ()>(
STAKE_CONTRACT,
Expand All @@ -84,7 +84,7 @@ fn do_get_provisioners(
fn do_insert_stake<Rng: RngCore + CryptoRng>(
rng: &mut Rng,
session: &mut Session,
) -> Result<(), Error> {
) -> Result<(), PiecrustError> {
let stake_data = StakeData {
amount: Some((TEST_STAKE, 0)),
nonce: 1,
Expand Down
9 changes: 1 addition & 8 deletions contracts/stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use state::StakeState;
/// The minimum amount of Dusk one can stake.
pub const MINIMUM_STAKE: Dusk = dusk(1_000.0);

use rusk_abi::{ContractId, PaymentInfo};
use rusk_abi::ContractId;

#[no_mangle]
static SELF_ID: ContractId = ContractId::uninitialized();
Expand Down Expand Up @@ -150,10 +150,3 @@ fn assert_external_caller() {
panic!("Can only be called from the outside the VM");
}
}

const PAYMENT_INFO: PaymentInfo = PaymentInfo::Transparent(None);

#[no_mangle]
fn payment_info(arg_len: u32) -> u32 {
rusk_abi::wrap_call(arg_len, |_: ()| PAYMENT_INFO)
}
16 changes: 9 additions & 7 deletions contracts/stake/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ use execution_core::{
value_commitment, JubJubScalar, Note, PublicKey, SchnorrSecretKey,
SecretKey, Sender, TxSkeleton, ViewKey,
};
use rusk_abi::{CallReceipt, ContractError, Error, Session, TRANSFER_CONTRACT};
use rusk_abi::{
CallReceipt, ContractError, PiecrustError, Session, TRANSFER_CONTRACT,
};

const POINT_LIMIT: u64 = 0x100000000;

pub fn leaves_from_height(
session: &mut Session,
height: u64,
) -> Result<Vec<TreeLeaf>, Error> {
) -> Result<Vec<TreeLeaf>, PiecrustError> {
let (feeder, receiver) = mpsc::channel();

session.feeder_call::<_, ()>(
Expand All @@ -49,7 +51,7 @@ pub fn leaves_from_height(
pub fn leaves_from_pos(
session: &mut Session,
pos: u64,
) -> Result<Vec<TreeLeaf>, Error> {
) -> Result<Vec<TreeLeaf>, PiecrustError> {
let (feeder, receiver) = mpsc::channel();

session.feeder_call::<_, ()>(
Expand All @@ -66,13 +68,13 @@ pub fn leaves_from_pos(
.collect())
}

pub fn update_root(session: &mut Session) -> Result<(), Error> {
pub fn update_root(session: &mut Session) -> Result<(), PiecrustError> {
session
.call(TRANSFER_CONTRACT, "update_root", &(), POINT_LIMIT)
.map(|r| r.data)
}

pub fn root(session: &mut Session) -> Result<BlsScalar, Error> {
pub fn root(session: &mut Session) -> Result<BlsScalar, PiecrustError> {
session
.call(TRANSFER_CONTRACT, "root", &(), POINT_LIMIT)
.map(|r| r.data)
Expand All @@ -81,7 +83,7 @@ pub fn root(session: &mut Session) -> Result<BlsScalar, Error> {
pub fn opening(
session: &mut Session,
pos: u64,
) -> Result<Option<PoseidonOpening<(), TRANSFER_TREE_DEPTH>>, Error> {
) -> Result<Option<PoseidonOpening<(), TRANSFER_TREE_DEPTH>>, PiecrustError> {
session
.call(TRANSFER_CONTRACT, "opening", &pos, POINT_LIMIT)
.map(|r| r.data)
Expand Down Expand Up @@ -123,7 +125,7 @@ pub fn filter_notes_owned_by<I: IntoIterator<Item = Note>>(
pub fn execute(
session: &mut Session,
tx: impl Into<Transaction>,
) -> Result<CallReceipt<Result<Vec<u8>, ContractError>>, Error> {
) -> Result<CallReceipt<Result<Vec<u8>, ContractError>>, PiecrustError> {
let tx = tx.into();

// Spend the inputs and execute the call. If this errors the transaction is
Expand Down
6 changes: 3 additions & 3 deletions contracts/stake/tests/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ use execution_core::{
BlsPublicKey, BlsSecretKey, PublicKey, SecretKey,
};
use rusk_abi::dusk::dusk;
use rusk_abi::{Error, STAKE_CONTRACT, TRANSFER_CONTRACT};
use rusk_abi::{PiecrustError, STAKE_CONTRACT, TRANSFER_CONTRACT};

use crate::common::assert::assert_event;
use crate::common::init::instantiate;

const GENESIS_VALUE: u64 = dusk(1_000_000.0);

#[test]
fn reward_slash() -> Result<(), Error> {
fn reward_slash() -> Result<(), PiecrustError> {
let rng = &mut StdRng::seed_from_u64(0xfeeb);

let vm = &mut rusk_abi::new_ephemeral_vm()
Expand Down Expand Up @@ -111,7 +111,7 @@ fn reward_slash() -> Result<(), Error> {
}

#[test]
fn stake_hard_slash() -> Result<(), Error> {
fn stake_hard_slash() -> Result<(), PiecrustError> {
let rng = &mut StdRng::seed_from_u64(0xfeeb);

let vm = &mut rusk_abi::new_ephemeral_vm()
Expand Down
7 changes: 2 additions & 5 deletions contracts/transfer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use alloc::vec::Vec;
use dusk_bytes::Serializable;
use poseidon_merkle::Opening as PoseidonOpening;
use ringbuffer::{ConstGenericRingBuffer, RingBuffer};
use rusk_abi::{ContractError, ContractId, PublicInput, STAKE_CONTRACT};
use rusk_abi::{ContractError, ContractId, STAKE_CONTRACT};

use execution_core::{
transfer::{
Expand Down Expand Up @@ -627,17 +627,14 @@ impl TransferState {
}

fn verify_tx_proof(tx: &PhoenixTransaction) -> bool {
let pis: Vec<PublicInput> =
tx.public_inputs().iter().map(|pi| pi.into()).collect();

// fetch the verifier data
let num_inputs = tx.payload().tx_skeleton.nullifiers.len();
let vd = verifier_data_execute(num_inputs)
.expect("No circuit available for given number of inputs!")
.to_vec();

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

#[cfg(test)]
Expand Down
22 changes: 12 additions & 10 deletions contracts/transfer/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use execution_core::{
value_commitment, BlsPublicKey, BlsScalar, BlsSecretKey, JubJubScalar,
Note, PublicKey, SchnorrSecretKey, SecretKey, Sender, TxSkeleton, ViewKey,
};
use rusk_abi::{ContractError, ContractId, Error, Session, TRANSFER_CONTRACT};
use rusk_abi::{
ContractError, ContractId, PiecrustError, Session, TRANSFER_CONTRACT,
};

use dusk_bytes::Serializable;
use dusk_plonk::prelude::*;
Expand All @@ -31,7 +33,7 @@ const GAS_LIMIT: u64 = 0x10_000_000;
pub fn leaves_from_height(
session: &mut Session,
height: u64,
) -> Result<Vec<TreeLeaf>, Error> {
) -> Result<Vec<TreeLeaf>, PiecrustError> {
let (feeder, receiver) = mpsc::channel();

session.feeder_call::<_, ()>(
Expand All @@ -51,7 +53,7 @@ pub fn leaves_from_height(
pub fn leaves_from_pos(
session: &mut Session,
pos: u64,
) -> Result<Vec<TreeLeaf>, Error> {
) -> Result<Vec<TreeLeaf>, PiecrustError> {
let (feeder, receiver) = mpsc::channel();

session.feeder_call::<_, ()>(
Expand All @@ -68,19 +70,19 @@ pub fn leaves_from_pos(
.collect())
}

pub fn num_notes(session: &mut Session) -> Result<u64, Error> {
pub fn num_notes(session: &mut Session) -> Result<u64, PiecrustError> {
session
.call(TRANSFER_CONTRACT, "num_notes", &(), u64::MAX)
.map(|r| r.data)
}

pub fn update_root(session: &mut Session) -> Result<(), Error> {
pub fn update_root(session: &mut Session) -> Result<(), PiecrustError> {
session
.call(TRANSFER_CONTRACT, "update_root", &(), GAS_LIMIT)
.map(|r| r.data)
}

pub fn root(session: &mut Session) -> Result<BlsScalar, Error> {
pub fn root(session: &mut Session) -> Result<BlsScalar, PiecrustError> {
session
.call(TRANSFER_CONTRACT, "root", &(), GAS_LIMIT)
.map(|r| r.data)
Expand All @@ -89,7 +91,7 @@ pub fn root(session: &mut Session) -> Result<BlsScalar, Error> {
pub fn account(
session: &mut Session,
pk: &BlsPublicKey,
) -> Result<AccountData, Error> {
) -> Result<AccountData, PiecrustError> {
session
.call(TRANSFER_CONTRACT, "account", pk, GAS_LIMIT)
.map(|r| r.data)
Expand All @@ -98,7 +100,7 @@ pub fn account(
pub fn contract_balance(
session: &mut Session,
contract: ContractId,
) -> Result<u64, Error> {
) -> Result<u64, PiecrustError> {
session
.call(TRANSFER_CONTRACT, "contract_balance", &contract, GAS_LIMIT)
.map(|r| r.data)
Expand All @@ -107,7 +109,7 @@ pub fn contract_balance(
pub fn opening(
session: &mut Session,
pos: u64,
) -> Result<Option<PoseidonOpening<(), TRANSFER_TREE_DEPTH>>, Error> {
) -> Result<Option<PoseidonOpening<(), TRANSFER_TREE_DEPTH>>, PiecrustError> {
session
.call(TRANSFER_CONTRACT, "opening", &pos, GAS_LIMIT)
.map(|r| r.data)
Expand Down Expand Up @@ -141,7 +143,7 @@ pub fn prover_verifier(input_notes: usize) -> (Prover, Verifier) {
pub fn execute(
session: &mut Session,
tx: impl Into<Transaction>,
) -> Result<u64, Error> {
) -> Result<u64, PiecrustError> {
let tx = tx.into();

let mut receipt = session.call::<_, Result<Vec<u8>, ContractError>>(
Expand Down
9 changes: 9 additions & 0 deletions execution-core/src/transfer/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ impl Transaction {
Self::Moonlight(tx) => tx.to_hash_input_bytes(),
}
}

/// Create the unique transaction hash.
#[must_use]
pub fn hash(&self) -> BlsScalar {
match self {
Self::Phoenix(tx) => tx.hash(),
Self::Moonlight(tx) => tx.hash(),
}
}
}

impl From<PhoenixTransaction> for Transaction {
Expand Down
1 change: 0 additions & 1 deletion node-data/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub use attestation::{
use crate::bls::PublicKeyBytes;
use crate::Serializable;

use rusk_abi::hash::Hasher;
use sha3::Digest;
use std::io::{self, Read, Write};

Expand Down
Loading
Loading