Skip to content

Commit

Permalink
piecrust: make owner optional in a ContractData
Browse files Browse the repository at this point in the history
  • Loading branch information
ureeves committed Feb 22, 2024
1 parent c90aa42 commit 467e69a
Show file tree
Hide file tree
Showing 27 changed files with 119 additions and 83 deletions.
6 changes: 6 additions & 0 deletions piecrust/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `ContractDataBuilder::owner` to allow for setting the owner of a contract
on deploy time [#336]

### Changed

- Make `owner` field optional in `ContractData` and `ContractDataBuilder` [#336]
- Change `ContractData` and `ContractDataBuilder` to take a `Vec<u8>` as owner
instead of `[u8; N]` [#336]
- Use empty constructor arguments by default [#316]
Expand Down
14 changes: 10 additions & 4 deletions piecrust/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::error::Error;
pub struct ContractData<'a, A> {
pub(crate) contract_id: Option<ContractId>,
pub(crate) constructor_arg: Option<&'a A>,
pub(crate) owner: Vec<u8>,
pub(crate) owner: Option<Vec<u8>>,
}

// `()` is done on purpose, since by default it should be that the constructor
Expand All @@ -26,11 +26,11 @@ impl<'a> ContractData<'a, ()> {
///
/// This function returns a builder that can be used to set optional fields
/// in contract deployment.
pub fn builder(owner: impl Into<Vec<u8>>) -> ContractDataBuilder<'a, ()> {
pub fn builder() -> ContractDataBuilder<'a, ()> {
ContractDataBuilder {
contract_id: None,
constructor_arg: None,
owner: owner.into(),
owner: None,
}
}
}
Expand All @@ -43,7 +43,7 @@ impl<'a, A> From<ContractDataBuilder<'a, A>> for ContractData<'a, A> {

pub struct ContractDataBuilder<'a, A> {
contract_id: Option<ContractId>,
owner: Vec<u8>,
owner: Option<Vec<u8>>,
constructor_arg: Option<&'a A>,
}

Expand All @@ -63,6 +63,12 @@ impl<'a, A> ContractDataBuilder<'a, A> {
}
}

/// Set the owner of the contract.
pub fn owner(mut self, owner: impl Into<Vec<u8>>) -> Self {
self.owner = Some(owner.into());
self
}

