Skip to content

Commit

Permalink
Upgrade to piecrust@0.6 version of the ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Leegwater Simões committed Jul 4, 2023
1 parent d652f0b commit 9e48f04
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dusk-jubjub = { version = "0.12", default-features = false }
dusk-poseidon = { version = "0.29.1-rc.0", default-features = false }
dusk-merkle = { version = "0.4.1-rc.0", features = ["poseidon", "rkyv-impl"] }
dusk-plonk = { version = "0.13", default-features = false }
rusk-abi = { version = "0.8.0-piecrust.0.5", features = ["host"] }
rusk-abi = { version = "0.9.0-piecrust.0.6" }
dusk-bls12_381-sign = { version = "0.4", default-features = false }
rkyv = { version = "0.7", default-features = false }

Expand Down
15 changes: 10 additions & 5 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,20 @@ impl StateClient for FfiStateClient {

let note = note.to_bytes();
unsafe {
let r = fetch_opening(&note, &mut opening_buf, &mut opening_len);
let r = fetch_opening(
&note,
opening_buf.as_mut_ptr(),
&mut opening_len,
);
if r != 0 {
return Err(r);
}
}

let branch = PoseidonBranch::from_slice(&opening_buf).map_err(
Error::<FfiStore, FfiStateClient, FfiProverClient>::from,
)?;
let branch = rkyv::from_bytes(&opening_buf[..opening_len as usize])
.map_err(
Error::<FfiStore, FfiStateClient, FfiProverClient>::from,
)?;

Ok(branch)
}
Expand Down Expand Up @@ -673,7 +678,7 @@ impl<S: Store, SC: StateClient, PC: ProverClient> From<Error<S, SC, PC>>
Error::Prover(_) => 251,
Error::NotEnoughBalance => 250,
Error::NoteCombinationProblem => 249,
Error::Rkyv(_) => 248,
Error::Rkyv => 248,
Error::Phoenix(_) => 247,
Error::AlreadyStaked { .. } => 246,
Error::NotStaked { .. } => 245,
Expand Down
29 changes: 19 additions & 10 deletions src/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use rkyv::ser::serializers::{
AllocScratchError, AllocSerializer, CompositeSerializerError,
SharedSerializeMapError,
};
use rkyv::validation::validators::CheckDeserializeError;
use rkyv::Serialize;
use rusk_abi::ContractId;

Expand Down Expand Up @@ -56,7 +57,7 @@ pub enum Error<S: Store, SC: StateClient, PC: ProverClient> {
/// Error originating from the prover client.
Prover(PC::Error),
/// Rkyv serialization.
Rkyv(SerializerError),
Rkyv,
/// Random number generator error.
Rng(RngError),
/// Serialization and deserialization of Dusk types.
Expand Down Expand Up @@ -114,8 +115,16 @@ impl<S: Store, SC: StateClient, PC: ProverClient> Error<S, SC, PC> {
impl<S: Store, SC: StateClient, PC: ProverClient> From<SerializerError>
for Error<S, SC, PC>
{
fn from(err: SerializerError) -> Self {
Self::Rkyv(err)
fn from(_: SerializerError) -> Self {
Self::Rkyv
}
}

impl<C, D, S: Store, SC: StateClient, PC: ProverClient>
From<CheckDeserializeError<C, D>> for Error<S, SC, PC>
{
fn from(_: CheckDeserializeError<C, D>) -> Self {
Self::Rkyv
}
}

Expand Down Expand Up @@ -448,10 +457,10 @@ where
fee.gas_limit = gas_limit;
fee.gas_price = gas_price;

let contract_id = rusk_abi::stake_module();
let address = rusk_abi::module_to_scalar(&contract_id);
let contract_id = rusk_abi::STAKE_CONTRACT;
let address = rusk_abi::contract_to_scalar(&contract_id);

let contract_id = rusk_abi::module_to_scalar(&rusk_abi::stake_module());
let contract_id = rusk_abi::contract_to_scalar(&contract_id);

let stct_message =
stct_signature_message(&crossover, value, contract_id);
Expand Down Expand Up @@ -487,7 +496,7 @@ where

let call_data = rkyv::to_bytes::<_, MAX_CALL_SIZE>(&stake)?.to_vec();
let call =
(rusk_abi::stake_module(), String::from(TX_STAKE), call_data);
(rusk_abi::STAKE_CONTRACT, String::from(TX_STAKE), call_data);

let utx = UnprovenTransaction::new(
rng,
Expand Down Expand Up @@ -580,7 +589,7 @@ where

let call_data = rkyv::to_bytes::<_, MAX_CALL_SIZE>(&unstake)?.to_vec();
let call = (
rusk_abi::stake_module(),
rusk_abi::STAKE_CONTRACT,
String::from(TX_UNSTAKE),
call_data,
);
Expand Down Expand Up @@ -664,7 +673,7 @@ where
};
let call_data = rkyv::to_bytes::<_, MAX_CALL_SIZE>(&withdraw)?.to_vec();

let contract_id = rusk_abi::stake_module();
let contract_id = rusk_abi::STAKE_CONTRACT;
let call = (contract_id, String::from(TX_WITHDRAW), call_data);

let utx = UnprovenTransaction::new(
Expand Down Expand Up @@ -740,7 +749,7 @@ where
};
let call_data = rkyv::to_bytes::<_, MAX_CALL_SIZE>(&allow)?.to_vec();

let contract_id = rusk_abi::stake_module();
let contract_id = rusk_abi::STAKE_CONTRACT;
let call = (contract_id, String::from(TX_ADD_ALLOWLIST), call_data);

let utx = UnprovenTransaction::new(
Expand Down
12 changes: 6 additions & 6 deletions src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use phoenix_core::transaction::Transaction;
use phoenix_core::{Crossover, Fee, Note};
use rand_core::{CryptoRng, RngCore};
use rusk_abi::hash::Hasher;
use rusk_abi::{ContractId, MODULE_ID_BYTES, POSEIDON_TREE_DEPTH};
use rusk_abi::{ContractId, CONTRACT_ID_BYTES, POSEIDON_TREE_DEPTH};

/// An input to a transaction that is yet to be proven.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -301,7 +301,7 @@ impl UnprovenTransaction {
.call
.as_ref()
.map(|(_, cname, cdata)| {
MODULE_ID_BYTES + u64::SIZE + cname.len() + cdata.len()
CONTRACT_ID_BYTES + u64::SIZE + cname.len() + cdata.len()
})
.unwrap_or(0);

Expand Down Expand Up @@ -458,16 +458,16 @@ fn read_optional_call(

// needs to be at least the size of a contract ID and have some call
// data.
if buf_len < MODULE_ID_BYTES {
if buf_len < CONTRACT_ID_BYTES {
return Err(BytesError::BadLength {
found: buf_len,
expected: MODULE_ID_BYTES,
expected: CONTRACT_ID_BYTES,
});
}
let (mid_buffer, mut buffer_left) = {
let (buf, left) = buffer.split_at(MODULE_ID_BYTES);
let (buf, left) = buffer.split_at(CONTRACT_ID_BYTES);

let mut mid_buf = [0u8; MODULE_ID_BYTES];
let mut mid_buf = [0u8; CONTRACT_ID_BYTES];
mid_buf.copy_from_slice(buf);

(mid_buf, left)
Expand Down

0 comments on commit 9e48f04

Please sign in to comment.