pub fn build(self) -> ContractData<'a, A> {
ContractData {
contract_id: self.contract_id,
Expand Down
8 changes: 7 additions & 1 deletion piecrust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@
//! const LIMIT: u64 = 1_000_000;
//!
//! let mut session = vm.session(SessionData::builder()).unwrap();
//! let counter_id = session.deploy(contract_bytecode!("counter"), ContractData::builder(OWNER), LIMIT).unwrap();
//! let counter_id = session
//! .deploy(
//! contract_bytecode!("counter"),
//! ContractData::builder().owner(OWNER),
//! LIMIT,
//! )
//! .unwrap();
//!
//! assert_eq!(session.call::<_, i64>(counter_id, "read_value", &(), LIMIT).unwrap().data, 0xfc);
//! session.call::<_, ()>(counter_id, "increment", &(), LIMIT).unwrap();
Expand Down
7 changes: 6 additions & 1 deletion piecrust/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ impl Session {
///
/// [`ContractId`]: ContractId
/// [`PersistenceError`]: PersistenceError
///
/// # Panics
/// If `deploy_data` does not specify an owner, this will panic.
pub fn deploy<'a, A, D>(
&mut self,
bytecode: &[u8],
Expand Down Expand Up @@ -248,7 +251,9 @@ impl Session {
contract_id,
bytecode,
constructor_arg,
deploy_data.owner.to_vec(),
deploy_data
.owner
.expect("Owner must be specified when deploying a contract"),
gas_limit,
)?;

Expand Down
4 changes: 2 additions & 2 deletions piecrust/tests/box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn box_set_get() -> Result<(), Error> {

let id = session.deploy(
contract_bytecode!("box"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand All @@ -43,7 +43,7 @@ pub fn box_set_get_raw() -> Result<(), Error> {

let id = session.deploy(
contract_bytecode!("box"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand Down
26 changes: 13 additions & 13 deletions piecrust/tests/callcenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn cc_read_counter() -> Result<(), Error> {

let counter_id = session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand All @@ -29,7 +29,7 @@ pub fn cc_read_counter() -> Result<(), Error> {

let center_id = session.deploy(
contract_bytecode!("callcenter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand All @@ -50,7 +50,7 @@ pub fn cc_direct() -> Result<(), Error> {

let counter_id = session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand All @@ -60,7 +60,7 @@ pub fn cc_direct() -> Result<(), Error> {

let center_id = session.deploy(
contract_bytecode!("callcenter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand Down Expand Up @@ -99,7 +99,7 @@ pub fn cc_passthrough() -> Result<(), Error> {

let center_id = session.deploy(
contract_bytecode!("callcenter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand All @@ -122,12 +122,12 @@ pub fn cc_delegated_read() -> Result<(), Error> {

let counter_id = session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;
let center_id = session.deploy(
contract_bytecode!("callcenter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand Down Expand Up @@ -159,12 +159,12 @@ pub fn cc_delegated_write() -> Result<(), Error> {
let mut session = vm.session(SessionData::builder())?;
let counter_id = session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;
let center_id = session.deploy(
contract_bytecode!("callcenter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand All @@ -190,7 +190,7 @@ pub fn cc_self() -> Result<(), Error> {

let center_id = session.deploy(
contract_bytecode!("callcenter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand All @@ -211,7 +211,7 @@ pub fn cc_caller() -> Result<(), Error> {

let center_id = session.deploy(
contract_bytecode!("callcenter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand All @@ -231,7 +231,7 @@ pub fn cc_caller_uninit() -> Result<(), Error> {

let center_id = session.deploy(
contract_bytecode!("callcenter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand All @@ -250,7 +250,7 @@ pub fn cc_self_id() -> Result<(), Error> {

let center_id = session.deploy(
contract_bytecode!("callcenter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand Down
3 changes: 2 additions & 1 deletion piecrust/tests/cold-reboot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::path::{Path, PathBuf};
use std::{env, fs};

use piecrust::{ContractData, ContractId, SessionData, VM};

const COUNTER_ID: ContractId = {
let mut bytes = [0u8; 32];
bytes[0] = 99;
Expand All @@ -28,7 +29,7 @@ fn initialize_counter<P: AsRef<Path>>(

session.deploy(
counter_bytecode,
ContractData::builder(OWNER).contract_id(COUNTER_ID),
ContractData::builder().owner(OWNER).contract_id(COUNTER_ID),
u64::MAX,
)?;
session.call::<_, ()>(COUNTER_ID, "increment", &(), u64::MAX)?;
Expand Down
20 changes: 10 additions & 10 deletions piecrust/tests/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn read_write_session() -> Result<(), Error> {
let mut session = vm.session(SessionData::builder())?;
let id = session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand All @@ -44,7 +44,7 @@ fn read_write_session() -> Result<(), Error> {
let mut other_session = vm.session(SessionData::builder())?;
let id = other_session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand Down Expand Up @@ -76,7 +76,7 @@ fn commit_restore() -> Result<(), Error> {
let mut session_1 = vm.session(SessionData::builder())?;
let id = session_1.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;
// commit 1
Expand Down Expand Up @@ -125,12 +125,12 @@ fn commit_restore_two_contracts_session() -> Result<(), Error> {
let mut session = vm.session(SessionData::builder())?;
let id_1 = session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;
let id_2 = session.deploy(
contract_bytecode!("box"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand Down Expand Up @@ -190,7 +190,7 @@ fn multiple_commits() -> Result<(), Error> {
let mut session = vm.session(SessionData::builder())?;
let id = session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;
// commit 1
Expand Down Expand Up @@ -240,12 +240,12 @@ fn root_equal_on_err() -> Result<(), Error> {

let callcenter_id = session.deploy(
contract_bytecode!("callcenter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;
let counter_id = session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand Down Expand Up @@ -311,7 +311,7 @@ fn concurrent_sessions() -> Result<(), Error> {
let mut session = vm.session(SessionData::builder())?;
let counter = session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand Down Expand Up @@ -405,7 +405,7 @@ fn make_session(vm: &VM) -> Result<(Session, ContractId), Error> {
vm.session(SessionData::builder().insert("height", HEIGHT)?)?;
let contract_id = session.deploy(
contract_bytecode!("everest"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;
Ok((session, contract_id))
Expand Down
6 changes: 4 additions & 2 deletions piecrust/tests/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ fn constructor() -> Result<(), Error> {

let id = session.deploy(
contract_bytecode!("constructor"),
ContractData::builder(OWNER).constructor_arg(&0xabu8),
ContractData::builder()
.owner(OWNER)
.constructor_arg(&0xabu8),
LIMIT,
)?;

Expand Down Expand Up @@ -80,7 +82,7 @@ fn empty_constructor_argument() -> Result<(), Error> {

let id = session.deploy(
contract_bytecode!("empty_constructor"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand Down
12 changes: 6 additions & 6 deletions piecrust/tests/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn counter_read_simple() -> Result<(), Error> {

let id = session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand All @@ -37,7 +37,7 @@ fn counter_read_write_simple() -> Result<(), Error> {

let id = session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand All @@ -64,12 +64,12 @@ fn call_through_c() -> Result<(), Error> {

let counter_id = session.deploy(
contract_bytecode!("counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;
let c_example_id = session.deploy(
contract_bytecode!("c_example"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand All @@ -79,7 +79,7 @@ fn call_through_c() -> Result<(), Error> {
c_example_id,
"increment_and_read",
&counter_id,
LIMIT
LIMIT,
)?
.data,
0xfd
Expand All @@ -96,7 +96,7 @@ fn increment_panic() -> Result<(), Error> {

let counter_id = session.deploy(
contract_bytecode!("fallible_counter"),
ContractData::builder(OWNER),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

Expand Down
Loading

0 comments on commit 467e69a

Please sign in to comment.